001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.krad.uif.util;
017    
018    import org.kuali.rice.krad.uif.component.Component;
019    import org.kuali.rice.krad.uif.component.PropertyReplacer;
020    import org.kuali.rice.krad.uif.container.CollectionFilter;
021    import org.kuali.rice.krad.uif.container.CollectionGroup;
022    import org.kuali.rice.krad.uif.container.Container;
023    import org.kuali.rice.krad.uif.container.PageGroup;
024    import org.kuali.rice.krad.uif.element.Action;
025    import org.kuali.rice.krad.uif.field.InputField;
026    import org.kuali.rice.krad.uif.modifier.ComponentModifier;
027    import org.kuali.rice.krad.uif.view.View;
028    
029    import java.util.ArrayList;
030    import java.util.HashMap;
031    import 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     */
044    public 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.setViewTemplates(new ArrayList<String>());
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.setAddLineLabel(null);
080            collectionGroup.setAddLineActions(new ArrayList<Action>());
081            collectionGroup.setLineActions(new ArrayList<Action>());
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.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    }