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  import java.util.Map;
20  import java.util.Set;
21  
22  import org.kuali.ole.sys.OLEPropertyConstants;
23  import org.kuali.ole.sys.businessobject.AccountingLine;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewDebitCreditAmountFieldDefinition;
26  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewFieldDefinition;
27  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.kns.web.ui.Field;
30  
31  /**
32   * A table joining element which adds two fields to an amount: debit amount and credit amount
33   */
34  public class AccountingLineViewDebitCreditAmountLayoutElement implements TableJoiningWithHeader, ReadOnlyable {
35      private Field debitAmountField;
36      private Field creditAmountField;
37      private AccountingLineViewDebitCreditAmountFieldDefinition definition;
38      private AccountingLineViewFieldDefinition debitFieldDefinition;
39      private AccountingLineViewFieldDefinition creditFieldDefinition;
40      
41      /**
42       * Returns whether the debit and the credit amount fields are both read only 
43       * @see org.kuali.ole.sys.document.web.ReadOnlyable#isReadOnly()
44       */
45      public boolean isReadOnly() {
46          return (debitAmountField == null || debitAmountField.isReadOnly()) && (creditAmountField == null || creditAmountField.isReadOnly());
47      }
48      
49      /**
50       * Read onlyizes both the credit and the debit amount fields
51       * @see org.kuali.ole.sys.document.web.ReadOnlyable#readOnlyize()
52       */
53      public void readOnlyize() {
54          if (debitAmountField != null) {
55              debitAmountField.setReadOnly(true);
56          }
57          if (creditAmountField != null) {
58              creditAmountField.setReadOnly(true);
59          }
60      }
61      
62      /**
63       * We don't generate headers
64       * @see org.kuali.ole.sys.document.web.TableJoiningWithHeader#createHeaderLabel()
65       */
66      public HeaderLabel createHeaderLabel() {
67          return null;
68      }
69      
70      /**
71       * This isn't hidden
72       * @see org.kuali.ole.sys.document.web.TableJoiningWithHeader#isHidden()
73       */
74      public boolean isHidden() {
75          return false;
76      }
77      
78      /**
79       * This renderable element...it ain't got no single name! 
80       * @see org.kuali.ole.sys.document.web.TableJoining#getName()
81       */
82      public String getName() {
83          return null;
84      }
85      
86      /**
87       * Request two rows - one for the header, one for the field
88       * @see org.kuali.ole.sys.document.web.TableJoining#getRequestedRowCount()
89       */
90      public int getRequestedRowCount() {
91          return 2;
92      }
93      
94      /**
95       * @see org.kuali.ole.sys.document.web.TableJoining#joinRow(org.kuali.ole.sys.document.web.AccountingLineTableRow, org.kuali.ole.sys.document.web.AccountingLineTableRow)
96       */
97      public void joinRow(AccountingLineTableRow headerLabelRow, AccountingLineTableRow row) {
98          if (debitAmountField != null) {
99              headerLabelRow.addCell(createHeaderCellForField(true));
100             row.addCell(createCellForField(debitAmountField, debitFieldDefinition, true));
101         }
102         if (creditAmountField != null) {
103             headerLabelRow.addCell(createHeaderCellForField(false));
104             row.addCell(createCellForField(creditAmountField, creditFieldDefinition, false));
105         }
106     }
107     
108     /**
109      * @see org.kuali.ole.sys.document.web.TableJoining#joinTable(java.util.List)
110      */
111     public void joinTable(List<AccountingLineTableRow> rows) {
112         final int remainingRowCount = rows.size() - 1;
113         
114         if (debitAmountField != null) {
115             rows.get(0).addCell(createHeaderCellForField(true));
116             
117             AccountingLineTableCell currentCell = createCellForField(debitAmountField, debitFieldDefinition, true);
118             currentCell.setRowSpan(remainingRowCount);
119             rows.get(1).addCell(currentCell);
120         }
121         if (creditAmountField != null) {
122             rows.get(0).addCell(createHeaderCellForField(false));
123             
124             AccountingLineTableCell baseCell = createCellForField(creditAmountField, creditFieldDefinition, false);
125             baseCell.setRowSpan(remainingRowCount);
126             rows.get(1).addCell(baseCell);
127         }
128     }
129     
130     /**
131      * Creates a table cell with a renderable field inside
132      * @param field the field to create a cell for
133      * @return a cell that wraps the given field 
134      * 
135      * KRAD Conversion: Customization of the fields - No use of data dictionary
136      */
137     protected AccountingLineTableCell createCellForField(Field field, AccountingLineViewFieldDefinition definition, boolean isDebit) {
138         AccountingLineTableCell cell = new AccountingLineTableCell();
139         AccountingLineViewDebitCreditAmountField renderableField = new AccountingLineViewDebitCreditAmountField(field, definition, isDebit, (isDebit ? this.definition.getNewLineDebitAmountProperty() : this.definition.getNewLineCreditAmountProperty()), this.definition.getVoucherLineHelperProperty());
140         cell.addRenderableElement(renderableField);
141         return cell;
142     }
143     
144     /**
145      * Creates a header cell for the given field
146      * @param field the field to create a header cell for
147      * @return a header cell
148      */
149     protected AccountingLineTableCell createHeaderCellForField(boolean isDebit) {
150         AccountingLineTableCell headerCell = new AccountingLineTableCell();
151         headerCell.setRendersAsHeader(true);
152         final String propertyName = isDebit ? getDebitPropertyName() : getCreditPropertyName();
153         final String label = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(propertyName); 
154         headerCell.addRenderableElement(new LiteralHeaderLabel(label));
155         return headerCell;
156     }
157     
158     /**
159      * @return the property name for debit labels
160      */
161     protected String getDebitPropertyName() {
162         return "label.document.journalVoucher.accountingLine.debit";
163     }
164     
165     /**
166      * @return the property name for credit labels
167      */
168     protected String getCreditPropertyName() {
169         return "label.document.journalVoucher.accountingLine.credit";
170     }
171     
172     /**
173      * @see org.kuali.ole.sys.document.web.TableJoining#performFieldTransformations(java.util.List, org.kuali.ole.sys.businessobject.AccountingLine, java.util.Map, java.util.Map)
174      */
175     public void performFieldTransformations(List<AccountingLineFieldRenderingTransformation> fieldTransformations, AccountingLine accountingLine, Map unconvertedValues) {
176         for (AccountingLineFieldRenderingTransformation fieldTransformation : fieldTransformations) {
177             fieldTransformation.transformField(accountingLine, getDebitAmountField(), getDebitFieldDefinition(), unconvertedValues);
178             fieldTransformation.transformField(accountingLine, getCreditAmountField(), getCreditFieldDefinition(), unconvertedValues);
179         }
180     }
181     
182     /**
183      * @see org.kuali.ole.sys.document.web.TableJoining#readOnlyizeReadOnlyBlocks(java.util.Set)
184      */
185     public void readOnlyizeReadOnlyBlocks(Set<String> readOnlyBlocks) {
186         if (readOnlyBlocks.contains(OLEPropertyConstants.AMOUNT)) {
187             if (debitAmountField != null) {
188                 debitAmountField.setReadOnly(true);
189             }
190             if (creditAmountField != null) {
191                 creditAmountField.setReadOnly(true);
192             }
193         }
194     }
195     
196     /**
197      * Does nothing - we don't have action blocks, like, ever
198      * @see org.kuali.ole.sys.document.web.TableJoining#removeAllActionBlocks()
199      */
200     public void removeAllActionBlocks() {}
201     
202     /**
203      * @see org.kuali.ole.sys.document.web.TableJoining#removeUnviewableBlocks(java.util.Set)
204      */
205     public void removeUnviewableBlocks(Set<String> unviewableBlocks) {
206         if (unviewableBlocks.contains(OLEPropertyConstants.AMOUNT)) {
207             if (debitAmountField != null) {
208                 debitAmountField = null;
209             }
210             if (creditAmountField != null) {
211                 creditAmountField = null;
212             }
213         }
214     }
215     
216     /**
217      * Gets the creditAmountField attribute. 
218      * @return Returns the creditAmountField.
219      * 
220      * KRAD Conversion: getting the field value - No use of data dictionary
221      * 
222      */
223     public Field getCreditAmountField() {
224         return creditAmountField;
225     }
226     /**
227      * Sets the creditAmountField attribute value.
228      * @param creditAmountField The creditAmountField to set.
229      * 
230      * KRAD Conversion: setting up the value of the fields - No use of data dictionary
231      */
232     public void setCreditAmountField(Field creditAmountField) {
233         this.creditAmountField = creditAmountField;
234     }
235     /**
236      * Gets the creditFieldDefinition attribute. 
237      * @return Returns the creditFieldDefinition.
238      */
239     public AccountingLineViewFieldDefinition getCreditFieldDefinition() {
240         return creditFieldDefinition;
241     }
242     /**
243      * Sets the creditFieldDefinition attribute value.
244      * @param creditFieldDefinition The creditFieldDefinition to set.
245      */
246     public void setCreditFieldDefinition(AccountingLineViewFieldDefinition creditFieldDefinition) {
247         this.creditFieldDefinition = creditFieldDefinition;
248     }
249     /**
250      * Gets the debitAmountField attribute. 
251      * @return Returns the debitAmountField.
252      */
253     public Field getDebitAmountField() {
254         return debitAmountField;
255     }
256     /**
257      * Sets the debitAmountField attribute value.
258      * @param debitAmountField The debitAmountField to set.
259      */
260     public void setDebitAmountField(Field debitAmountField) {
261         this.debitAmountField = debitAmountField;
262     }
263     /**
264      * Gets the debitFieldDefinition attribute. 
265      * @return Returns the debitFieldDefinition.
266      */
267     public AccountingLineViewFieldDefinition getDebitFieldDefinition() {
268         return debitFieldDefinition;
269     }
270     /**
271      * Sets the debitFieldDefinition attribute value.
272      * @param debitFieldDefinition The debitFieldDefinition to set.
273      */
274     public void setDebitFieldDefinition(AccountingLineViewFieldDefinition debitFieldDefinition) {
275         this.debitFieldDefinition = debitFieldDefinition;
276     }
277     /**
278      * Gets the definition attribute. 
279      * @return Returns the definition.
280      */
281     public AccountingLineViewDebitCreditAmountFieldDefinition getDefinition() {
282         return definition;
283     }
284     /**
285      * Sets the definition attribute value.
286      * @param definition The definition to set.
287      */
288     public void setDefinition(AccountingLineViewDebitCreditAmountFieldDefinition definition) {
289         this.definition = definition;
290     }
291 
292     /**
293      * @see org.kuali.ole.sys.document.web.TableJoining#setEditableBlocks(java.util.Set)
294      */
295     public void setEditableBlocks(Set<String> editableBlocks) {
296         if (editableBlocks.contains(OLEPropertyConstants.AMOUNT)) {
297             if (debitAmountField != null) {
298                 debitAmountField.setReadOnly(false);
299             }
300             if (creditAmountField != null) {
301                 creditAmountField.setReadOnly(false);
302             }
303         }
304     }
305 
306     /**
307      * @see org.kuali.ole.sys.document.web.ReadOnlyable#setEditable()
308      */
309     public void setEditable() {
310         if (debitAmountField != null) {
311             debitAmountField.setReadOnly(false);
312         }
313         
314         if (creditAmountField != null) {
315             creditAmountField.setReadOnly(false);
316         }
317     }
318 }