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 java.io.Serializable;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.log4j.Logger;
26  import org.kuali.rice.krad.uif.component.Component;
27  import org.kuali.rice.krad.uif.container.CollectionGroup;
28  import org.kuali.rice.krad.uif.container.Container;
29  import org.kuali.rice.krad.uif.field.DataField;
30  import org.kuali.rice.krad.uif.util.CopyUtils;
31  import org.kuali.rice.krad.uif.util.LifecycleElement;
32  
33  
34  
35  
36  
37  
38  public class ViewIndex implements Serializable {
39      private static final long serialVersionUID = 4700818801272201371L;
40      private static final Logger LOG = Logger.getLogger(ViewIndex.class);
41  
42      private Map<String, Component> index;
43      private Map<String, DataField> dataFieldIndex;
44  
45      private Map<String, CollectionGroup> collectionsIndex;
46      private Map<String, LifecycleElement> lifecycleElementsByPath;
47  
48      private Set<String> assignedIds;
49  
50      
51  
52  
53      public ViewIndex() {
54          index = new HashMap<String, Component>();
55          dataFieldIndex = new HashMap<String, DataField>();
56          collectionsIndex = new HashMap<String, CollectionGroup>();
57          lifecycleElementsByPath = new HashMap<String, LifecycleElement>();
58          assignedIds = new HashSet<String>();
59      }
60  
61      
62  
63  
64      protected void clearIndex(View view) {
65          index = new HashMap<String, Component>();
66          dataFieldIndex = new HashMap<String, DataField>();
67          collectionsIndex = new HashMap<String, CollectionGroup>();
68          lifecycleElementsByPath = new HashMap<String, LifecycleElement>();
69      }
70  
71      
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93      public void indexComponent(Component component) {
94          if (component == null) {
95              return;
96          }
97  
98          synchronized (index) {
99              index.put(component.getId(), component);
100         }
101 
102         synchronized (lifecycleElementsByPath) {
103             lifecycleElementsByPath.put(component.getViewPath(), component);
104         }
105 
106         if (component instanceof Container) {
107             Container container = (Container) component;
108             if (container.getLayoutManager() != null) {
109                 synchronized (lifecycleElementsByPath) {
110                     lifecycleElementsByPath.put(container.getLayoutManager().getViewPath(),
111                             container.getLayoutManager());
112                 }
113             }
114         }
115 
116         if (component instanceof DataField) {
117             DataField field = (DataField) component;
118 
119             synchronized (dataFieldIndex) {
120                 dataFieldIndex.put(field.getBindingInfo().getBindingPath(), field);
121             }
122         } else if (component instanceof CollectionGroup) {
123             CollectionGroup collectionGroup = (CollectionGroup) component;
124 
125             synchronized (collectionsIndex) {
126                 collectionsIndex.put(collectionGroup.getBindingInfo().getBindingPath(), collectionGroup);
127             }
128         }
129     }
130 
131     
132 
133 
134 
135 
136 
137     public boolean observeAssignedId(String id) {
138         if (assignedIds.contains(id)) {
139             return false;
140         }
141 
142         synchronized (assignedIds) {
143             return assignedIds.add(id);
144         }
145     }
146 
147     
148 
149 
150 
151 
152 
153     public Component getComponentById(String id) {
154         return index.get(id);
155     }
156 
157     
158 
159 
160 
161 
162 
163     public DataField getDataFieldByPath(String propertyPath) {
164         return dataFieldIndex.get(propertyPath);
165     }
166 
167     
168 
169 
170 
171 
172 
173 
174     public DataField getDataFieldByPropertyName(String propertyName) {
175         DataField dataField = null;
176 
177         for (DataField field : dataFieldIndex.values()) {
178             if (StringUtils.equals(propertyName, field.getPropertyName())) {
179                 dataField = field;
180                 break;
181             }
182         }
183 
184         return dataField;
185     }
186 
187     
188 
189 
190 
191 
192     public Map<String, LifecycleElement> getLifecycleElementsByPath() {
193         return lifecycleElementsByPath;
194     }
195 
196     
197 
198 
199 
200 
201 
202     public LifecycleElement getLifecycleElementByPath(String path) {
203         if ((lifecycleElementsByPath != null) && lifecycleElementsByPath.containsKey(path)) {
204             return lifecycleElementsByPath.get(path);
205         }
206 
207         return null;
208     }
209 
210     
211 
212 
213 
214 
215 
216     public Map<String, DataField> getDataFieldIndex() {
217         return this.dataFieldIndex;
218     }
219 
220     
221 
222 
223 
224 
225 
226     public Map<String, CollectionGroup> getCollectionsIndex() {
227         return this.collectionsIndex;
228     }
229 
230     
231 
232 
233 
234 
235 
236     public CollectionGroup getCollectionGroupByPath(String collectionPath) {
237         return collectionsIndex.get(collectionPath);
238     }
239 
240     
241 
242 
243 
244 
245     public ViewIndex copy() {
246         ViewIndex viewIndexCopy = new ViewIndex();
247 
248         if (this.index != null) {
249             Map<String, Component> indexCopy = new HashMap<String, Component>();
250             for (Map.Entry<String, Component> indexEntry : this.index.entrySet()) {
251                 if (indexEntry.getValue() instanceof View) {
252                     LOG.warn("View reference at " + indexEntry);
253                 } else {
254                     indexCopy.put(indexEntry.getKey(), (Component) CopyUtils.copy(indexEntry.getValue()));
255                 }
256             }
257 
258             viewIndexCopy.index = indexCopy;
259         }
260 
261         if (this.dataFieldIndex != null) {
262             Map<String, DataField> dataFieldIndexCopy = new HashMap<String, DataField>();
263             for (Map.Entry<String, DataField> indexEntry : this.dataFieldIndex.entrySet()) {
264                 dataFieldIndexCopy.put(indexEntry.getKey(), (DataField) CopyUtils.copy(indexEntry.getValue()));
265             }
266 
267             viewIndexCopy.dataFieldIndex = dataFieldIndexCopy;
268         }
269 
270         if (this.collectionsIndex != null) {
271             Map<String, CollectionGroup> collectionsIndexCopy = new HashMap<String, CollectionGroup>();
272             for (Map.Entry<String, CollectionGroup> indexEntry : this.collectionsIndex.entrySet()) {
273                 collectionsIndexCopy.put(indexEntry.getKey(), (CollectionGroup) CopyUtils.copy(indexEntry.getValue()));
274             }
275 
276             viewIndexCopy.collectionsIndex = collectionsIndexCopy;
277         }
278 
279         return viewIndexCopy;
280     }
281 
282 }