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 org.kuali.kfs.sys.businessobject.AccountingLine;
019    import org.kuali.kfs.sys.context.SpringContext;
020    import org.kuali.kfs.sys.document.service.AccountingLineRenderingService;
021    import org.kuali.kfs.sys.document.web.AccountingLineViewDebitCreditAmountLayoutElement;
022    import org.kuali.kfs.sys.document.web.TableJoining;
023    import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
024    import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
025    import org.kuali.rice.kns.service.DataDictionaryService;
026    import org.kuali.rice.kns.util.FieldUtils;
027    import org.kuali.rice.kns.web.ui.Field;
028    import org.kuali.rice.kns.web.ui.FieldBridge;
029    
030    /**
031     * Data dictionary meta data that represents a debit/credit amount field.  By default, it expects the new line debit amount property on the form
032     * to be called "newSourceLineDebit", the debit amount on a new line to be called "newSourceLineCredit", and for the
033     * form to have a collection VoucherAccountingLineHelper implementation objects called "voucherLineHelpers", though
034     * these can be overridden.
035     */
036    public class AccountingLineViewDebitCreditAmountFieldDefinition extends MaintainableFieldDefinition implements AccountingLineViewRenderableElementDefinition {
037        private String newLineDebitAmountProperty = "newSourceLineDebit";
038        private String newLineCreditAmountProperty = "newSourceLineCredit";
039        private String voucherLineHelperProperty = "voucherLineHelper";
040        private boolean useShortLabels = true;
041        private String amountFieldName = "amount";
042        
043        /**
044         * @see org.kuali.kfs.sys.document.datadictionary.AccountingLineViewRenderableElementDefinition#createLayoutElement(java.lang.Class)
045         */
046        public TableJoining createLayoutElement(Class<? extends AccountingLine> accountingLineClass) {
047            AccountingLineViewDebitCreditAmountLayoutElement layoutElement = new AccountingLineViewDebitCreditAmountLayoutElement();
048            
049            layoutElement.setDebitAmountField(createFieldForPropertyName(amountFieldName, accountingLineClass));
050            layoutElement.setDebitFieldDefinition(createFieldDefinitionForProperty(amountFieldName));
051            
052            layoutElement.setCreditAmountField(createFieldForPropertyName(amountFieldName, accountingLineClass));
053            layoutElement.setCreditFieldDefinition(createFieldDefinitionForProperty(amountFieldName));
054            
055            layoutElement.setDefinition(this);
056            
057            return layoutElement;
058        }
059        
060        /**
061         * Creates a field for the given AccountingLine class and property name
062         * @param propertyName the name of the property to create a Field for
063         * @param accountingLineClass the Class of the AccountingLine we're planning on rendering
064         * @return an appropriately created Field
065         */
066        protected Field createFieldForPropertyName(String propertyName, Class<? extends AccountingLine> accountingLineClass) {
067            Field realField = FieldUtils.getPropertyField(accountingLineClass, propertyName, false);
068            FieldBridge.setupField(realField, this, null);
069            if (useShortLabels) {
070                BusinessObjectEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(accountingLineClass.getName());
071                realField.setFieldLabel(boEntry.getAttributeDefinition(propertyName).getShortLabel());
072            }
073            return realField;
074        }
075        
076        /**
077         * Creates an AccountingLineViewFieldDefinition for the given property name
078         * @param propertyName the name of the field property that we're creating a definition for
079         * @return an appropriately created AccountingLineViewFieldDefinition
080         */
081        protected AccountingLineViewFieldDefinition createFieldDefinitionForProperty(String propertyName) {
082            AccountingLineViewFieldDefinition fieldDefinition = SpringContext.getBean(AccountingLineRenderingService.class).createGenericAccountingLineViewFieldDefinition(this);
083            fieldDefinition.setName(propertyName);
084            return fieldDefinition;
085        }
086    
087        /**
088         * Gets the newLineCreditAmountProperty attribute. 
089         * @return Returns the newLineCreditAmountProperty.
090         */
091        public String getNewLineCreditAmountProperty() {
092            return newLineCreditAmountProperty;
093        }
094    
095        /**
096         * Sets the newLineCreditAmountProperty attribute value.
097         * @param newLineCreditAmountProperty The newLineCreditAmountProperty to set.
098         */
099        public void setNewLineCreditAmountProperty(String newLineCreditAmountProperty) {
100            this.newLineCreditAmountProperty = newLineCreditAmountProperty;
101        }
102    
103        /**
104         * Gets the newLineDebitAmountProperty attribute. 
105         * @return Returns the newLineDebitAmountProperty.
106         */
107        public String getNewLineDebitAmountProperty() {
108            return newLineDebitAmountProperty;
109        }
110    
111        /**
112         * Sets the newLineDebitAmountProperty attribute value.
113         * @param newLineDebitAmountProperty The newLineDebitAmountProperty to set.
114         */
115        public void setNewLineDebitAmountProperty(String newLineDebitAmountProperty) {
116            this.newLineDebitAmountProperty = newLineDebitAmountProperty;
117        }
118    
119        /**
120         * Gets the voucherLineHelpersProperty attribute. 
121         * @return Returns the voucherLineHelpersProperty.
122         */
123        public String getVoucherLineHelperProperty() {
124            return voucherLineHelperProperty;
125        }
126    
127        /**
128         * Sets the voucherLineHelpersProperty attribute value.
129         * @param voucherLineHelpersProperty The voucherLineHelpersProperty to set.
130         */
131        public void setVoucherLineHelperProperty(String voucherLineHelpersProperty) {
132            this.voucherLineHelperProperty = voucherLineHelpersProperty;
133        }
134    
135        /**
136         * Gets the useShortLabels attribute. 
137         * @return Returns the useShortLabels.
138         */
139        public boolean shouldUseShortLabels() {
140            return useShortLabels;
141        }
142    
143        /**
144         * Sets the useShortLabels attribute value.
145         * @param useShortLabels The useShortLabels to set.
146         */
147        public void setUseShortLabels(boolean useShortLabels) {
148            this.useShortLabels = useShortLabels;
149        }
150    
151        /**
152         * Gets the amountFieldName attribute. 
153         * @return Returns the amountFieldName.
154         */
155        public String getAmountFieldName() {
156            return amountFieldName;
157        }
158    
159        /**
160         * Sets the amountFieldName attribute value.
161         * @param amountFieldName The amountFieldName to set.
162         */
163        public void setAmountFieldName(String amountFieldName) {
164            this.amountFieldName = amountFieldName;
165        }
166        
167    }