Advertisement
Guest User

Untitled

a guest
Jan 4th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.30 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 aIsToB(or A is To B) is null or empty or whitespace
  47. * @should fail validation if bIsToA(or B is To A) is null or empty or whitespace
  48. * @should fail validation if Description is null or empty or whitespace
  49. * @should pass validation if all required fields are set
  50. */
  51. public void validate(Object obj, Errors errors) {
  52. RelationshipType relationshipType = (RelationshipType) obj;
  53. if (relationshipType == null) {
  54. errors.rejectValue("relationshipType", "error.general");
  55. }
  56. else {
  57. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aIsToB", "RelationshipType.aIsToB.required");
  58. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bIsToA", "RelationshipType.bIsToA.required");
  59. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "Description", "error.description");
  60. }
  61. }
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. /**
  69. * The contents of this file are subject to the OpenMRS Public License
  70. * Version 1.0 (the "License"); you may not use this file except in
  71. * compliance with the License. You may obtain a copy of the License at
  72. *
  73. * Software distributed under the License is distributed on an "AS IS"
  74. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  75. * License for the specific language governing rights and limitations
  76. * under the License.
  77. *
  78. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  79. */
  80. package org.openmrs.validator;
  81.  
  82. import org.junit.Assert;
  83. import org.junit.Test;
  84. import org.openmrs.RelationshipType;
  85. import org.springframework.validation.BindException;
  86. import org.springframework.validation.Errors;
  87.  
  88. /**
  89. * @since 1.10
  90. */
  91. public class RelationshipTypeValidatorTest {
  92.  
  93. /**
  94. * Tests for Tests for aIsToB being null or empty or whitespace, then fail the test
  95. * @see RelationshipTypeValidator#validate(Object,Errors)
  96. * @verifies fail validation if aIsToB(or A is To B) is null or empty or whitespace
  97. */
  98. @Test
  99. public void validate_shouldFailValidationIfaIsToBIsNullOrEmptyOrWhitespace()
  100. throws Exception {
  101. RelationshipType type = new RelationshipType();
  102. type.setaIsToB(null);
  103. type.setDescription("A is To B");
  104.  
  105. Errors errors = new BindException(type, "type");
  106. new RelationshipTypeValidator().validate(type, errors);
  107. Assert.assertTrue(errors.hasFieldErrors("aIsToB"));
  108.  
  109. type.setaIsToB("");
  110. errors = new BindException(type, "type");
  111. new RelationshipTypeValidator().validate(type, errors);
  112. Assert.assertTrue(errors.hasFieldErrors("aIsToB"));
  113.  
  114. type.setaIsToB(" ");
  115. errors = new BindException(type, "type");
  116. new RelationshipTypeValidator().validate(type, errors);
  117. Assert.assertTrue(errors.hasFieldErrors("aIsToB"));
  118. }
  119.  
  120. /**
  121. * Tests for bIsToA being null or empty or whitespace, then fail the test
  122. * @see RelationshipTypeValidator#validate(Object,Errors)
  123. * @verifies fail validation if bIsToA(or B is To A) is null or empty or whitespace
  124. */
  125. @Test
  126. public void validate_shouldFailValidationIfbIsToAIsNullOrEmptyOrWhitespace()
  127. throws Exception {
  128. RelationshipType type = new RelationshipType();
  129. type.setbIsToA(null);
  130. type.setDescription("B is To A");
  131.  
  132. Errors errors = new BindException(type, "type");
  133. new RelationshipTypeValidator().validate(type, errors);
  134. Assert.assertTrue(errors.hasFieldErrors("bIsToA"));
  135.  
  136. type.setbIsToA("");
  137. errors = new BindException(type, "type");
  138. new RelationshipTypeValidator().validate(type, errors);
  139. Assert.assertTrue(errors.hasFieldErrors("bIsToA"));
  140.  
  141. type.setbIsToA(" ");
  142. errors = new BindException(type, "type");
  143. new RelationshipTypeValidator().validate(type, errors);
  144. Assert.assertTrue(errors.hasFieldErrors("bIsToA"));
  145. }
  146.  
  147. /**
  148. *Tests for description being null or empty or whitespace, then fail the test
  149. * @see RelationshipTypeValidator#validate(Object,Errors)
  150. * @verifies fail validation if description is null or empty or whitespace
  151. */
  152. @Test
  153. public void validate_shouldFailValidationIfDescriptionIsNullOrEmptyOrWhitespace()
  154. throws Exception {
  155. RelationshipType type = new RelationshipType();
  156. type.setName("name");
  157. type.setDescription(null);
  158.  
  159. Errors errors = new BindException(type, "type");
  160. new RelationshipTypeValidator().validate(type, errors);
  161. Assert.assertFalse(errors.hasFieldErrors("description"));
  162.  
  163. type.setDescription("");
  164. errors = new BindException(type, "type");
  165. new RelationshipTypeValidator().validate(type, errors);
  166. Assert.assertFalse(errors.hasFieldErrors("description"));
  167.  
  168. type.setDescription(" ");
  169. errors = new BindException(type, "type");
  170. new RelationshipTypeValidator().validate(type, errors);
  171. Assert.assertFalse(errors.hasFieldErrors("description"));
  172. }
  173.  
  174. /**
  175. * Test for all the field being set to some values
  176. * @see RelationshipTypeValidator#validate(Object,Errors)
  177. * @verifies pass validation if all required fields are set
  178. */
  179. @Test
  180. public void validate_shouldPassValidationIfAllRequiredFieldsAreSet()
  181. throws Exception {
  182. RelationshipType type = new RelationshipType();
  183. type.setaIsToB("A is To B");
  184. type.setbIsToA("B is To A");
  185. type.setDescription("description");
  186.  
  187. Errors errors = new BindException(type, "type");
  188. new RelationshipTypeValidator().validate(type, errors);
  189. Assert.assertFalse(errors.hasErrors());
  190. }
  191. }
  192.  
  193.  
  194.  
  195. /**
  196. * The contents of this file are subject to the OpenMRS Public License
  197. * Version 1.0 (the "License"); you may not use this file except in
  198. * compliance with the License. You may obtain a copy of the License at
  199. * http://license.openmrs.org
  200. *
  201. * Software distributed under the License is distributed on an "AS IS"
  202. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  203. * License for the specific language governing rights and limitations
  204. * under the License.
  205. *
  206. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  207. */
  208. package org.openmrs.web.controller.person;
  209.  
  210. import javax.servlet.ServletException;
  211. import javax.servlet.http.HttpServletRequest;
  212. import javax.servlet.http.HttpServletResponse;
  213. import javax.servlet.http.HttpSession;
  214.  
  215. import org.apache.commons.logging.Log;
  216. import org.apache.commons.logging.LogFactory;
  217. import org.openmrs.RelationshipType;
  218. import org.openmrs.api.APIException;
  219. import org.openmrs.api.PersonService;
  220. import org.openmrs.api.context.Context;
  221. import org.openmrs.validator.RelationshipTypeValidator;
  222. import org.openmrs.web.WebConstants;
  223. import org.springframework.beans.propertyeditors.CustomNumberEditor;
  224. import org.springframework.dao.DataIntegrityViolationException;
  225. import org.springframework.util.StringUtils;
  226. import org.springframework.validation.BindException;
  227. import org.springframework.web.bind.ServletRequestDataBinder;
  228. import org.springframework.web.servlet.ModelAndView;
  229. import org.springframework.web.servlet.mvc.SimpleFormController;
  230. import org.springframework.web.servlet.view.RedirectView;
  231.  
  232. public class RelationshipTypeFormController extends SimpleFormController {
  233.  
  234. /** Logger for this class and subclasses */
  235. protected final Log log = LogFactory.getLog(getClass());
  236.  
  237. /**
  238. * Allows for Integers to be used as values in input tags. Normally, only strings and lists are
  239. * expected
  240. *
  241. * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest,
  242. * org.springframework.web.bind.ServletRequestDataBinder)
  243. */
  244. protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
  245. super.initBinder(request, binder);
  246. //NumberFormat nf = NumberFormat.getInstance(new Locale("en_US"));
  247. binder.registerCustomEditor(java.lang.Integer.class, new CustomNumberEditor(java.lang.Integer.class, true));
  248. }
  249.  
  250. /**
  251. * @see org.springframework.web.servlet.mvc.SimpleFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
  252. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  253. * org.springframework.validation.BindException)
  254. */
  255. @Override
  256. protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command,
  257. BindException errors) throws Exception {
  258.  
  259. RelationshipType type = (RelationshipType) command;
  260. new RelationshipTypeValidator().validate(type, errors);
  261.  
  262. return super.processFormSubmission(request, response, type, errors);
  263. }
  264.  
  265. /**
  266. * The onSubmit function receives the form/command object that was modified by the input form
  267. * and saves it to the db
  268. *
  269. * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  270. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  271. * org.springframework.validation.BindException)
  272. */
  273. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  274. BindException errors) throws Exception {
  275.  
  276. HttpSession httpSession = request.getSession();
  277.  
  278. String view = getFormView();
  279.  
  280. if (Context.isAuthenticated()) {
  281. RelationshipType relationshipType = (RelationshipType) obj;
  282. PersonService ps = Context.getPersonService();
  283.  
  284. //to save the relationship type
  285. if (request.getParameter("save") != null) {
  286. ps.saveRelationshipType(relationshipType);
  287. view = getSuccessView();
  288. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.saved");
  289. }
  290.  
  291. // if the user is retiring out the relationshipType
  292. else if (request.getParameter("retire") != null) {
  293. String retireReason = request.getParameter("retireReason");
  294. if (relationshipType.getRelationshipTypeId() != null && !(StringUtils.hasText(retireReason))) {
  295. errors.reject("retireReason", "general.retiredReason.empty");
  296. return showForm(request, response, errors);
  297. }
  298.  
  299. ps.retireRelationshipType(relationshipType, retireReason);
  300. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.retiredSuccessfully");
  301.  
  302. view = getSuccessView();
  303. }
  304.  
  305. // if the user is purging the relationshipType
  306. else if (request.getParameter("purge") != null) {
  307. try {
  308. ps.purgeRelationshipType(relationshipType);
  309. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.purgedSuccessfully");
  310. view = getSuccessView();
  311. }
  312. catch (DataIntegrityViolationException e) {
  313. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
  314. return showForm(request, response, errors);
  315. }
  316. catch (APIException e) {
  317. httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: " + e.getLocalizedMessage());
  318. return showForm(request, response, errors);
  319. }
  320. }
  321. // if the user unretiring relationship type
  322. else if (request.getParameter("unretire") != null) {
  323. ps.unretireRelationshipType(relationshipType);
  324. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "RelationshipType.unretiredSuccessfully");
  325. view = getSuccessView();
  326. }
  327.  
  328. }
  329.  
  330. return new ModelAndView(new RedirectView(view));
  331. }
  332.  
  333. /**
  334. * This is called prior to displaying a form for the first time. It tells Spring the
  335. * form/command object to load into the request
  336. *
  337. * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  338. */
  339. protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  340.  
  341. RelationshipType identifierType = null;
  342.  
  343. if (Context.isAuthenticated()) {
  344. PersonService ps = Context.getPersonService();
  345. String relationshipTypeId = request.getParameter("relationshipTypeId");
  346. if (relationshipTypeId != null)
  347. identifierType = ps.getRelationshipType(Integer.valueOf(relationshipTypeId));
  348. }
  349.  
  350. if (identifierType == null)
  351. identifierType = new RelationshipType();
  352.  
  353. return identifierType;
  354. }
  355.  
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement