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

http://thoughtsite.googlecode.com/ · Java · 160 lines · 125 code · 13 blank · 22 comment · 21 complexity · ebcb5a14f5d38f55c84d277b0fea7a67 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.Idea;
  17. import com.google.ie.common.constants.IdeaExchangeConstants;
  18. import com.google.ie.common.constants.IdeaExchangeErrorCodes;
  19. import org.apache.commons.lang.StringUtils;
  20. import org.apache.log4j.Logger;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.beans.factory.annotation.Qualifier;
  23. import org.springframework.stereotype.Component;
  24. import org.springframework.validation.Errors;
  25. import org.springframework.validation.FieldError;
  26. import org.springframework.validation.ValidationUtils;
  27. import org.springframework.validation.Validator;
  28. import java.util.Iterator;
  29. /**
  30. * {@link Validator} implementation for validating {@link Idea} object.
  31. *
  32. * @author asirohi
  33. *
  34. *
  35. */
  36. @Component("ideaValidator")
  37. public class IdeaValidator implements Validator {
  38. private static Logger log = Logger.getLogger(IdeaValidator.class);
  39. /* Attribute name on which validation runs */
  40. private static final String TITLE = "title";
  41. private static final String DESCRIPTION = "description";
  42. private static final String IDEA_RIGHTS_GIVEN_UP = "ideaRightsGivenUp";
  43. private static final String IP_GIVEN_UP = "ipGivenUp";
  44. private static final String IDEA_SUMMARY = "ideaSummary";
  45. private static final String MONETIZATION = "monetization";
  46. private static final String TARGET_AUDIENCE = "targetAudience";
  47. private static final int LENGTH_500 = 500;
  48. private static final int ZERO = 0;
  49. private static final int LENGTH_3000 = 3000;
  50. private static final String COMPETITION = "competition";
  51. @Autowired
  52. @Qualifier("tagTitleValidator")
  53. private Validator stringValidatorForTagTitle;
  54. @Override
  55. public boolean supports(Class<?> clazz) {
  56. return Idea.class.isAssignableFrom(clazz);
  57. }
  58. @Override
  59. public void validate(Object target, Errors errors) {
  60. Idea idea = (Idea) target;
  61. ValidationUtils.rejectIfEmptyOrWhitespace(errors, TITLE,
  62. IdeaExchangeErrorCodes.FIELD_REQUIRED,
  63. IdeaExchangeConstants.Messages.REQUIRED_FIELD);
  64. ValidationUtils.rejectIfEmptyOrWhitespace(errors, DESCRIPTION,
  65. IdeaExchangeErrorCodes.FIELD_REQUIRED,
  66. IdeaExchangeConstants.Messages.REQUIRED_FIELD);
  67. if (!idea.isIdeaRightsGivenUp()) {
  68. errors.rejectValue(IDEA_RIGHTS_GIVEN_UP,
  69. IdeaExchangeErrorCodes.FIELD_ALWAYS_TRUE,
  70. IdeaExchangeConstants.Messages.FIELD_ALWAYS_TRUE);
  71. log.warn("Validation Error : " + IDEA_RIGHTS_GIVEN_UP + " -: "
  72. + IdeaExchangeConstants.Messages.FIELD_ALWAYS_TRUE);
  73. }
  74. if (!idea.isIpGivenUp()) {
  75. errors.rejectValue(IP_GIVEN_UP,
  76. IdeaExchangeErrorCodes.FIELD_ALWAYS_TRUE,
  77. IdeaExchangeConstants.Messages.FIELD_ALWAYS_TRUE);
  78. log.warn("Validation Error : " + IP_GIVEN_UP + " -: "
  79. + IdeaExchangeConstants.Messages.FIELD_ALWAYS_TRUE);
  80. }
  81. if (!StringUtils.isBlank(idea.getTitle())
  82. && idea.getTitle().trim().length() > LENGTH_500) {
  83. errors.rejectValue(TITLE,
  84. IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
  85. IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  86. log.warn("Validation Error : " + TITLE + " -: "
  87. + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  88. }
  89. if (!StringUtils.isBlank(idea.getDescription())
  90. && idea.getDescription().trim().length() > LENGTH_3000) {
  91. errors.rejectValue(DESCRIPTION,
  92. IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
  93. IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  94. log.warn("Validation Error : " + DESCRIPTION + " -: "
  95. + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  96. }
  97. if (!StringUtils.isBlank(idea.getIdeaSummary())
  98. && idea.getIdeaSummary().trim().length() > LENGTH_3000) {
  99. errors.rejectValue(IDEA_SUMMARY,
  100. IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
  101. IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  102. log.warn("Validation Error : " + IDEA_SUMMARY + " -: "
  103. + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  104. }
  105. if (!StringUtils.isBlank(idea.getMonetization())
  106. && idea.getMonetization().trim().length() > LENGTH_500) {
  107. errors.rejectValue(MONETIZATION,
  108. IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
  109. IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  110. log.warn("Validation Error : " + MONETIZATION + " -: "
  111. + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  112. }
  113. if (!StringUtils.isBlank(idea.getTargetAudience())
  114. && idea.getTargetAudience().trim().length() > LENGTH_500) {
  115. errors.rejectValue(TARGET_AUDIENCE,
  116. IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
  117. IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  118. log.warn("Validation Error : " + TARGET_AUDIENCE + " -: "
  119. + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  120. }
  121. if (!StringUtils.isBlank(idea.getCompetition())
  122. && idea.getCompetition().trim().length() > LENGTH_500) {
  123. errors.rejectValue(COMPETITION,
  124. IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
  125. IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  126. log.warn("Validation Error : " + COMPETITION + " -: "
  127. + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
  128. }
  129. if (idea.getTags() != null && idea.getTags().length() > ZERO) {
  130. ValidationUtils.invokeValidator(stringValidatorForTagTitle, idea.getTags(),
  131. errors);
  132. }
  133. if (log.isDebugEnabled()) {
  134. if (errors.hasErrors()) {
  135. log.debug("Validator found " + errors.getErrorCount() + " errors");
  136. for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator
  137. .hasNext();) {
  138. FieldError fieldError = iterator.next();
  139. log.debug("Error found in field: " + fieldError.getField() + " Message :"
  140. + fieldError.getDefaultMessage());
  141. }
  142. } else {
  143. log.debug("Validator found no errors");
  144. }
  145. }
  146. }
  147. }