]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/wot/AssurePage.java
upd: enforce pattern of making templates static and final.
[gigi.git] / src / org / cacert / gigi / pages / wot / AssurePage.java
index 39a5aa901b83d6a529dec7c733f04fc352b8ce04..4603e8436263e12634f697dbeed91acda975d834 100644 (file)
@@ -2,8 +2,6 @@ package org.cacert.gigi.pages.wot;
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.sql.Date;
-import java.util.Calendar;
 import java.util.HashMap;
 
 import javax.servlet.http.HttpServletRequest;
@@ -27,7 +25,7 @@ public class AssurePage extends Page {
 
     DateSelector ds = new DateSelector("day", "month", "year");
 
-    Template t;
+    private final Template t;
 
     public AssurePage() {
         super("Assure someone");
@@ -49,36 +47,34 @@ public class AssurePage extends Page {
         return ac != null && ac.canAssure();
     }
 
-    private void outputForm(HttpServletRequest req, PrintWriter out, AssuranceForm form) {
-        User myself = LoginPage.getUser(req);
-        try {
-            Notary.checkAssuranceIsPossible(myself, form.getAssuree());
-        } catch (GigiApiException e) {
-            e.format(out, Page.getLanguage(req));
-        }
-
-        form.output(out, getLanguage(req), new HashMap<String, Object>());
-    }
-
     @Override
     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         PrintWriter out = resp.getWriter();
         if (req.getParameter("search") == null) {
             AssuranceForm form = Form.getForm(req, AssuranceForm.class);
-            if (form.submit(out, req)) {
-                out.println(translate(req, "Assurance complete."));
-            } else {
-                outputForm(req, resp.getWriter(), form);
+            try {
+                if (form.submit(out, req)) {
+                    out.println(translate(req, "Assurance complete."));
+                    return;
+                }
+            } catch (GigiApiException e) {
+                e.format(out, Page.getLanguage(req));
+                try {
+                    Notary.checkAssuranceIsPossible(LoginPage.getUser(req), form.getAssuree());
+                    form.output(out, getLanguage(req), new HashMap<String, Object>());
+                } catch (GigiApiException e1) {
+                    e1.format(out, Page.getLanguage(req));
+                }
             }
 
             return;
         }
 
         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id`, `verified` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id`=`users`.`id` WHERE `email`=? AND `dob`=? AND `deleted` IS NULL")) {
+            ds.update(req);
+
             ps.setString(1, req.getParameter("email"));
-            Calendar c = Calendar.getInstance();
-            c.set(Integer.parseInt(req.getParameter("year")), Integer.parseInt(req.getParameter("month")) - 1, Integer.parseInt(req.getParameter("day")));
-            ps.setDate(2, new Date(c.getTimeInMillis()));
+            ps.setDate(2, ds.getDate().toSQLDate());
             GigiResultSet rs = ps.executeQuery();
             int id = 0;
             if (rs.next()) {
@@ -92,17 +88,23 @@ public class AssurePage extends Page {
                     } else if (getUser(req).getId() == id) {
 
                     } else {
-                        AssuranceForm form = new AssuranceForm(req, User.getById(id));
-                        outputForm(req, out, form);
+                        User assuree = User.getById(id);
+                        User myself = LoginPage.getUser(req);
+                        try {
+                            Notary.checkAssuranceIsPossible(myself, assuree);
+                            new AssuranceForm(req, assuree).output(out, getLanguage(req), new HashMap<String, Object>());
+                        } catch (GigiApiException e) {
+                            e.format(out, Page.getLanguage(req));
+                        }
                     }
                 }
             } else {
-                out.print("<div class='formError'>");
-
-                out.println(translate(req, "I'm sorry, there was no email and date of birth matching" + " what you entered in the system. Please double check" + " your information."));
-                out.print("</div>");
+                throw new GigiApiException("I'm sorry, there was no email and date of birth matching" //
+                        + " what you entered in the system. Please double check your information.");
             }
 
+        } catch (GigiApiException e) {
+            e.format(out, getLanguage(req));
         }
     }
 }