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