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