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;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.bo.BusinessObject;
020import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
021
022/**
023 *                     The reference element specifies the name of a reference
024                    object that is required to exist in order for the primary
025                    business object to be created or modified on a BO.
026
027                    DD: See ReferenceDefinition.java
028
029                    JSTL: references are Maps with the following keys:
030                    * attributeName (String)
031                    * activeIndicatorAttributeName (String)
032                    * activeIndicatorReversed (boolean String)
033                    * attributeToHighlightOnFail (String)
034                    * displayFieldName (String)
035
036 *
037 */
038public class ReferenceDefinition extends DataDictionaryDefinitionBase {
039    private static final long serialVersionUID = 1737968024207302931L;
040    
041        protected String attributeName;
042    protected String attributeToHighlightOnFail;
043    protected String displayFieldName;
044    protected String collection;
045    protected Class<? extends BusinessObject> collectionBusinessObjectClass;
046    protected Class<? extends BusinessObject> businessObjectClass;
047    
048    public ReferenceDefinition() {}
049
050    /**
051     * @return attributeName
052     */
053    public String getAttributeName() {
054        return attributeName;
055    }
056
057    /**
058     * attributeName is the name of a reference object that
059                        must exist and not be null.  In the case of a collection,
060                        then this is the name of a reference object within the
061                        collection element.
062     * 
063     * @throws IllegalArgumentException if the given attributeName is blank
064     */
065    public void setAttributeName(String attributeName) {
066        if (StringUtils.isBlank(attributeName)) {
067            throw new IllegalArgumentException("invalid (blank) attributeName");
068        }
069        this.attributeName = attributeName;
070    }
071
072    /**
073     * Gets the attributeToHighlightOnFail attribute.
074     * 
075     * @return Returns the attributeToHighlightOnFail.
076     */
077    public String getAttributeToHighlightOnFail() {
078        return attributeToHighlightOnFail;
079    }
080
081    /**
082            attributeToHighlightOnFail is the name of the busines
083                        object attribute which will be highlighted when
084                        the default existence check fails.
085     */
086    public void setAttributeToHighlightOnFail(String attributeToHighlightOnFail) {
087        if (StringUtils.isBlank(attributeToHighlightOnFail)) {
088            throw new IllegalArgumentException("invalid (blank) attributeToHighlightOnFail");
089        }
090        this.attributeToHighlightOnFail = attributeToHighlightOnFail;
091    }
092
093    /**
094     * Gets the displayFieldName attribute.
095     * 
096     * @return Returns the displayFieldName.
097     */
098    public String getDisplayFieldName() {
099        return displayFieldName;
100    }
101
102    /**
103        displayFieldName is the name of the field to pull the label as it will
104                        appear in an error message.  e.g. "chartOfAccountsCode".
105     */
106    public void setDisplayFieldName(String displayFieldName) {
107        this.displayFieldName = displayFieldName;
108    }
109
110    /**
111     * This method returns true if the displayFieldName is set, otherwise it returns false. Whether the displayFieldName is set is
112     * defined by whether it has any non-whitespace content in it.
113     * 
114     * @return
115     */
116    public boolean isDisplayFieldNameSet() {
117        return StringUtils.isNotBlank(displayFieldName);
118    }
119
120    public String getCollection() {
121        return collection;
122    }
123
124    /**
125        collection is the name of a collection that must exist
126     */
127    public void setCollection(String collection) {
128        this.collection = collection;
129    }
130
131    public boolean isCollectionReference() {
132        return StringUtils.isNotBlank(getCollection());
133    }
134
135    public Class<? extends BusinessObject> getCollectionBusinessObjectClass() {
136        if( collectionBusinessObjectClass == null && isCollectionReference() ){
137            collectionBusinessObjectClass=DataDictionary.getCollectionElementClass(businessObjectClass, collection);
138        }
139
140        return collectionBusinessObjectClass;
141    }
142
143    /** Class that the specified collection represents.  Does not need to be set.  The DD
144     * Will set this attribute through introspection.
145     */
146    public void setCollectionBusinessObjectClass(Class<? extends BusinessObject> collectionBusinessObjectClass) {
147        this.collectionBusinessObjectClass = collectionBusinessObjectClass;
148    }
149
150    /**
151     * Directly validate simple fields.
152     * 
153     * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
154     */
155    public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
156
157
158        // make sure the attributeName is actually a property of the BO
159        String tmpAttributeName = isCollectionReference() ? collection : attributeName;
160        if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, tmpAttributeName)) {
161            throw new AttributeValidationException("unable to find attribute '" + tmpAttributeName + "' in rootBusinessObjectClass '" + rootBusinessObjectClass.getName() + "' (" + "" + ")");
162        }
163        // make sure the attributeToHighlightOnFail is actually a property of the BO
164        if (isCollectionReference()) {
165            getCollectionBusinessObjectClass(); // forces loading of the class
166            if ( collectionBusinessObjectClass == null ) {
167                throw new AttributeValidationException("Unable to determine collectionBusinessObjectClass for collection '" + businessObjectClass.getName() + "." + collection + "'");
168            }
169            
170            if (!DataDictionary.isPropertyOf(collectionBusinessObjectClass, attributeToHighlightOnFail)) {
171                throw new AttributeValidationException("unable to find attribute '" + attributeToHighlightOnFail + "' in collectionBusinessObjectClass '" + collectionBusinessObjectClass.getName() + "' (" + "" + ")");
172            }
173        }
174        else {
175            if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, attributeToHighlightOnFail)) {
176                throw new AttributeValidationException("unable to find attribute '" + attributeToHighlightOnFail + "' in rootBusinessObjectClass '" + rootBusinessObjectClass.getName() + "' (" + "" + ")");
177            }
178        }
179
180    }
181
182
183    /**
184     * @see java.lang.Object#toString()
185     */
186    public String toString() {
187        return "ReferenceDefinition for attribute " + getAttributeName();
188    }
189
190    public Class<? extends BusinessObject> getBusinessObjectClass() {
191        return businessObjectClass;
192    }
193
194    public void setBusinessObjectClass(Class<? extends BusinessObject> businessObjectClass) {
195        this.businessObjectClass = businessObjectClass;
196    }
197}