Advertisement
k-joseph

Untitled

Jul 18th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  2. BindException errors) throws Exception {
  3.  
  4. HttpSession httpSession = request.getSession();
  5. String view = getFormView();
  6.  
  7. if (Context.isAuthenticated()) {
  8. Form form = (Form) obj;
  9. MessageSourceAccessor msa = getMessageSourceAccessor();
  10. String action = request.getParameter("action");
  11. if (action == null) {
  12. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.not.saved");
  13. } else {
  14. if (action.equals(msa.getMessage("Form.save"))) {
  15. try {
  16. // save form
  17. form = Context.getFormService().saveForm(form);
  18. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Form.saved");
  19. }
  20. catch (Exception e) {
  21. log.error("Error while saving form " + form.getFormId(), e);
  22. errors.reject(e.getMessage());
  23. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.not.saved");
  24. return showForm(request, response, errors);
  25. }
  26. } else if (action.equals(msa.getMessage("Form.delete"))) {
  27. try {
  28. Context.getFormService().purgeForm(form);
  29. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Form.deleted");
  30. }
  31. catch (DataIntegrityViolationException e) {
  32. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.cannot.delete");
  33. return new ModelAndView(new RedirectView("formEdit.form?formId=" + form.getFormId()));
  34. }
  35. catch (Exception e) {
  36. log.error("Error while deleting form " + form.getFormId(), e);
  37. errors.reject(e.getMessage());
  38. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.cannot.delete");
  39. return showForm(request, response, errors);
  40. //return new ModelAndView(new RedirectView(getSuccessView()));
  41. }
  42. } else if (action.equals(msa.getMessage("Form.updateSortOrder"))) {
  43.  
  44. FormService fs = Context.getFormService();
  45.  
  46. TreeMap<Integer, TreeSet<FormField>> treeMap = FormUtil.getFormStructure(form);
  47. for (Integer parentFormFieldId : treeMap.keySet()) {
  48. float sortWeight = 0;
  49. for (FormField formField : treeMap.get(parentFormFieldId)) {
  50. formField.setSortWeight(sortWeight);
  51. fs.saveFormField(formField);
  52. sortWeight += 50;
  53. }
  54. }
  55.  
  56. } else {
  57. try {
  58. Context.getFormService().duplicateForm(form);
  59. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Form.duplicated");
  60. }
  61. catch (Exception e) {
  62. log.error("Error while duplicating form " + form.getFormId(), e);
  63. errors.reject(e.getMessage());
  64. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.cannot.duplicate");
  65. return showForm(request, response, errors);
  66. }
  67. }
  68.  
  69. view = getSuccessView();
  70. }
  71. }
  72.  
  73. return new ModelAndView(new RedirectView(view));
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement