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