/src/main/java/com/google/ie/common/validation/AuditValidator.java

http://thoughtsite.googlecode.com/ · Java · 81 lines · 50 code · 11 blank · 20 comment · 4 complexity · da282c72266d25a16b90c27e115572e1 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.common.validation;
  16. import com.google.ie.business.domain.Audit;
  17. import com.google.ie.common.constants.IdeaExchangeConstants;
  18. import com.google.ie.common.constants.IdeaExchangeErrorCodes;
  19. import org.apache.log4j.Logger;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.validation.Errors;
  22. import org.springframework.validation.FieldError;
  23. import org.springframework.validation.ValidationUtils;
  24. import org.springframework.validation.Validator;
  25. import java.util.Iterator;
  26. /**
  27. * {@link Validator} implementation to validate {@link Audit} object.
  28. *
  29. * @author asirohi
  30. *
  31. */
  32. @Component
  33. public class AuditValidator implements Validator {
  34. private static Logger log = Logger.getLogger(AuditValidator.class);
  35. @Override
  36. public boolean supports(Class<?> clazz) {
  37. return Audit.class.isAssignableFrom(clazz);
  38. }
  39. @Override
  40. public void validate(Object target, Errors errors) {
  41. ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.USER_KEY,
  42. IdeaExchangeErrorCodes.FIELD_REQUIRED,
  43. IdeaExchangeConstants.Messages.REQUIRED_FIELD);
  44. ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.ENTITY_KEY,
  45. IdeaExchangeErrorCodes.FIELD_REQUIRED,
  46. IdeaExchangeConstants.Messages.REQUIRED_FIELD);
  47. ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.ACTION,
  48. IdeaExchangeErrorCodes.FIELD_REQUIRED,
  49. IdeaExchangeConstants.Messages.REQUIRED_FIELD);
  50. ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.ENTITY_TYPE,
  51. IdeaExchangeErrorCodes.FIELD_REQUIRED,
  52. IdeaExchangeConstants.Messages.REQUIRED_FIELD);
  53. ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.AUDIT_DATE,
  54. IdeaExchangeErrorCodes.FIELD_REQUIRED,
  55. IdeaExchangeConstants.Messages.REQUIRED_FIELD);
  56. if (log.isDebugEnabled()) {
  57. if (errors.hasErrors()) {
  58. log.debug("Validator found " + errors.getErrorCount() + " errors");
  59. for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator
  60. .hasNext();) {
  61. FieldError fieldError = iterator.next();
  62. log.debug("Error found in field: " + fieldError.getField() + " Message :"
  63. + fieldError.getDefaultMessage());
  64. }
  65. } else {
  66. log.debug("Validator found no errors");
  67. }
  68. }
  69. }
  70. }