View Javadoc
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.List;
19  
20  import org.kuali.ole.sys.document.validation.Validation;
21  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
22  
23  
24  /**
25   * This validation represents a hiearchy of validations
26   */
27  public class CompositeValidation implements Validation {
28      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CompositeValidation.class);
29      
30      protected List<Validation> subValidations;
31      protected boolean shouldQuitOnFail = false;
32  
33      /**
34       * Validates each sub-validation in turn, returning the cumulative effect of each
35       * @see org.kuali.ole.rules.Validation#validate(java.lang.Object[])
36       */
37      public boolean validate(AttributedDocumentEvent event) {
38          if (subValidations == null) {
39              throw new IllegalStateException("A composite validation must have children validations");
40          }
41          
42          boolean result = true;
43          boolean currResult;
44          
45          for (Validation validation: subValidations) {
46              currResult = validation.stageValidation(event);
47              result &= currResult;
48              
49              if(!currResult) {
50                  if ( LOG.isDebugEnabled() ) {
51                      LOG.debug(validation.getClass() + " failed");
52                  }
53              }
54              
55              if (!currResult && validation.shouldQuitOnFail()) {
56                  break;
57              }
58          }
59          
60          return result;
61      }
62  
63      /**
64       * Just call validate!
65       * @see org.kuali.ole.sys.document.validation.Validation#stageValidation(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
66       */
67      public boolean stageValidation(AttributedDocumentEvent event) {
68          return validate(event);
69      }
70  
71      /**
72       * Returns the List of validations to test
73       * @return a List of validations to test
74       */
75      public List<Validation> getValidations() {
76          return this.subValidations;
77      }
78      
79      /**
80       * Sets the List of validations to test
81       * @param validations a List of validations to test against
82       */
83      public void setValidations(List<Validation> validations) {
84          this.subValidations = validations;
85      }
86  
87      /**
88       * Gets the shouldQuitOnFail attribute. 
89       * @return Returns the shouldQuitOnFail.
90       */
91      public boolean shouldQuitOnFail() {
92          return shouldQuitOnFail;
93      }
94  
95      /**
96       * Sets the shouldQuitOnFail attribute value.
97       * @param shouldQuitOnFail The shouldQuitOnFail to set.
98       */
99      public void setQuitOnFail(boolean shouldQuitOnFail) {
100         this.shouldQuitOnFail = shouldQuitOnFail;
101     }
102 }