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.HashSet;
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.AccountingLineViewLineDefinition;
25  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
26  
27  /**
28   * Represents a single table row to be rendered as part of an accounting line view.
29   */
30  public class AccountingLineViewLine implements ReadOnlyable, AccountingLineViewLineFillingElement {
31      private List<RenderableElement> elements;
32      private AccountingLineViewLineDefinition definition;
33   
34      /**
35       * Gets the definition attribute. 
36       * @return Returns the definition.
37       */
38      public AccountingLineViewLineDefinition getDefinition() {
39          return definition;
40      }
41      /**
42       * Sets the definition attribute value.
43       * @param definition The definition to set.
44       */
45      public void setDefinition(AccountingLineViewLineDefinition definition) {
46          this.definition = definition;
47      }
48      
49      /**
50       * @see org.kuali.ole.sys.document.web.AccountingLineViewRenderableElementContainer#getName()
51       */
52      public String getName() {
53          return definition.getElementName();
54      }
55      /**
56       * Gets the elements attribute. 
57       * @return Returns the elements.
58       */
59      public List<RenderableElement> getElements() {
60          return elements;
61      }
62      /**
63       * Sets the elements attribute value.
64       * @param elements The elements to set.
65       */
66      public void setElements(List<RenderableElement> fields) {
67          this.elements = fields;
68      }
69      
70      /**
71       * Gets the number of actual rows requested (1)
72       * @see org.kuali.ole.sys.document.web.AccountingLineViewRenderableElement#getRequestedRowCount()
73       */
74      public int getRequestedRowCount() {
75          return getMaxRequestedRowCount();
76      }
77      
78      /**
79       * Determines the max requested row count in the line. 
80       * @return the number of rows to create for this line
81       */
82      protected int getMaxRequestedRowCount() {
83          for (RenderableElement element : elements) {
84              if (element instanceof TableJoiningWithHeader && !element.isHidden()) {
85                  return 2;
86              }
87          }
88          return 1;
89      }
90      
91      /**
92       * @see org.kuali.ole.sys.document.web.TableJoining#joinRow(org.kuali.ole.sys.document.web.AccountingLineTableRow)
93       */
94      public void joinRow(AccountingLineTableRow headerRow, AccountingLineTableRow row) {
95         for (RenderableElement element : elements) {
96             if (element instanceof TableJoining) {
97                 ((TableJoining)element).joinRow(headerRow, row);
98             } else {
99                 headerRow.addCell(createTableCellForNonTableJoining(element));
100            }
101        }
102     }
103     
104     /**
105      * Creates a table cell to wrap the given rendering element
106      * @param element the element to wrap
107      * @return a table cell wrapping that element
108      */
109     protected AccountingLineTableCell createTableCellForNonTableJoining(RenderableElement element) {
110         AccountingLineTableCell cell = new AccountingLineTableCell();
111         cell.addRenderableElement(element);
112         cell.setRowSpan(getRequestedRowCount());
113         return cell;
114     }
115     
116     /**
117      * Always throws an exception - a line may only join a table through a parent lines element
118      * @see org.kuali.ole.sys.document.web.TableJoining#joinTable(java.util.List)
119      */
120     public void joinTable(List<AccountingLineTableRow> rows) {
121         throw new IllegalStateException("Line elements may not join a table directly; the specified rendering is incorrect");
122     }
123     
124     /**
125      * This element should be padded out
126      * @see org.kuali.ole.sys.document.web.AccountingLineViewLineFillingElement#stretchToFillLine()
127      */
128     public boolean shouldStretchToFillLine() {
129         return false;
130     }
131     
132     /**
133      * @see org.kuali.ole.sys.document.web.TableJoining#readOnlyize()
134      */
135     public void readOnlyize() {
136         for (RenderableElement element : elements) {
137             if (element instanceof ReadOnlyable) {
138                 ((ReadOnlyable)element).readOnlyize();
139             }
140         }
141     }
142     
143     /**
144      * @see org.kuali.ole.sys.document.web.ReadOnlyable#isReadOnly()
145      */
146     public boolean isReadOnly() {
147         for (RenderableElement element : elements) {
148             if (element instanceof ReadOnlyable && !((ReadOnlyable)element).isReadOnly()) {
149                 return false;
150             }
151         }
152         return true;
153     }
154     
155     /**
156      * @see org.kuali.ole.sys.document.web.TableJoining#removeAllActionBlocks()
157      */
158     public void removeAllActionBlocks() {
159         Set<RenderableElement> actionBlocksToRemove = new HashSet<RenderableElement>();
160         for (RenderableElement element : elements) {
161             if (element.isActionBlock()) {
162                 actionBlocksToRemove.add(element);
163             }
164         }
165         elements.removeAll(actionBlocksToRemove);
166     }
167     
168     /**
169      * @see org.kuali.ole.sys.document.web.TableJoining#removeUnviewableBlocks()
170      */
171     public void removeUnviewableBlocks(Set<String> unviewableBlocks) {
172         Set<RenderableElement> elementsToRemove = new HashSet<RenderableElement>();
173         for (RenderableElement element : elements) {
174             if (element instanceof TableJoining) {
175                 if (unviewableBlocks.contains(((TableJoining)element).getName())) {
176                     elementsToRemove.add(element);
177                 } else {
178                     ((TableJoining)element).removeUnviewableBlocks(unviewableBlocks);
179                 }
180             }
181         }
182         elements.removeAll(elementsToRemove);
183     }
184     
185     /**
186      * Shuffles responsibility on to any TableJoining children
187      * @see org.kuali.ole.sys.document.web.TableJoining#readOnlyizeReadOnlyBlocks(java.util.Set)
188      */
189     public void readOnlyizeReadOnlyBlocks(Set<String> readOnlyBlocks) {
190         for (RenderableElement element : elements) {
191             if (element instanceof TableJoining) {
192                 ((TableJoining)element).readOnlyizeReadOnlyBlocks(readOnlyBlocks);
193             }
194         }
195     }
196     
197     /**
198      * @see org.kuali.ole.sys.document.web.TableJoining#performFieldTransformation(org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation, org.kuali.ole.sys.businessobject.AccountingLine, java.util.Map, java.util.Map)
199      */
200     public void performFieldTransformations(List<AccountingLineFieldRenderingTransformation> fieldTransformations, AccountingLine accountingLine, Map unconvertedValues) {
201         for (RenderableElement element : elements) {
202             if (element instanceof TableJoining) {
203                 ((TableJoining)element).performFieldTransformations(fieldTransformations, accountingLine, unconvertedValues);
204             }
205         }
206     }
207     
208     /**
209      * Finds the number of table cells this line expects to take up
210      * @return the number of displayed table cells this line expects to render as
211      */
212     public int getDisplayingFieldWidth() {
213         int count = 0;
214         for (RenderableElement element : elements) {
215             if (!element.isHidden()) {
216                 if (element instanceof AccountingLineViewField && ((AccountingLineViewField)element).getColSpanOverride() > 1) {
217                     count += ((AccountingLineViewField)element).getColSpanOverride();
218                 } else {
219                     count += 1;
220                 }
221             }
222         }
223         return count;
224     }
225     
226     /**
227      * @see org.kuali.ole.sys.document.web.ReadOnlyable#setEditable()
228      */
229     public void setEditable() {
230         for (RenderableElement element : elements) {
231             if (element instanceof ReadOnlyable) {
232                 ((ReadOnlyable)element).setEditable();
233             }
234         }
235     }
236     
237     /**
238      * @see org.kuali.ole.sys.document.web.TableJoining#setEditableBlocks(java.util.Set)
239      */
240     public void setEditableBlocks(Set<String> editableBlocks) {
241         for (RenderableElement element : elements) {
242             if (element instanceof TableJoining) {
243                 ((TableJoining)element).setEditableBlocks(editableBlocks);
244             }
245         }
246     }
247 }