PageRenderTime 30ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://thoughtsite.googlecode.com/
Java | 123 lines | 89 code | 12 blank | 22 comment | 16 complexity | 90e22d865061bc014e6964d299604099 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.Developer;
 19import com.google.ie.common.constants.IdeaExchangeConstants;
 20import com.google.ie.common.constants.IdeaExchangeErrorCodes;
 21import com.google.ie.dto.ProjectDetail;
 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.Validator;
 29
 30import java.util.Iterator;
 31import java.util.List;
 32
 33/**
 34 * {@link Validator} implementation for validating {@link ProjectDetail} object.
 35 * 
 36 * @author sbhatnagar
 37 * 
 38 * 
 39 */
 40@Component("projectValidator")
 41public class ProjectValidator implements Validator {
 42
 43    private static Logger log = Logger.getLogger(ProjectValidator.class);
 44    /* Attribute name on which validation runs */
 45    private static final String NAME = "name";
 46    private static final String DESCRIPTION = "description";
 47    private static final String DEVELOPER_NAME = "name";
 48    private static final String DEVELOPER_EMAIL = "emailId";
 49    private static final int LENGTH_500 = 500;
 50    private static final int LENGTH_50 = 50;
 51    private static final int LENGTH_3000 = 3000;
 52
 53    @Override
 54    public boolean supports(Class<?> clazz) {
 55        return ProjectDetail.class.isAssignableFrom(clazz);
 56    }
 57
 58    @Override
 59    public void validate(Object target, Errors errors) {
 60        ProjectDetail projectDetail = (ProjectDetail) target;
 61        if (StringUtils.isBlank(projectDetail.getProject().getName())) {
 62            errors.rejectValue(NAME,
 63                            IdeaExchangeErrorCodes.FIELD_REQUIRED,
 64                            IdeaExchangeConstants.Messages.REQUIRED_FIELD);
 65            log.warn("Validation Error : " + NAME + " -: "
 66                            + IdeaExchangeConstants.Messages.REQUIRED_FIELD);
 67        }
 68        if (!StringUtils.isBlank(projectDetail.getProject().getName())
 69                        && projectDetail.getProject().getName().trim().length() > LENGTH_500) {
 70            errors.rejectValue(NAME,
 71                            IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
 72                            IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
 73            log.warn("Validation Error : " + NAME + " -: "
 74                            + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
 75        }
 76        if (!StringUtils.isBlank(projectDetail.getProject().getDescription())
 77                        && projectDetail.getProject().getDescription().trim().length() > LENGTH_3000) {
 78            errors.rejectValue(DESCRIPTION,
 79                            IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
 80                            IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
 81            log.warn("Validation Error : " + DESCRIPTION + " -: "
 82                            + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
 83        }
 84        List<Developer> developers = projectDetail.getDevelopers();
 85        if (developers != null) {
 86            for (Developer developer : developers) {
 87                if (!StringUtils.isBlank(developer.getName())
 88                                && developer.getName().trim().length() > LENGTH_50) {
 89                    errors.rejectValue(DEVELOPER_NAME,
 90                                    IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT,
 91                                    IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
 92                    log.warn("Validation Error : " + DEVELOPER_NAME + " -: "
 93                                    + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
 94                }
 95                if (!StringUtils.isBlank(developer.getEmailId())
 96                                && developer.getEmailId().trim().length() > LENGTH_50) {
 97                    errors.reject(IdeaExchangeErrorCodes.LENGTH_EXCEED_LIMIT, DEVELOPER_EMAIL
 98                                    + ":" +
 99                                    IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
100                    log.warn("Validation Error : " + DEVELOPER_EMAIL + " -: "
101                                    + IdeaExchangeConstants.Messages.LENGTH_EXCEED_LIMIT_MESSAGE);
102                }
103            }
104        }
105
106        if (log.isDebugEnabled()) {
107
108            if (errors.hasErrors()) {
109                log.debug("Validator found " + errors.getErrorCount() + " errors");
110                for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator
111                                .hasNext();) {
112                    FieldError fieldError = iterator.next();
113                    log.debug("Error found in field: " + fieldError.getField() + " Message :"
114                                    + fieldError.getDefaultMessage());
115                }
116            } else {
117                log.debug("Validator found no errors");
118            }
119
120        }
121    }
122}
123