001/**
002 * Copyright 2005-2014 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 * This class allows a single attribute value to be exposed to the validation service, along
027 * with some guidance about how that value should be interpreted, provided by the AttributeDefinition
028 * that corresponds. It's a special AttributeValueReader since it explicitly doesn't expose any
029 * other attribute values, so it should only be used when the underlying business object is not available
030 * and we want to limit access to (for example) validation that requires only a single attribute value.
031 * This eliminates more complicated validation like 'this field is required when another field is filled in.'
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class SingleAttributeValueReader extends BaseAttributeValueReader {
036
037    private Object value;
038    private AttributeDefinition definition;
039
040    public SingleAttributeValueReader(Object value, String entryName, String attributeName,
041            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) ?
052                definition : null;
053    }
054
055    @Override
056    public List<Constrainable> getDefinitions() {
057        return null;
058    }
059
060    /**
061     * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getEntry()
062     */
063    @Override
064    public Constrainable getEntry() {
065        return null;
066    }
067
068    @Override
069    public String getLabel(String attributeName) {
070        if (definition != null && definition.getName() != null && definition.getName().equals(attributeName)) {
071            return definition.getLabel();
072        }
073
074        return attributeName;
075    }
076
077    @Override
078    public Object getObject() {
079        return null;
080    }
081
082    @Override
083    public String getPath() {
084        return attributeName;
085    }
086
087    @Override
088    public Class<?> getType(String selectedAttributeName) {
089        Constrainable attributeDefinition = getDefinition(selectedAttributeName);
090
091        if (attributeDefinition != null && attributeDefinition instanceof DataTypeConstraint) {
092            DataTypeConstraint dataTypeConstraint = (DataTypeConstraint) attributeDefinition;
093            if (dataTypeConstraint.getDataType() != null) {
094                return dataTypeConstraint.getDataType().getType();
095            }
096        }
097
098        // Assuming we can reliably guess
099        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}