-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdatevalidate.jspf
60 lines (48 loc) · 2.77 KB
/
datevalidate.jspf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<%--
@(#)datevalidate.jspf 0.01 26/08/2018
Copyright (C) 2018 - 20xx MER-C
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<%--
Fetches and validates date ranges. Defaults may be supplied as the earliest_default
and latest_default request attributes. Adds the following local variables:
* earliest: the earliest date as a String, or the appropriate default value (pass to HTML)
* latest: the latest date as a String, or the appropriate default value (pass to HTML)
* earliest_ldate: the earliest date as a LocalDate, or the default value
* latest_ldate: the latest date as a LocalDate, or the default value
* earliest_odt: the earliest date as a OffsetDateTime, or the default value (pass to Wiki.RequestHelper)
* latest_odt: the latest date as an OffsetDateTime, or the default value (pass to Wiki.RequestHelper)
If the date range is invalid, an "error" attribute is added to the request.
--%>
<%
String earliest = ServletUtils.sanitizeForAttribute(request.getParameter("earliest"));
String latest = ServletUtils.sanitizeForAttribute(request.getParameter("latest"));
LocalDate earliest_ldate = (LocalDate)request.getAttribute("earliest_default");
LocalDate latest_ldate = (LocalDate)request.getAttribute("latest_default");
if (!earliest.equals(""))
earliest_ldate = LocalDate.parse(earliest);
else if (earliest_ldate != null)
earliest = earliest_ldate.format(DateTimeFormatter.ISO_LOCAL_DATE);
if (!latest.equals(""))
latest_ldate = LocalDate.parse(latest);
else if (latest_ldate != null)
latest = latest_ldate.format(DateTimeFormatter.ISO_LOCAL_DATE);
// validate dates
if (earliest_ldate != null && latest_ldate != null && earliest_ldate.isAfter(latest_ldate))
request.setAttribute("error", "Earliest date is after latest date!");
// cleanup
OffsetDateTime earliest_odt = null, latest_odt = null;
if (earliest_ldate != null)
earliest_odt = earliest_ldate.atTime(OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC));
if (latest_ldate != null)
latest_odt = latest_ldate.atTime(OffsetTime.of(23, 59, 59, 0, ZoneOffset.UTC));
%>