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 if (!sessionForm.getClass().isAssignableFrom(requestForm.getClass())) {
184 throw new RuntimeException(
185 "Session form mismatch, session form class not assignable from request form class");
186 }
187
188 List<Field> fields = new ArrayList<Field>();
189 fields = ObjectUtils.getAllFields(fields, sessionForm.getClass(), UifFormBase.class);
190 for (Field field : fields) {
191 boolean copyValue = true;
192 for (Annotation an : field.getAnnotations()) {
193 if (an instanceof SessionTransient) {
194 copyValue = false;
195 }
196 }
197
198 if (copyValue && ObjectPropertyUtils.isReadableProperty(sessionForm, field.getName()) && ObjectPropertyUtils
199 .isWritableProperty(sessionForm, field.getName())) {
200 Object fieldValue = ObjectPropertyUtils.getPropertyValue(sessionForm, field.getName());
201 ObjectPropertyUtils.setPropertyValue(requestForm, field.getName(), fieldValue);
202 }
203 }
204 }
205
206
207
208
209
210
211 public void purgeForm(UifFormBase form) {
212 List<Field> fields = new ArrayList<Field>();
213 fields = ObjectUtils.getAllFields(fields, form.getClass(), UifFormBase.class);
214 for (Field field : fields) {
215 boolean purgeValue = false;
216
217 if (!field.getType().isPrimitive()) {
218 for (Annotation an : field.getAnnotations()) {
219 if (an instanceof SessionTransient) {
220 purgeValue = true;
221 }
222 }
223 }
224
225 if (purgeValue && ObjectPropertyUtils.isWritableProperty(form, field.getName())) {
226 ObjectPropertyUtils.setPropertyValue(form, field.getName(), null);
227 }
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 protected Vector getAccessedFormKeys() {
243 return accessedFormKeys;
244 }
245
246
247
248
249
250
251 public int getMaxNumberOfSessionForms() {
252 return maxNumberOfSessionForms;
253 }
254
255
256
257
258
259
260 public void setMaxNumberOfSessionForms(int maxNumberOfSessionForms) {
261 this.maxNumberOfSessionForms = maxNumberOfSessionForms;
262 }
263
264 }