View Javadoc
1   /**
2    * Copyright 2005-2016 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.rice.krad.datadictionary.validation.result;
17  
18  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
19  import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
20  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
21  import org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor;
22  
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  /**
28   * defines the information expected when a processor has processed a constraint
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   * @see ConstraintProcessor
32   */
33  public class ProcessorResult {
34  
35      private final transient List<Constraint> constraints;
36      private final transient Constrainable definition;
37      private final transient AttributeValueReader attributeValueReader;
38  
39      private final List<ConstraintValidationResult> constraintValidationResults;
40  
41      /**
42       * creates a processor result from the provided constraint validation result
43       *
44       * @param constraintValidationResult - the constraint validation result
45       */
46      public ProcessorResult(ConstraintValidationResult constraintValidationResult) {
47          this(constraintValidationResult, null, null);
48      }
49  
50      /**
51       * creates a processor result from the parameters provided
52       *
53       * @param constraintValidationResult - the constraint validation result
54       * @param definition - a Data Dictionary definition e.g. {@code ComplexAttributeDefinition} or {@code
55       * CollectionDefinition}
56       * @param attributeValueReader - provides access to the attribute being validated
57       * @param constraints - optional constraints to use
58       */
59      public ProcessorResult(ConstraintValidationResult constraintValidationResult, Constrainable definition,
60              AttributeValueReader attributeValueReader, Constraint... constraints) {
61          this.constraintValidationResults = Collections.singletonList(constraintValidationResult);
62          this.definition = definition;
63          this.attributeValueReader = attributeValueReader;
64          this.constraints = Arrays.asList(constraints);
65      }
66  
67      /**
68       * creates a processor result from the parameters provided
69       *
70       * @param constraintValidationResult - the constraint validation result
71       * @param definition - a Data Dictionary definition e.g. {@code ComplexAttributeDefinition} or {@code
72       * CollectionDefinition}
73       * @param attributeValueReader - provides access to the attribute being validated
74       * @param constraints - the list of constraints to use
75       */
76      public ProcessorResult(ConstraintValidationResult constraintValidationResult, Constrainable definition,
77              AttributeValueReader attributeValueReader, List<Constraint> constraints) {
78          this.constraintValidationResults = Collections.singletonList(constraintValidationResult);
79          this.definition = definition;
80          this.attributeValueReader = attributeValueReader;
81          this.constraints = constraints;
82      }
83  
84      /**
85       * creates a processor result from the provided constraint validation results
86       *
87       * @param constraintValidationResults - the constraint validation results to use
88       */
89      public ProcessorResult(List<ConstraintValidationResult> constraintValidationResults) {
90          this(constraintValidationResults, null, null);
91      }
92  
93      /**
94       * creates a processor result from the parameters provided
95       *
96       * @param constraintValidationResults - the constraint validation results
97       * @param definition - a Data Dictionary definition e.g. {@code ComplexAttributeDefinition} or {@code
98       * CollectionDefinition}
99       * @param attributeValueReader - provides access to the attribute being validated
100      * @param constraints - optional constraints to use
101      */
102     public ProcessorResult(List<ConstraintValidationResult> constraintValidationResults, Constrainable definition,
103             AttributeValueReader attributeValueReader, Constraint... constraints) {
104         this.constraintValidationResults = constraintValidationResults;
105         this.definition = definition;
106         this.attributeValueReader = attributeValueReader;
107         this.constraints = Arrays.asList(constraints);
108     }
109 
110     /**
111      * checks whether this object has a single constraint validation result
112      *
113      * @return true if only one result found, false if not
114      */
115     public boolean isSingleConstraintResult() {
116         return this.constraintValidationResults.size() == 1;
117     }
118 
119     /**
120      * checks whether a Data Dictionary definition is present
121      *
122      * @return true if definition is present, false otherwise
123      */
124     public boolean isDefinitionProvided() {
125         return definition != null;
126     }
127 
128     /**
129      * checks whether an attribute value reader is present
130      *
131      * @return true if reader is present, false otherwise
132      */
133     public boolean isAttributeValueReaderProvided() {
134         return attributeValueReader != null;
135     }
136 
137     /**
138      * gets the first constraint validation result
139      *
140      * @return null if there are no results
141      */
142     public ConstraintValidationResult getFirstConstraintValidationResult() {
143         return this.constraintValidationResults.isEmpty() ? null : this.constraintValidationResults.get(0);
144     }
145 
146     /**
147      * @return the constraintValidationResults
148      */
149     public List<ConstraintValidationResult> getConstraintValidationResults() {
150         return this.constraintValidationResults;
151     }
152 
153     /**
154      * @return the definition
155      */
156     public Constrainable getDefinition() {
157         return this.definition;
158     }
159 
160     /**
161      * @return the attributeValueReader
162      */
163     public AttributeValueReader getAttributeValueReader() {
164         return this.attributeValueReader;
165     }
166 
167     /**
168      * @return the constraints
169      */
170     public List<Constraint> getConstraints() {
171         return this.constraints;
172     }
173 
174 }