Guest User

Untitled

a guest
Jul 9th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 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.validator;
  15.  
  16. import org.apache.commons.logging.Log;
  17. import org.apache.commons.logging.LogFactory;
  18. import org.openmrs.ConceptNameTag;
  19. import org.openmrs.annotation.Handler;
  20. import org.openmrs.api.context.Context;
  21. import org.springframework.validation.Errors;
  22. import org.springframework.validation.ValidationUtils;
  23. import org.springframework.validation.Validator;
  24.  
  25. /**
  26. * Validates attributes on the {@link ConceptNameTag} object.
  27. *
  28. * @since 1.10
  29. */
  30. @Handler(supports = { ConceptNameTag.class }, order = 50)
  31. public class ConceptNameTagValidator implements Validator {
  32.  
  33. /** Log for this class and subclasses */
  34. protected final Log log = LogFactory.getLog(getClass());
  35.  
  36. /**
  37. * Determines if the command object being submitted is a valid type
  38. *
  39. * @see org.springframework.validation.Validator#supports(java.lang.Class)
  40. */
  41. @SuppressWarnings("unchecked")
  42. public boolean supports(Class c) {
  43. return ConceptNameTag.class.isAssignableFrom(c);
  44. }
  45.  
  46. /**
  47. * Checks the form object for any inconsistencies/errors
  48. *
  49. * @see org.springframework.validation.Validator#validate(java.lang.Object,
  50. * org.springframework.validation.Errors)
  51. * @should fail validation if tag is null or empty or whitespace
  52. * @should pass validation if description is null or empty or whitespace
  53. * @should pass validation if all required fields have proper values
  54. */
  55.  
  56. public void validate(Object obj, Errors errors) {
  57. ConceptNameTag cnt = (ConceptNameTag) obj;
  58. if (cnt == null) {
  59. errors.rejectValue("conceptNameTag", "error.general");
  60. } else {
  61. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "tag", "error.name");
  62. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "error.description");
  63.  
  64. for (ConceptNameTag currentTag : Context.getConceptService().getAllConceptNameTags()) {
  65. if (currentTag.getTag().trim()==(cnt.getTag().trim())) {
  66. errors.rejectValue("tag", "conceptsearch.error.duplicate");
  67. }
  68. }
  69. }
  70. }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment