1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36 public class EditablePropertiesHistoryHolder implements java.io.Serializable {
37 private Map<String, Set<String>> editablePropertiesMap;
38 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
45
46
47 public EditablePropertiesHistoryHolder() {
48 editablePropertiesMap = new HashMap<String, Set<String>>();
49 historyOrder = new LinkedList<String>();
50 }
51
52
53
54
55 public int getMaxHistoryLength() {
56 if (maxLength == null) {
57 final String historyLengthAsString = getConfigurationService().getPropertyString(EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME);
58 if (historyLengthAsString == null) {
59 maxLength = new Integer(20);
60 } else {
61 try {
62 maxLength = new Integer(historyLengthAsString);
63 } catch (NumberFormatException nfe) {
64 throw new RuntimeException("Cannot convert property "+EditablePropertiesHistoryHolder.EDITABLE_PROPERTIES_HISTORY_SIZE_PROPERTY_NAME+" with value "+historyLengthAsString+" to integer", nfe);
65 }
66 }
67 }
68 return maxLength.intValue();
69 }
70
71
72
73
74
75
76
77 public String addEditablePropertiesToHistory(Set<String> editableProperties) {
78 String guid = generateNewGuid();
79
80 if (getHistoryOrder().size() > getMaxHistoryLength()) {
81 final String guidForRemoval = getHistoryOrder().remove();
82 getEditablePropertiesMap().remove(guidForRemoval);
83 }
84 getHistoryOrder().add(guid);
85 getEditablePropertiesMap().put(guid, editableProperties);
86
87 return guid;
88 }
89
90
91
92
93
94 public String generateNewGuid() {
95 final String guid = new Guid().toString();
96 return guid;
97 }
98
99
100
101
102
103
104 public Set<String> getEditableProperties(String guid) {
105 return getEditablePropertiesMap().get(guid);
106 }
107
108
109
110
111
112 public void clearEditableProperties(String guid) {
113 getEditablePropertiesMap().put(guid, createNewEditablePropertiesEntry());
114 }
115
116
117
118
119 protected Queue<String> getHistoryOrder() {
120 return historyOrder;
121 }
122
123
124
125
126 protected Map<String, Set<String>> getEditablePropertiesMap() {
127 return editablePropertiesMap;
128 }
129
130
131
132
133 protected Set<String> createNewEditablePropertiesEntry() {
134 return new HashSet<String>();
135 }
136
137
138
139
140 protected KualiConfigurationService getConfigurationService() {
141 if (configurationService == null) {
142 configurationService = KNSServiceLocator.getKualiConfigurationService();
143 }
144 return configurationService;
145 }
146 }