001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.sys.document.datadictionary;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.ole.sys.businessobject.AccountingLine;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.ole.sys.document.service.DynamicNameLabelGenerator;
025import org.kuali.ole.sys.document.web.AccountingLineViewField;
026import org.kuali.ole.sys.document.web.AccountingLineViewOverrideField;
027import org.kuali.ole.sys.document.web.TableJoining;
028import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
029import org.kuali.rice.kns.service.DataDictionaryService;
030import org.kuali.rice.kns.util.FieldUtils;
031import org.kuali.rice.kns.web.ui.Field;
032import org.kuali.rice.kns.web.ui.FieldBridge;
033
034/**
035 * Data dictionary definition of a field to be rendered as part of an accounting line view.
036 */
037public class AccountingLineViewFieldDefinition extends MaintainableFieldDefinition implements AccountingLineViewRenderableElementDefinition {
038    private String dynamicLabelProperty;
039    private boolean useShortLabel = false;
040    private boolean hidden = false;
041    private List<AccountingLineViewOverrideFieldDefinition> overrideFields;
042    private String dynamicNameLabelGeneratorBeanName;
043    private int overrideColSpan = -1;
044    private Class<? extends AccountingLineViewField> accountingLineViewFieldClass = org.kuali.ole.sys.document.web.AccountingLineViewField.class;
045    private String overrideLookupParameters;
046    
047    private DynamicNameLabelGenerator dynamicNameLabelGenerator;
048
049    /**
050     * Gets the dynamicLabelProperty attribute. 
051     * @return Returns the dynamicLabelProperty.
052     */
053    public String getDynamicLabelProperty() {
054        return dynamicLabelProperty;
055    }
056
057    /**
058     * Sets the dynamicLabelProperty attribute value.
059     * @param dynamicLabelProperty The dynamicLabelProperty to set.
060     */
061    public void setDynamicLabelProperty(String dynamicLabelProperty) {
062        this.dynamicLabelProperty = dynamicLabelProperty;
063    }
064    
065    /**
066     * Gets the useShortLabel attribute. 
067     * @return Returns the useShortLabel.
068     */
069    public boolean shouldUseShortLabel() {
070        return useShortLabel;
071    }
072
073    /**
074     * Sets the useShortLabel attribute value.
075     * @param useShortLabel The useShortLabel to set.
076     */
077    public void setUseShortLabel(boolean useShortLabel) {
078        this.useShortLabel = useShortLabel;
079    }
080
081    /**
082     * Gets the hidden attribute. 
083     * @return Returns the hidden.
084     */
085    public boolean isHidden() {
086        return hidden;
087    }
088
089    /**
090     * Sets the hidden attribute value.
091     * @param hidden The hidden to set.
092     */
093    public void setHidden(boolean hidden) {
094        this.hidden = hidden;
095    }
096
097    /**
098     * Gets the overrideFields attribute. 
099     * @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}