001    /*
002     * Copyright 2007-2008 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.kns.util;
017    
018    import java.util.Map;
019    
020    /**
021     * Inquiry screens and maintenance documents may render a collection of BOs on a screen.  These
022     * BOs may implement {@link org.kuali.rice.kns.bo.Inactivateable}, which means that the BO has an active
023     * flag of true or false.  Some screens may give the user the ability to not render (i.e. hide) inactive
024     * collection elements.  This class has several utilities to control that behavior. 
025     * 
026     * @author Kuali Rice Team (rice.collab@kuali.org)
027     */
028    public class InactiveRecordsHidingUtils {
029        /**
030         * Returns whether a collection has been set to show inactive records.  Note that if a collection has not been set to show inactive inactive records, then
031         * this method will return false.
032         * 
033         * @param inactiveRecordDisplay a Map used to keep state between invocations of this method and {@link #setShowInactiveRecords(Map, String, boolean)}
034         * @param collectionName the name of the collection 
035         * @return
036         */
037        public static boolean getShowInactiveRecords(Map<String, Boolean> inactiveRecordDisplay, String collectionName) {
038            // by default, show the actives
039            boolean showInactive = true;
040            
041            if (collectionName == null) {
042                throw new IllegalArgumentException("collection name cannot be null");
043            }
044            // remove periods from the collection name due to parsing limitation in Apache beanutils 
045            collectionName = collectionName.replace( '.', '_' );
046            
047            if (inactiveRecordDisplay.containsKey(collectionName)) {
048                Object inactiveSetting = inactiveRecordDisplay.get(collectionName);
049                
050                // warren: i copied this code from somewhere else, and have no idea why they're testing to see whether it's a
051                // Boolean, but I'm guessing that it has to do w/ the PojoFormBase not setting things correctly
052                if (inactiveSetting instanceof Boolean) {
053                    showInactive = ((Boolean) inactiveSetting).booleanValue();
054                }
055                else {
056                    showInactive = Boolean.parseBoolean(((String[]) inactiveSetting)[0]);
057                }
058            }
059            
060            return showInactive;
061        }
062        
063        /**
064         * Sets whether a method should show inactive records
065         * 
066         * @param inactiveRecordDisplay a Map used to keep state between invocations of this method and {@link #getShowInactiveRecords(Map, String)}
067         * @param collectionName the name of the collection
068         * @param showInactive whether to show inactive records
069         */
070        public static void setShowInactiveRecords(Map<String, Boolean> inactiveRecordDisplay, String collectionName, boolean showInactive) {
071            if (collectionName == null) {
072                throw new IllegalArgumentException("collection name cannot be null");
073            }
074            
075            // remove periods from the collection name due to parsing limitation in Apache beanutils 
076            collectionName = collectionName.replace( '.', '_' );
077    
078            inactiveRecordDisplay.put(collectionName, new Boolean(showInactive));
079        }
080        
081        public static String formatCollectionName(String collectionName) {
082            return collectionName.replace( '.', '_' );
083        }
084    }