View Javadoc
1   /**
2    * Copyright 2005-2016 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.container.CollectionGroup;
20  import org.kuali.rice.krad.uif.container.Container;
21  import org.kuali.rice.krad.uif.container.Group;
22  import org.kuali.rice.krad.uif.container.PageGroup;
23  import org.kuali.rice.krad.uif.field.DataField;
24  import org.kuali.rice.krad.uif.field.FieldGroup;
25  import org.kuali.rice.krad.uif.field.InputField;
26  import org.kuali.rice.krad.uif.layout.StackedLayoutManager;
27  import org.kuali.rice.krad.uif.layout.TableLayoutManager;
28  import org.kuali.rice.krad.uif.view.View;
29  import org.kuali.rice.krad.uif.view.ViewIndex;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * Utility class for trimming component instances for storage
36   *
37   * <p>
38   * Invoked to trim the view instance before storing on the form as the post view. Certain information is keep
39   * around to support post methods that need to operate on the previous view configuration. Examples include component
40   * refresh and collection add/delete line.
41   * </p>
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class ViewCleaner {
46  
47      /**
48       * Cleans a view instance removing all pages except the current page and then invoking the view
49       * index to perform cleaning on contained components
50       *
51       * @param view view instance to clean
52       */
53      public static void cleanView(View view) {
54          view.setApplicationHeader(null);
55          view.setApplicationFooter(null);
56          view.setBreadcrumbs(null);
57          view.setBreadcrumbOptions(null);
58          view.setBreadcrumbItem(null);
59          view.setParentLocation(null);
60          view.setPathBasedBreadcrumbs(null);
61          view.setNavigation(null);
62          view.setPage(null);
63          view.setNavigation(null);
64          view.setAdditionalCssFiles(null);
65          view.setAdditionalScriptFiles(null);
66          view.setActionFlags(null);
67          view.setEditModes(null);
68          view.setViewMenuLink(null);
69          view.setViewMenuLink(null);
70          view.setPreLoadScript(null);
71          view.setViewTemplates(new ArrayList<String>());
72          view.setSessionPolicy(null);
73          view.setViewHelperServiceClass(null);
74  
75          view.getViewIndex().clearIndexesAfterRender();
76  
77          // clear all view pages exception the current page
78          PageGroup currentPage = view.getCurrentPage();
79          cleanComponent(currentPage, view.getViewIndex());
80  
81          cleanComponent(view, view.getViewIndex());
82  
83          List<Component> pages = new ArrayList<Component>();
84          pages.add(currentPage);
85  
86          view.setItems(pages);
87      }
88  
89      /**
90       * Cleans a component instance removing properties not needed for posting
91       *
92       * @param component instance to clean
93       */
94      public static void cleanComponent(Component component, ViewIndex viewIndex) {
95          if (component == null) {
96              return;
97          }
98  
99          if (component.isForceSessionPersistence()) {
100             return;
101         }
102 
103         component.setTemplate(null);
104         component.setTemplateName(null);
105         component.setTitle(null);
106         component.setProgressiveRender(null);
107         component.setConditionalRefresh(null);
108         component.setRefreshWhenChangedPropertyNames(null);
109         component.setAdditionalComponentsToRefresh(null);
110         component.setAlign(null);
111         component.setValign(null);
112         component.setWidth(null);
113         component.setCellCssClasses(null);
114         component.setCellStyle(null);
115         component.setCellWidth(null);
116         component.setStyle(null);
117         component.setLibraryCssClasses(null);
118         component.setCssClasses(null);
119         component.setAdditionalCssClasses(null);
120         component.setToolTip(null);
121         component.setRenderedHtmlOutput(null);
122         component.setComponentSecurity(null);
123         component.setOnLoadScript(null);
124         component.setOnUnloadScript(null);
125         component.setOnCloseScript(null);
126         component.setOnBlurScript(null);
127         component.setOnChangeScript(null);
128         component.setOnClickScript(null);
129         component.setOnDblClickScript(null);
130         component.setOnFocusScript(null);
131         component.setOnSubmitScript(null);
132         component.setOnKeyPressScript(null);
133         component.setOnKeyUpScript(null);
134         component.setOnKeyDownScript(null);
135         component.setOnMouseOverScript(null);
136         component.setOnMouseOutScript(null);
137         component.setOnMouseUpScript(null);
138         component.setOnMouseDownScript(null);
139         component.setOnMouseMoveScript(null);
140         component.setOnDocumentReadyScript(null);
141         component.setComponentModifiers(null);
142         component.setTemplateOptions(null);
143         component.setTemplateOptionsJSString(null);
144         component.setPropertyReplacers(null);
145 
146         if (!viewIndex.isIdForRefreshComponent(component.getId())) {
147             component.setDataAttributes(null);
148         }
149 
150         component.setPreRenderContent(null);
151         component.setPostRenderContent(null);
152         component.setExpressionGraph(null);
153         component.setPropertyExpressions(null);
154 
155         // keep context for the view, page, and any component that can be refreshed, since they
156         // are needed for that process
157         if (!viewIndex.isIdForRefreshComponent(component.getId()) && !(component instanceof PageGroup)
158                 && !(component instanceof View)) {
159             component.setContext(null);
160         }
161 
162         if (component instanceof Container) {
163             cleanContainer((Container) component, viewIndex);
164         }
165 
166         if (component instanceof DataField) {
167             cleanDataField((DataField) component);
168         }
169 
170         if (component instanceof FieldGroup) {
171             Component group = ((FieldGroup) component).getGroup();
172             cleanComponent(group, viewIndex);
173         }
174     }
175 
176     /**
177      * General purpose method to clean any container, removes all nested components except the items list
178      *
179      * @param container container instance to clean
180      */
181     protected static void cleanContainer(Container container, ViewIndex viewIndex) {
182         container.setHeader(null);
183         container.setFooter(null);
184         container.setHelp(null);
185         container.setInstructionalMessage(null);
186 
187         // keep validation messages for page, since they are refreshed with component refresh
188         if (!(container instanceof PageGroup)) {
189             container.setValidationMessages(null);
190         }
191 
192         if (container.getItems() != null) {
193             for (Component item : container.getItems()) {
194                 cleanComponent(item, viewIndex);
195             }
196         }
197 
198         if (container instanceof Group) {
199             Group group = (Group) container;
200 
201             group.setDisclosure(null);
202             group.setScrollpane(null);
203         }
204 
205         if (container instanceof CollectionGroup) {
206             cleanCollectionGroup((CollectionGroup) container, viewIndex);
207         }
208         else {
209             container.setLayoutManager(null);
210         }
211     }
212 
213     /**
214      * Cleans a collection group instance removing the items and collection prototypes (note add line fields
215      * are keep around to support the add line action)
216      *
217      * @param collectionGroup collection group instance to clean
218      */
219     protected static void cleanCollectionGroup(CollectionGroup collectionGroup, ViewIndex viewIndex) {
220         collectionGroup.setAddLineLabel(null);
221 
222         if (collectionGroup.getAddLineItems() != null) {
223             for (Component item : collectionGroup.getAddLineItems()) {
224                 cleanComponent(item, viewIndex);
225             }
226         }
227 
228         collectionGroup.setAddLineActions(null);
229         collectionGroup.setLineActions(null);
230         collectionGroup.setActiveCollectionFilter(null);
231         collectionGroup.setFilters(null);
232         collectionGroup.setSubCollections(null);
233         collectionGroup.setCollectionGroupBuilder(null);
234         collectionGroup.setNewItemsCssClass(null);
235         collectionGroup.setAddItemCssClass(null);
236         collectionGroup.setAddBlankLineAction(null);
237         collectionGroup.setAddViaLightBoxAction(null);
238 
239         if ((collectionGroup.getLayoutManager() != null) && (collectionGroup
240                 .getLayoutManager() instanceof TableLayoutManager) && !collectionGroup.isUseServerPaging()) {
241 
242             cleanTableLayoutManager((TableLayoutManager) collectionGroup.getLayoutManager());
243         }
244         else if ((collectionGroup.getLayoutManager() != null) && (collectionGroup
245                         .getLayoutManager() instanceof StackedLayoutManager)) {
246              collectionGroup.setLayoutManager(null);
247         }
248     }
249 
250     /**
251      * Cleans a table layout manager instance removing unneeded prototypes
252      *
253      * @param tableLayoutManager table layout instance to clean
254      */
255     protected static void cleanTableLayoutManager(TableLayoutManager tableLayoutManager) {
256         tableLayoutManager.setHeaderLabelPrototype(null);
257         tableLayoutManager.setSequenceFieldPrototype(null);
258         tableLayoutManager.setActionFieldPrototype(null);
259         tableLayoutManager.setSubCollectionFieldGroupPrototype(null);
260         tableLayoutManager.setSelectFieldPrototype(null);
261         tableLayoutManager.setAddLineGroup(null);
262         tableLayoutManager.setRowDetailsGroup(null);
263         tableLayoutManager.setToggleAllDetailsAction(null);
264         tableLayoutManager.setExpandDetailsActionPrototype(null);
265         tableLayoutManager.setGroupingPropertyNames(null);
266         tableLayoutManager.setTotalLabel(null);
267         tableLayoutManager.setPageTotalLabel(null);
268         tableLayoutManager.setGroupTotalLabelPrototype(null);
269         tableLayoutManager.setColumnCalculations(null);
270     }
271 
272     /**
273      * Cleans an data field instance
274      *
275      * @param dataField data field instance to clean
276      */
277     protected static void cleanDataField(DataField dataField) {
278         dataField.setLabel(null);
279         dataField.setFieldLabel(null);
280         dataField.setDictionaryAttributeName(null);
281         dataField.setDictionaryObjectEntry(null);
282         dataField.setForcedValue(null);
283         dataField.setReadOnlyDisplayReplacementPropertyName(null);
284         dataField.setReadOnlyDisplaySuffixPropertyName(null);
285         dataField.setReadOnlyDisplayReplacement(null);
286         dataField.setReadOnlyDisplaySuffix(null);
287         dataField.setReadOnlyListDisplayType(null);
288         dataField.setReadOnlyListDelimiter(null);
289         dataField.setMaskFormatter(null);
290         dataField.setAdditionalHiddenPropertyNames(null);
291         dataField.setPropertyNamesForAdditionalDisplay(null);
292         dataField.setInquiry(null);
293         dataField.setHelp(null);
294 
295         if (dataField instanceof InputField) {
296             cleanInputField((InputField) dataField);
297         }
298     }
299 
300     /**
301      * Cleans an input field instance removing the control and inherited component properties
302      *
303      * @param inputField input field instance to clean
304      */
305     protected static void cleanInputField(InputField inputField) {
306         inputField.setCustomValidatorClass(null);
307         inputField.setValidCharactersConstraint(null);
308         inputField.setCaseConstraint(null);
309         inputField.setDependencyConstraints(null);
310         inputField.setMustOccurConstraints(null);
311         inputField.setControl(null);
312         inputField.setOptionsFinder(null);
313         inputField.setValidationMessages(null);
314         inputField.setConstraintText(null);
315         inputField.setInstructionalText(null);
316         inputField.setInstructionalMessage(null);
317         inputField.setConstraintMessage(null);
318         inputField.setQuickfinder(null);
319     }
320 }