Advertisement
Guest User

Untitled

a guest
Dec 30th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 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. *
  6. * Software distributed under the License is distributed on an "AS IS"
  7. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  8. * License for the specific language governing rights and limitations
  9. * under the License.
  10. *
  11. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  12. */
  13. package org.openmrs.validator;
  14.  
  15. import org.apache.commons.logging.Log;
  16. import org.apache.commons.logging.LogFactory;
  17. import org.openmrs.RelationshipType;
  18. import org.openmrs.annotation.Handler;
  19. import org.springframework.validation.Errors;
  20. import org.springframework.validation.ValidationUtils;
  21. import org.springframework.validation.Validator;
  22.  
  23. /**
  24. * Validates attributes on the {@link RelationshipType} object.
  25. *
  26. * @since 1.10
  27. */
  28. @Handler(supports = { RelationshipType.class }, order = 50)
  29. public class RelationshipTypeValidator implements Validator {
  30.  
  31. /** Log for this class and subclasses */
  32. protected final Log log = LogFactory.getLog(getClass());
  33.  
  34. /**
  35. * Determines if the command object being submitted is a valid type
  36. *
  37. * @see org.springframework.validation.Validator#supports(java.lang.Class)
  38. */
  39. @SuppressWarnings("unchecked")
  40. public boolean supports(Class c) {
  41. return c.equals(RelationshipType.class);
  42. }
  43.  
  44. /**
  45. * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
  46. * @should fail validation if name is null or empty or whitespace
  47. * @should fail validation if description is null or empty or whitespace
  48. */
  49. public void validate(Object obj, Errors errors) {
  50. RelationshipType relationshipType = (RelationshipType) obj;
  51. RelationshipType type = (RelationshipType) obj;
  52.  
  53. if (type.getaIsToB() == null || type.getaIsToB().equals(""))
  54. errors.rejectValue("aIsToB", "RelationshipType.aIsToB.required");
  55.  
  56. if (type.getbIsToA() == null || type.getbIsToA().equals(""))
  57. errors.rejectValue("bIsToA", "RelationshipType.bIsToA.required");
  58.  
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. /**
  74. * The contents of this file are subject to the OpenMRS Public License
  75. * Version 1.0 (the "License"); you may not use this file except in
  76. * compliance with the License. You may obtain a copy of the License at
  77. * http://license.openmrs.org
  78. *
  79. * Software distributed under the License is distributed on an "AS IS"
  80. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  81. * License for the specific language governing rights and limitations
  82. * under the License.
  83. *
  84. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  85. */
  86. package org.openmrs.web.controller.person;
  87.  
  88. import javax.servlet.ServletException;
  89. import javax.servlet.http.HttpServletRequest;
  90. import javax.servlet.http.HttpServletResponse;
  91. import javax.servlet.http.HttpSession;
  92.  
  93. import org.apache.commons.logging.Log;
  94. import org.apache.commons.logging.LogFactory;
  95. import org.openmrs.RelationshipType;
  96. import org.openmrs.api.APIException;
  97. import org.openmrs.api.PersonService;
  98. import org.openmrs.api.context.Context;
  99. import org.openmrs.validator.RelationshipTypeValidator;
  100. import org.openmrs.web.WebConstants;
  101. import org.springframework.beans.propertyeditors.CustomNumberEditor;
  102. import org.springframework.dao.DataIntegrityViolationException;
  103. import org.springframework.util.StringUtils;
  104. import org.springframework.validation.BindException;
  105. import org.springframework.validation.ValidationUtils;
  106. import org.springframework.web.bind.ServletRequestDataBinder;
  107. import org.springframework.web.servlet.ModelAndView;
  108. import org.springframework.web.servlet.mvc.SimpleFormController;
  109. import org.springframework.web.servlet.view.RedirectView;
  110.  
  111. public class RelationshipTypeFormController extends SimpleFormController {
  112.  
  113. /** Logger for this class and subclasses */
  114. protected final Log log = LogFactory.getLog(getClass());
  115.  
  116. /**
  117. * Allows for Integers to be used as values in input tags. Normally, only strings and lists are
  118. * expected
  119. *
  120. * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest,
  121. * org.springframework.web.bind.ServletRequestDataBinder)
  122. */
  123. protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
  124. super.initBinder(request, binder);
  125. //NumberFormat nf = NumberFormat.getInstance(new Locale("en_US"));
  126. binder.registerCustomEditor(java.lang.Integer.class, new CustomNumberEditor(java.lang.Integer.class, true));
  127. }
  128.  
  129. /**
  130. * @see org.springframework.web.servlet.mvc.SimpleFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
  131. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  132. * org.springframework.validation.BindException)
  133. */
  134. @Override
  135. protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command,
  136. BindException errors) throws Exception {
  137.  
  138. RelationshipType type = (RelationshipType) command;
  139.  
  140. new RelationshipTypeValidator().validate(type, errors);
  141.  
  142. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aIsToB", "error.aIsToB");
  143. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bIsToA", "error.bIsToA");
  144.  
  145. if (!StringUtils.hasText(type.getDescription()))
  146. errors.rejectValue("description", "error.required", new Object[] { Context.getMessageSourceService().getMessage(
  147. "general.description") }, null);
  148.  
  149. return super.processFormSubmission(request, response, type, errors);
  150. }
  151.  
  152. /**
  153. * The onSubmit function receives the form/command object that was modified by the input form
  154. * and saves it to the db
  155. *
  156. * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  157. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  158. * org.springframework.validation.BindException)
  159. */
  160. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  161. BindException errors) throws Exception {
  162.  
  163. HttpSession httpSession = request.getSession();
  164.  
  165. String view = getFormView();
  166.  
  167. if (Context.isAuthenticated()) {
  168. RelationshipType relationshipType = (RelationshipType) obj;
  169. PersonService ps = Context.getPersonService();
  170.  
  171. //to save the relationship type
  172. if (request.getParameter("save") != null) {
  173. ps.saveRelationshipType(relationshipType);
  174. view = getSuccessView();
  175. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.saved");
  176. }
  177.  
  178. // if the user is retiring out the relationshipType
  179. else if (request.getParameter("retire") != null) {
  180. String retireReason = request.getParameter("retireReason");
  181. if (relationshipType.getRelationshipTypeId() != null && !(StringUtils.hasText(retireReason))) {
  182. errors.reject("retireReason", "general.retiredReason.empty");
  183. return showForm(request, response, errors);
  184. }
  185.  
  186. ps.retireRelationshipType(relationshipType, retireReason);
  187. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.retiredSuccessfully");
  188.  
  189. view = getSuccessView();
  190. }
  191.  
  192. // if the user is purging the relationshipType
  193. else if (request.getParameter("purge") != null) {
  194. try {
  195. ps.purgeRelationshipType(relationshipType);
  196. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.purgedSuccessfully");
  197. view = getSuccessView();
  198. }
  199. catch (DataIntegrityViolationException e) {
  200. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
  201. return showForm(request, response, errors);
  202. }
  203. catch (APIException e) {
  204. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: " + e.getLocalizedMessage());
  205. return showForm(request, response, errors);
  206. }
  207. }
  208. // if the user unretiring relationship type
  209. else if (request.getParameter("unretire") != null) {
  210. ps.unretireRelationshipType(relationshipType);
  211. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.unretiredSuccessfully");
  212. view = getSuccessView();
  213. }
  214.  
  215. }
  216.  
  217. return new ModelAndView(new RedirectView(view));
  218. }
  219.  
  220. /**
  221. * This is called prior to displaying a form for the first time. It tells Spring the
  222. * form/command object to load into the request
  223. *
  224. * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  225. */
  226. protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  227.  
  228. RelationshipType identifierType = null;
  229.  
  230. if (Context.isAuthenticated()) {
  231. PersonService ps = Context.getPersonService();
  232. String relationshipTypeId = request.getParameter("relationshipTypeId");
  233. if (relationshipTypeId != null)
  234. identifierType = ps.getRelationshipType(Integer.valueOf(relationshipTypeId));
  235. }
  236.  
  237. if (identifierType == null)
  238. identifierType = new RelationshipType();
  239.  
  240. return identifierType;
  241. }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement