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.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import org.kuali.ole.sys.businessobject.AccountingLine;
24  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewColumnsDefinition;
25  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
26  
27  /**
28   * A layout element that renders elements 
29   */
30  public class AccountingLineViewColumns implements AccountingLineViewLineFillingElement {
31      private List<AccountingLineViewField> fields;
32      private AccountingLineViewColumnsDefinition definition;
33      
34      /**
35       * Constructs a AccountingLineViewColumns
36       * @param definition the data dictionary validation of this columns layout element
37       * @param fields the fields to render within this columns layout element
38       */
39      public AccountingLineViewColumns(AccountingLineViewColumnsDefinition definition, List<AccountingLineViewField> fields) {
40          this.definition = definition;
41          this.fields = fields;
42      }
43  
44      /**
45       * Returns the name of this element
46       * @see org.kuali.ole.sys.document.web.TableJoining#getName()
47       */
48      public String getName() {
49          return definition.getName();
50      }
51  
52      /**
53       * 
54       * @see org.kuali.ole.sys.document.web.TableJoining#getRequestedRowCount()
55       */
56      public int getRequestedRowCount() {
57          return 1;
58      }
59  
60      /**
61       * This element should be stretched
62       * @see org.kuali.ole.sys.document.web.AccountingLineViewLineFillingElement#stretchToFillLine()
63       */
64      public boolean shouldStretchToFillLine() {
65          return true;
66      }
67  
68      /**
69       * Joins the header row with a line filling cell, which includes within it an inner table that shows all the child fields
70       * @see org.kuali.ole.sys.document.web.TableJoining#joinRow(org.kuali.ole.sys.document.web.AccountingLineTableRow, org.kuali.ole.sys.document.web.AccountingLineTableRow)
71       */
72      public void joinRow(AccountingLineTableRow headerLabelRow, AccountingLineTableRow row) {
73          AccountingLineTableCell cell = new AccountingLineTableCell();
74  
75          AccountingLineTable columnsTable = new AccountingLineTable();
76  
77          List<AccountingLineTableRow> rows = createRowsForFields();
78          
79          columnsTable.setRows(rows);
80          cell.addRenderableElement(columnsTable);
81          headerLabelRow.addCell(cell);
82      }
83      
84      /**
85       * Creates rows for the inner tables for each field inside this columsn definition
86       * @return a List of created AccountingLineTableRows
87       */
88      protected List<AccountingLineTableRow> createRowsForFields() {
89          List<AccountingLineTableRow> rows = new ArrayList<AccountingLineTableRow>();
90          
91          int countForThisRow = 0;
92          AccountingLineTableRow row = new AccountingLineTableRow();
93          for (AccountingLineViewField field : fields) {
94              row.addCell(createHeaderCellForField(field));
95              row.addCell(createCellForField(field));
96              countForThisRow += 1;
97              
98              if (countForThisRow == definition.getColumnCount()) {
99                  rows.add(row);
100                 countForThisRow = 0;
101                 row = new AccountingLineTableRow();
102             }
103         }
104         if (countForThisRow > 0) { // oops! we stopped mid-row and now need to fill it out
105             while (countForThisRow < definition.getColumnCount()) {
106                 row.addCell(createPaddingCell());
107                 countForThisRow += 1;
108             }
109             rows.add(row);
110         }
111         
112         return rows;
113     }
114     
115     /**
116      * Creates a header cell for for the given field 
117      * @param field the field to create a header cell for
118      * @return a header cell
119      */
120     protected AccountingLineTableCell createHeaderCellForField(AccountingLineViewField field) {
121         AccountingLineTableCell headerCell = new AccountingLineTableCell();
122         headerCell.setRendersAsHeader(true);
123         headerCell.addRenderableElement(field.createHeaderLabel());
124         return headerCell;
125     }
126     
127     /**
128      * Creates the "field" cell for the given field
129      * @param field the field to create a cell for
130      * @return the cell withe field in it
131      */
132     protected AccountingLineTableCell createCellForField(AccountingLineViewField field) {
133         AccountingLineTableCell cell = new AccountingLineTableCell();
134         cell.addRenderableElement(field);
135         return cell;
136     }
137     
138     /**
139      * Creates an empty cell to pad out the place typically held for a cell
140      * @return an empty table cell that spans two columns
141      */
142     protected AccountingLineTableCell createPaddingCell() {
143         AccountingLineTableCell cell = new AccountingLineTableCell();
144         cell.setColSpan(2);
145         cell.setNeverEmpty(true);
146         return cell;
147     }
148 
149     /**
150      * An exception state; line filling elements can only join tables through lines 
151      * @see org.kuali.ole.sys.document.web.TableJoining#joinTable(java.util.List)
152      */
153     public void joinTable(List<AccountingLineTableRow> rows) {
154         throw new IllegalStateException("Line elements may not join a table directly; the specified rendering is incorrect");
155     }
156 
157     /**
158      * Has fields perform the transformations
159      * @see org.kuali.ole.sys.document.web.TableJoining#performFieldTransformations(java.util.List, org.kuali.ole.sys.businessobject.AccountingLine, java.util.Map, java.util.Map)
160      */
161     public void performFieldTransformations(List<AccountingLineFieldRenderingTransformation> fieldTransformations, AccountingLine accountingLine, Map unconvertedValues) {
162         int count = 0;
163         for (AccountingLineViewField field : fields) {
164             for (AccountingLineFieldRenderingTransformation transformation : fieldTransformations) {
165                 transformation.transformField(accountingLine, field.getField(), field.getDefinition(), unconvertedValues);
166             }
167         }
168     }
169 
170     /**
171      * Removes any child action blocks; surviving blocks are instructed to remove child blocks they have
172      * @see org.kuali.ole.sys.document.web.TableJoining#removeAllActionBlocks()
173      */
174     public void removeAllActionBlocks() {
175         List<AccountingLineViewField> fieldsToRemove = new ArrayList<AccountingLineViewField>();
176         for (AccountingLineViewField field : fields) {
177             if (field.isActionBlock()) {
178                 fieldsToRemove.add(field);
179             } else {
180                 field.removeAllActionBlocks();
181             }
182         }
183         fields.removeAll(fieldsToRemove);
184     }
185 
186     /**
187      * Goes through all child fields; removes any fields which match unviewable blocks or otherwise, has the field remove unviewable blocks
188      * @see org.kuali.ole.sys.document.web.TableJoining#removeUnviewableBlocks(java.util.Set)
189      */
190     public void removeUnviewableBlocks(Set<String> unviewableBlocks) {
191         List<AccountingLineViewField> unviewableFields = new ArrayList<AccountingLineViewField>();
192         for (AccountingLineViewField field : fields) {
193             if (unviewableBlocks.contains(field.getName())) {
194                 unviewableFields.add(field);
195             } else {
196                 field.removeUnviewableBlocks(unviewableBlocks);
197             }
198         }
199         fields.removeAll(unviewableFields);
200     }
201 
202     /**
203      * Has each field readOnlyize
204      * @see org.kuali.ole.sys.document.web.TableJoining#readOnlyizeReadOnlyBlocks(java.util.Set)
205      */
206     public void readOnlyizeReadOnlyBlocks(Set<String> readOnlyBlocks) {
207         for (AccountingLineViewField field : fields) {
208             field.readOnlyizeReadOnlyBlocks(readOnlyBlocks);
209         }
210     }
211 
212     /**
213      * Gets the fields attribute. 
214      * @return Returns the fields.
215      */
216     public List<AccountingLineViewField> getFields() {
217         return fields;
218     }
219 
220     /**
221      * Sets the fields attribute value.
222      * @param fields The fields to set.
223      */
224     public void setFields(List<AccountingLineViewField> fields) {
225         this.fields = fields;
226     }
227 
228     /**
229      * @see org.kuali.ole.sys.document.web.ReadOnlyable#isReadOnly()
230      */
231     public boolean isReadOnly() {
232         for (AccountingLineViewField field : fields) {
233             if (!field.isReadOnly()) return false;
234         }
235         return true;
236     }
237 
238     /**
239      * @see org.kuali.ole.sys.document.web.ReadOnlyable#readOnlyize()
240      */
241     public void readOnlyize() {
242         for (AccountingLineViewField field : fields) {
243             field.readOnlyize();
244         }
245     }
246 
247     /**
248      * Always returns 1; this will build an inner table in one cell
249      * @see org.kuali.ole.sys.document.web.AccountingLineViewLineFillingElement#getDisplayingFieldWidth()
250      */
251     public int getDisplayingFieldWidth() {
252         return 1;
253     }
254 
255     /**
256      * @see org.kuali.ole.sys.document.web.TableJoining#setEditableBlocks(java.util.Set)
257      */
258     public void setEditableBlocks(Set<String> editableBlocks) {
259         for (AccountingLineViewField field : fields) {
260             field.setEditableBlocks(editableBlocks);
261         }
262     }
263 
264     /**
265      * @see org.kuali.ole.sys.document.web.ReadOnlyable#setEditable()
266      */
267     public void setEditable() {
268         for (AccountingLineViewField field : fields) {
269             field.setEditable();
270         }
271     }
272 
273 }