View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.web;
17  
18  import org.kuali.rice.core.api.CoreApiServiceLocator;
19  import org.kuali.rice.core.api.config.property.ConfigurationService;
20  
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.LinkedList;
24  import java.util.Map;
25  import java.util.Queue;
26  import java.util.Set;
27  import java.util.UUID;
28  
29  
30  /**
31   * A class which will hold a Map of editable properties, dropping editable properties when too many
32   * are filled in. 
33   * 
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   *
36   */
37  @Deprecated
38  public class EditablePropertiesHistoryHolder implements java.io.Serializable {
39  	private Map<String, Set<String>> editablePropertiesMap;
40  	private Integer maxLength = null;
41  	private Queue<String> historyOrder;
42  	private static final String EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME = "kns.editable.properties.history.size";
43  	private transient ConfigurationService configurationService;
44  	
45  	/**
46  	 * Constructs the EditablePropertiesHistoryHolder
47  	 *
48  	 */
49  	public EditablePropertiesHistoryHolder() {
50  		editablePropertiesMap = new HashMap<String, Set<String>>();
51  		historyOrder = new LinkedList<String>();
52  	}
53  	
54  	/**
55  	 * @return the maximum length of the history that this will hold
56  	 */
57  	public int getMaxHistoryLength() {
58  		if (maxLength == null) {
59  			final String historyLengthAsString = getConfigurationService().getPropertyValueAsString(
60                      EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME);
61  			if (historyLengthAsString == null) {
62  				maxLength = new Integer(20);
63  			} else {
64  				try {
65  					maxLength = new Integer(historyLengthAsString);
66  				} catch (NumberFormatException nfe) {
67  					throw new RuntimeException("Cannot convert property "+EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME+" with value "+historyLengthAsString+" to integer", nfe);
68  				}
69  			}
70  		}
71  		return maxLength.intValue();
72  	}
73  	
74  	/**
75  	 * Adds a Set of editable property names to the history, keyed with the given guid String.  If the editable properties exceeds the buffer size,
76  	 * the earliest editable properties will be bumped
77  	 * @param editableProperties the Set of editable property names to save in the history
78  	 * @return a String to act as a key (or guid) to the editable properties
79  	 */
80  	public String addEditablePropertiesToHistory(Set<String> editableProperties) {
81  		String guid = generateNewGuid();
82  		
83  		if (getHistoryOrder().size() > getMaxHistoryLength()) {
84  			final String guidForRemoval = getHistoryOrder().remove();
85  			getEditablePropertiesMap().remove(guidForRemoval);
86  		}
87  		getHistoryOrder().add(guid);
88  		getEditablePropertiesMap().put(guid, editableProperties);
89  		
90  		return guid;
91  	}
92  	
93  	/**
94  	 * 
95  	 * @return a newly generated Guid to act as a key to an editable properties Set
96  	 */
97  	public String generateNewGuid() {
98  		final String guid = UUID.randomUUID().toString();
99  		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 }