001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.krad.datadictionary;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.kuali.rice.krad.datadictionary.control.ControlDefinition;
022    import org.kuali.rice.krad.datadictionary.exception.CompletionException;
023    import org.kuali.rice.krad.datadictionary.parse.BeanTag;
024    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
025    import org.kuali.rice.krad.datadictionary.validation.ValidationPattern;
026    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
027    
028    /**
029     * A single attribute definition in the DataDictionary, which contains
030     * information relating to the display, validation, and general maintenance of a
031     * specific attribute of an entry.
032     */
033    @BeanTag(name = "externalizableAttributeDefinitionProxy-bean")
034    public class ExternalizableAttributeDefinitionProxy extends AttributeDefinition {
035        private static final long serialVersionUID = -3204870440281417429L;
036    
037        // logger
038        private static Log LOG = LogFactory.getLog(ExternalizableAttributeDefinitionProxy.class);
039    
040        private String sourceExternalizableBusinessObjectInterface;
041        private String sourceAttributeName;
042        private AttributeDefinition delegate;
043    
044        /**
045         * Constructs an AttributeReferenceDefinition
046         */
047        public ExternalizableAttributeDefinitionProxy() {
048            LOG.debug("creating new ExternalizableAttributeDefinitionProxy");
049        }
050    
051        public void setSourceExternalizableBusinessObjectInterface(String sourceClassName) {
052            if (StringUtils.isBlank(sourceClassName)) {
053                throw new IllegalArgumentException("invalid (blank) sourceClassName");
054            }
055    
056            this.sourceExternalizableBusinessObjectInterface = sourceClassName;
057        }
058    
059        @BeanTagAttribute(name = "sourceExternalizableBusinessObjectInterface")
060        public String getSourceExternalizableBusinessObjectInterface() {
061            return this.sourceExternalizableBusinessObjectInterface;
062        }
063    
064        public void setSourceAttributeName(String sourceAttributeName) {
065            if (StringUtils.isBlank(sourceAttributeName)) {
066                throw new IllegalArgumentException("invalid (blank) sourceAttributeName");
067            }
068    
069            this.sourceAttributeName = sourceAttributeName;
070        }
071    
072        @BeanTagAttribute(name = "sourceAttributeName")
073        public String getSourceAttributeName() {
074            return this.sourceAttributeName;
075        }
076    
077        /**
078         * @return AttributeDefinition acting as delegate for this
079         *         AttributeReferenceDefinition
080         */
081        @BeanTagAttribute(name = "delegate", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
082        AttributeDefinition getDelegate() {
083            BusinessObjectEntry delegateEntry = null;
084            if (delegate == null) {
085                try {
086                    delegateEntry = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(Class.forName(
087                            getSourceExternalizableBusinessObjectInterface()))
088                            .getExternalizableBusinessObjectDictionaryEntry(Class.forName(
089                                    getSourceExternalizableBusinessObjectInterface()));
090                } catch (ClassNotFoundException e) {
091                    LOG.error("Unable to get delegate entry for sourceExternalizableBusinessObjectInterface", e);
092                }
093    
094                if (delegateEntry == null) {
095                    throw new CompletionException("no BusinessObjectEntry exists for sourceClassName '"
096                            + getSourceExternalizableBusinessObjectInterface()
097                            + "'");
098                }
099                delegate = delegateEntry.getAttributeDefinition(getSourceAttributeName());
100                if (delegate == null) {
101                    throw new CompletionException("no AttributeDefnintion exists for sourceAttributeName '"
102                            + getSourceExternalizableBusinessObjectInterface()
103                            + "."
104                            + getSourceAttributeName()
105                            + "'");
106                }
107            }
108    
109            return delegate;
110        }
111    
112        /**
113         * Sets the given AttributeDefinition as the delegate for this instance
114         *
115         * @param delegate
116         */
117        void setDelegate(AttributeDefinition delegate) {
118            if (delegate == null) {
119                throw new IllegalArgumentException("invalid (null) delegate");
120            }
121    
122            this.delegate = delegate;
123        }
124    
125        /**
126         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getForceUppercase()
127         */
128        public Boolean getForceUppercase() {
129            Boolean value = super.getForceUppercase();
130            if (value == null) {
131                value = getDelegate().getForceUppercase();
132            }
133    
134            return value;
135        }
136    
137        /**
138         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getName()
139         */
140        public String getName() {
141            String name = super.getName();
142            if (name == null) {
143                name = getDelegate().getName();
144            }
145    
146            return name;
147        }
148    
149        /**
150         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getLabel()
151         */
152        public String getLabel() {
153            String label = super.getLabel();
154    
155            if (label == null) {
156                label = getDelegate().getLabel();
157            }
158    
159            return label;
160        }
161    
162        /**
163         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getShortLabel()
164         */
165        public String getShortLabel() {
166            String shortLabel = super.getDirectShortLabel();
167            if (shortLabel == null) {
168                shortLabel = getDelegate().getShortLabel();
169            }
170    
171            return shortLabel;
172        }
173    
174        /**
175         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getMaxLength()
176         */
177        public Integer getMaxLength() {
178            Integer maxLength = super.getMaxLength();
179            if (maxLength == null) {
180                maxLength = getDelegate().getMaxLength();
181            }
182    
183            return maxLength;
184        }
185    
186        /**
187         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#hasValidationPattern()
188         */
189        public boolean hasValidationPattern() {
190            return (getValidationPattern() != null);
191        }
192    
193        /**
194         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getValidationPattern()
195         */
196        public ValidationPattern getValidationPattern() {
197            ValidationPattern validationPattern = super.getValidationPattern();
198            if (validationPattern == null) {
199                validationPattern = getDelegate().getValidationPattern();
200            }
201    
202            return validationPattern;
203        }
204    
205        /**
206         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#isRequired()
207         */
208        public Boolean isRequired() {
209            Boolean required = super.isRequired();
210            if (required == null) {
211                required = getDelegate().isRequired();
212            }
213    
214            return required;
215        }
216    
217        /**
218         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getControl()
219         */
220        public ControlDefinition getControl() {
221            ControlDefinition control = super.getControl();
222            if (control == null) {
223                control = getDelegate().getControl();
224            }
225    
226            return control;
227        }
228    
229        /**
230         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getSummary()
231         */
232        public String getSummary() {
233            String summary = super.getSummary();
234            if (summary == null) {
235                summary = getDelegate().getSummary();
236            }
237    
238            return summary;
239        }
240    
241        /**
242         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getDescription()
243         */
244        public String getDescription() {
245            String description = super.getDescription();
246            if (description == null) {
247                description = getDelegate().getDescription();
248            }
249    
250            return description;
251        }
252    
253        /**
254         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#hasFormatterClass()
255         */
256        public boolean hasFormatterClass() {
257            return (getFormatterClass() != null);
258        }
259    
260        /**
261         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getFormatterClass()
262         */
263        public String getFormatterClass() {
264            String formatterClass = super.getFormatterClass();
265            if (formatterClass == null) {
266                formatterClass = getDelegate().getFormatterClass();
267            }
268    
269            return formatterClass;
270        }
271    
272        /**
273         * @see org.kuali.rice.krad.datadictionary.AttributeDefinition#getDisplayLabelAttribute()
274         */
275        @Override
276        public String getDisplayLabelAttribute() {
277            String displayLabelAttribute = super.getDisplayLabelAttribute();
278            if (StringUtils.isBlank(displayLabelAttribute)) {
279                displayLabelAttribute = getDelegate().getDisplayLabelAttribute();
280            }
281            return displayLabelAttribute;
282        }
283    
284        /**
285         * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntry#completeValidation()
286         */
287        @Override
288        public void completeValidation(Class rootObjectClass, Class otherObjectClass) {
289            if (StringUtils.isBlank(sourceExternalizableBusinessObjectInterface)) {
290                throw new IllegalArgumentException("invalid (blank) sourceClassName for attribute '"
291                        + rootObjectClass.getName()
292                        + "."
293                        + getName()
294                        + "'");
295            }
296            if (StringUtils.isBlank(sourceAttributeName)) {
297                throw new IllegalArgumentException("invalid (blank) sourceAttributeName for attribute '"
298                        + rootObjectClass.getName()
299                        + "."
300                        + getName()
301                        + "'");
302            }
303            if (DataDictionary.validateEBOs) {
304                getDelegate(); // forces validation
305                super.completeValidation(rootObjectClass, otherObjectClass);
306            }
307        }
308    
309        /**
310         * @see java.lang.Object#toString()
311         */
312        public String toString() {
313            String name = super.getName();
314    
315            // workaround for the mysterious,
316            // still-unreproducible-on-my-machine, null delegate exception on
317            // Tomcat startup
318            if ((name == null) && (getDelegate() != null)) {
319                name = getDelegate().getName();
320            }
321            return "AttributeReferenceDefinition for attribute " + name;
322        }
323    }