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.impl;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20
21 import org.kuali.ole.sys.document.validation.Validation;
22 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
23 import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
24 import org.kuali.rice.krad.util.GlobalVariables;
25 import org.kuali.rice.krad.util.ObjectUtils;
26
27 /**
28 * A validation that runs a list of child validations over each member of a collection.
29 */
30 public class CollectionValidation extends CompositeValidation {
31 protected String collectionProperty;
32
33 /**
34 * Iterates over each member of the collection, which is assumed to be the property of the validation event named by the given collectionProperty
35 * @see org.kuali.ole.sys.document.validation.impl.CompositeValidation#validate(java.lang.Object[])
36 */
37 public boolean validate(AttributedDocumentEvent event) {
38 boolean result = true;
39
40 if (collectionProperty == null) {
41 throw new IllegalStateException("collectionProperty must not be null");
42 }
43
44 Iterator iter = getCollection(event).iterator();
45
46 // hold on to any existing iteration subject until after we're done doing our thing
47 Object parentIterationSubject = event.getIterationSubject();
48
49 int count = 0;
50 while (iter.hasNext()) {
51 result &= validateEachObject(event, iter.next(), count);
52 count += 1;
53 }
54
55 event.setIterationSubject(parentIterationSubject); // or back to null if the event didn't have an iteration subject in the first place
56
57 return result;
58 }
59
60 /**
61 * Validates each object in the collection
62 * @param event the event to validate against
63 * @param objectToValidate the object from the collection which is being validated
64 * @return true if object passed all child sub-validations, false otherwise
65 */
66 protected boolean validateEachObject(AttributedDocumentEvent event, Object objectToValidate, int count) {
67 boolean result = true;
68 String errorPath = buildPropertyName(count);
69 GlobalVariables.getMessageMap().addToErrorPath(errorPath);
70 boolean currentResult = true;
71 event.setIterationSubject(objectToValidate);
72 for (Validation validation: getValidations()) {
73 currentResult = validation.stageValidation(event);
74 result &= currentResult;
75 if (!currentResult && validation.shouldQuitOnFail()) {
76 break;
77 }
78 }
79 GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
80 return result;
81 }
82
83 /**
84 * Here's a hack for all my homies: this method builds the property name of a specific item in a collection by chopping the presumed "s" at the end of collectionProperties,
85 * and then attaches array/list syntax to it to hold the count. It'll work for most stuff, won't it?
86 * @params count the count of the current item in the collection
87 * @return
88 */
89 protected String buildPropertyName(int count) {
90 return new StringBuilder().append(collectionProperty.substring(0, collectionProperty.length()-1)).append('[').append(count).append(']').toString();
91 }
92
93 /**
94 * Based on the collection property, finds the events
95 * @param event
96 * @return
97 */
98 protected Collection getCollection(KualiDocumentEvent event) {
99 return (Collection)ObjectUtils.getPropertyValue(event, collectionProperty);
100 }
101
102 /**
103 * Gets the collectionProperty attribute.
104 * @return Returns the collectionProperty.
105 */
106 public String getCollectionProperty() {
107 return collectionProperty;
108 }
109
110 /**
111 * Sets the collectionProperty attribute value.
112 * @param collectionProperty The collectionProperty to set.
113 */
114 public void setCollectionProperty(String collectionProperty) {
115 this.collectionProperty = collectionProperty;
116 }
117 }