View Javadoc

1   /*
2    * Copyright 2006-2007 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;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.bo.BusinessObject;
20  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
21  
22  /**
23   *                     The reference element specifies the name of a reference
24                      object that is required to exist in order for the primary
25                      business object to be created or modified on a BO.
26  
27                      DD: See ReferenceDefinition.java
28  
29                      JSTL: references are Maps with the following keys:
30                      * attributeName (String)
31                      * activeIndicatorAttributeName (String)
32                      * activeIndicatorReversed (boolean String)
33                      * attributeToHighlightOnFail (String)
34                      * displayFieldName (String)
35  
36   *
37   */
38  public class ReferenceDefinition extends DataDictionaryDefinitionBase {
39      private static final long serialVersionUID = 1737968024207302931L;
40      
41  	protected String attributeName;
42      protected String attributeToHighlightOnFail;
43      protected String displayFieldName;
44      protected String collection;
45      protected Class<? extends BusinessObject> collectionBusinessObjectClass;
46      protected Class<? extends BusinessObject> businessObjectClass;
47      
48      public ReferenceDefinition() {}
49  
50      /**
51       * @return attributeName
52       */
53      public String getAttributeName() {
54          return attributeName;
55      }
56  
57      /**
58       * attributeName is the name of a reference object that
59                          must exist and not be null.  In the case of a collection,
60                          then this is the name of a reference object within the
61                          collection element.
62       * 
63       * @throws IllegalArgumentException if the given attributeName is blank
64       */
65      public void setAttributeName(String attributeName) {
66          if (StringUtils.isBlank(attributeName)) {
67              throw new IllegalArgumentException("invalid (blank) attributeName");
68          }
69          this.attributeName = attributeName;
70      }
71  
72      /**
73       * Gets the attributeToHighlightOnFail attribute.
74       * 
75       * @return Returns the attributeToHighlightOnFail.
76       */
77      public String getAttributeToHighlightOnFail() {
78          return attributeToHighlightOnFail;
79      }
80  
81      /**
82              attributeToHighlightOnFail is the name of the busines
83                          object attribute which will be highlighted when
84                          the default existence check fails.
85       */
86      public void setAttributeToHighlightOnFail(String attributeToHighlightOnFail) {
87          if (StringUtils.isBlank(attributeToHighlightOnFail)) {
88              throw new IllegalArgumentException("invalid (blank) attributeToHighlightOnFail");
89          }
90          this.attributeToHighlightOnFail = attributeToHighlightOnFail;
91      }
92  
93      /**
94       * Gets the displayFieldName attribute.
95       * 
96       * @return Returns the displayFieldName.
97       */
98      public String getDisplayFieldName() {
99          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 }