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.businessobject.AccountingLine;
23  import org.kuali.ole.sys.document.service.AccountingLineRenderingTransformation;
24  import org.kuali.ole.sys.document.web.ReadOnlyable;
25  import org.kuali.ole.sys.document.web.RenderableElement;
26  import org.kuali.ole.sys.document.web.TableJoining;
27  
28  /**
29   * If all the fields of a line are read only, then any actions blocks should be completely removed.
30   */
31  public class AllReadOnlyNoActionsAccountingLineRenderingTransformationImpl implements AccountingLineRenderingTransformation {
32  
33      /**
34       * Traverses through the elements to see if they're all read only; if so, traverses through again and removes any action blocks
35       * @see org.kuali.ole.sys.document.service.AccountingLineRenderingTransformation#transformElements(java.util.List, org.kuali.ole.sys.businessobject.AccountingLine)
36       */
37      public void transformElements(List<TableJoining> elements, AccountingLine accountingLine) {
38          if (allReadOnly(elements)) {
39              removeActionBlocks(elements);
40          }
41      }
42      
43      /**
44       * Traverses all elements, determining if all of the elements are read only
45       * @param elements the elements to render
46       * @return true if all elements are read only, false otherwise
47       */
48      protected boolean allReadOnly(List<TableJoining> elements) {
49          for (TableJoining element : elements) {
50              if (element instanceof ReadOnlyable && !((ReadOnlyable)element).isReadOnly()) {
51                  return false;
52              }
53          }
54          return true;
55      }
56      
57      /**
58       * Takes any action blocks out of the line
59       * @param elements the elements which contain action blocks to remove
60       */
61      protected void removeActionBlocks(List<? extends TableJoining> elements) {
62          Set<TableJoining> elementsToRemove = new HashSet<TableJoining>();
63          for (TableJoining element : elements) {
64              element.removeAllActionBlocks();
65              if (element instanceof RenderableElement && ((RenderableElement)element).isActionBlock()) {
66                  elementsToRemove.add(element);
67              }
68          }
69          elements.removeAll(elementsToRemove);
70      }
71  
72  }