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