Advertisement
Guest User

Untitled

a guest
Dec 30th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 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.person;
  15.  
  16. import javax.servlet.ServletException;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import javax.servlet.http.HttpSession;
  20.  
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.openmrs.RelationshipType;
  24. import org.openmrs.api.APIException;
  25. import org.openmrs.api.PersonService;
  26. import org.openmrs.api.context.Context;
  27. import org.openmrs.validator.RelationshipTypeValidator;
  28. import org.openmrs.web.WebConstants;
  29. import org.springframework.beans.propertyeditors.CustomNumberEditor;
  30. import org.springframework.dao.DataIntegrityViolationException;
  31. import org.springframework.util.StringUtils;
  32. import org.springframework.validation.BindException;
  33. import org.springframework.web.bind.ServletRequestDataBinder;
  34. import org.springframework.web.servlet.ModelAndView;
  35. import org.springframework.web.servlet.mvc.SimpleFormController;
  36. import org.springframework.web.servlet.view.RedirectView;
  37.  
  38. public class RelationshipTypeFormController extends SimpleFormController {
  39.  
  40. /** Logger for this class and subclasses */
  41. protected final Log log = LogFactory.getLog(getClass());
  42.  
  43. /**
  44. * Allows for Integers to be used as values in input tags. Normally, only strings and lists are
  45. * expected
  46. *
  47. * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest,
  48. * org.springframework.web.bind.ServletRequestDataBinder)
  49. */
  50. protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
  51. super.initBinder(request, binder);
  52. //NumberFormat nf = NumberFormat.getInstance(new Locale("en_US"));
  53. binder.registerCustomEditor(java.lang.Integer.class, new CustomNumberEditor(java.lang.Integer.class, true));
  54. }
  55.  
  56. /**
  57. * @see org.springframework.web.servlet.mvc.SimpleFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
  58. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  59. * org.springframework.validation.BindException)
  60. */
  61. @Override
  62. protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command,
  63. BindException errors) throws Exception {
  64.  
  65. RelationshipType type = (RelationshipType) command;
  66.  
  67. new RelationshipTypeValidator().validate(type, errors);
  68. if (type.getaIsToB() == null || type.getaIsToB().equals(""))
  69. errors.rejectValue("aIsToB", "RelationshipType.aIsToB.required");
  70.  
  71. if (type.getbIsToA() == null || type.getbIsToA().equals(""))
  72. errors.rejectValue("bIsToA", "RelationshipType.bIsToA.required");
  73.  
  74. if (!StringUtils.hasText(type.getDescription()))
  75. errors.rejectValue("description", "error.required", new Object[] { Context.getMessageSourceService().getMessage(
  76. "general.description") }, null);
  77.  
  78. return super.processFormSubmission(request, response, type, errors);
  79. }
  80.  
  81. /**
  82. * The onSubmit function receives the form/command object that was modified by the input form
  83. * and saves it to the db
  84. *
  85. * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  86. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  87. * org.springframework.validation.BindException)
  88. */
  89. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  90. BindException errors) throws Exception {
  91.  
  92. HttpSession httpSession = request.getSession();
  93.  
  94. String view = getFormView();
  95.  
  96. if (Context.isAuthenticated()) {
  97. RelationshipType relationshipType = (RelationshipType) obj;
  98. PersonService ps = Context.getPersonService();
  99.  
  100. //to save the relationship type
  101. if (request.getParameter("save") != null) {
  102. ps.saveRelationshipType(relationshipType);
  103. view = getSuccessView();
  104. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.saved");
  105. }
  106.  
  107. // if the user is retiring out the relationshipType
  108. else if (request.getParameter("retire") != null) {
  109. String retireReason = request.getParameter("retireReason");
  110. if (relationshipType.getRelationshipTypeId() != null && !(StringUtils.hasText(retireReason))) {
  111. errors.reject("retireReason", "general.retiredReason.empty");
  112. return showForm(request, response, errors);
  113. }
  114.  
  115. ps.retireRelationshipType(relationshipType, retireReason);
  116. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.retiredSuccessfully");
  117.  
  118. view = getSuccessView();
  119. }
  120.  
  121. // if the user is purging the relationshipType
  122. else if (request.getParameter("purge") != null) {
  123. try {
  124. ps.purgeRelationshipType(relationshipType);
  125. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.purgedSuccessfully");
  126. view = getSuccessView();
  127. }
  128. catch (DataIntegrityViolationException e) {
  129. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
  130. return showForm(request, response, errors);
  131. }
  132. catch (APIException e) {
  133. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: " + e.getLocalizedMessage());
  134. return showForm(request, response, errors);
  135. }
  136. }
  137. // if the user unretiring relationship type
  138. else if (request.getParameter("unretire") != null) {
  139. ps.unretireRelationshipType(relationshipType);
  140. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.unretiredSuccessfully");
  141. view = getSuccessView();
  142. }
  143.  
  144. }
  145.  
  146. return new ModelAndView(new RedirectView(view));
  147. }
  148.  
  149. /**
  150. * This is called prior to displaying a form for the first time. It tells Spring the
  151. * form/command object to load into the request
  152. *
  153. * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  154. */
  155. protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  156.  
  157. RelationshipType identifierType = null;
  158.  
  159. if (Context.isAuthenticated()) {
  160. PersonService ps = Context.getPersonService();
  161. String relationshipTypeId = request.getParameter("relationshipTypeId");
  162. if (relationshipTypeId != null)
  163. identifierType = ps.getRelationshipType(Integer.valueOf(relationshipTypeId));
  164. }
  165.  
  166. if (identifierType == null)
  167. identifierType = new RelationshipType();
  168.  
  169. return identifierType;
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement