]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/DateSelector.java
2557282abe8dad4b8737b9ff6fd6baa0e972d6d4
[gigi.git] / src / org / cacert / gigi / output / DateSelector.java
1 package org.cacert.gigi.output;
2
3 import java.io.PrintWriter;
4 import java.text.SimpleDateFormat;
5 import java.util.Arrays;
6 import java.util.Calendar;
7 import java.util.Map;
8 import java.util.TimeZone;
9
10 import javax.servlet.http.HttpServletRequest;
11
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.Outputable;
15 import org.cacert.gigi.util.CalendarUtil;
16 import org.cacert.gigi.util.DayDate;
17 import org.cacert.gigi.util.HTMLEncoder;
18
19 public class DateSelector implements Outputable {
20
21     private String[] names;
22
23     public DateSelector(String day, String month, String year, DayDate date) {
24         this(day, month, year);
25         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
26         cal.setTimeInMillis(date.getTime());
27         this.day = cal.get(Calendar.DAY_OF_MONTH);
28         this.month = cal.get(Calendar.MONTH) + 1;
29         this.year = cal.get(Calendar.YEAR);
30     }
31
32     public DateSelector(String day, String month, String year) {
33         this.names = new String[] {
34                 HTMLEncoder.encodeHTML(day), HTMLEncoder.encodeHTML(month), HTMLEncoder.encodeHTML(year)
35         };
36     }
37
38     private int day;
39
40     private int month;
41
42     private int year;
43
44     private static ThreadLocal<SimpleDateFormat> fmt = new ThreadLocal<>();
45
46     @Override
47     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
48         out.print("<nobr>");
49         outputYear(out);
50         outputMonth(out, l);
51         outputDay(out);
52         out.print("</nobr>");
53     }
54
55     private void outputDay(PrintWriter out) {
56         out.print("<select name=\"");
57         out.print(names[0]);
58         out.println("\">");
59         for (int i = 1; i <= 31; i++) {
60             out.print("<option");
61             if (i == day) {
62                 out.print(" selected=\"selected\"");
63             }
64             out.println(">" + i + "</option>");
65         }
66         out.println("</select>");
67     }
68
69     private void outputMonth(PrintWriter out, Language l) {
70         SimpleDateFormat sdf = new SimpleDateFormat("MMMM", l.getLocale());
71         out.print("<select name=\"");
72         out.print(names[1]);
73         out.println("\">");
74         Calendar c = sdf.getCalendar();
75         for (int i = 1; i <= 12; i++) {
76             c.set(Calendar.MONTH, i - 1);
77             out.print("<option value='" + i + "'");
78             if (i == month) {
79                 out.print(" selected=\"selected\"");
80             }
81             out.println(">" + sdf.format(c.getTime()) + " (" + i + ")</option>");
82         }
83         out.println("</select>");
84     }
85
86     private void outputYear(PrintWriter out) {
87         out.print("<input type=\"text\" name=\"");
88         out.print(names[2]);
89         out.print("\" value=\"");
90         if (year != 0) {
91             out.print(year);
92         }
93         out.println("\" size=\"4\" autocomplete=\"off\">");
94     }
95
96     public void update(HttpServletRequest r) throws GigiApiException {
97         try {
98             String dayS = r.getParameter(names[0]);
99             if (dayS != null) {
100                 day = Integer.parseInt(dayS);
101             }
102
103             String monthS = r.getParameter(names[1]);
104             if (monthS != null) {
105                 month = Integer.parseInt(monthS);
106             }
107
108             String yearS = r.getParameter(names[2]);
109             if (yearS != null) {
110                 year = Integer.parseInt(yearS);
111             }
112         } catch (NumberFormatException e) {
113             throw new GigiApiException("Unparsable date.");
114         }
115     }
116
117     public boolean isValid() {
118         if ( !(1890 < year && 1 <= month && month <= 12 && 1 <= day && day <= 32)) {
119             return false;
120         }
121
122         if ( !CalendarUtil.isDateValid(year, month, day)) {
123             return false;
124         }
125
126         return true;
127     }
128
129     @Override
130     public String toString() {
131         return "DateSelector [names=" + Arrays.toString(names) + ", day=" + day + ", month=" + month + ", year=" + year + "]";
132     }
133
134     public DayDate getDate() {
135         return CalendarUtil.getDateFromComponents(year, month, day);
136     }
137
138     public static SimpleDateFormat getDateFormat() {
139         SimpleDateFormat local = fmt.get();
140         if (local == null) {
141             local = new SimpleDateFormat("yyyy-MM-dd");
142             local.setLenient(false);
143             local.setTimeZone(TimeZone.getTimeZone("UTC"));
144             fmt.set(local);
145         }
146         return local;
147     }
148
149 }