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.businessobject.AccountingLine;
23  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewCurrentBaseAmountFieldDefinition;
24  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewFieldDefinition;
25  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
26  import org.kuali.rice.kns.web.ui.Field;
27  
28  /**
29   * Represents a current/base amount for an accounting line
30   */
31  public class AccountingLineViewCurrentBaseAmount implements TableJoiningWithHeader, ReadOnlyable {
32      private Field currentAmountField;
33      private Field baseAmountField;
34      private AccountingLineViewCurrentBaseAmountFieldDefinition definition;
35      private AccountingLineViewFieldDefinition currentAmountFieldDefinition;
36      private AccountingLineViewFieldDefinition baseAmountFieldDefinition;
37  
38      /**
39       * Returns null; we don't want to participate in normal naming schemes
40       * @see org.kuali.ole.sys.document.web.TableJoining#getName()
41       */
42      public String getName() {
43          return null;
44      }
45  
46      /**
47       * Checks if either the current amount field or base amount field need to be set to read only
48       * @see org.kuali.ole.sys.document.web.FieldTableJoining#readOnlyizeReadOnlyBlocks(java.util.Set)
49       */
50      public void readOnlyizeReadOnlyBlocks(Set<String> readOnlyBlocks) {
51          if (currentAmountField != null) {
52              readOnlyizeField(currentAmountField, readOnlyBlocks);
53          }
54          if (baseAmountField != null) {
55              readOnlyizeField(baseAmountField, readOnlyBlocks);
56          }
57          if (baseAmountField != null && currentAmountField != null) {
58              if (baseAmountField.isReadOnly() && !currentAmountField.isReadOnly()) {
59                  currentAmountField.setFieldRequired(true);
60              } else if (currentAmountField.isReadOnly() && !baseAmountField.isReadOnly()) {
61                  baseAmountField.setFieldRequired(true);
62              }
63          }
64      }
65      
66      /**
67       * @see org.kuali.ole.sys.document.web.ReadOnlyable#isReadOnly()
68       */
69      public boolean isReadOnly() {
70          boolean readOnly = true;
71          if (currentAmountField != null) {
72              readOnly &= currentAmountField.isReadOnly();
73          }
74          if (baseAmountField != null) {
75              readOnly &= baseAmountField.isReadOnly();
76          }
77          return readOnly;
78      }
79  
80      /**
81       * @see org.kuali.ole.sys.document.web.ReadOnlyable#readOnlyize()
82       */
83      public void readOnlyize() {
84          if (currentAmountField != null) {
85              currentAmountField.setReadOnly(true);
86          }
87          if (baseAmountField != null) {
88              baseAmountField.setReadOnly(true);
89          }
90      }
91  
92      /**
93       * Checks if the given field is named as a readOnlyBlock; if so, makes it read only
94       * @param field the field to check
95       * @param readOnlyBlocks the names of all read only blocks
96       * 
97       * KRAD Conversion: Customization of the fields by setting them to read only - No use of data dictionary
98       */
99      protected void readOnlyizeField(Field field, Set<String> readOnlyBlocks) {
100         if (field != null && readOnlyBlocks.contains(field.getPropertyName())) {
101             field.setReadOnly(true);
102         }
103     }
104 
105     /**
106      * Always returns 2 - one line for the header, one line for the fields
107      * @see org.kuali.ole.sys.document.web.TableJoining#getRequestedRowCount()
108      */
109     public int getRequestedRowCount() {
110         return 2;
111     }
112 
113     /**
114      * Adds the header cell to the first row, and the regular cell to the second row
115      * @see org.kuali.ole.sys.document.web.TableJoining#joinRow(org.kuali.ole.sys.document.web.AccountingLineTableRow, org.kuali.ole.sys.document.web.AccountingLineTableRow)
116      */
117     public void joinRow(AccountingLineTableRow headerLabelRow, AccountingLineTableRow row) {
118         if (currentAmountField != null) {
119             headerLabelRow.addCell(createHeaderCellForField(currentAmountField));
120             row.addCell(createCellForField(currentAmountField, currentAmountFieldDefinition));
121         }
122         if (baseAmountField != null) {
123             headerLabelRow.addCell(createHeaderCellForField(baseAmountField));
124             row.addCell(createCellForField(baseAmountField, baseAmountFieldDefinition));
125         }
126     }
127 
128     /**
129      * Adds the header cell to the first row and adds to the second row a cell that spans all remaining rows
130      * @see org.kuali.ole.sys.document.web.TableJoining#joinTable(java.util.List)
131      */
132     public void joinTable(List<AccountingLineTableRow> rows) {
133         final int remainingRowCount = rows.size() - 1;
134         
135         if (currentAmountField != null) {
136             rows.get(0).addCell(createHeaderCellForField(currentAmountField));
137             
138             AccountingLineTableCell currentCell = createCellForField(currentAmountField, currentAmountFieldDefinition);
139             currentCell.setRowSpan(remainingRowCount);
140             rows.get(1).addCell(currentCell);
141         }
142         if (baseAmountField != null) {
143             rows.get(0).addCell(createHeaderCellForField(baseAmountField));
144             
145             AccountingLineTableCell baseCell = createCellForField(baseAmountField, baseAmountFieldDefinition);
146             baseCell.setRowSpan(remainingRowCount);
147             rows.get(1).addCell(baseCell);
148         }
149         
150     }
151 
152     /**
153      * Does nothing - we don't have action blocks, like, ever
154      * @see org.kuali.ole.sys.document.web.TableJoining#removeAllActionBlocks()
155      */
156     public void removeAllActionBlocks() {}
157 
158     /**
159      * Checks to see if either the current amount or the base amount are unviewable; if so, sets them to null
160      * @see org.kuali.ole.sys.document.web.FieldTableJoining#removeUnviewableBlocks(java.util.Set)
161      */
162     public void removeUnviewableBlocks(Set<String> unviewableBlocks) {
163         if (isFieldUnviewable(currentAmountField, unviewableBlocks)) {
164             currentAmountField = null;
165         }
166         if (isFieldUnviewable(baseAmountField, unviewableBlocks)) {
167             baseAmountField = null;
168         }
169     }
170     
171     /**
172      * Determines if the given field is among the blocks which should not be viewable
173      * @param field the field to check for unviewability
174      * @param unviewableBlocks the names of all unviewable blocks
175      * @return true if the field should not be viewable, false if we can see it
176      */
177     protected boolean isFieldUnviewable(Field field, Set<String> unviewableBlocks) {
178         return field != null && unviewableBlocks.contains(field.getPropertyName());
179     }
180     
181     /**
182      * Creates a table cell with a renderable field inside
183      * @param field the field to create a cell for
184      * @return a cell that wraps the given field 
185      * 
186      * KRAD Conversion: Performs setting up the field for the cell
187      */
188     protected AccountingLineTableCell createCellForField(Field field, AccountingLineViewFieldDefinition definition) {
189         AccountingLineTableCell cell = new AccountingLineTableCell();
190         AccountingLineViewField renderableField = new AccountingLineViewField();
191         renderableField.setField(field);
192         renderableField.setDefinition(definition);
193         cell.addRenderableElement(renderableField);
194         return cell;
195     }
196     
197     /**
198      * Creates a header cell for the given field
199      * @param field the field to create a header cell for
200      * @return a header cell
201      * 
202      * KRAD Conversion: Customization of the header cell with label from fields - No use of data dictionary
203      * 
204      */
205     protected AccountingLineTableCell createHeaderCellForField(Field field) {
206         AccountingLineTableCell headerCell = new AccountingLineTableCell();
207         headerCell.setRendersAsHeader(true);
208         headerCell.addRenderableElement(new LiteralHeaderLabel(field.getFieldLabel()));
209         return headerCell;
210     }
211 
212     /**
213      * @see org.kuali.ole.sys.document.web.FieldTableJoining#performFieldTransformations(java.util.List, org.kuali.ole.sys.businessobject.AccountingLine, java.util.Map, java.util.Map)
214      */
215     public void performFieldTransformations(List<AccountingLineFieldRenderingTransformation> fieldTransformations, AccountingLine accountingLine, Map unconvertedValues) {
216         for (AccountingLineFieldRenderingTransformation fieldTransformation : fieldTransformations) {
217             fieldTransformation.transformField(accountingLine, getCurrentAmountField(), getCurrentAmountFieldDefinition(), unconvertedValues);
218             fieldTransformation.transformField(accountingLine, getBaseAmountField(), getBaseAmountFieldDefinition(), unconvertedValues);
219         }
220     }
221 
222     /**
223      * Not used; returns null.
224      * @see org.kuali.ole.sys.document.web.TableJoiningWithHeader#createHeaderLabel()
225      */
226     public HeaderLabel createHeaderLabel() {
227         return null;
228     }
229 
230     /**
231      * This field is never hidden
232      * @see org.kuali.ole.sys.document.web.TableJoiningWithHeader#isHidden()
233      */
234     public boolean isHidden() {
235         return false;
236     }
237 
238     /**
239      * Gets the baseAmountField attribute. 
240      * @return Returns the baseAmountField.
241      * 
242      * KRAD Conversion: Retrieving the field
243      */
244     public Field getBaseAmountField() {
245         return baseAmountField;
246     }
247 
248     /**
249      * Sets the baseAmountField attribute value.
250      * @param baseAmountField The baseAmountField to set.
251      * 
252      * KRAD Conversion: Setting up field's amount
253      */
254     public void setBaseAmountField(Field baseAmountField) {
255         this.baseAmountField = baseAmountField;
256     }
257 
258     /**
259      * Gets the currentAmountField attribute. 
260      * @return Returns the currentAmountField.
261      */
262     public Field getCurrentAmountField() {
263         return currentAmountField;
264     }
265 
266     /**
267      * Sets the currentAmountField attribute value.
268      * @param currentAmountField The currentAmountField to set.
269      */
270     public void setCurrentAmountField(Field currentAmountField) {
271         this.currentAmountField = currentAmountField;
272     }
273 
274     /**
275      * Gets the definition attribute. 
276      * @return Returns the definition.
277      */
278     public AccountingLineViewCurrentBaseAmountFieldDefinition getDefinition() {
279         return definition;
280     }
281 
282     /**
283      * Sets the definition attribute value.
284      * @param definition The definition to set.
285      */
286     public void setDefinition(AccountingLineViewCurrentBaseAmountFieldDefinition definition) {
287         this.definition = definition;
288     }
289 
290     /**
291      * Gets the baseAmountFieldDefinition attribute. 
292      * @return Returns the baseAmountFieldDefinition.
293      */
294     public AccountingLineViewFieldDefinition getBaseAmountFieldDefinition() {
295         return baseAmountFieldDefinition;
296     }
297 
298     /**
299      * Sets the baseAmountFieldDefinition attribute value.
300      * @param baseAmountFieldDefinition The baseAmountFieldDefinition to set.
301      */
302     public void setBaseAmountFieldDefinition(AccountingLineViewFieldDefinition baseAmountFieldDefinition) {
303         this.baseAmountFieldDefinition = baseAmountFieldDefinition;
304     }
305 
306     /**
307      * Gets the currentAmountFieldDefinition attribute. 
308      * @return Returns the currentAmountFieldDefinition.
309      */
310     public AccountingLineViewFieldDefinition getCurrentAmountFieldDefinition() {
311         return currentAmountFieldDefinition;
312     }
313 
314     /**
315      * Sets the currentAmountFieldDefinition attribute value.
316      * @param currentAmountFieldDefinition The currentAmountFieldDefinition to set.
317      */
318     public void setCurrentAmountFieldDefinition(AccountingLineViewFieldDefinition currentAmountFieldDefinition) {
319         this.currentAmountFieldDefinition = currentAmountFieldDefinition;
320     }
321 
322     /**
323      * @see org.kuali.ole.sys.document.web.TableJoining#setEditableBlocks(java.util.Set)
324      */
325     public void setEditableBlocks(Set<String> editableBlocks) {
326         if (currentAmountField != null) {
327             setEditableField(currentAmountField, editableBlocks);
328         }
329         if (baseAmountField != null) {
330             setEditableField(baseAmountField, editableBlocks);
331         }
332         
333         if (baseAmountField != null && currentAmountField != null) {
334             if (baseAmountField.isReadOnly() && !currentAmountField.isReadOnly()) {
335                 currentAmountField.setReadOnly(false);
336             } 
337             else if (currentAmountField.isReadOnly() && !baseAmountField.isReadOnly()) {
338                 baseAmountField.setReadOnly(false);
339             }
340         }
341     }
342 
343     /**
344      * @see org.kuali.ole.sys.document.web.ReadOnlyable#setEditable()
345      */
346     public void setEditable() {
347         if (currentAmountField != null) {
348             currentAmountField.setReadOnly(false);
349         }
350         
351         if (baseAmountField != null) {
352             baseAmountField.setReadOnly(false);
353         }
354     }
355     
356     /**
357      * Checks if the given field is named as an editableBlocks; if so, makes it editable
358      * @param field the field to check
359      * @param editableBlocks the names of all editable blocks
360      * 
361      * KRAD Conversion: Customization of the fields - No use of data dictionary
362      */
363     protected void setEditableField(Field field, Set<String> editableBlocks) {
364         if (field != null && editableBlocks.contains(field.getPropertyName())) {
365             field.setReadOnly(false);
366         }
367     }    
368 }