1 package club.wpia.gigi.output;
3 import java.io.PrintWriter;
5 import java.text.ParseException;
8 import javax.servlet.http.HttpServletRequest;
10 import club.wpia.gigi.GigiApiException;
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.template.Outputable;
13 import club.wpia.gigi.util.HTMLEncoder;
15 public class CertificateValiditySelector implements Outputable {
17 private static final long DAY = 1000 * 60 * 60 * 24;
21 private String val = "2y";
23 public CertificateValiditySelector() {
28 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
29 out.print("<label for='validFrom'>");
30 out.println(l.getTranslation("start: "));
31 out.print("</label><select name='validFrom'><option value='now'");
33 out.print(" selected='selected'");
36 out.print(l.getTranslation("now"));
37 out.print("</option>");
38 long base = getCurrentDayBase();
39 for (int i = 0; i < 14; i++) {
40 long date = base + DAY * i;
41 String d = DateSelector.getDateFormat().format(new Date(date));
42 out.print("<option value='");
45 if (from != null && from.getTime() == date) {
46 out.print(" selected='selected'");
50 out.println("</option>");
52 out.println("</select>");
54 out.print("<label for='validity'>");
55 out.println(l.getTranslation("end: "));
56 out.print("</label>");
57 out.print("<input type='text' name='validity' value='");
58 out.print(HTMLEncoder.encodeHTML(val));
67 private long getCurrentDayBase() {
68 long base = System.currentTimeMillis();
74 public void update(HttpServletRequest r) throws GigiApiException {
75 String from = r.getParameter("validFrom");
77 GigiApiException gae = new GigiApiException();
80 } catch (GigiApiException e) {
84 String validity = r.getParameter("validity");
85 if (validity != null) {
86 checkValidityLength(validity);
89 } catch (GigiApiException e) {
92 if ( !gae.isEmpty()) {
98 public static void checkValidityLength(String newval) throws GigiApiException {
99 if (newval.endsWith("y") || newval.endsWith("m")) {
100 if (newval.length() > 10) { // for database
101 throw new GigiApiException("The validity interval entered is invalid.");
103 String num = newval.substring(0, newval.length() - 1);
105 int len = Integer.parseInt(num);
107 throw new GigiApiException("The validity interval entered is invalid.");
109 } catch (NumberFormatException e) {
110 throw new GigiApiException("The validity interval entered is invalid.");
114 DateSelector.getDateFormat().parse(newval);
115 } catch (ParseException e) {
116 throw new GigiApiException("The validity interval entered is invalid.");
121 private void saveStartDate(String from) throws GigiApiException {
122 if (from == null || "now".equals(from)) {
126 this.from = new Date(DateSelector.getDateFormat().parse(from).getTime());
127 } catch (ParseException e) {
128 throw new GigiApiException("The validity start date entered is invalid.");
133 public Date getFrom() {
137 public String getTo() {