Advertisement
Guest User

Untitled

a guest
Dec 31st, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 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. if (!StringUtils.hasText(type.getDescription()))
  68. errors.rejectValue("description", "error.required", new Object[] { Context.getMessageSourceService().getMessage(
  69. "general.description") }, null);
  70.  
  71. new RelationshipTypeValidator().validate(type, errors);
  72.  
  73. return super.processFormSubmission(request, response, type, errors);
  74. }
  75.  
  76. /**
  77. * The onSubmit function receives the form/command object that was modified by the input form
  78. * and saves it to the db
  79. *
  80. * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  81. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  82. * org.springframework.validation.BindException)
  83. */
  84. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  85. BindException errors) throws Exception {
  86.  
  87. HttpSession httpSession = request.getSession();
  88.  
  89. String view = getFormView();
  90.  
  91. if (Context.isAuthenticated()) {
  92. RelationshipType relationshipType = (RelationshipType) obj;
  93. PersonService ps = Context.getPersonService();
  94.  
  95. //to save the relationship type
  96. if (request.getParameter("save") != null) {
  97. ps.saveRelationshipType(relationshipType);
  98. view = getSuccessView();
  99. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.saved");
  100. }
  101.  
  102. // if the user is retiring out the relationshipType
  103. else if (request.getParameter("retire") != null) {
  104. String retireReason = request.getParameter("retireReason");
  105. if (relationshipType.getRelationshipTypeId() != null && !(StringUtils.hasText(retireReason))) {
  106. errors.reject("retireReason", "general.retiredReason.empty");
  107. return showForm(request, response, errors);
  108. }
  109.  
  110. ps.retireRelationshipType(relationshipType, retireReason);
  111. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.retiredSuccessfully");
  112.  
  113. view = getSuccessView();
  114. }
  115.  
  116. // if the user is purging the relationshipType
  117. else if (request.getParameter("purge") != null) {
  118. try {
  119. ps.purgeRelationshipType(relationshipType);
  120. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.purgedSuccessfully");
  121. view = getSuccessView();
  122. }
  123. catch (DataIntegrityViolationException e) {
  124. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
  125. return showForm(request, response, errors);
  126. }
  127. catch (APIException e) {
  128. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: " + e.getLocalizedMessage());
  129. return showForm(request, response, errors);
  130. }
  131. }
  132. // if the user unretiring relationship type
  133. else if (request.getParameter("unretire") != null) {
  134. ps.unretireRelationshipType(relationshipType);
  135. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.unretiredSuccessfully");
  136. view = getSuccessView();
  137. }
  138.  
  139. }
  140.  
  141. return new ModelAndView(new RedirectView(view));
  142. }
  143.  
  144. /**
  145. * This is called prior to displaying a form for the first time. It tells Spring the
  146. * form/command object to load into the request
  147. *
  148. * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  149. */
  150. protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  151.  
  152. RelationshipType identifierType = null;
  153.  
  154. if (Context.isAuthenticated()) {
  155. PersonService ps = Context.getPersonService();
  156. String relationshipTypeId = request.getParameter("relationshipTypeId");
  157. if (relationshipTypeId != null)
  158. identifierType = ps.getRelationshipType(Integer.valueOf(relationshipTypeId));
  159. }
  160.  
  161. if (identifierType == null)
  162. identifierType = new RelationshipType();
  163.  
  164. return identifierType;
  165. }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement