1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.form;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.CoreApiServiceLocator;
20 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
22 import org.kuali.rice.krad.uif.util.SessionTransient;
23 import org.kuali.rice.krad.util.ObjectUtils;
24
25 import java.io.Serializable;
26 import java.lang.annotation.Annotation;
27 import java.lang.reflect.Field;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.Vector;
35
36
37
38
39
40
41 public class UifFormManager implements Serializable {
42 private static final long serialVersionUID = -6323378881342207080L;
43
44 private int maxNumberOfSessionForms = 5;
45
46 private Vector accessedFormKeys;
47
48 private Map<String, UifFormBase> sessionForms;
49
50
51
52
53 public UifFormManager() {
54 this.accessedFormKeys = new Vector();
55 this.sessionForms = new HashMap<String, UifFormBase>();
56
57 String maxNumberOfSessionFormsConfig =
58 CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString("maxNumberOfSessionForms");
59 if (StringUtils.isNotBlank(maxNumberOfSessionFormsConfig)) {
60 maxNumberOfSessionForms = Integer.parseInt(maxNumberOfSessionFormsConfig);
61 }
62 }
63
64
65
66
67
68
69 public synchronized void addSessionForm(UifFormBase form) {
70 if (form == null || StringUtils.isBlank(form.getFormKey())) {
71 throw new RiceIllegalArgumentException("Form or form key was null");
72 }
73
74 sessionForms.put(form.getFormKey(), form);
75
76
77 if (accessedFormKeys.contains(form.getFormKey())) {
78 accessedFormKeys.removeElement(form.getFormKey());
79 }
80 accessedFormKeys.add(form.getFormKey());
81
82
83 if (sessionForms.size() > maxNumberOfSessionForms) {
84
85
86 Set<String> formKeys = new HashSet<String>(sessionForms.keySet());
87 for (String formKey : formKeys) {
88 UifFormBase sessionForm = sessionForms.get(formKey);
89 if ((sessionForm instanceof InquiryForm) && (!formKey.equals(form.getFormKey()))) {
90 sessionForms.remove(formKey);
91 accessedFormKeys.remove(formKey);
92 }
93 }
94 }
95
96
97 if (sessionForms.size() > maxNumberOfSessionForms) {
98
99 String removeFormKey = (String) accessedFormKeys.get(0);
100 if (sessionForms.containsKey(removeFormKey)) {
101 sessionForms.remove(removeFormKey);
102 }
103 accessedFormKeys.removeElementAt(0);
104 }
105 }
106
107
108
109
110
111
112
113 public UifFormBase getSessionForm(String formKey) {
114 if (sessionForms.containsKey(formKey)) {
115 return sessionForms.get(formKey);
116 }
117
118 return null;
119 }
120
121
122
123
124
125
126 public void removeSessionForm(UifFormBase form) {
127 if (form == null || StringUtils.isBlank(form.getFormKey())) {
128 return;
129 }
130
131 removeSessionFormByKey(form.getFormKey());
132 }
133
134
135
136
137
138
139 public void removeFormWithHistoryFormsByKey(String formKey) {
140 if (sessionForms.containsKey(formKey)) {
141 removeSessionFormByKey(formKey);
142 }
143 }
144
145
146
147
148
149
150 public void removeSessionFormByKey(String formKey) {
151 if (accessedFormKeys.contains(formKey)) {
152 accessedFormKeys.removeElement(formKey);
153 }
154
155 if (sessionForms.containsKey(formKey)) {
156 sessionForms.remove(formKey);
157 }
158 }
159
160
161
162
163
164
165
166 public boolean hasSessionForm(String formKey) {
167 return sessionForms.containsKey(formKey);
168 }
169
170
171
172
173
174
175
176
177 public void updateFormWithSession(UifFormBase requestForm, String formKey) {
178 UifFormBase sessionForm = sessionForms.get(formKey);
179 if (sessionForm == null) {
180 return;
181 }
182
183 List<Field> fields = new ArrayList<Field>();
184 fields = ObjectUtils.getAllFields(fields, sessionForm.getClass(), UifFormBase.class);
185 for (Field field : fields) {
186 boolean copyValue = true;
187 for (Annotation an : field.getAnnotations()) {
188 if (an instanceof SessionTransient) {
189 copyValue = false;
190 }
191 }
192
193 if (copyValue && ObjectPropertyUtils.isReadableProperty(sessionForm, field.getName()) && ObjectPropertyUtils
194 .isWritableProperty(sessionForm, field.getName())) {
195 Object fieldValue = ObjectPropertyUtils.getPropertyValue(sessionForm, field.getName());
196 ObjectPropertyUtils.setPropertyValue(requestForm, field.getName(), fieldValue);
197 }
198 }
199 }
200
201
202
203
204
205
206 public void purgeForm(UifFormBase form) {
207 List<Field> fields = new ArrayList<Field>();
208 fields = ObjectUtils.getAllFields(fields, form.getClass(), UifFormBase.class);
209 for (Field field : fields) {
210 boolean purgeValue = false;
211
212 if (!field.getType().isPrimitive()) {
213 for (Annotation an : field.getAnnotations()) {
214 if (an instanceof SessionTransient) {
215 purgeValue = true;
216 }
217 }
218 }
219
220 if (purgeValue && ObjectPropertyUtils.isWritableProperty(form, field.getName())) {
221 ObjectPropertyUtils.setPropertyValue(form, field.getName(), null);
222 }
223 }
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237 protected Vector getAccessedFormKeys() {
238 return accessedFormKeys;
239 }
240
241
242
243
244
245
246 public int getMaxNumberOfSessionForms() {
247 return maxNumberOfSessionForms;
248 }
249
250
251
252
253
254
255 public void setMaxNumberOfSessionForms(int maxNumberOfSessionForms) {
256 this.maxNumberOfSessionForms = maxNumberOfSessionForms;
257 }
258
259 }