001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.util;
017
018import org.kuali.rice.krad.uif.component.Component;
019import org.kuali.rice.krad.uif.component.PropertyReplacer;
020import org.kuali.rice.krad.uif.container.CollectionFilter;
021import org.kuali.rice.krad.uif.container.CollectionGroup;
022import org.kuali.rice.krad.uif.container.Container;
023import org.kuali.rice.krad.uif.container.PageGroup;
024import org.kuali.rice.krad.uif.field.ActionField;
025import org.kuali.rice.krad.uif.field.InputField;
026import org.kuali.rice.krad.uif.modifier.ComponentModifier;
027import org.kuali.rice.krad.uif.view.View;
028
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.List;
032
033/**
034 * Utility class for trimming component instances for storage
035 *
036 * <p>
037 * Invoked to trim the view instance before storing on the form as the post view. Certain information is keep
038 * around to support post methods that need to operate on the previous view configuration. Examples include component
039 * refresh and collection add/delete line.
040 * </p>
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044public class ViewCleaner {
045
046    /**
047     * Cleans a view instance removing all pages except the current page and then invoking the view
048     * index to perform cleaning on contained components
049     *
050     * @param view - view instance to clean
051     */
052    public static void cleanView(View view) {
053        view.setApplicationHeader(null);
054        view.setApplicationFooter(null);
055        view.setNavigation(null);
056        view.setPage(null);
057        view.setViewMenuLink(null);
058        view.setClientSideState(new HashMap<String, Object>());
059
060        // clear all view pages exception the current page
061        PageGroup currentPage = view.getCurrentPage();
062
063        List<Component> pages = new ArrayList<Component>();
064        pages.add(currentPage);
065        view.setItems(pages);
066
067        cleanContainer(view);
068
069        view.getViewIndex().clearIndexesAfterRender();
070    }
071
072    /**
073     * Cleans a collection group instance removing the items and collection prototypes (note add line fields
074     * are keep around to support the add line action)
075     *
076     * @param collectionGroup - collection group instance to clean
077     */
078    public static void cleanCollectionGroup(CollectionGroup collectionGroup) {
079        collectionGroup.setAddLineLabelField(null);
080        collectionGroup.setAddLineActionFields(new ArrayList<ActionField>());
081        collectionGroup.setActionFields(new ArrayList<ActionField>());
082        collectionGroup.setSubCollections(new ArrayList<CollectionGroup>());
083        collectionGroup.setActiveCollectionFilter(null);
084        collectionGroup.setFilters(new ArrayList<CollectionFilter>());
085
086        cleanContainer(collectionGroup);
087    }
088
089    /**
090     * General purpose method to clean any container, removes all nested components except the items list
091     *
092     * @param container - container instance to clean
093     */
094    public static void cleanContainer(Container container) {
095        container.setHeader(null);
096        container.setFooter(null);
097        container.setHelp(null);
098        container.setLayoutManager(null);
099        container.setInstructionalMessageField(null);
100        container.setComponentOptions(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.setInstructionalMessageField(null);
113        inputField.setConstraintMessageField(null);
114        inputField.setFieldDirectInquiry(null);
115        inputField.setFieldInquiry(null);
116        inputField.setLabelField(null);
117        inputField.setComponentOptions(new HashMap<String, String>());
118        inputField.setComponentModifiers(new ArrayList<ComponentModifier>());
119        inputField.setPropertyReplacers(new ArrayList<PropertyReplacer>());
120    }
121}