001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.datadictionary.validation;
017
018 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
019 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
020 import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
021 import org.kuali.rice.krad.datadictionary.validation.constraint.DataTypeConstraint;
022
023 import java.util.List;
024
025
026 /**
027 * This class allows a single attribute value to be exposed to the validation service, along
028 * with some guidance about how that value should be interpreted, provided by the AttributeDefinition
029 * that corresponds. It's a special AttributeValueReader since it explicitly doesn't expose any
030 * other attribute values, so it should only be used when the underlying business object is not available
031 * and we want to limit access to (for example) validation that requires only a single attribute value.
032 * This eliminates more complicated validation like 'this field is required when another field is filled in.'
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036 public class SingleAttributeValueReader extends BaseAttributeValueReader {
037
038 private Object value;
039 private AttributeDefinition definition;
040
041 public SingleAttributeValueReader(Object value, String entryName, String attributeName, AttributeDefinition definition) {
042 this.value = value;
043 this.entryName = entryName;
044 this.attributeName = attributeName;
045 this.definition = definition;
046 }
047
048 @Override
049 public Constrainable getDefinition(String attributeName) {
050 // Only return the definition if you have it, and if it's the definition for the passed attribute name
051 return definition != null && definition.getName() != null && definition.getName().equals(attributeName) ? definition : null;
052 }
053
054 @Override
055 public List<Constrainable> getDefinitions() {
056 return null;
057 }
058
059 /**
060 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getEntry()
061 */
062 @Override
063 public Constrainable getEntry() {
064 return null;
065 }
066
067 @Override
068 public String getLabel(String attributeName) {
069 if (definition != null && definition.getName() != null && definition.getName().equals(attributeName))
070 return definition.getLabel();
071
072 return attributeName;
073 }
074
075 @Override
076 public Object getObject() {
077 return null;
078 }
079
080 @Override
081 public String getPath() {
082 return attributeName;
083 }
084
085 @Override
086 public Class<?> getType(String selectedAttributeName) {
087 Constrainable attributeDefinition = getDefinition(selectedAttributeName);
088
089 if (attributeDefinition != null && attributeDefinition instanceof DataTypeConstraint) {
090 DataTypeConstraint dataTypeConstraint = (DataTypeConstraint)attributeDefinition;
091 if (dataTypeConstraint.getDataType() != null)
092 return dataTypeConstraint.getDataType().getType();
093 }
094
095 // Assuming we can reliably guess
096 return value != null ? value.getClass() : null;
097 }
098
099 @Override
100 public <X> X getValue() throws AttributeValidationException {
101 return (X) value;
102 }
103
104 @Override
105 public <X> X getValue(String attributeName) throws AttributeValidationException {
106 Constrainable attributeDefinition = getDefinition(attributeName);
107
108 if (attributeDefinition != null)
109 return (X) value;
110
111 return null;
112 }
113
114 }