1 /*
2 * Copyright 2008 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.ole.sys.document.validation;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.List;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
23 import org.kuali.rice.core.web.format.FormatException;
24 import org.kuali.rice.krad.util.ObjectUtils;
25
26 /**
27 * An abstract class that defines methods needed to act on parameter properties for a validation.
28 */
29 public abstract class ParameterizedValidation {
30 private List<ValidationFieldConvertible> parameterProperties;
31
32 /**
33 * Gets the parameterProperties attribute.
34 * @return Returns the parameterProperties.
35 */
36 protected List<ValidationFieldConvertible> getParameterProperties() {
37 return parameterProperties;
38 }
39
40 /**
41 * Sets the parameterProperties attribute value.
42 * @param parameterProperties The parameterProperties to set.
43 */
44 public void setParameterProperties(List<ValidationFieldConvertible> parameterProperties) {
45 this.parameterProperties = parameterProperties;
46 }
47
48 /**
49 * Given an event and the parameterProperties given by the validations, copies the values from the events to the proper fields in the validation.
50 * @param event an array to derive properties from
51 * @param the parameter to set the parameters on
52 */
53 public void populateParametersFromEvent(AttributedDocumentEvent event) {
54 if (getParameterProperties() != null) {
55 for (ValidationFieldConvertible property: getParameterProperties()) {
56 populateParameterFromEvent(event, property);
57 }
58 }
59 }
60
61 /**
62 * Populates a single parameter field based on a field conversion, given an event to populate data from
63 * @param event the event which acts as the source of data
64 * @param validation the validation to populate
65 * @param conversion the conversion information
66 */
67 protected void populateParameterFromEvent(AttributedDocumentEvent event, ValidationFieldConvertible conversion) {
68 try {
69 Class propertyClass = PropertyUtils.getPropertyType(event, conversion.getSourceEventProperty());
70 Object propertyValue = ObjectUtils.getPropertyValue(event, conversion.getSourceEventProperty());
71 if (propertyValue != null) {
72 ObjectUtils.setObjectProperty(this, conversion.getTargetValidationProperty(), propertyClass, propertyValue);
73 }
74 }
75 catch (FormatException fe) {
76 throw new RuntimeException(fe);
77 }
78 catch (IllegalAccessException iae) {
79 throw new RuntimeException(iae);
80 }
81 catch (InvocationTargetException ite) {
82 throw new RuntimeException(ite);
83 }
84 catch (NoSuchMethodException nsme) {
85 throw new RuntimeException(nsme);
86 }
87 }
88 }