PageRenderTime 107ms CodeModel.GetById 40ms app.highlight 24ms RepoModel.GetById 36ms app.codeStats 0ms

/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
16package com.google.ie.common.validation;
17
18import com.google.ie.business.domain.Audit;
19import com.google.ie.common.constants.IdeaExchangeConstants;
20import com.google.ie.common.constants.IdeaExchangeErrorCodes;
21
22import org.apache.log4j.Logger;
23import org.springframework.stereotype.Component;
24import org.springframework.validation.Errors;
25import org.springframework.validation.FieldError;
26import org.springframework.validation.ValidationUtils;
27import org.springframework.validation.Validator;
28
29import java.util.Iterator;
30
31/**
32 * {@link Validator} implementation to validate {@link Audit} object.
33 * 
34 * @author asirohi
35 * 
36 */
37@Component
38public class AuditValidator implements Validator {
39    private static Logger log = Logger.getLogger(AuditValidator.class);
40
41    @Override
42    public boolean supports(Class<?> clazz) {
43        return Audit.class.isAssignableFrom(clazz);
44    }
45
46    @Override
47    public void validate(Object target, Errors errors) {
48
49        ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.USER_KEY,
50                        IdeaExchangeErrorCodes.FIELD_REQUIRED,
51                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
52        ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.ENTITY_KEY,
53                        IdeaExchangeErrorCodes.FIELD_REQUIRED,
54                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
55        ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.ACTION,
56                        IdeaExchangeErrorCodes.FIELD_REQUIRED,
57                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
58        ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.ENTITY_TYPE,
59                        IdeaExchangeErrorCodes.FIELD_REQUIRED,
60                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
61        ValidationUtils.rejectIfEmptyOrWhitespace(errors, IdeaExchangeConstants.AUDIT_DATE,
62                        IdeaExchangeErrorCodes.FIELD_REQUIRED,
63                        IdeaExchangeConstants.Messages.REQUIRED_FIELD);
64
65        if (log.isDebugEnabled()) {
66            if (errors.hasErrors()) {
67                log.debug("Validator found " + errors.getErrorCount() + " errors");
68                for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator
69                                .hasNext();) {
70                    FieldError fieldError = iterator.next();
71                    log.debug("Error found in field: " + fieldError.getField() + " Message :"
72                                    + fieldError.getDefaultMessage());
73                }
74            } else {
75                log.debug("Validator found no errors");
76            }
77
78        }
79    }
80}
81