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.web;
17  
18  import java.util.List;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.PageContext;
22  import javax.servlet.jsp.tagext.Tag;
23  
24  import org.kuali.ole.fp.document.web.struts.VoucherForm;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewFieldDefinition;
27  import org.kuali.ole.sys.document.service.AccountingLineRenderingService;
28  import org.kuali.ole.sys.document.web.renderers.FieldRenderer;
29  import org.kuali.rice.kns.web.ui.Field;
30  import org.kuali.rice.krad.util.ObjectUtils;
31  
32  /**
33   * 
34   * This class...
35   */
36  public class AccountingLineViewDebitCreditAmountField implements RenderableElement, ElementNamable {
37      private Field debitOrCreditField;
38      private AccountingLineViewFieldDefinition definition;
39      private boolean isDebit;
40      private String newLineProperty;
41      private String collectionProperty;
42      private int arbitrarilyHighIndex;
43      
44      /**
45       * Constructs a AccountingLineViewDebitOrCreditAmountField
46       * @param debitOrCreditField
47       * @param definition
48       * @param isDebit
49       * @param newLineProperty
50       * @param collectionProperty
51       */
52      public AccountingLineViewDebitCreditAmountField(Field debitOrCreditField, AccountingLineViewFieldDefinition definition, boolean isDebit, String newLineProperty, String collectionProperty) {
53          this.debitOrCreditField = debitOrCreditField;
54          this.definition = definition;
55          this.isDebit = isDebit;
56          this.newLineProperty = newLineProperty;
57          this.collectionProperty = collectionProperty;
58      }
59  
60      /**
61       * @see org.kuali.ole.sys.document.web.RenderableElement#appendFields(java.util.List)
62       * 
63       * KRAD Conversion: Customization of the fields - Adding fields - No use of data dictionary
64       */
65      public void appendFields(List<Field> fields) {
66          fields.add(debitOrCreditField);
67      }
68  
69      /**
70       * This is not an action block
71       * @see org.kuali.ole.sys.document.web.RenderableElement#isActionBlock()
72       */
73      public boolean isActionBlock() {
74          return false;
75      }
76  
77      /**
78       * This isn't empty
79       * @see org.kuali.ole.sys.document.web.RenderableElement#isEmpty()
80       */
81      public boolean isEmpty() {
82          return false;
83      }
84  
85      /**
86       * This is not hidden
87       * @see org.kuali.ole.sys.document.web.RenderableElement#isHidden()
88       */
89      public boolean isHidden() {
90          return false;
91      }
92  
93      /**
94       * @see org.kuali.ole.sys.document.web.RenderableElement#populateWithTabIndexIfRequested(int)
95       */
96      public void populateWithTabIndexIfRequested(int reallyHighIndex) {
97          this.arbitrarilyHighIndex = reallyHighIndex;
98      }
99  
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 }