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.service.impl;
17  
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.kuali.ole.sys.document.service.AccountingLineTableTransformation;
23  import org.kuali.ole.sys.document.web.AccountingLineTableCell;
24  import org.kuali.ole.sys.document.web.AccountingLineTableRow;
25  import org.kuali.ole.sys.document.web.RenderableElement;
26  
27  /**
28   * A transformation which takes all of the non-visible hidden fields of the accounting line and moves them
29   * to the first possible position
30   */
31  public class HiddenFieldRearrangementAccountingLineRenderingTransformationImpl implements AccountingLineTableTransformation {
32  
33      public void transformRows(List<AccountingLineTableRow> rows) {
34          // 1. find the first top-level container element with non-hidden variables
35          AccountingLineTableCell cell = findFirstNonHiddenCell(rows);
36          // 2. move all the hidden fields
37          for (AccountingLineTableRow row : rows) {
38              moveHiddenFields(row, cell);
39          }
40      }
41      
42      /**
43       * Finds the first top-level non-hidden container from the given list of elements
44       * @param elements the elements to find the first non-hidden container from
45       * @return the first top-level non-hidden container
46       */
47      protected AccountingLineTableCell findFirstNonHiddenCell(List<AccountingLineTableRow> rows) {
48          for (AccountingLineTableRow row : rows) {
49              for (AccountingLineTableCell cell : row.getCells()) {
50                  if (!cell.isHidden()) {
51                      return cell;
52                  }
53              }
54          }
55          throw new IllegalArgumentException("The renderable element tree specified does not seem to have any elements that will display as non-hidden specified");
56      }
57      
58      /**
59       * Moves any hidden fields in the source container to the target container 
60       * @param sourceContainer the container which may have hidden fields
61       * @param targetContainer the container which should be carrying all hidden fields
62       */
63      protected void moveHiddenFields(AccountingLineTableRow sourceRow, AccountingLineTableCell targetCell) {
64          for (AccountingLineTableCell cell : sourceRow.getCells()) {
65              moveHiddenFields(cell, targetCell);
66          }
67      }
68      
69      /**
70       * Moves any hidden renderable fields in the source cell to the target cell
71       * @param sourceCell the source cell to move hidden elements from
72       * @param targetCell the target cell to move hidden elements to
73       */
74      protected void moveHiddenFields(AccountingLineTableCell sourceCell, AccountingLineTableCell targetCell) {
75          Set<RenderableElement> renderableElementsToRemove = new HashSet<RenderableElement>();
76          for (RenderableElement element : sourceCell.getRenderableElement()) {
77              if (element.isHidden()) {
78                  renderableElementsToRemove.add(element);
79              }
80          }
81          sourceCell.getRenderableElement().removeAll(renderableElementsToRemove);
82          targetCell.getRenderableElement().addAll(renderableElementsToRemove);
83      }
84  }