1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.container;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.uif.core.BindingInfo;
20 import org.kuali.rice.krad.uif.core.Component;
21 import org.kuali.rice.krad.uif.field.AttributeField;
22
23 import java.io.Serializable;
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28
29
30
31
32 public class ViewIndex implements Serializable {
33 private static final long serialVersionUID = 4700818801272201371L;
34
35 private Map<String, Component> index;
36 private Map<String, AttributeField> attributeFieldIndex;
37 private Map<String, CollectionGroup> collectionsIndex;
38
39
40
41
42
43
44 public ViewIndex(View view) {
45 index(view);
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 protected void index(View view) {
65 index = new HashMap<String, Component>();
66 attributeFieldIndex = new HashMap<String, AttributeField>();
67 collectionsIndex = new HashMap<String, CollectionGroup>();
68
69 indexComponent(view);
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public void indexComponent(Component component) {
85 if (component == null) {
86 return;
87 }
88
89 index.put(component.getId(), component);
90
91 if (component instanceof AttributeField) {
92 AttributeField field = (AttributeField) component;
93 attributeFieldIndex.put(field.getBindingInfo().getBindingPath(), field);
94 } else if (component instanceof CollectionGroup) {
95 CollectionGroup collectionGroup = (CollectionGroup) component;
96 collectionsIndex.put(collectionGroup.getBindingInfo().getBindingPath(), collectionGroup);
97 }
98
99 for (Component nestedComponent : component.getNestedComponents()) {
100 indexComponent(nestedComponent);
101 }
102 }
103
104
105
106
107
108
109
110 public Component getComponentById(String id) {
111 return index.get(id);
112 }
113
114
115
116
117
118
119
120 public AttributeField getAttributeFieldByPath(String attributePath) {
121 return attributeFieldIndex.get(attributePath);
122 }
123
124
125
126
127
128
129
130
131 public AttributeField getAttributeFieldByPropertyName(String propertyName) {
132 AttributeField attributeField = null;
133
134 for (AttributeField field : attributeFieldIndex.values()) {
135 if (StringUtils.equals(propertyName, field.getPropertyName())) {
136 attributeField = field;
137 break;
138 }
139 }
140
141 return attributeField;
142 }
143
144
145
146
147
148
149
150
151 public Map<String, AttributeField> getAttributeFieldIndex() {
152 return this.attributeFieldIndex;
153 }
154
155
156
157
158
159
160
161
162 public Map<String, CollectionGroup> getCollectionsIndex() {
163 return this.collectionsIndex;
164 }
165
166
167
168
169
170
171
172
173 public CollectionGroup getCollectionGroupByPath(String collectionPath) {
174 return collectionsIndex.get(collectionPath);
175 }
176
177
178
179
180
181
182
183
184
185
186
187 public AttributeField getAttributeField(BindingInfo bindingInfo) {
188
189 AttributeField attributeField = getAttributeFieldByPath(bindingInfo.getBindingPath());
190
191 if (attributeField == null) {
192
193 String path = bindingInfo.getBindingObjectPath() + "." + bindingInfo.getBindByNamePrefix();
194
195 CollectionGroup collectionGroup = getCollectionGroupByPath(stripIndexesFromPropertyPath(path));
196 if (collectionGroup != null) {
197 for (Component item : ((CollectionGroup) collectionGroup).getItems()) {
198 if (item instanceof AttributeField) {
199 if (StringUtils
200 .equals(((AttributeField) item).getPropertyName(), bindingInfo.getBindingName())) {
201 attributeField = (AttributeField) item;
202 break;
203 }
204 }
205 }
206 }
207 }
208
209 return attributeField;
210 }
211
212
213
214
215
216
217
218
219
220
221
222 private String stripIndexesFromPropertyPath(String propertyPath) {
223
224 String returnValue = propertyPath;
225 String index = StringUtils.substringBetween(propertyPath, "[", "]");
226
227 if (StringUtils.isNotBlank(index)) {
228 returnValue = StringUtils.remove(propertyPath, "[" + index + "]");
229 return stripIndexesFromPropertyPath(returnValue);
230 } else {
231 return returnValue;
232 }
233 }
234 }