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  import java.util.Collections;
29  
30  
31  /**
32   * A class which will hold a Map of editable properties, dropping editable properties when too many
33   * are filled in. 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
38   */
39  @Deprecated
40  public class EditablePropertiesHistoryHolder implements java.io.Serializable {
41  	private Map<String, Set<String>> editablePropertiesMap;
42  	private Integer maxLength = null;
43  	private Queue<String> historyOrder;
44  	private static final String EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME = "kns.editable.properties.history.size";
45  	private transient ConfigurationService configurationService;
46  	
47  	/**
48  	 * Constructs the EditablePropertiesHistoryHolder
49  	 *
50  	 */
51  	public EditablePropertiesHistoryHolder() {
52          editablePropertiesMap = Collections.synchronizedMap(new HashMap<String, Set<String>>());
53  		historyOrder = new LinkedList<String>();
54  	}
55  	
56  	/**
57  	 * @return the maximum length of the history that this will hold
58  	 */
59  	public int getMaxHistoryLength() {
60  		if (maxLength == null) {
61  			final String historyLengthAsString = getConfigurationService().getPropertyValueAsString(
62                      EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME);
63  			if (historyLengthAsString == null) {
64  				maxLength = new Integer(20);
65  			} else {
66  				try {
67  					maxLength = new Integer(historyLengthAsString);
68  				} catch (NumberFormatException nfe) {
69  					throw new RuntimeException("Cannot convert property "+EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME+" with value "+historyLengthAsString+" to integer", nfe);
70  				}
71  			}
72  		}
73  		return maxLength.intValue();
74  	}
75  	
76  	/**
77  	 * Adds a Set of editable property names to the history, keyed with the given guid String.  If the editable properties exceeds the buffer size,
78  	 * the earliest editable properties will be bumped
79  	 * @param editableProperties the Set of editable property names to save in the history
80  	 * @return a String to act as a key (or guid) to the editable properties
81  	 */
82  	public String addEditablePropertiesToHistory(Set<String> editableProperties) {
83  		String guid = generateNewGuid();
84  		
85  		if (getHistoryOrder().size() > getMaxHistoryLength()) {
86  			final String guidForRemoval = getHistoryOrder().remove();
87  			getEditablePropertiesMap().remove(guidForRemoval);
88  		}
89  		getHistoryOrder().add(guid);
90  		getEditablePropertiesMap().put(guid, editableProperties);
91  		
92  		return guid;
93  	}
94  	
95  	/**
96  	 * 
97  	 * @return a newly generated Guid to act as a key to an editable properties Set
98  	 */
99  	public String generateNewGuid() {
100 		final String guid = UUID.randomUUID().toString();
101 		return guid;
102 	}
103 	
104 	/**
105 	 * Returns the editable properties registered with the current guid
106 	 * @param guid the guid to find editable properties for
107 	 * @return a Set<String> of editable properties
108 	 */
109 	public Set<String> getEditableProperties(String guid) {
110 		return getEditablePropertiesMap().get(guid);
111 	}
112 	
113 	/**
114 	 * Clears out the editable properties associated with the given guid
115 	 * @param guid the guid to clear out editable properties for
116 	 */
117 	public void clearEditableProperties(String guid) {
118 		getEditablePropertiesMap().put(guid, createNewEditablePropertiesEntry());
119 	}
120 	
121 	/**
122 	 * @return the order of the entries as they chronologically were created
123 	 */
124 	protected Queue<String> getHistoryOrder() {
125 		return historyOrder;
126 	}
127 	
128 	/**
129 	 * @return the Map which associates editable property guids with Sets of editable property names
130 	 */
131 	protected Map<String, Set<String>> getEditablePropertiesMap() {
132 		return editablePropertiesMap;
133 	}
134 	
135 	/**
136 	 * @return a new Entry to hold the names of editable properties
137 	 */
138 	protected Set<String> createNewEditablePropertiesEntry() {
139 		return new HashSet<String>();
140 	}
141 	
142 	/**
143 	 * @return an implementation of the ConfigurationService
144 	 */
145 	protected ConfigurationService getConfigurationService() {
146 		if (configurationService == null) {
147 			configurationService = CoreApiServiceLocator.getKualiConfigurationService();
148 		}
149 		return configurationService;
150 	}
151 }