View Javadoc

1   /**
2    * Copyright 2005-2013 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;
17  
18  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
19  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20  import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
21  import org.kuali.rice.krad.datadictionary.validation.constraint.DataTypeConstraint;
22  
23  import java.util.List;
24  
25  /**
26   * This class allows a single attribute value to be exposed to the validation service, along
27   * with some guidance about how that value should be interpreted, provided by the AttributeDefinition
28   * that corresponds. It's a special AttributeValueReader since it explicitly doesn't expose any
29   * other attribute values, so it should only be used when the underlying business object is not available
30   * and we want to limit access to (for example) validation that requires only a single attribute value.
31   * This eliminates more complicated validation like 'this field is required when another field is filled in.'
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class SingleAttributeValueReader extends BaseAttributeValueReader {
36  
37      private Object value;
38      private AttributeDefinition definition;
39  
40      public SingleAttributeValueReader(Object value, String entryName, String attributeName,
41              AttributeDefinition definition) {
42          this.value = value;
43          this.entryName = entryName;
44          this.attributeName = attributeName;
45          this.definition = definition;
46      }
47  
48      @Override
49      public Constrainable getDefinition(String attributeName) {
50          // Only return the definition if you have it, and if it's the definition for the passed attribute name
51          return definition != null && definition.getName() != null && definition.getName().equals(attributeName) ?
52                  definition : null;
53      }
54  
55      @Override
56      public List<Constrainable> getDefinitions() {
57          return null;
58      }
59  
60      /**
61       * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getEntry()
62       */
63      @Override
64      public Constrainable getEntry() {
65          return null;
66      }
67  
68      @Override
69      public String getLabel(String attributeName) {
70          if (definition != null && definition.getName() != null && definition.getName().equals(attributeName)) {
71              return definition.getLabel();
72          }
73  
74          return attributeName;
75      }
76  
77      @Override
78      public Object getObject() {
79          return null;
80      }
81  
82      @Override
83      public String getPath() {
84          return attributeName;
85      }
86  
87      @Override
88      public Class<?> getType(String selectedAttributeName) {
89          Constrainable attributeDefinition = getDefinition(selectedAttributeName);
90  
91          if (attributeDefinition != null && attributeDefinition instanceof DataTypeConstraint) {
92              DataTypeConstraint dataTypeConstraint = (DataTypeConstraint) attributeDefinition;
93              if (dataTypeConstraint.getDataType() != null) {
94                  return dataTypeConstraint.getDataType().getType();
95              }
96          }
97  
98          // Assuming we can reliably guess
99          return value != null ? value.getClass() : null;
100     }
101 
102     @Override
103     public boolean isReadable() {
104         return true;
105     }
106 
107     @Override
108     public <X> X getValue() throws AttributeValidationException {
109         return (X) value;
110     }
111 
112     @Override
113     public <X> X getValue(String attributeName) throws AttributeValidationException {
114         Constrainable attributeDefinition = getDefinition(attributeName);
115 
116         if (attributeDefinition != null) {
117             return (X) value;
118         }
119 
120         return null;
121     }
122 
123     @Override
124     public AttributeValueReader clone() {
125         SingleAttributeValueReader clone = new SingleAttributeValueReader(this.value, this.entryName,
126                 this.attributeName, this.definition);
127         clone.setAttributeName(this.attributeName);
128         return clone;
129     }
130 
131 }