View Javadoc

1   /*
2    * Copyright 2007-2008 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.kns.datadictionary;
17  
18  /**
19   * A single attribute definition in the DataDictionary, which contains information relating to the display, validation, and general
20   * maintenance of a specific attribute of an entry.
21   * 
22   * 
23   */
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.kuali.rice.kns.datadictionary.control.ControlDefinition;
28  import org.kuali.rice.kns.datadictionary.exception.CompletionException;
29  import org.kuali.rice.kns.datadictionary.validation.ValidationPattern;
30  import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
31  
32  /**
33   * A single attribute definition in the DataDictionary, which contains
34   * information relating to the display, validation, and general maintenance of a
35   * specific attribute of an entry.
36   * 
37   * 
38   */
39  public class ExternalizableAttributeDefinitionProxy extends AttributeDefinition {
40  	private static final long serialVersionUID = -3204870440281417429L;
41  
42  	// logger
43  	private static Log LOG = LogFactory
44  			.getLog(ExternalizableAttributeDefinitionProxy.class);
45  
46  	private String sourceExternalizableBusinessObjectInterface;
47  	private String sourceAttributeName;
48  	private AttributeDefinition delegate;
49  
50  	/**
51  	 * Constructs an AttributeReferenceDefinition
52  	 */
53  	public ExternalizableAttributeDefinitionProxy() {
54  		LOG.debug("creating new ExternalizableAttributeDefinitionProxy");
55  	}
56  
57  	public void setSourceExternalizableBusinessObjectInterface(
58  			String sourceClassName) {
59  		if (StringUtils.isBlank(sourceClassName)) {
60  			throw new IllegalArgumentException(
61  					"invalid (blank) sourceClassName");
62  		}
63  
64  		this.sourceExternalizableBusinessObjectInterface = sourceClassName;
65  	}
66  
67  	public String getSourceExternalizableBusinessObjectInterface() {
68  		return this.sourceExternalizableBusinessObjectInterface;
69  	}
70  
71  	public void setSourceAttributeName(String sourceAttributeName) {
72  		if (StringUtils.isBlank(sourceAttributeName)) {
73  			throw new IllegalArgumentException(
74  					"invalid (blank) sourceAttributeName");
75  		}
76  
77  		this.sourceAttributeName = sourceAttributeName;
78  	}
79  
80  	public String getSourceAttributeName() {
81  		return this.sourceAttributeName;
82  	}
83  
84  	/**
85  	 * @return AttributeDefinition acting as delegate for this
86  	 *         AttributeReferenceDefinition
87  	 */
88  	AttributeDefinition getDelegate() {
89  		BusinessObjectEntry delegateEntry = null;
90  		if ( delegate == null ) {
91  			try {
92  				delegateEntry = KNSServiceLocatorWeb
93  						.getKualiModuleService()
94  						.getResponsibleModuleService(
95  								Class
96  										.forName(getSourceExternalizableBusinessObjectInterface()))
97  						.getExternalizableBusinessObjectDictionaryEntry(
98  								Class
99  										.forName(getSourceExternalizableBusinessObjectInterface()));
100 			} catch (ClassNotFoundException e) {
101 				LOG.error("Unable to get delegate entry for sourceExternalizableBusinessObjectInterface",e);
102 			}
103 	
104 			if (delegateEntry == null) {
105 				throw new CompletionException(
106 						"no BusinessObjectEntry exists for sourceClassName '"
107 								+ getSourceExternalizableBusinessObjectInterface()
108 								+ "'");
109 			}
110 			delegate = delegateEntry
111 					.getAttributeDefinition(getSourceAttributeName());
112 			if (delegate == null) {
113 				throw new CompletionException(
114 						"no AttributeDefnintion exists for sourceAttributeName '"
115 								+ getSourceExternalizableBusinessObjectInterface()
116 								+ "." + getSourceAttributeName() + "'");
117 			}
118 		}
119 
120 		return delegate;
121 	}
122 
123 	/**
124 	 * Sets the given AttributeDefinition as the delegate for this instance
125 	 * 
126 	 * @param delegate
127 	 */
128 	void setDelegate(AttributeDefinition delegate) {
129 		if (delegate == null) {
130 			throw new IllegalArgumentException("invalid (null) delegate");
131 		}
132 
133 		this.delegate = delegate;
134 	}
135 
136 	/**
137 	 * If forceUppercase wasn't set on this instance, use the value from its
138 	 * delegate.
139 	 * 
140 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getForceUppercase()
141 	 */
142 	public Boolean getForceUppercase() {
143 		Boolean value = super.getForceUppercase();
144 		if (value == null) {
145 			value = getDelegate().getForceUppercase();
146 		}
147 
148 		return value;
149 	}
150 
151 	/**
152 	 * If name wasn't set on this instance, use the value from its delegate.
153 	 * 
154 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getName()
155 	 */
156 	public String getName() {
157 		String name = super.getName();
158 		if (name == null) {
159 			name = getDelegate().getName();
160 		}
161 
162 		return name;
163 	}
164 
165 	/**
166 	 * If label wasn't set on this instance, use the value from its delegate.
167 	 * 
168 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getLabel()
169 	 */
170 	public String getLabel() {
171 		String label = super.getLabel();
172 
173 		if (label == null) {
174 			label = getDelegate().getLabel();
175 		}
176 
177 		return label;
178 	}
179 
180 	/**
181 	 * If shortlabel wasn't set on this instance, use the value from its
182 	 * delegate.
183 	 * 
184 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getShortLabel()
185 	 */
186 	public String getShortLabel() {
187 		String shortLabel = super.getDirectShortLabel();
188 		if (shortLabel == null) {
189 			shortLabel = getDelegate().getShortLabel();
190 		}
191 
192 		return shortLabel;
193 	}
194 
195 	/**
196 	 * If maxLength wasn't set on this instance, use the value from its
197 	 * delegate.
198 	 * 
199 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getMaxLength()
200 	 */
201 	public Integer getMaxLength() {
202 		Integer maxLength = super.getMaxLength();
203 		if (maxLength == null) {
204 			maxLength = getDelegate().getMaxLength();
205 		}
206 
207 		return maxLength;
208 	}
209 
210 	/**
211 	 * @return true if a validationPattern is available, directly or indirectly
212 	 * 
213 	 * @see org.kuali.core.datadictionary.AttributeDefinition#hasValidationPattern()
214 	 */
215 	public boolean hasValidationPattern() {
216 		return (getValidationPattern() != null);
217 	}
218 
219 	/**
220 	 * If validationPattern wasn't set on this instance, use the value from its
221 	 * delegate.
222 	 * 
223 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getValidationPattern()
224 	 */
225 	public ValidationPattern getValidationPattern() {
226 		ValidationPattern validationPattern = super.getValidationPattern();
227 		if (validationPattern == null) {
228 			validationPattern = getDelegate().getValidationPattern();
229 		}
230 
231 		return validationPattern;
232 	}
233 
234 	/**
235 	 * If required wasn't set on this instance, use the value from its delegate.
236 	 * 
237 	 * @see org.kuali.core.datadictionary.AttributeDefinition#isRequired()
238 	 */
239 	public Boolean isRequired() {
240 		Boolean required = super.isRequired();
241 		if (required == null) {
242 			required = getDelegate().isRequired();
243 		}
244 
245 		return required;
246 	}
247 
248 	/**
249 	 * If control wasn't set on this instance, use the value from its delegate.
250 	 * 
251 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getControl()
252 	 */
253 	public ControlDefinition getControl() {
254 		ControlDefinition control = super.getControl();
255 		if (control == null) {
256 			control = getDelegate().getControl();
257 		}
258 
259 		return control;
260 	}
261 
262 	/**
263 	 * If summary wasn't set on this instance, use the value from its delegate.
264 	 * 
265 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getSummary()
266 	 */
267 	public String getSummary() {
268 		String summary = super.getSummary();
269 		if (summary == null) {
270 			summary = getDelegate().getSummary();
271 		}
272 
273 		return summary;
274 	}
275 
276 	/**
277 	 * If description wasn't set on this instance, use the value from its
278 	 * delegate.
279 	 * 
280 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getDescription()
281 	 */
282 	public String getDescription() {
283 		String description = super.getDescription();
284 		if (description == null) {
285 			description = getDelegate().getDescription();
286 		}
287 
288 		return description;
289 	}
290 
291 	/**
292 	 * @return true if a formatterClass is available, directly or indirectly
293 	 * 
294 	 * @see org.kuali.core.datadictionary.AttributeDefinition#hasFormatterClass()
295 	 */
296 	public boolean hasFormatterClass() {
297 		return (getFormatterClass() != null);
298 	}
299 
300 	/**
301 	 * If a formatterClass wasn't set for this instance, use the value from its
302 	 * delegate.
303 	 * 
304 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getFormatterClass()
305 	 */
306 	public String getFormatterClass() {
307 		String formatterClass = super.getFormatterClass();
308 		if (formatterClass == null) {
309 			formatterClass = getDelegate().getFormatterClass();
310 		}
311 
312 		return formatterClass;
313 	}
314 
315 	/**
316 	 * @see org.kuali.core.datadictionary.AttributeDefinition#getDisplayLabelAttribute()
317 	 */
318 	@Override
319 	public String getDisplayLabelAttribute() {
320 		String displayLabelAttribute = super.getDisplayLabelAttribute();
321 		if (StringUtils.isBlank(displayLabelAttribute)) {
322 			displayLabelAttribute = getDelegate().getDisplayLabelAttribute();
323 		}
324 		return displayLabelAttribute;
325 	}
326 
327 	/**
328 	 * Validate the fields associated with locating the delegate. Other
329 	 * validation must be deferred until the delegate class has been assigned.
330 	 * 
331 	 * @see org.kuali.core.datadictionary.DataDictionaryEntry#completeValidation()
332 	 */
333 	@Override
334 	public void completeValidation(Class rootObjectClass, Class otherObjectClass) {
335 		if (StringUtils.isBlank(sourceExternalizableBusinessObjectInterface)) {
336 			throw new IllegalArgumentException(
337 					"invalid (blank) sourceClassName for attribute '"
338 							+ rootObjectClass.getName() + "." + getName() + "'");
339 		}
340 		if (StringUtils.isBlank(sourceAttributeName)) {
341 			throw new IllegalArgumentException(
342 					"invalid (blank) sourceAttributeName for attribute '"
343 							+ rootObjectClass.getName() + "." + getName() + "'");
344 		}
345 		if ( DataDictionary.validateEBOs ) {
346 			getDelegate(); // forces validation
347 			super.completeValidation(rootObjectClass, otherObjectClass);
348 		}
349 	}
350 
351 	/**
352 	 * @see java.lang.Object#toString()
353 	 */
354 	public String toString() {
355 		String name = super.getName();
356 
357 		// workaround for the mysterious,
358 		// still-unreproducible-on-my-machine, null delegate exception on
359 		// Tomcat startup
360 		if ((name == null) && (getDelegate() != null)) {
361 			name = getDelegate().getName();
362 		}
363 		return "AttributeReferenceDefinition for attribute " + name;
364 	}
365 }