View Javadoc

1   /**
2    * Copyright 2005-2012 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.rice.krad.uif.util;
17  
18  import org.kuali.rice.krad.uif.component.Component;
19  import org.kuali.rice.krad.uif.component.PropertyReplacer;
20  import org.kuali.rice.krad.uif.container.CollectionFilter;
21  import org.kuali.rice.krad.uif.container.CollectionGroup;
22  import org.kuali.rice.krad.uif.container.Container;
23  import org.kuali.rice.krad.uif.container.PageGroup;
24  import org.kuali.rice.krad.uif.element.Action;
25  import org.kuali.rice.krad.uif.field.InputField;
26  import org.kuali.rice.krad.uif.modifier.ComponentModifier;
27  import org.kuali.rice.krad.uif.view.View;
28  
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  
33  /**
34   * Utility class for trimming component instances for storage
35   *
36   * <p>
37   * Invoked to trim the view instance before storing on the form as the post view. Certain information is keep
38   * around to support post methods that need to operate on the previous view configuration. Examples include component
39   * refresh and collection add/delete line.
40   * </p>
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class ViewCleaner {
45  
46      /**
47       * Cleans a view instance removing all pages except the current page and then invoking the view
48       * index to perform cleaning on contained components
49       *
50       * @param view - view instance to clean
51       */
52      public static void cleanView(View view) {
53          view.setApplicationHeader(null);
54          view.setApplicationFooter(null);
55          view.setNavigation(null);
56          view.setPage(null);
57          view.setViewMenuLink(null);
58          view.setClientSideState(new HashMap<String, Object>());
59  
60          // clear all view pages exception the current page
61          PageGroup currentPage = view.getCurrentPage();
62  
63          List<Component> pages = new ArrayList<Component>();
64          pages.add(currentPage);
65          view.setItems(pages);
66  
67          cleanContainer(view);
68  
69          view.getViewIndex().clearIndexesAfterRender();
70      }
71  
72      /**
73       * Cleans a collection group instance removing the items and collection prototypes (note add line fields
74       * are keep around to support the add line action)
75       *
76       * @param collectionGroup - collection group instance to clean
77       */
78      public static void cleanCollectionGroup(CollectionGroup collectionGroup) {
79          collectionGroup.setAddLineLabel(null);
80          collectionGroup.setAddLineActions(new ArrayList<Action>());
81          collectionGroup.setLineActions(new ArrayList<Action>());
82          collectionGroup.setSubCollections(new ArrayList<CollectionGroup>());
83          collectionGroup.setActiveCollectionFilter(null);
84          collectionGroup.setFilters(new ArrayList<CollectionFilter>());
85  
86          cleanContainer(collectionGroup);
87      }
88  
89      /**
90       * General purpose method to clean any container, removes all nested components except the items list
91       *
92       * @param container - container instance to clean
93       */
94      public static void cleanContainer(Container container) {
95          container.setHeader(null);
96          container.setFooter(null);
97          container.setHelp(null);
98          container.setLayoutManager(null);
99          container.setInstructionalMessage(null);
100         container.setTemplateOptions(new HashMap<String, String>());
101         container.setComponentModifiers(new ArrayList<ComponentModifier>());
102         container.setPropertyReplacers(new ArrayList<PropertyReplacer>());
103     }
104 
105     /**
106      * Cleans an input field instance removing the control and inherited component properties
107      *
108      * @param inputField - input field instance to clean
109      */
110     public static void cleanInputField(InputField inputField) {
111         inputField.setControl(null);
112         inputField.setInstructionalMessage(null);
113         inputField.setConstraintMessage(null);
114         inputField.setInquiry(null);
115         inputField.setLabel(null);
116         inputField.setTemplateOptions(new HashMap<String, String>());
117         inputField.setComponentModifiers(new ArrayList<ComponentModifier>());
118         inputField.setPropertyReplacers(new ArrayList<PropertyReplacer>());
119     }
120 }