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