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.web;
017
018import org.kuali.rice.core.api.CoreApiServiceLocator;
019import org.kuali.rice.core.api.config.property.ConfigurationService;
020
021import java.util.HashMap;
022import java.util.HashSet;
023import java.util.LinkedList;
024import java.util.Map;
025import java.util.Queue;
026import java.util.Set;
027import java.util.UUID;
028
029
030/**
031 * A class which will hold a Map of editable properties, dropping editable properties when too many
032 * are filled in. 
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 *
036 */
037@Deprecated
038public class EditablePropertiesHistoryHolder implements java.io.Serializable {
039        private Map<String, Set<String>> editablePropertiesMap;
040        private Integer maxLength = null;
041        private Queue<String> historyOrder;
042        private static final String EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME = "kns.editable.properties.history.size";
043        private transient ConfigurationService configurationService;
044        
045        /**
046         * Constructs the EditablePropertiesHistoryHolder
047         *
048         */
049        public EditablePropertiesHistoryHolder() {
050                editablePropertiesMap = new HashMap<String, Set<String>>();
051                historyOrder = new LinkedList<String>();
052        }
053        
054        /**
055         * @return the maximum length of the history that this will hold
056         */
057        public int getMaxHistoryLength() {
058                if (maxLength == null) {
059                        final String historyLengthAsString = getConfigurationService().getPropertyValueAsString(
060                    EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME);
061                        if (historyLengthAsString == null) {
062                                maxLength = new Integer(20);
063                        } else {
064                                try {
065                                        maxLength = new Integer(historyLengthAsString);
066                                } catch (NumberFormatException nfe) {
067                                        throw new RuntimeException("Cannot convert property "+EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME+" with value "+historyLengthAsString+" to integer", nfe);
068                                }
069                        }
070                }
071                return maxLength.intValue();
072        }
073        
074        /**
075         * Adds a Set of editable property names to the history, keyed with the given guid String.  If the editable properties exceeds the buffer size,
076         * the earliest editable properties will be bumped
077         * @param editableProperties the Set of editable property names to save in the history
078         * @return a String to act as a key (or guid) to the editable properties
079         */
080        public String addEditablePropertiesToHistory(Set<String> editableProperties) {
081                String guid = generateNewGuid();
082                
083                if (getHistoryOrder().size() > getMaxHistoryLength()) {
084                        final String guidForRemoval = getHistoryOrder().remove();
085                        getEditablePropertiesMap().remove(guidForRemoval);
086                }
087                getHistoryOrder().add(guid);
088                getEditablePropertiesMap().put(guid, editableProperties);
089                
090                return guid;
091        }
092        
093        /**
094         * 
095         * @return a newly generated Guid to act as a key to an editable properties Set
096         */
097        public String generateNewGuid() {
098                final String guid = UUID.randomUUID().toString();
099                return guid;
100        }
101        
102        /**
103         * Returns the editable properties registered with the current guid
104         * @param guid the guid to find editable properties for
105         * @return a Set<String> of editable properties
106         */
107        public Set<String> getEditableProperties(String guid) {
108                return getEditablePropertiesMap().get(guid);
109        }
110        
111        /**
112         * Clears out the editable properties associated with the given guid
113         * @param guid the guid to clear out editable properties for
114         */
115        public void clearEditableProperties(String guid) {
116                getEditablePropertiesMap().put(guid, createNewEditablePropertiesEntry());
117        }
118        
119        /**
120         * @return the order of the entries as they chronologically were created
121         */
122        protected Queue<String> getHistoryOrder() {
123                return historyOrder;
124        }
125        
126        /**
127         * @return the Map which associates editable property guids with Sets of editable property names
128         */
129        protected Map<String, Set<String>> getEditablePropertiesMap() {
130                return editablePropertiesMap;
131        }
132        
133        /**
134         * @return a new Entry to hold the names of editable properties
135         */
136        protected Set<String> createNewEditablePropertiesEntry() {
137                return new HashSet<String>();
138        }
139        
140        /**
141         * @return an implementation of the ConfigurationService
142         */
143        protected ConfigurationService getConfigurationService() {
144                if (configurationService == null) {
145                        configurationService = CoreApiServiceLocator.getKualiConfigurationService();
146                }
147                return configurationService;
148        }
149}