001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.rules.reporting;
017    
018    import java.util.Collections;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import org.springframework.binding.PropertyAccessStrategy;
024    import org.springframework.binding.support.BeanPropertyAccessStrategy;
025    import org.springframework.rules.constraint.Constraint;
026    import org.springframework.rules.constraint.property.PropertyConstraint;
027    
028    /**
029     * @author Keith Donald
030     */
031    public class BeanValidationResultsBuilder extends ValidationResultsBuilder
032                    implements BeanValidationResults {
033    
034            private String currentProperty;
035    
036            private Object currentPropertyValue;
037    
038            private Map beanResults = new HashMap();
039    
040            private PropertyAccessStrategy beanPropertyAccessStrategy;
041    
042            public BeanValidationResultsBuilder(Object bean) {
043                    super();
044                    if (bean instanceof PropertyAccessStrategy) {
045                            this.beanPropertyAccessStrategy = (PropertyAccessStrategy) bean;
046                    }
047                    else {
048                            this.beanPropertyAccessStrategy = new BeanPropertyAccessStrategy(
049                                            bean);
050                    }
051            }
052    
053            public Map getResults() {
054                    return Collections.unmodifiableMap(beanResults);
055            }
056    
057            public PropertyResults getResults(String propertyName) {
058                    return (PropertyResults) beanResults.get(propertyName);
059            }
060    
061            public int getViolatedCount() {
062                    int count = 0;
063                    Iterator it = beanResults.values().iterator();
064                    while (it.hasNext()) {
065                            count += ((PropertyResults) it.next()).getViolatedCount();
066                    }
067                    return count;
068            }
069    
070            protected void constraintViolated(Constraint constraint) {
071                    if (logger.isDebugEnabled()) {
072                            logger.debug("[Done] collecting results for property '"
073                                            + getCurrentPropertyName() + "'.  Constraints violated: ["
074                                            + constraint + "]");
075                    }
076                    PropertyResults results = new PropertyResults(getCurrentPropertyName(),
077                                    getCurrentPropertyValue(), constraint);
078                    beanResults.put(getCurrentPropertyName(), results);
079            }
080    
081            protected void constraintSatisfied() {
082                    if (logger.isDebugEnabled()) {
083                            logger.debug("[Done] collecting results for property '"
084                                            + getCurrentPropertyName() + "'.  All constraints met.");
085                    }
086            }
087    
088            public String getCurrentPropertyName() {
089                    return currentProperty;
090            }
091    
092            public Object getCurrentPropertyValue() {
093                    return currentPropertyValue;
094            }
095    
096            public void setCurrentBeanPropertyExpression(
097                            PropertyConstraint expression) {
098                    this.currentProperty = expression.getPropertyName();
099                    this.currentPropertyValue = getPropertyValue(this.currentProperty);
100                    super.clear();
101            }
102    
103            private Object getPropertyValue(String propertyName) {
104                    return beanPropertyAccessStrategy.getPropertyValue(propertyName);
105            }
106    
107    }