1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22 import org.kuali.rice.krad.uif.UifConstants;
23 import org.kuali.rice.krad.uif.component.Component;
24 import org.kuali.rice.krad.uif.util.ComponentUtils;
25 import org.kuali.rice.krad.uif.util.ViewModelUtils;
26 import org.kuali.rice.krad.uif.view.View;
27 import org.kuali.rice.krad.uif.service.ViewTypeService;
28 import org.springframework.beans.PropertyValues;
29 import org.springframework.beans.factory.config.BeanDefinition;
30 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
31 import org.kuali.rice.krad.uif.UifConstants.ViewType;
32
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38
39
40
41
42
43
44
45
46
47
48
49
50 public class UifDictionaryIndex implements Runnable {
51 private static final Log LOG = LogFactory.getLog(UifDictionaryIndex.class);
52
53 private DefaultListableBeanFactory ddBeans;
54
55
56 private Map<String, String> viewBeanEntriesById;
57
58
59 private Map<String, ViewTypeDictionaryIndex> viewEntriesByType;
60
61 public UifDictionaryIndex(DefaultListableBeanFactory ddBeans) {
62 this.ddBeans = ddBeans;
63 }
64
65 public void run() {
66 LOG.info("Starting View Index Building");
67 buildViewIndicies();
68 LOG.info("Completed View Index Building");
69 }
70
71
72
73
74
75
76
77
78
79 public View getViewById(String viewId) {
80 String beanName = viewBeanEntriesById.get(viewId);
81 if (StringUtils.isBlank(beanName)) {
82 throw new DataDictionaryException("Unable to find View with id: " + viewId);
83 }
84
85 return ddBeans.getBean(beanName, View.class);
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99 public View getViewByTypeIndex(ViewType viewTypeName, Map<String, String> indexKey) {
100 String index = buildTypeIndex(indexKey);
101
102 ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewTypeName);
103
104 String beanName = typeIndex.get(index);
105 if (StringUtils.isNotBlank(beanName)) {
106 return ddBeans.getBean(beanName, View.class);
107 }
108
109 return null;
110 }
111
112
113
114
115
116
117
118
119
120 public boolean viewByTypeExist(ViewType viewTypeName, Map<String, String> indexKey) {
121 boolean viewExist = false;
122
123 String index = buildTypeIndex(indexKey);
124 ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewTypeName);
125
126 String beanName = typeIndex.get(index);
127 if (StringUtils.isNotBlank(beanName)) {
128 viewExist = true;
129 }
130
131 return viewExist;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145 public PropertyValues getViewPropertiesById(String viewId) {
146 String beanName = viewBeanEntriesById.get(viewId);
147 if (StringUtils.isBlank(beanName)) {
148 BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName);
149
150 return beanDefinition.getPropertyValues();
151 }
152
153 return null;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public PropertyValues getViewPropertiesByType(ViewType viewTypeName, Map<String, String> indexKey) {
171 String index = buildTypeIndex(indexKey);
172
173 ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewTypeName);
174
175 String beanName = typeIndex.get(index);
176 if (StringUtils.isNotBlank(beanName)) {
177 BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName);
178
179 return beanDefinition.getPropertyValues();
180 }
181
182 return null;
183 }
184
185
186
187
188
189
190
191
192
193 public List<View> getViewsForType(ViewType viewTypeName) {
194 List<View> typeViews = new ArrayList<View>();
195
196
197 if (viewEntriesByType.containsKey(viewTypeName.name())) {
198 ViewTypeDictionaryIndex typeIndex = viewEntriesByType.get(viewTypeName.name());
199 for (Entry<String, String> typeEntry : typeIndex.getViewIndex().entrySet()) {
200 View typeView = ddBeans.getBean(typeEntry.getValue(), View.class);
201 typeViews.add(typeView);
202 }
203 } else {
204 throw new DataDictionaryException("Unable to find view index for type: " + viewTypeName);
205 }
206
207 return typeViews;
208 }
209
210
211
212
213
214
215 protected void buildViewIndicies() {
216 viewBeanEntriesById = new HashMap<String, String>();
217 viewEntriesByType = new HashMap<String, ViewTypeDictionaryIndex>();
218
219 String[] beanNames = ddBeans.getBeanNamesForType(View.class);
220 for (int i = 0; i < beanNames.length; i++) {
221 String beanName = beanNames[i];
222 BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName);
223 PropertyValues propertyValues = beanDefinition.getPropertyValues();
224
225 String id = ViewModelUtils.getStringValFromPVs(propertyValues, "id");
226 if (StringUtils.isBlank(id)) {
227 id = beanName;
228 }
229
230 if (viewBeanEntriesById.containsKey(id)) {
231 throw new DataDictionaryException("Two views must not share the same id. Found duplicate id: " + id);
232 }
233 viewBeanEntriesById.put(id, beanName);
234
235 indexViewForType(propertyValues, beanName);
236 }
237 }
238
239
240
241
242
243
244
245
246
247
248 protected void indexViewForType(PropertyValues propertyValues, String beanName) {
249 String viewTypeName = ViewModelUtils.getStringValFromPVs(propertyValues, "viewTypeName");
250 if (StringUtils.isBlank(viewTypeName)) {
251 return;
252 }
253
254 UifConstants.ViewType viewType = ViewType.valueOf(viewTypeName);
255
256 ViewTypeService typeService = KRADServiceLocatorWeb.getViewService().getViewTypeService(viewType);
257 if (typeService == null) {
258
259 return;
260 }
261
262
263 Map<String, String> typeParameters = typeService.getParametersFromViewConfiguration(propertyValues);
264
265
266 String index = buildTypeIndex(typeParameters);
267
268
269 ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewType);
270
271 typeIndex.put(index, beanName);
272 }
273
274
275
276
277
278
279
280
281
282 protected ViewTypeDictionaryIndex getTypeIndex(UifConstants.ViewType viewType) {
283 ViewTypeDictionaryIndex typeIndex = null;
284
285 if (viewEntriesByType.containsKey(viewType.name())) {
286 typeIndex = viewEntriesByType.get(viewType.name());
287 } else {
288 typeIndex = new ViewTypeDictionaryIndex();
289 viewEntriesByType.put(viewType.name(), typeIndex);
290 }
291
292 return typeIndex;
293 }
294
295
296
297
298
299
300
301 protected String buildTypeIndex(Map<String, String> typeParameters) {
302 String index = "";
303
304 for (String parameterName : typeParameters.keySet()) {
305 if (StringUtils.isNotBlank(index)) {
306 index += "|||";
307 }
308 index += parameterName + "^^" + typeParameters.get(parameterName);
309 }
310
311 return index;
312 }
313
314 }