Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2013
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://license.openmrs.org
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  13. */
  14. package org.openmrs.web.controller.program;
  15.  
  16. import java.util.HashSet;
  17. import java.util.Set;
  18. import java.util.StringTokenizer;
  19.  
  20. import javax.servlet.ServletException;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import javax.servlet.http.HttpSession;
  24.  
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.openmrs.ProgramWorkflow;
  28. import org.openmrs.ProgramWorkflowState;
  29. import org.openmrs.api.APIException;
  30. import org.openmrs.api.ProgramWorkflowService;
  31. import org.openmrs.api.context.Context;
  32. import org.openmrs.web.WebConstants;
  33. import org.springframework.beans.propertyeditors.CustomNumberEditor;
  34. import org.springframework.dao.DataIntegrityViolationException;
  35. import org.springframework.validation.BindException;
  36. import org.springframework.web.bind.ServletRequestDataBinder;
  37. import org.springframework.web.servlet.ModelAndView;
  38. import org.springframework.web.servlet.mvc.SimpleFormController;
  39. import org.springframework.web.servlet.view.RedirectView;
  40.  
  41. public class WorkflowFormController extends SimpleFormController {
  42.  
  43. protected final Log log = LogFactory.getLog(getClass());
  44.  
  45. protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
  46. super.initBinder(request, binder);
  47. binder.registerCustomEditor(java.lang.Integer.class, new CustomNumberEditor(java.lang.Integer.class, true));
  48. }
  49.  
  50. /**
  51. * This is called prior to displaying a form for the first time. It tells Spring the
  52. * form/command object to load into the request
  53. *
  54. * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  55. */
  56. protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  57. log.debug("called formBackingObject");
  58.  
  59. ProgramWorkflow wf = null;
  60.  
  61. if (Context.isAuthenticated()) {
  62. ProgramWorkflowService ps = Context.getProgramWorkflowService();
  63. String programWorkflowId = request.getParameter("programWorkflowId");
  64. if (programWorkflowId != null)
  65. wf = ps.getWorkflow(Integer.valueOf(programWorkflowId));
  66.  
  67. if (wf == null)
  68. throw new IllegalArgumentException("Can't find workflow");
  69. }
  70.  
  71. if (wf == null)
  72. wf = new ProgramWorkflow();
  73.  
  74. return wf;
  75. }
  76.  
  77. /**
  78. * The onSubmit function receives the form/command object that was modified by the input form
  79. * and saves it to the db
  80. *
  81. * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  82. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  83. * org.springframework.validation.BindException)
  84. */
  85. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  86. BindException errors) throws Exception {
  87. log.debug("about to save " + obj);
  88.  
  89. HttpSession httpSession = request.getSession();
  90.  
  91. String view = getFormView();
  92.  
  93. if (Context.isAuthenticated()) {
  94. ProgramWorkflow wf = (ProgramWorkflow) obj;
  95.  
  96. if (request.getParameter("delete") != null) {
  97. String detstatesStr = request.getParameter("deletedStates");
  98. for (StringTokenizer st = new StringTokenizer(detstatesStr, "|"); st.hasMoreTokens();) {
  99. String str = st.nextToken();
  100. String[] tmp = str.split(",");
  101. Integer conceptId = Integer.valueOf(tmp[0]);
  102. ProgramWorkflowState pws = null;
  103. for (ProgramWorkflowState s : wf.getStates()) {
  104. if (s.getConcept().getConceptId().equals(conceptId)) {
  105. pws = s;
  106. break;
  107. }
  108. }
  109. if (pws == null) {
  110. System.out.println("not found ");
  111. } else
  112. System.out.println("found ");
  113. try {
  114. Context.getProgramWorkflowService().deleteProgramWorkflowState(pws);
  115. }
  116. catch (DataIntegrityViolationException e) {
  117. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
  118. }
  119. catch (APIException e) {
  120. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: "
  121. + e.getLocalizedMessage());
  122. }
  123.  
  124. view = getSuccessView();
  125. //httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Workflow.saved");
  126. return new ModelAndView(new RedirectView(view));
  127.  
  128. }
  129.  
  130. /*try {
  131. es.purgeEncounterType(encounterType);
  132. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "EncounterType.purgedSuccessfully");
  133. view = getSuccessView();
  134. }
  135. catch (DataIntegrityViolationException e) {
  136. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
  137. view = "encounterType.form?encounterTypeId=" + encounterType.getEncounterTypeId();
  138. }
  139. catch (APIException e) {
  140. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: " + e.getLocalizedMessage());
  141. view = "encounterType.form?encounterTypeId=" + encounterType.getEncounterTypeId();
  142. }*/
  143. }
  144.  
  145. // get list of states, and update the command object
  146. String statesStr = request.getParameter("newStates");
  147. // This is a brute-force algorithm, but n will be small.
  148. Set<Integer> doneSoFar = new HashSet<Integer>(); // concept ids done so far
  149. for (StringTokenizer st = new StringTokenizer(statesStr, "|"); st.hasMoreTokens();) {
  150. String str = st.nextToken();
  151. String[] tmp = str.split(",");
  152. Integer conceptId = Integer.valueOf(tmp[0]);
  153. doneSoFar.add(conceptId);
  154. ProgramWorkflowState pws = null;
  155. for (ProgramWorkflowState s : wf.getStates()) {
  156. if (s.getConcept().getConceptId().equals(conceptId)) {
  157. pws = s;
  158. break;
  159. }
  160. }
  161. if (pws == null) {
  162. pws = new ProgramWorkflowState();
  163. pws.setConcept(Context.getConceptService().getConcept(conceptId));
  164. wf.addState(pws);
  165. } else {
  166. // un-retire if necessary
  167. if (pws.isRetired()) {
  168. pws.setRetired(false);
  169. }
  170. }
  171. pws.setInitial(Boolean.valueOf(tmp[1]));
  172. pws.setTerminal(Boolean.valueOf(tmp[2]));
  173. log.debug("pws: " + pws);
  174. }
  175. // retire states if we didn't see their concept during the loop above
  176. for (ProgramWorkflowState s : wf.getStates()) {
  177. if (!doneSoFar.contains(s.getConcept().getConceptId())) {
  178. s.setRetired(true);
  179. }
  180. }
  181.  
  182. Context.getProgramWorkflowService().updateWorkflow(wf);
  183. view = getSuccessView();
  184. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Workflow.saved");
  185. }
  186.  
  187. return new ModelAndView(new RedirectView(view));
  188. }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement