001    /*
002     * Copyright 2011 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.kfs.sys.document.datadictionary;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.sys.businessobject.AccountingLine;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.service.DynamicNameLabelGenerator;
026    import org.kuali.kfs.sys.document.web.AccountingLineViewField;
027    import org.kuali.kfs.sys.document.web.AccountingLineViewOverrideField;
028    import org.kuali.kfs.sys.document.web.TableJoining;
029    import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
030    import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
031    import org.kuali.rice.kns.service.DataDictionaryService;
032    import org.kuali.rice.kns.util.FieldUtils;
033    import org.kuali.rice.kns.web.ui.Field;
034    import org.kuali.rice.kns.web.ui.FieldBridge;
035    
036    /**
037     * Data dictionary definition of a field to be rendered as part of an accounting line view.
038     */
039    public class AccountingLineViewFieldDefinition extends MaintainableFieldDefinition implements AccountingLineViewRenderableElementDefinition {
040        private String dynamicLabelProperty;
041        private boolean useShortLabel = false;
042        private boolean hidden = false;
043        private List<AccountingLineViewOverrideFieldDefinition> overrideFields;
044        private String dynamicNameLabelGeneratorBeanName;
045        private int overrideColSpan = -1;
046        private Class<? extends AccountingLineViewField> accountingLineViewFieldClass = org.kuali.kfs.sys.document.web.AccountingLineViewField.class;
047        private String overrideLookupParameters;
048        
049        private DynamicNameLabelGenerator dynamicNameLabelGenerator;
050    
051        /**
052         * Gets the dynamicLabelProperty attribute. 
053         * @return Returns the dynamicLabelProperty.
054         */
055        public String getDynamicLabelProperty() {
056            return dynamicLabelProperty;
057        }
058    
059        /**
060         * Sets the dynamicLabelProperty attribute value.
061         * @param dynamicLabelProperty The dynamicLabelProperty to set.
062         */
063        public void setDynamicLabelProperty(String dynamicLabelProperty) {
064            this.dynamicLabelProperty = dynamicLabelProperty;
065        }
066        
067        /**
068         * Gets the useShortLabel attribute. 
069         * @return Returns the useShortLabel.
070         */
071        public boolean shouldUseShortLabel() {
072            return useShortLabel;
073        }
074    
075        /**
076         * Sets the useShortLabel attribute value.
077         * @param useShortLabel The useShortLabel to set.
078         */
079        public void setUseShortLabel(boolean useShortLabel) {
080            this.useShortLabel = useShortLabel;
081        }
082    
083        /**
084         * Gets the hidden attribute. 
085         * @return Returns the hidden.
086         */
087        public boolean isHidden() {
088            return hidden;
089        }
090    
091        /**
092         * Sets the hidden attribute value.
093         * @param hidden The hidden to set.
094         */
095        public void setHidden(boolean hidden) {
096            this.hidden = hidden;
097        }
098    
099        /**
100         * Gets the overrideFields attribute. 
101         * @return Returns the overrideFields.
102         */
103        public List<AccountingLineViewOverrideFieldDefinition> getOverrideFields() {
104            return overrideFields;
105        }
106    
107        /**
108         * Sets the overrideFields attribute value.
109         * @param overrideFields The overrideFields to set.
110         */
111        public void setOverrideFields(List<AccountingLineViewOverrideFieldDefinition> overrideFields) {
112            this.overrideFields = overrideFields;
113        }
114    
115        /**
116         * Gets the dynamicNameLabelGeneratorBeanName attribute. 
117         * @return Returns the dynamicNameLabelGeneratorBeanName.
118         */
119        public String getDynamicNameLabelGeneratorBeanName() {
120            return dynamicNameLabelGeneratorBeanName;
121        }
122    
123        /**
124         * Sets the dynamicNameLabelGeneratorBeanName attribute value.
125         * @param dynamicNameLabelGeneratorBeanName The dynamicNameLabelGeneratorBeanName to set.
126         */
127        public void setDynamicNameLabelGeneratorBeanName(String dynamicNameLabelGeneratorBeanName) {
128            this.dynamicNameLabelGeneratorBeanName = dynamicNameLabelGeneratorBeanName;
129        }
130        
131        /**
132         * Gets the overrideColSpan attribute. 
133         * @return Returns the overrideColSpan.
134         */
135        public int getOverrideColSpan() {
136            return overrideColSpan;
137        }
138    
139        /**
140         * Sets the overrideColSpan attribute value.
141         * @param overrideColSpan The overrideColSpan to set.
142         */
143        public void setOverrideColSpan(int overrideColSpan) {
144            this.overrideColSpan = overrideColSpan;
145        }
146    
147        /**
148         * Gets the accountingLineViewFieldClass attribute. 
149         * @return Returns the accountingLineViewFieldClass.
150         */
151        public Class<? extends AccountingLineViewField> getAccountingLineViewFieldClass() {
152            return accountingLineViewFieldClass;
153        }
154    
155        /**
156         * Sets the accountingLineViewFieldClass attribute value.
157         * @param accountingLineViewFieldClass The accountingLineViewFieldClass to set.
158         */
159        public void setAccountingLineViewFieldClass(Class<? extends AccountingLineViewField> accountingLineViewFieldClass) {
160            if (accountingLineViewFieldClass != null) {
161                this.accountingLineViewFieldClass = accountingLineViewFieldClass;
162            }
163        }
164    
165        /**
166         * Returns the dynamicNameLabelGenerator for this field definition, if it has one
167         * @return an implementation of DynamicNameLabelGenerator to use for this field
168         */
169        public DynamicNameLabelGenerator getDynamicNameLabelGenerator() {
170            if (!StringUtils.isBlank(dynamicNameLabelGeneratorBeanName) && dynamicNameLabelGenerator == null) {
171                dynamicNameLabelGenerator = SpringContext.getBean(DynamicNameLabelGenerator.class,dynamicNameLabelGeneratorBeanName);
172            }
173            return dynamicNameLabelGenerator;
174        }
175    
176        /**
177         * @see org.kuali.kfs.sys.document.datadictionary.AccountingLineViewRenderableElementDefinition#createLayoutElement()
178         */
179        public TableJoining createLayoutElement(Class<? extends AccountingLine> accountingLineClass) {
180            AccountingLineViewField layoutElement = getNewAccountingLineViewField();
181            layoutElement.setDefinition(this);
182            layoutElement.setField(getKNSFieldForDefinition(accountingLineClass));
183            layoutElement.setOverrideFields(getFieldsForOverrideFields(layoutElement, accountingLineClass));
184            return layoutElement;
185        }
186        
187        /**
188         * Creates a new instance of the accounting line view field class this definition uses
189         * @return a new AccountingLineViewField instance or child class instance
190         */
191        protected AccountingLineViewField getNewAccountingLineViewField() {
192            AccountingLineViewField layoutElement = null;
193            try {
194                layoutElement = (AccountingLineViewField)getAccountingLineViewFieldClass().newInstance();
195            }
196            catch (InstantiationException ie) {
197                throw new RuntimeException("Could not instantiate instance of class "+getAccountingLineViewFieldClass().getName(), ie);
198            }
199            catch (IllegalAccessException iae) {
200                throw new RuntimeException("IllegalAccessException while attempting to instantiate "+getAccountingLineViewFieldClass().getName(), iae);
201            }
202            return layoutElement;
203        }
204        
205        /**
206         * Creates a KNS Field for an AccountingLineViewField definition
207         * @param accountingLineClass the class of the accounting line used by this definition
208         * @return a properly initialized KNS field
209         */
210        public Field getKNSFieldForDefinition(Class<? extends AccountingLine> accountingLineClass) {
211            Field realField = FieldUtils.getPropertyField(accountingLineClass, getName(), false);
212            FieldBridge.setupField(realField, this, null);
213            if (isHidden()) {
214                realField.setFieldType(Field.HIDDEN);
215            }
216            if (shouldUseShortLabel()) {
217                BusinessObjectEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(accountingLineClass.getName());
218                realField.setFieldLabel(boEntry.getAttributeDefinition(getName()).getShortLabel());
219            }
220            return realField;
221        }
222        
223        /**
224         * For each defined override field within this definition, creates a Field and puts them together as a List
225         * @param parentField the AccountingLineViewField which will own all of the override fields
226         * @param accountingLineClass the class of accounting lines which will be rendered
227         * @return a List of override fields, or if no override fields were defined, an empty List
228         */
229        protected List<AccountingLineViewOverrideField> getFieldsForOverrideFields(AccountingLineViewField parentField, Class<? extends AccountingLine> accountingLineClass) {
230            List<AccountingLineViewOverrideField> fields = new ArrayList<AccountingLineViewOverrideField>();
231            if (getOverrideFields() != null && getOverrideFields().size() > 0) {
232                for (AccountingLineViewOverrideFieldDefinition overrideFieldDefinition : getOverrideFields()) {
233                    fields.add(overrideFieldDefinition.getOverrideFieldForDefinition(parentField, accountingLineClass));
234                }
235            }
236            return fields;
237        }
238    
239        /**
240         * Gets the overrideLookupParameters attribute. 
241         * @return Returns the overrideLookupParameters.
242         */
243        public String getOverrideLookupParameters() {
244            return overrideLookupParameters;
245        }
246    
247        /**
248         * Sets the overrideLookupParameters attribute value.
249         * @param overrideLookupParameters The overrideLookupParameters to set.
250         */
251        public void setOverrideLookupParameters(String overrideLookupParameters) {
252            this.overrideLookupParameters = overrideLookupParameters;
253        }
254    }