Advertisement
Guest User

Untitled

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