001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.document.validation;
017    
018    import java.lang.reflect.InvocationTargetException;
019    import java.util.List;
020    
021    import org.apache.commons.beanutils.PropertyUtils;
022    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
023    import org.kuali.rice.kns.util.ObjectUtils;
024    import org.kuali.rice.kns.web.format.FormatException;
025    
026    /**
027     * An abstract class that defines methods needed to act on parameter properties for a validation.
028     */
029    public abstract class ParameterizedValidation {
030        private List<ValidationFieldConvertible> parameterProperties;
031        
032        /**
033         * Gets the parameterProperties attribute. 
034         * @return Returns the parameterProperties.
035         */
036        protected List<ValidationFieldConvertible> getParameterProperties() {
037            return parameterProperties;
038        }
039    
040        /**
041         * Sets the parameterProperties attribute value.
042         * @param parameterProperties The parameterProperties to set.
043         */
044        public void setParameterProperties(List<ValidationFieldConvertible> parameterProperties) {
045            this.parameterProperties = parameterProperties;
046        }
047        
048        /**
049         * Given an event and the parameterProperties given by the validations, copies the values from the events to the proper fields in the validation. 
050         * @param event an array to derive properties from
051         * @param the parameter to set the parameters on
052         */
053        public void populateParametersFromEvent(AttributedDocumentEvent event) {
054            if (getParameterProperties() != null) {
055                for (ValidationFieldConvertible property: getParameterProperties()) {
056                    populateParameterFromEvent(event, property);
057                }
058            }
059        }
060        
061        /**
062         * Populates a single parameter field based on a field conversion, given an event to populate data from
063         * @param event the event which acts as the source of data
064         * @param validation the validation to populate
065         * @param conversion the conversion information
066         */
067        protected void populateParameterFromEvent(AttributedDocumentEvent event, ValidationFieldConvertible conversion) {
068            try {
069                Class propertyClass = PropertyUtils.getPropertyType(event, conversion.getSourceEventProperty());
070                Object propertyValue = ObjectUtils.getPropertyValue(event, conversion.getSourceEventProperty());
071                if (propertyValue != null) {
072                    ObjectUtils.setObjectProperty(this, conversion.getTargetValidationProperty(), propertyClass, propertyValue);
073                }
074            }
075            catch (FormatException fe) {
076                throw new RuntimeException(fe);
077            }
078            catch (IllegalAccessException iae) {
079                throw new RuntimeException(iae);
080            }
081            catch (InvocationTargetException ite) {
082                throw new RuntimeException(ite);
083            }
084            catch (NoSuchMethodException nsme) {
085                throw new RuntimeException(nsme);
086            }
087        }
088    }