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