001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.datadictionary.validation;
017
018import org.kuali.rice.krad.datadictionary.AttributeDefinition;
019import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
020import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
021import org.kuali.rice.krad.datadictionary.validation.constraint.DataTypeConstraint;
022
023import 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 */
036public 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 boolean isReadable() {
101        return true;
102    }
103
104    @Override
105        public <X> X getValue() throws AttributeValidationException {
106                return (X) value;
107        }
108        
109        @Override
110        public <X> X getValue(String attributeName) throws AttributeValidationException {
111                Constrainable attributeDefinition = getDefinition(attributeName);
112                
113                if (attributeDefinition != null)
114                        return (X) value;
115                
116                return null;
117        }
118    
119    @Override
120    public AttributeValueReader clone(){
121        SingleAttributeValueReader clone = new SingleAttributeValueReader(this.value, this.entryName, this.attributeName, this.definition);
122        clone.setAttributeName(this.attributeName);
123        return clone;
124    }
125
126}