1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.view;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.uif.container.CollectionGroup;
20 import org.kuali.rice.krad.uif.component.Component;
21 import org.kuali.rice.krad.uif.field.DataField;
22 import org.kuali.rice.krad.uif.field.InputField;
23 import org.kuali.rice.krad.uif.util.ComponentUtils;
24 import org.kuali.rice.krad.uif.util.ViewCleaner;
25
26 import java.beans.PropertyEditor;
27 import java.io.Serializable;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
32
33
34
35
36
37
38 public class ViewIndex implements Serializable {
39 private static final long serialVersionUID = 4700818801272201371L;
40
41 private Map<String, Component> index;
42 private Map<String, DataField> dataFieldIndex;
43 private Map<String, CollectionGroup> collectionsIndex;
44
45 private Map<String, Component> initialComponentStates;
46
47 private Map<String, PropertyEditor> fieldPropertyEditors;
48 private Map<String, PropertyEditor> secureFieldPropertyEditors;
49 private Map<String, Integer> idSequenceSnapshot;
50 private Map<String, Map<String, String>> componentExpressionGraphs;
51
52
53
54
55 public ViewIndex() {
56 index = new HashMap<String, Component>();
57 dataFieldIndex = new HashMap<String, DataField>();
58 collectionsIndex = new HashMap<String, CollectionGroup>();
59 initialComponentStates = new HashMap<String, Component>();
60 fieldPropertyEditors = new HashMap<String, PropertyEditor>();
61 secureFieldPropertyEditors = new HashMap<String, PropertyEditor>();
62 idSequenceSnapshot = new HashMap<String, Integer>();
63 componentExpressionGraphs = new HashMap<String, Map<String, String>>();
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 protected void index(View view) {
83 index = new HashMap<String, Component>();
84 dataFieldIndex = new HashMap<String, DataField>();
85 collectionsIndex = new HashMap<String, CollectionGroup>();
86 fieldPropertyEditors = new HashMap<String, PropertyEditor>();
87 secureFieldPropertyEditors = new HashMap<String, PropertyEditor>();
88
89 indexComponent(view);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public void indexComponent(Component component) {
110 if (component == null) {
111 return;
112 }
113
114 index.put(component.getId(), component);
115
116 if (component instanceof DataField) {
117 DataField field = (DataField) component;
118 dataFieldIndex.put(field.getBindingInfo().getBindingPath(), field);
119
120
121 if (component.isRender()) {
122 if (field.hasSecureValue()) {
123 secureFieldPropertyEditors.put(field.getBindingInfo().getBindingPath(), field.getPropertyEditor());
124 } else {
125 fieldPropertyEditors.put(field.getBindingInfo().getBindingPath(), field.getPropertyEditor());
126 }
127 }
128 } else if (component instanceof CollectionGroup) {
129 CollectionGroup collectionGroup = (CollectionGroup) component;
130 collectionsIndex.put(collectionGroup.getBindingInfo().getBindingPath(), collectionGroup);
131 }
132
133 for (Component nestedComponent : component.getComponentsForLifecycle()) {
134 indexComponent(nestedComponent);
135 }
136 }
137
138
139
140
141
142 public void clearIndexesAfterRender() {
143
144 Set<String> holdIds = new HashSet<String>();
145 Set<String> holdFactoryIds = new HashSet<String>();
146 for (Component component : index.values()) {
147 if (component != null) {
148
149 if ((StringUtils.isNotBlank(component.getProgressiveRender()) || StringUtils.isNotBlank(
150 component.getConditionalRefresh()) || component.getRefreshTimer() > 0 ||
151 (component.getRefreshWhenChangedPropertyNames() != null &&
152 !component.getRefreshWhenChangedPropertyNames().isEmpty()) ||
153 component.isRefreshedByAction() || component.isDisclosedByAction()) &&
154 !component.isDisableSessionPersistence()) {
155 holdFactoryIds.add(component.getBaseId());
156 holdIds.add(component.getId());
157 }
158
159 else if (component.isForceSessionPersistence()) {
160 holdFactoryIds.add(component.getBaseId());
161 holdIds.add(component.getId());
162 }
163
164 else if (component instanceof CollectionGroup && !component.isDisableSessionPersistence()) {
165 ViewCleaner.cleanCollectionGroup((CollectionGroup) component);
166 holdFactoryIds.add(component.getBaseId());
167 holdIds.add(component.getId());
168 }
169
170 else if ((component instanceof InputField) && !component.isDisableSessionPersistence()) {
171 InputField inputField = (InputField) component;
172 if ((inputField.getAttributeQuery() != null) || ((inputField.getSuggest() != null) && inputField
173 .getSuggest().isRender())) {
174 holdIds.add(component.getId());
175 }
176 }
177 }
178 }
179
180
181 Map<String, Component> holdInitialComponentStates = new HashMap<String, Component>();
182 for (String factoryId : initialComponentStates.keySet()) {
183 if (holdFactoryIds.contains(factoryId)) {
184 holdInitialComponentStates.put(factoryId, initialComponentStates.get(factoryId));
185 }
186 }
187 initialComponentStates = holdInitialComponentStates;
188
189
190 Map<String, Component> holdComponentStates = new HashMap<String, Component>();
191 for (String id : index.keySet()) {
192 if (holdIds.contains(id)) {
193 Component component = index.get(id);
194 holdComponentStates.put(id, component);
195
196
197 if (!component.getRefreshExpressionGraph().isEmpty()) {
198 componentExpressionGraphs.put(component.getBaseId(), component.getRefreshExpressionGraph());
199 }
200 }
201 }
202 index = holdComponentStates;
203
204 dataFieldIndex = new HashMap<String, DataField>();
205 }
206
207
208
209
210
211
212
213 public Component getComponentById(String id) {
214 return index.get(id);
215 }
216
217
218
219
220
221
222
223 public DataField getDataFieldByPath(String propertyPath) {
224 return dataFieldIndex.get(propertyPath);
225 }
226
227
228
229
230
231
232
233
234 public DataField getDataFieldByPropertyName(String propertyName) {
235 DataField dataField = null;
236
237 for (DataField field : dataFieldIndex.values()) {
238 if (StringUtils.equals(propertyName, field.getPropertyName())) {
239 dataField = field;
240 break;
241 }
242 }
243
244 return dataField;
245 }
246
247
248
249
250
251
252
253
254 public Map<String, DataField> getDataFieldIndex() {
255 return this.dataFieldIndex;
256 }
257
258
259
260
261
262
263
264
265 public Map<String, CollectionGroup> getCollectionsIndex() {
266 return this.collectionsIndex;
267 }
268
269
270
271
272
273
274
275
276 public CollectionGroup getCollectionGroupByPath(String collectionPath) {
277 return collectionsIndex.get(collectionPath);
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 public Map<String, Component> getInitialComponentStates() {
299 return initialComponentStates;
300 }
301
302
303
304
305
306
307
308
309
310
311
312 public void addInitialComponentStateIfNeeded(Component component) {
313 if (StringUtils.isBlank(component.getBaseId())) {
314 component.setBaseId(component.getId());
315 initialComponentStates.put(component.getBaseId(), ComponentUtils.copy(component));
316 }
317 }
318
319
320
321
322
323
324 public void setInitialComponentStates(Map<String, Component> initialComponentStates) {
325 this.initialComponentStates = initialComponentStates;
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340 public Map<String, PropertyEditor> getFieldPropertyEditors() {
341 return fieldPropertyEditors;
342 }
343
344
345
346
347
348
349
350
351
352
353
354
355
356 public Map<String, PropertyEditor> getSecureFieldPropertyEditors() {
357 return secureFieldPropertyEditors;
358 }
359
360
361
362
363
364
365 public Map<String, Integer> getIdSequenceSnapshot() {
366 return idSequenceSnapshot;
367 }
368
369
370
371
372
373
374
375 public void addSequenceValueToSnapshot(String componentId, int sequenceVal) {
376 idSequenceSnapshot.put(componentId, sequenceVal);
377 }
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 public Map<String, Map<String, String>> getComponentExpressionGraphs() {
395 return componentExpressionGraphs;
396 }
397
398 }