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