harshadura

d

Jul 16th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. package org.openmrs.module.patientnarratives.web.controller;
  2.  
  3. import java.io.PrintWriter;
  4. import java.io.StringWriter;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. import org.apache.commons.logging.Log;
  13. import org.apache.commons.logging.LogFactory;
  14. import org.openmrs.Encounter;
  15. import org.openmrs.Form;
  16. import org.openmrs.Patient;
  17. import org.openmrs.api.context.Context;
  18. import org.openmrs.module.htmlformentry.BadFormDesignException;
  19. import org.openmrs.module.htmlformentry.FormEntryContext.Mode;
  20. import org.openmrs.module.htmlformentry.FormEntrySession;
  21. import org.openmrs.module.htmlformentry.FormSubmissionError;
  22. import org.openmrs.module.htmlformentry.HtmlForm;
  23. import org.openmrs.module.htmlformentry.HtmlFormEntryUtil;
  24. import org.openmrs.module.htmlformentry.ValidationException;
  25. import org.openmrs.util.OpenmrsUtil;
  26. import org.openmrs.web.controller.PortletController;
  27. import org.springframework.stereotype.Controller;
  28. import org.springframework.ui.Model;
  29. import org.springframework.util.StringUtils;
  30. import org.springframework.validation.Errors;
  31. import org.springframework.web.bind.annotation.ModelAttribute;
  32. import org.springframework.web.bind.annotation.RequestMapping;
  33. import org.springframework.web.bind.annotation.RequestMethod;
  34. import org.springframework.web.bind.annotation.RequestParam;
  35. import org.springframework.web.servlet.ModelAndView;
  36. import org.springframework.web.servlet.view.RedirectView;
  37.  
  38. /**
  39. * The controller for entering/viewing a form.
  40. * <p/>
  41. * Handles {@code htmlFormEntry.form} requests. Renders view {@code htmlFormEntry.jsp}.
  42. * <p/>
  43. * TODO: This has a bit too much logic in the onSubmit method. Move that into the FormEntrySession.
  44. */
  45. public class HtmlFormEntryPortletController extends PortletController {
  46.  
  47. protected final Log log = LogFactory.getLog(getClass());
  48. // public final static String closeDialogView = "/module/htmlformentry/closeDialog";
  49. public final static String FORM_IN_PROGRESS_KEY = "HTML_FORM_IN_PROGRESS_KEY";
  50. public final static String FORM_IN_PROGRESS_VALUE = "HTML_FORM_IN_PROGRESS_VALUE";
  51. public final static String FORM_PATH = "/module/patientnarratives/htmlFormEntry";
  52.  
  53. @Override
  54. protected void populateModel(HttpServletRequest request, Map<String, Object> model) {
  55.  
  56. model.put("command", getFormEntrySession(request));
  57. }
  58.  
  59. public FormEntrySession getFormEntrySession(HttpServletRequest request){
  60.  
  61. long ts = System.currentTimeMillis();
  62. Mode mode = Mode.VIEW;
  63.  
  64. String returnUrl = null;
  65. Long formModifiedTimestamp = null;
  66. Long encounterModifiedTimestamp = null;
  67. String hasChangedInd = null;
  68.  
  69. String globalPropertyFormId = Context.getAdministrationService().getGlobalProperty("patientnarratives.formid");
  70. String globalPropertyPatientId = Context.getAdministrationService().getGlobalProperty("patientnarratives.patientid");
  71.  
  72. Integer formId = Integer.valueOf(globalPropertyFormId);
  73. Integer patientId = Integer.valueOf(globalPropertyPatientId);
  74.  
  75. Integer personId = null;
  76. Integer htmlFormId = null;
  77.  
  78. String modeParam = request.getParameter("mode");
  79. if ("enter".equalsIgnoreCase(modeParam)) {
  80. mode = Mode.ENTER;
  81. }
  82. else if ("edit".equalsIgnoreCase(modeParam)) {
  83. mode = Mode.EDIT;
  84. }
  85.  
  86. Patient patient = null;
  87. Encounter encounter = null;
  88. Form form = null;
  89. HtmlForm htmlForm = null;
  90.  
  91. if (StringUtils.hasText(request.getParameter("encounterId"))) {
  92.  
  93. Integer encounterId = Integer.valueOf(request.getParameter("encounterId"));
  94. encounter = Context.getEncounterService().getEncounter(encounterId);
  95. if (encounter == null)
  96. throw new IllegalArgumentException("No encounter with id=" + encounterId);
  97. patient = encounter.getPatient();
  98. patientId = patient.getPatientId();
  99. personId = patient.getPersonId();
  100.  
  101. if (formId != null) { // I think formId is allowed to differ from encounter.form.id because of HtmlFormFlowsheet
  102. form = Context.getFormService().getForm(formId);
  103. htmlForm = HtmlFormEntryUtil.getService().getHtmlFormByForm(form);
  104. if (htmlForm == null)
  105. throw new IllegalArgumentException("No HtmlForm associated with formId " + formId);
  106. } else {
  107. form = encounter.getForm();
  108. htmlForm = HtmlFormEntryUtil.getService().getHtmlFormByForm(encounter.getForm());
  109. if (htmlForm == null)
  110. throw new IllegalArgumentException("The form for the specified encounter (" + encounter.getForm() + ") does not have an HtmlForm associated with it");
  111. }
  112.  
  113. } else { // no encounter specified
  114.  
  115. // get person from patientId/personId (register module uses patientId, htmlformentry uses personId)
  116. if (patientId != null) {
  117. personId = patientId;
  118. }
  119. if (personId != null) {
  120. patient = Context.getPatientService().getPatient(personId);
  121. }
  122.  
  123. // determine form
  124. if (htmlFormId != null) {
  125. htmlForm = HtmlFormEntryUtil.getService().getHtmlForm(htmlFormId);
  126. } else if (formId != null) {
  127. form = Context.getFormService().getForm(formId);
  128. htmlForm = HtmlFormEntryUtil.getService().getHtmlFormByForm(form);
  129. }
  130. if (htmlForm == null) {
  131. throw new IllegalArgumentException("You must specify either an htmlFormId or a formId for a valid html form");
  132. }
  133.  
  134. String which = request.getParameter("which");
  135. if (StringUtils.hasText(which)) {
  136. if (patient == null)
  137. throw new IllegalArgumentException("Cannot specify 'which' without specifying a person/patient");
  138. List<Encounter> encs = Context.getEncounterService().getEncounters(patient, null, null, null, Collections.singleton(form), null, null, false);
  139. if (which.equals("first")) {
  140. encounter = encs.get(0);
  141. } else if (which.equals("last")) {
  142. encounter = encs.get(encs.size() - 1);
  143. } else {
  144. throw new IllegalArgumentException("which must be 'first' or 'last'");
  145. }
  146. }
  147. }
  148.  
  149. try{
  150. if (mode != Mode.ENTER && patient == null)
  151. throw new IllegalArgumentException("No patient with id of personId=" + personId + " or patientId=" + patientId);
  152.  
  153. FormEntrySession session = null;
  154. if (mode == Mode.ENTER && patient == null) {
  155. patient = new Patient();
  156. }
  157. if (encounter != null) {
  158. session = new FormEntrySession(patient, encounter, mode, htmlForm, request.getSession());
  159. }
  160. else {
  161. session = new FormEntrySession(patient, htmlForm, request.getSession());
  162. }
  163.  
  164. Context.setVolatileUserData(FORM_IN_PROGRESS_KEY, session);
  165.  
  166. log.info("Took " + (System.currentTimeMillis() - ts) + " ms");
  167.  
  168. if (StringUtils.hasText(returnUrl)) {
  169. session.setReturnUrl(returnUrl);
  170. }
  171.  
  172. // Since we're not using a sessionForm, we need to check for the case where the underlying form was modified while a user was filling a form out
  173. if (formModifiedTimestamp != null) {
  174. if (!OpenmrsUtil.nullSafeEquals(formModifiedTimestamp, session.getFormModifiedTimestamp())) {
  175. throw new RuntimeException(Context.getMessageSourceService().getMessage("htmlformentry.error.formModifiedBeforeSubmission"));
  176. }
  177. }
  178.  
  179. // Since we're not using a sessionForm, we need to make sure this encounter hasn't been modified since the user opened it
  180. if (encounter != null) {
  181. if (encounterModifiedTimestamp != null && !OpenmrsUtil.nullSafeEquals(encounterModifiedTimestamp, session.getEncounterModifiedTimestamp())) {
  182. throw new RuntimeException(Context.getMessageSourceService().getMessage("htmlformentry.error.encounterModifiedBeforeSubmission"));
  183. }
  184. }
  185.  
  186. if (hasChangedInd != null) session.setHasChangedInd(hasChangedInd);
  187.  
  188. return session;
  189. }catch(Exception e){
  190.  
  191. }
  192. return null;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment