PageRenderTime 24ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 122 lines | 81 code | 21 blank | 20 comment | 13 complexity | ba530a32339d515cf95316598e8b9445 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.IdeaComment;
 19import com.google.ie.business.domain.ProjectComment;
 20import com.google.ie.common.constants.IdeaExchangeConstants;
 21import com.google.ie.common.constants.IdeaExchangeErrorCodes;
 22
 23import org.apache.commons.lang.StringUtils;
 24import org.apache.log4j.Logger;
 25import org.springframework.stereotype.Component;
 26import org.springframework.validation.Errors;
 27import org.springframework.validation.FieldError;
 28import org.springframework.validation.ValidationUtils;
 29import org.springframework.validation.Validator;
 30
 31import java.util.Iterator;
 32
 33/**
 34 * {@link Validator} implementation to validate {@link Comment} object.
 35 * 
 36 * @author asirohi
 37 * 
 38 */
 39@Component
 40public class CommentValidator implements Validator {
 41
 42    private static Logger log = Logger.getLogger(CommentValidator.class);
 43
 44    private static final String IDEA_KEY = "ideaKey";
 45    private static final String PROJECT_KEY = "projectKey";
 46    private static final String TEXT = "text";
 47    private static final int COMMENT_LENGTH = 3000;
 48
 49    @Override
 50    public boolean supports(Class<?> clazz) {
 51        return IdeaComment.class.isAssignableFrom(clazz);
 52    }
 53
 54    @Override
 55    public void validate(Object object, Errors errors) {
 56
 57        if (object.getClass().equals(IdeaComment.class)) {
 58            validateIdeaComments(object, errors);
 59        } else if (object.getClass().equals(ProjectComment.class)) {
 60            validateProjectComments(object, errors);
 61        }
 62
 63    }
 64
 65    public void validateIdeaComments(Object object, Errors errors) {
 66        IdeaComment ideaComment = (IdeaComment) object;
 67
 68        ValidationUtils.rejectIfEmptyOrWhitespace(errors, IDEA_KEY,
 69                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
 70        ValidationUtils.rejectIfEmptyOrWhitespace(errors, TEXT,
 71                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
 72        if (StringUtils.length(ideaComment.getText()) > COMMENT_LENGTH) {
 73            errors.rejectValue(TEXT, IdeaExchangeErrorCodes.COMMENT_LENGTH_EXCEEDS,
 74                            IdeaExchangeConstants.Messages.COMMENT_LENGTH_EXCEEDS);
 75        }
 76
 77        if (log.isDebugEnabled()) {
 78            if (errors.hasErrors()) {
 79                log.debug("Validator found " + errors.getErrorCount() + " errors");
 80                for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator
 81                                .hasNext();) {
 82                    FieldError fieldError = iterator.next();
 83                    log.debug("Error found in field: " + fieldError.getField() + " Message :"
 84                                    + fieldError.getDefaultMessage());
 85                }
 86            } else {
 87                log.debug("Validator found no errors");
 88            }
 89
 90        }
 91    }
 92
 93    public void validateProjectComments(Object object, Errors errors) {
 94
 95        ProjectComment projectComment = (ProjectComment) object;
 96
 97        ValidationUtils.rejectIfEmptyOrWhitespace(errors, PROJECT_KEY,
 98                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
 99        ValidationUtils.rejectIfEmptyOrWhitespace(errors, TEXT,
100                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
101        if (StringUtils.length(projectComment.getText()) > COMMENT_LENGTH) {
102            errors.rejectValue(TEXT, IdeaExchangeErrorCodes.COMMENT_LENGTH_EXCEEDS,
103                            IdeaExchangeConstants.Messages.COMMENT_LENGTH_EXCEEDS);
104        }
105
106        if (log.isDebugEnabled()) {
107            if (errors.hasErrors()) {
108                log.debug("Validator found " + errors.getErrorCount() + " errors");
109                for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator
110                                .hasNext();) {
111                    FieldError fieldError = iterator.next();
112                    log.debug("Error found in field: " + fieldError.getField() + " Message :"
113                                    + fieldError.getDefaultMessage());
114                }
115            } else {
116                log.debug("Validator found no errors");
117            }
118
119        }
120    }
121}
122