View Javadoc

1   /**
2    * Copyright 2005-2013 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          view.setViewTemplates(new ArrayList<String>());
60  
61          // clear all view pages exception the current page
62          PageGroup currentPage = view.getCurrentPage();
63  
64          List<Component> pages = new ArrayList<Component>();
65          pages.add(currentPage);
66          view.setItems(pages);
67  
68          cleanContainer(view);
69  
70          view.getViewIndex().clearIndexesAfterRender();
71      }
72  
73      /**
74       * Cleans a collection group instance removing the items and collection prototypes (note add line fields
75       * are keep around to support the add line action)
76       *
77       * @param collectionGroup - collection group instance to clean
78       */
79      public static void cleanCollectionGroup(CollectionGroup collectionGroup) {
80          collectionGroup.setAddLineLabel(null);
81          collectionGroup.setAddLineActions(new ArrayList<Action>());
82          collectionGroup.setLineActions(new ArrayList<Action>());
83          collectionGroup.setSubCollections(new ArrayList<CollectionGroup>());
84          collectionGroup.setActiveCollectionFilter(null);
85          collectionGroup.setFilters(new ArrayList<CollectionFilter>());
86  
87          cleanContainer(collectionGroup);
88      }
89  
90      /**
91       * General purpose method to clean any container, removes all nested components except the items list
92       *
93       * @param container - container instance to clean
94       */
95      public static void cleanContainer(Container container) {
96          container.setHeader(null);
97          container.setFooter(null);
98          container.setHelp(null);
99          container.setLayoutManager(null);
100         container.setInstructionalMessage(null);
101         container.setTemplateOptions(new HashMap<String, String>());
102         container.setComponentModifiers(new ArrayList<ComponentModifier>());
103         container.setPropertyReplacers(new ArrayList<PropertyReplacer>());
104     }
105 
106     /**
107      * Cleans an input field instance removing the control and inherited component properties
108      *
109      * @param inputField - input field instance to clean
110      */
111     public static void cleanInputField(InputField inputField) {
112         inputField.setControl(null);
113         inputField.setInstructionalMessage(null);
114         inputField.setConstraintMessage(null);
115         inputField.setInquiry(null);
116         inputField.setLabel(null);
117         inputField.setTemplateOptions(new HashMap<String, String>());
118         inputField.setComponentModifiers(new ArrayList<ComponentModifier>());
119         inputField.setPropertyReplacers(new ArrayList<PropertyReplacer>());
120     }
121 }