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.web;
017
018import java.util.List;
019
020import javax.servlet.jsp.JspException;
021import javax.servlet.jsp.PageContext;
022import javax.servlet.jsp.tagext.Tag;
023
024import org.kuali.ole.fp.document.web.struts.VoucherForm;
025import org.kuali.ole.sys.context.SpringContext;
026import org.kuali.ole.sys.document.datadictionary.AccountingLineViewFieldDefinition;
027import org.kuali.ole.sys.document.service.AccountingLineRenderingService;
028import org.kuali.ole.sys.document.web.renderers.FieldRenderer;
029import org.kuali.rice.kns.web.ui.Field;
030import org.kuali.rice.krad.util.ObjectUtils;
031
032/**
033 * 
034 * This class...
035 */
036public class AccountingLineViewDebitCreditAmountField implements RenderableElement, ElementNamable {
037    private Field debitOrCreditField;
038    private AccountingLineViewFieldDefinition definition;
039    private boolean isDebit;
040    private String newLineProperty;
041    private String collectionProperty;
042    private int arbitrarilyHighIndex;
043    
044    /**
045     * Constructs a AccountingLineViewDebitOrCreditAmountField
046     * @param debitOrCreditField
047     * @param definition
048     * @param isDebit
049     * @param newLineProperty
050     * @param collectionProperty
051     */
052    public AccountingLineViewDebitCreditAmountField(Field debitOrCreditField, AccountingLineViewFieldDefinition definition, boolean isDebit, String newLineProperty, String collectionProperty) {
053        this.debitOrCreditField = debitOrCreditField;
054        this.definition = definition;
055        this.isDebit = isDebit;
056        this.newLineProperty = newLineProperty;
057        this.collectionProperty = collectionProperty;
058    }
059
060    /**
061     * @see org.kuali.ole.sys.document.web.RenderableElement#appendFields(java.util.List)
062     * 
063     * KRAD Conversion: Customization of the fields - Adding fields - No use of data dictionary
064     */
065    public void appendFields(List<Field> fields) {
066        fields.add(debitOrCreditField);
067    }
068
069    /**
070     * This is not an action block
071     * @see org.kuali.ole.sys.document.web.RenderableElement#isActionBlock()
072     */
073    public boolean isActionBlock() {
074        return false;
075    }
076
077    /**
078     * This isn't empty
079     * @see org.kuali.ole.sys.document.web.RenderableElement#isEmpty()
080     */
081    public boolean isEmpty() {
082        return false;
083    }
084
085    /**
086     * This is not hidden
087     * @see org.kuali.ole.sys.document.web.RenderableElement#isHidden()
088     */
089    public boolean isHidden() {
090        return false;
091    }
092
093    /**
094     * @see org.kuali.ole.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int)
095     */
096    public void populateWithTabIndexIfRequested(int reallyHighIndex) {
097        this.arbitrarilyHighIndex = reallyHighIndex;
098    }
099
100    /**
101     * @see org.kuali.ole.sys.document.web.RenderableElement#renderElement(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.ole.sys.document.web.AccountingLineRenderingContext)
102     */
103    public void renderElement(PageContext pageContext, Tag parentTag, AccountingLineRenderingContext renderingContext) throws JspException {
104        if (!renderingContext.isFieldModifyable(debitOrCreditField.getPropertyName())) {
105            debitOrCreditField.setReadOnly(true);
106        }
107        FieldRenderer renderer = SpringContext.getBean(AccountingLineRenderingService.class).getFieldRendererForField(getDebitOrCreditField(), renderingContext.getAccountingLine());
108        if (renderer != null) {
109            prepareFieldForRendering(getDebitOrCreditField(), (VoucherForm)renderingContext.getForm(), renderingContext.getCurrentLineCount());
110            renderer.setField(getDebitOrCreditField());
111            renderer.render(pageContext, parentTag);
112            renderer.clear();
113        }
114    }
115    
116    /**
117     * Sets up the field for rendering by setting the right property name and zeroing out amounts which aren't needed
118     * @param field the field to prepare
119     * @param accountingLine the accounting line being rendered
120     * @param count the count of the current line in the source lines, or null if it's a new line
121     * 
122     * KRAD Conversion: Customization of preparing the fields for rendering - No use of data dictionary
123     */
124    protected void prepareFieldForRendering(Field field, VoucherForm form, Integer count) {
125        getDebitOrCreditField().setPropertyPrefix(null);
126        
127        // set the right property name
128        if (count == null) {
129            field.setPropertyName(getNewLineProperty());
130        } else {
131            final String subPropertyName = isDebit ? "debit" : "credit";
132            field.setPropertyName(getCollectionProperty()+"["+count.toString()+"]."+subPropertyName);
133        }
134        
135        // get the value from the form
136        field.setPropertyValue(ObjectUtils.getPropertyValue(form, field.getPropertyName()));
137    }
138
139    /**
140     * Gets the arbitrarilyHighIndex attribute. 
141     * @return Returns the arbitrarilyHighIndex.
142     */
143    public int getArbitrarilyHighIndex() {
144        return arbitrarilyHighIndex;
145    }
146
147    /**
148     * Gets the collectionProperty attribute. 
149     * @return Returns the collectionProperty.
150     */
151    public String getCollectionProperty() {
152        return collectionProperty;
153    }
154
155    /**
156     * Gets the debitOrCreditField attribute. 
157     * @return Returns the debitOrCreditField.
158     */
159    public Field getDebitOrCreditField() {
160        return debitOrCreditField;
161    }
162
163    /**
164     * Gets the definition attribute. 
165     * @return Returns the definition.
166     */
167    public AccountingLineViewFieldDefinition getDefinition() {
168        return definition;
169    }
170
171    /**
172     * Gets the isDebit attribute. 
173     * @return Returns the isDebit.
174     */
175    public boolean isDebit() {
176        return isDebit;
177    }
178
179    /**
180     * Gets the newLineProperty attribute. 
181     * @return Returns the newLineProperty.
182     */
183    public String getNewLineProperty() {
184        return newLineProperty;
185    }
186
187    /**
188     * @see org.kuali.ole.sys.document.web.ElementNamable#getName()
189     */
190    public String getName() {
191        return this.getDebitOrCreditField().getPropertyName();
192    }
193
194}