PageRenderTime 978ms CodeModel.GetById 179ms app.highlight 284ms RepoModel.GetById 513ms app.codeStats 0ms

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