Advertisement
Guest User

Untitled

a guest
Dec 31st, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 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.openmrs.api.context.Context;
  20. import org.springframework.util.StringUtils;
  21. import org.springframework.validation.Errors;
  22. import org.springframework.validation.Validator;
  23.  
  24. /**
  25. * Validates attributes on the {@link RelationshipType} object.
  26. *
  27. * @since 1.10
  28. */
  29. @Handler(supports = { RelationshipType.class }, order = 50)
  30. public class RelationshipTypeValidator implements Validator {
  31.  
  32. /** Log for this class and subclasses */
  33. protected final Log log = LogFactory.getLog(getClass());
  34.  
  35. /**
  36. * Determines if the command object being submitted is a valid type
  37. *
  38. * @see org.springframework.validation.Validator#supports(java.lang.Class)
  39. */
  40. @SuppressWarnings("unchecked")
  41. public boolean supports(Class c) {
  42. return c.equals(RelationshipType.class);
  43. }
  44.  
  45. /**
  46. * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
  47. * @should fail validation if name is null or empty or whitespace
  48. * @should fail validation if description is null or empty or whitespace
  49. */
  50. public void validate(Object obj, Errors errors) {
  51. RelationshipType relationshipType = (RelationshipType) obj;
  52. if (relationshipType == null) {
  53. errors.rejectValue("relationshipType", "error.general");
  54. }
  55.  
  56. RelationshipType type = (RelationshipType) obj;
  57.  
  58. new RelationshipTypeValidator().validate(type, errors);
  59.  
  60. if (type.getaIsToB() == null || type.getaIsToB().equals(""))
  61. errors.rejectValue("aIsToB", "RelationshipType.aIsToB.required");
  62.  
  63. if (type.getbIsToA() == null || type.getbIsToA().equals(""))
  64. errors.rejectValue("bIsToA", "RelationshipType.bIsToA.required");
  65.  
  66. if (!StringUtils.hasText(type.getDescription()))
  67. errors.rejectValue("description", "error.required", new Object[] { Context.getMessageSourceService().getMessage(
  68. "general.description") }, null);
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement