View Javadoc
1   /*
2    * Copyright 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.ole.sys.document.datadictionary;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.sys.businessobject.AccountingLine;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.ole.sys.document.service.DynamicNameLabelGenerator;
25  import org.kuali.ole.sys.document.web.AccountingLineViewField;
26  import org.kuali.ole.sys.document.web.AccountingLineViewOverrideField;
27  import org.kuali.ole.sys.document.web.TableJoining;
28  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
29  import org.kuali.rice.kns.service.DataDictionaryService;
30  import org.kuali.rice.kns.util.FieldUtils;
31  import org.kuali.rice.kns.web.ui.Field;
32  import org.kuali.rice.kns.web.ui.FieldBridge;
33  
34  /**
35   * Data dictionary definition of a field to be rendered as part of an accounting line view.
36   */
37  public class AccountingLineViewFieldDefinition extends MaintainableFieldDefinition implements AccountingLineViewRenderableElementDefinition {
38      private String dynamicLabelProperty;
39      private boolean useShortLabel = false;
40      private boolean hidden = false;
41      private List<AccountingLineViewOverrideFieldDefinition> overrideFields;
42      private String dynamicNameLabelGeneratorBeanName;
43      private int overrideColSpan = -1;
44      private Class<? extends AccountingLineViewField> accountingLineViewFieldClass = org.kuali.ole.sys.document.web.AccountingLineViewField.class;
45      private String overrideLookupParameters;
46      
47      private DynamicNameLabelGenerator dynamicNameLabelGenerator;
48  
49      /**
50       * Gets the dynamicLabelProperty attribute. 
51       * @return Returns the dynamicLabelProperty.
52       */
53      public String getDynamicLabelProperty() {
54          return dynamicLabelProperty;
55      }
56  
57      /**
58       * Sets the dynamicLabelProperty attribute value.
59       * @param dynamicLabelProperty The dynamicLabelProperty to set.
60       */
61      public void setDynamicLabelProperty(String dynamicLabelProperty) {
62          this.dynamicLabelProperty = dynamicLabelProperty;
63      }
64      
65      /**
66       * Gets the useShortLabel attribute. 
67       * @return Returns the useShortLabel.
68       */
69      public boolean shouldUseShortLabel() {
70          return useShortLabel;
71      }
72  
73      /**
74       * Sets the useShortLabel attribute value.
75       * @param useShortLabel The useShortLabel to set.
76       */
77      public void setUseShortLabel(boolean useShortLabel) {
78          this.useShortLabel = useShortLabel;
79      }
80  
81      /**
82       * Gets the hidden attribute. 
83       * @return Returns the hidden.
84       */
85      public boolean isHidden() {
86          return hidden;
87      }
88  
89      /**
90       * Sets the hidden attribute value.
91       * @param hidden The hidden to set.
92       */
93      public void setHidden(boolean hidden) {
94          this.hidden = hidden;
95      }
96  
97      /**
98       * Gets the overrideFields attribute. 
99       * @return Returns the overrideFields.
100      */
101     public List<AccountingLineViewOverrideFieldDefinition> getOverrideFields() {
102         return overrideFields;
103     }
104 
105     /**
106      * Sets the overrideFields attribute value.
107      * @param overrideFields The overrideFields to set.
108      */
109     public void setOverrideFields(List<AccountingLineViewOverrideFieldDefinition> overrideFields) {
110         this.overrideFields = overrideFields;
111     }
112 
113     /**
114      * Gets the dynamicNameLabelGeneratorBeanName attribute. 
115      * @return Returns the dynamicNameLabelGeneratorBeanName.
116      */
117     public String getDynamicNameLabelGeneratorBeanName() {
118         return dynamicNameLabelGeneratorBeanName;
119     }
120 
121     /**
122      * Sets the dynamicNameLabelGeneratorBeanName attribute value.
123      * @param dynamicNameLabelGeneratorBeanName The dynamicNameLabelGeneratorBeanName to set.
124      */
125     public void setDynamicNameLabelGeneratorBeanName(String dynamicNameLabelGeneratorBeanName) {
126         this.dynamicNameLabelGeneratorBeanName = dynamicNameLabelGeneratorBeanName;
127     }
128     
129     /**
130      * Gets the overrideColSpan attribute. 
131      * @return Returns the overrideColSpan.
132      */
133     public int getOverrideColSpan() {
134         return overrideColSpan;
135     }
136 
137     /**
138      * Sets the overrideColSpan attribute value.
139      * @param overrideColSpan The overrideColSpan to set.
140      */
141     public void setOverrideColSpan(int overrideColSpan) {
142         this.overrideColSpan = overrideColSpan;
143     }
144 
145     /**
146      * Gets the accountingLineViewFieldClass attribute. 
147      * @return Returns the accountingLineViewFieldClass.
148      */
149     public Class<? extends AccountingLineViewField> getAccountingLineViewFieldClass() {
150         return accountingLineViewFieldClass;
151     }
152 
153     /**
154      * Sets the accountingLineViewFieldClass attribute value.
155      * @param accountingLineViewFieldClass The accountingLineViewFieldClass to set.
156      */
157     public void setAccountingLineViewFieldClass(Class<? extends AccountingLineViewField> accountingLineViewFieldClass) {
158         if (accountingLineViewFieldClass != null) {
159             this.accountingLineViewFieldClass = accountingLineViewFieldClass;
160         }
161     }
162 
163     /**
164      * Returns the dynamicNameLabelGenerator for this field definition, if it has one
165      * @return an implementation of DynamicNameLabelGenerator to use for this field
166      */
167     public DynamicNameLabelGenerator getDynamicNameLabelGenerator() {
168         if (!StringUtils.isBlank(dynamicNameLabelGeneratorBeanName) && dynamicNameLabelGenerator == null) {
169             dynamicNameLabelGenerator = SpringContext.getBean(DynamicNameLabelGenerator.class,dynamicNameLabelGeneratorBeanName);
170         }
171         return dynamicNameLabelGenerator;
172     }
173 
174     /**
175      * @see org.kuali.ole.sys.document.datadictionary.AccountingLineViewRenderableElementDefinition#createLayoutElement()
176      */
177     public TableJoining createLayoutElement(Class<? extends AccountingLine> accountingLineClass) {
178         AccountingLineViewField layoutElement = getNewAccountingLineViewField();
179         layoutElement.setDefinition(this);
180         layoutElement.setField(getKNSFieldForDefinition(accountingLineClass));
181         layoutElement.setOverrideFields(getFieldsForOverrideFields(layoutElement, accountingLineClass));
182         return layoutElement;
183     }
184     
185     /**
186      * Creates a new instance of the accounting line view field class this definition uses
187      * @return a new AccountingLineViewField instance or child class instance
188      */
189     protected AccountingLineViewField getNewAccountingLineViewField() {
190         AccountingLineViewField layoutElement = null;
191         try {
192             layoutElement = (AccountingLineViewField)getAccountingLineViewFieldClass().newInstance();
193         }
194         catch (InstantiationException ie) {
195             throw new RuntimeException("Could not instantiate instance of class "+getAccountingLineViewFieldClass().getName(), ie);
196         }
197         catch (IllegalAccessException iae) {
198             throw new RuntimeException("IllegalAccessException while attempting to instantiate "+getAccountingLineViewFieldClass().getName(), iae);
199         }
200         return layoutElement;
201     }
202     
203     /**
204      * Creates a KNS Field for an AccountingLineViewField definition
205      * @param accountingLineClass the class of the accounting line used by this definition
206      * @return a properly initialized KNS field
207      */
208     public Field getKNSFieldForDefinition(Class<? extends AccountingLine> accountingLineClass) {
209         Field realField = FieldUtils.getPropertyField(accountingLineClass, getName(), false);
210         FieldBridge.setupField(realField, this, null);
211         if (isHidden()) {
212             realField.setFieldType(Field.HIDDEN);
213         }
214         if (shouldUseShortLabel()) {
215             org.kuali.rice.krad.datadictionary.BusinessObjectEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(accountingLineClass.getName());
216             realField.setFieldLabel(boEntry.getAttributeDefinition(getName()).getShortLabel());
217         }
218         return realField;
219     }
220     
221     /**
222      * For each defined override field within this definition, creates a Field and puts them together as a List
223      * @param parentField the AccountingLineViewField which will own all of the override fields
224      * @param accountingLineClass the class of accounting lines which will be rendered
225      * @return a List of override fields, or if no override fields were defined, an empty List
226      */
227     protected List<AccountingLineViewOverrideField> getFieldsForOverrideFields(AccountingLineViewField parentField, Class<? extends AccountingLine> accountingLineClass) {
228         List<AccountingLineViewOverrideField> fields = new ArrayList<AccountingLineViewOverrideField>();
229         if (getOverrideFields() != null && getOverrideFields().size() > 0) {
230             for (AccountingLineViewOverrideFieldDefinition overrideFieldDefinition : getOverrideFields()) {
231                 fields.add(overrideFieldDefinition.getOverrideFieldForDefinition(parentField, accountingLineClass));
232             }
233         }
234         return fields;
235     }
236 
237     /**
238      * Gets the overrideLookupParameters attribute. 
239      * @return Returns the overrideLookupParameters.
240      */
241     public String getOverrideLookupParameters() {
242         return overrideLookupParameters;
243     }
244 
245     /**
246      * Sets the overrideLookupParameters attribute value.
247      * @param overrideLookupParameters The overrideLookupParameters to set.
248      */
249     public void setOverrideLookupParameters(String overrideLookupParameters) {
250         this.overrideLookupParameters = overrideLookupParameters;
251     }
252 }