+ /**
+ * Update the forms internal state based on submitted data.
+ *
+ * @param req
+ * the request to take the initial data from.
+ * @return true, iff the form succeeded and the user should be redirected.
+ * @throws GigiApiException
+ * if form data had problems or operations went wrong.
+ */
+ public abstract SubmissionResult submit(HttpServletRequest req) throws GigiApiException;
+
+ public boolean submitExceptionProtected(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ try {
+ SubmissionResult res = submit(req);
+ req.setAttribute(SUBMIT_RESULT, res);
+ if (res instanceof RedirectResult) {
+ resp.sendRedirect(((RedirectResult) res).target);
+ return true;
+ }
+ if (res.endsForm()) {
+ HttpSession hs = req.getSession();
+ hs.removeAttribute("form/" + getClass().getName() + "/" + csrf);
+ }
+ return false;
+ } catch (PermamentFormException e) {
+ req.setAttribute(SUBMIT_RESULT, e);
+ return false;
+ } catch (GigiApiException e) {
+ req.setAttribute(SUBMIT_RESULT, e);
+ return false;
+ }
+ }
+
+ /**
+ * Prints any errors in any form submits on this request.
+ *
+ * @param req
+ * The request to extract the errors from.
+ * @param out
+ * the output stream to the user to write the errors to.
+ * @return true if no permanent errors occurred and the form should be
+ * reprinted (and it has not already been successfully submitted)
+ */
+ public static boolean printFormErrors(HttpServletRequest req, PrintWriter out) {
+ Object o = req.getAttribute(SUBMIT_RESULT);
+ if (o != null && (o instanceof PermamentFormException)) {
+ ((PermamentFormException) o).getCause().format(out, Page.getLanguage(req));
+ return false;
+ }
+ if (o != null && (o instanceof GigiApiException)) {
+ ((GigiApiException) o).format(out, Page.getLanguage(req));
+ return true;
+ }
+ if (o != null && (o instanceof FormContinue)) {
+ return true;
+ }
+ if (o != null && (o instanceof SuccessMessageResult)) {
+ Outputable message = ((SuccessMessageResult) o).message;
+ if (message != null) {
+ out.println("<div class='alert alert-success'>");
+ message.output(out, Page.getLanguage(req), new HashMap<String, Object>());
+ out.println("</div>");
+ }
+ return false;
+ }
+ return true;
+ }