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.kns.util;
017
018import 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.core.api.mo.common.active.MutableInactivatable}, 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 * @deprecated Only used in KNS classes, use KRAD.
029 */
030@Deprecated
031public final class InactiveRecordsHidingUtils {
032        
033        private InactiveRecordsHidingUtils() {
034                throw new UnsupportedOperationException("do not call");
035        }
036        
037    /**
038     * 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
039     * this method will return false.
040     * 
041     * @param inactiveRecordDisplay a Map used to keep state between invocations of this method and {@link #setShowInactiveRecords(Map, String, boolean)}
042     * @param collectionName the name of the collection 
043     * @return
044     */
045    public static boolean getShowInactiveRecords(Map<String, Boolean> inactiveRecordDisplay, String collectionName) {
046        // by default, show the actives
047        boolean showInactive = true;
048        
049        if (collectionName == null) {
050            throw new IllegalArgumentException("collection name cannot be null");
051        }
052        // remove periods from the collection name due to parsing limitation in Apache beanutils 
053        collectionName = collectionName.replace( '.', '_' );
054        
055        if (inactiveRecordDisplay.containsKey(collectionName)) {
056            Object inactiveSetting = inactiveRecordDisplay.get(collectionName);
057            
058            // warren: i copied this code from somewhere else, and have no idea why they're testing to see whether it's a
059            // Boolean, but I'm guessing that it has to do w/ the PojoFormBase not setting things correctly
060            if (inactiveSetting instanceof Boolean) {
061                showInactive = ((Boolean) inactiveSetting).booleanValue();
062            }
063            else {
064                showInactive = Boolean.parseBoolean(((String[]) inactiveSetting)[0]);
065            }
066        }
067        
068        return showInactive;
069    }
070    
071    /**
072     * Sets whether a method should show inactive records
073     * 
074     * @param inactiveRecordDisplay a Map used to keep state between invocations of this method and {@link #getShowInactiveRecords(Map, String)}
075     * @param collectionName the name of the collection
076     * @param showInactive whether to show inactive records
077     */
078    public static void setShowInactiveRecords(Map<String, Boolean> inactiveRecordDisplay, String collectionName, boolean showInactive) {
079        if (collectionName == null) {
080            throw new IllegalArgumentException("collection name cannot be null");
081        }
082        
083        // remove periods from the collection name due to parsing limitation in Apache beanutils 
084        collectionName = collectionName.replace( '.', '_' );
085
086        inactiveRecordDisplay.put(collectionName, new Boolean(showInactive));
087    }
088    
089    public static String formatCollectionName(String collectionName) {
090        return collectionName.replace( '.', '_' );
091    }
092}