@Override protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object commandObject, BindException errors) throws Exception { FormEntrySession session = (FormEntrySession) commandObject; try { session.prepareForSubmit(); session.getSubmissionController().handleFormSubmission(session, request); if (session.getContext().getMode() == Mode.ENTER && (session.getSubmissionActions().getEncountersToCreate() == null || session.getSubmissionActions().getEncountersToCreate().size() == 0)) throw new IllegalArgumentException("This form is not going to create an encounter"); session.applyActions(); String successView = session.getReturnUrlWithParameters(); if (successView == null) successView = getSuccessView() + "?patientId=" + session.getPatient().getPersonId(); if (StringUtils.hasText(request.getParameter("closeAfterSubmission"))) { return new ModelAndView( closeDialogView, "dialogToClose", request.getParameter("closeAfterSubmission")); } else { return new ModelAndView(new RedirectView(successView)); } } catch (BadFormDesignException ex) { log.error("Bad Form Design:", ex); errors.reject(ex.getMessage()); return showForm(request, response, errors); } catch (Exception ex) { log.error("Exception trying to submit form", ex); StringWriter sw = new StringWriter(); ex.printStackTrace(new PrintWriter(sw)); errors.reject("Exception! " + ex.getMessage() + "<br/>" + sw.toString()); return showForm(request, response, errors); } }
/* * I'm using a return type of ModelAndView so I can use RedirectView rather than "redirect:" and preserve the fact that * returnUrl values from the pre-annotated-controller days will have the context path already */ @RequestMapping(method = RequestMethod.POST, value = FORM_PATH) public ModelAndView handleSubmit( @ModelAttribute("command") FormEntrySession session, Errors errors, HttpServletRequest request, Model model) throws Exception { try { List<FormSubmissionError> validationErrors = session.getSubmissionController().validateSubmission(session.getContext(), request); if (validationErrors != null && validationErrors.size() > 0) { errors.reject("Fix errors"); } } catch (Exception ex) { log.error("Exception during form validation", ex); errors.reject("Exception during form validation, see log for more details: " + ex); } if (errors.hasErrors()) { return new ModelAndView(FORM_PATH, "command", session); } // no form validation errors, proceed with submission session.prepareForSubmit(); if (session.getContext().getMode() == Mode.ENTER && session.hasPatientTag() && session.getPatient() == null && (session.getSubmissionActions().getPersonsToCreate() == null || session.getSubmissionActions().getPersonsToCreate().size() == 0)) throw new IllegalArgumentException("This form is not going to create an Patient"); if (session.getContext().getMode() == Mode.ENTER && session.hasEncouterTag() && (session.getSubmissionActions().getEncountersToCreate() == null || session.getSubmissionActions().getEncountersToCreate().size() == 0)) throw new IllegalArgumentException("This form is not going to create an encounter"); try { session.getSubmissionController().handleFormSubmission(session, request); session.applyActions(); String successView = session.getReturnUrlWithParameters(); if (successView == null) successView = request.getContextPath() + "/patientDashboard.form" + getQueryPrameters(request, session); if (StringUtils.hasText(request.getParameter("closeAfterSubmission"))) { return new ModelAndView( closeDialogView, "dialogToClose", request.getParameter("closeAfterSubmission")); } else { return new ModelAndView(new RedirectView(successView)); } } catch (ValidationException ex) { log.error("Invalid input:", ex); errors.reject(ex.getMessage()); } catch (BadFormDesignException ex) { log.error("Bad Form Design:", ex); errors.reject(ex.getMessage()); } catch (Exception ex) { log.error("Exception trying to submit form", ex); StringWriter sw = new StringWriter(); ex.printStackTrace(new PrintWriter(sw)); errors.reject("Exception! " + ex.getMessage() + "<br/>" + sw.toString()); } // if we get here it's because we caught an error trying to submit/apply return new ModelAndView(FORM_PATH, "command", session); }
@RequestMapping(FORM_PATH) public ModelAndView handleRequest(HttpServletRequest request) { returnUrl = request.getContextPath() + "/module/patientnarratives/patientNarrativesForm.form"; HtmlFormEntryPortletController htmlFormEntryPortletController; FormEntrySession session = null; try { htmlFormEntryPortletController = new HtmlFormEntryPortletController(); session = htmlFormEntryPortletController.getFormEntrySession(request); List<FormSubmissionError> validationErrors = session.getSubmissionController().validateSubmission(session.getContext(), request); if (validationErrors != null && validationErrors.size() > 0) { // errors.reject("Fix errors"); } } catch (Exception ex) { log.error("Exception during form validation", ex); // errors.reject("Exception during form validation, see log for more details: " + ex); } // if (errors.hasErrors()) { // return new ModelAndView(FORM_PATH, "command", session); // } // no form validation errors, proceed with submission try { session.prepareForSubmit(); if (session.getContext().getMode() == FormEntryContext.Mode.ENTER && session.hasPatientTag() && session.getPatient() == null && (session.getSubmissionActions().getPersonsToCreate() == null || session.getSubmissionActions().getPersonsToCreate().size() == 0)) throw new IllegalArgumentException("This form is not going to create an Patient"); if (session.getContext().getMode() == FormEntryContext.Mode.ENTER && session.hasEncouterTag() && (session.getSubmissionActions().getEncountersToCreate() == null || session.getSubmissionActions().getEncountersToCreate().size() == 0)) throw new IllegalArgumentException("This form is not going to create an encounter"); session.getSubmissionController().handleFormSubmission(session, request); HtmlFormEntryUtil.getService().applyActions(session); String successView = session.getReturnUrlWithParameters(); request .getSession() .setAttribute( WebConstants.OPENMRS_MSG_ATTR, "patientnarratives.module.narrative.save.success"); if (successView == null) successView = request.getContextPath() + "/module/patientnarratives/patientNarrativesForm.form"; if (StringUtils.hasText(request.getParameter("closeAfterSubmission"))) { return new ModelAndView(new RedirectView(returnUrl)); // return new ModelAndView(closeDialogView, "dialogToClose", // request.getParameter("closeAfterSubmission")); } else { return new ModelAndView(new RedirectView(returnUrl)); // return new ModelAndView(new RedirectView(successView)); } } catch (ValidationException ex) { log.error("Invalid input:", ex); // errors.reject(ex.getMessage()); } catch (BadFormDesignException ex) { log.error("Bad Form Design:", ex); // errors.reject(ex.getMessage()); } catch (Exception ex) { log.error("Exception trying to submit form", ex); StringWriter sw = new StringWriter(); ex.printStackTrace(new PrintWriter(sw)); // errors.reject("Exception! " + ex.getMessage() + "<br/>" + sw.toString()); } // if ((alert != null) && (alert == true)) { // } // if we get here it's because we caught an error trying to submit/apply // return new ModelAndView(returnUrl, "command", session); return new ModelAndView(new RedirectView(returnUrl)); }