1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.kuali.rice.kns.service.impl; |
12 | |
|
13 | |
import java.util.ArrayList; |
14 | |
import java.util.HashMap; |
15 | |
import java.util.Iterator; |
16 | |
import java.util.List; |
17 | |
import java.util.Map; |
18 | |
import java.util.TreeMap; |
19 | |
|
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.kuali.rice.kns.bo.BusinessObjectRelationship; |
22 | |
import org.kuali.rice.kns.datadictionary.DataDictionaryEntry; |
23 | |
import org.kuali.rice.kns.datadictionary.DataObjectEntry; |
24 | |
import org.kuali.rice.kns.datadictionary.PrimitiveAttributeDefinition; |
25 | |
import org.kuali.rice.kns.datadictionary.RelationshipDefinition; |
26 | |
import org.kuali.rice.kns.datadictionary.SupportAttributeDefinition; |
27 | |
import org.kuali.rice.kns.service.DataDictionaryService; |
28 | |
import org.kuali.rice.kns.service.DataObjectMetaDataService; |
29 | |
import org.kuali.rice.kns.service.KNSServiceLocatorWeb; |
30 | |
import org.kuali.rice.kns.service.KualiModuleService; |
31 | |
import org.kuali.rice.kns.service.ModuleService; |
32 | |
import org.kuali.rice.kns.service.PersistenceStructureService; |
33 | |
import org.kuali.rice.kns.uif.service.ViewDictionaryService; |
34 | |
import org.kuali.rice.kns.uif.util.ObjectPropertyUtils; |
35 | |
import org.kuali.rice.kns.util.ObjectUtils; |
36 | |
import org.springframework.beans.BeanWrapper; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class DataObjectMetaDataServiceImpl implements DataObjectMetaDataService { |
42 | |
|
43 | |
private DataDictionaryService dataDictionaryService; |
44 | |
private KualiModuleService kualiModuleService; |
45 | |
private PersistenceStructureService persistenceStructureService; |
46 | |
private ViewDictionaryService viewDictionaryService; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@Override |
52 | |
public List<String> listPrimaryKeyFieldNames(Class<?> clazz) { |
53 | 0 | if (persistenceStructureService.isPersistable(clazz)) { |
54 | 0 | return persistenceStructureService.listPrimaryKeyFieldNames(clazz); |
55 | |
} |
56 | |
|
57 | 0 | ModuleService responsibleModuleService = getKualiModuleService().getResponsibleModuleService(clazz); |
58 | 0 | if (responsibleModuleService != null && responsibleModuleService.isExternalizable(clazz)) |
59 | 0 | return responsibleModuleService.listPrimaryKeyFieldNames(clazz); |
60 | |
|
61 | |
|
62 | 0 | List<String> pks = dataDictionaryService.getDataDictionary().getDataObjectEntry(clazz.getName()) |
63 | |
.getPrimaryKeys(); |
64 | 0 | if (pks != null && !pks.isEmpty()) |
65 | 0 | return pks; |
66 | |
|
67 | 0 | return new ArrayList<String>(); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
@Override |
74 | |
public Map<String, ?> getPrimaryKeyFieldValues(Object dataObject) { |
75 | 0 | return getPrimaryKeyFieldValues(dataObject, false); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
@Override |
83 | |
public Map<String, ?> getPrimaryKeyFieldValues(Object dataObject, boolean sortFieldNames) { |
84 | |
Map<String, Object> keyValueMap; |
85 | |
|
86 | 0 | if (sortFieldNames) { |
87 | 0 | keyValueMap = new TreeMap<String, Object>(); |
88 | |
} else { |
89 | 0 | keyValueMap = new HashMap<String, Object>(); |
90 | |
} |
91 | |
|
92 | 0 | BeanWrapper wrapper = ObjectPropertyUtils.wrapObject(dataObject); |
93 | |
|
94 | 0 | List<String> fields = listPrimaryKeyFieldNames(dataObject.getClass()); |
95 | 0 | for (String fieldName : fields) { |
96 | 0 | keyValueMap.put(fieldName, wrapper.getPropertyValue(fieldName)); |
97 | |
} |
98 | |
|
99 | 0 | return keyValueMap; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
public boolean equalsByPrimaryKeys(Object do1, Object do2) { |
108 | 0 | boolean equal = true; |
109 | |
|
110 | 0 | if (do1 == null && do2 == null) { |
111 | 0 | equal = true; |
112 | 0 | } else if (do1 == null || do2 == null) { |
113 | 0 | equal = false; |
114 | 0 | } else if (!do1.getClass().getName().equals(do2.getClass().getName())) { |
115 | 0 | equal = false; |
116 | |
} else { |
117 | 0 | Map<String, ?> do1Keys = getPrimaryKeyFieldValues(do1); |
118 | 0 | Map<String, ?> do2Keys = getPrimaryKeyFieldValues(do2); |
119 | 0 | for (Iterator<String> iter = do1Keys.keySet().iterator(); iter.hasNext();) { |
120 | 0 | String keyName = iter.next(); |
121 | 0 | if (do1Keys.get(keyName) != null && do2Keys.get(keyName) != null) { |
122 | 0 | if (!do1Keys.get(keyName).toString().equals(do2Keys.get(keyName).toString())) { |
123 | 0 | equal = false; |
124 | |
} |
125 | |
} else { |
126 | 0 | equal = false; |
127 | |
} |
128 | 0 | } |
129 | |
} |
130 | |
|
131 | 0 | return equal; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public BusinessObjectRelationship getDataObjectRelationship(Object dataObject, Class<?> dataObjectClass, |
140 | |
String attributeName, String attributePrefix, boolean keysOnly, boolean supportsLookup, |
141 | |
boolean supportsInquiry) { |
142 | 0 | RelationshipDefinition ddReference = getDictionaryRelationship(dataObjectClass, attributeName); |
143 | |
|
144 | 0 | return getDataObjectRelationship(ddReference, dataObject, dataObjectClass, attributeName, attributePrefix, |
145 | |
keysOnly, supportsLookup, supportsInquiry); |
146 | |
} |
147 | |
|
148 | |
protected BusinessObjectRelationship getDataObjectRelationship(RelationshipDefinition ddReference, |
149 | |
Object dataObject, Class<?> dataObjectClass, String attributeName, String attributePrefix, |
150 | |
boolean keysOnly, boolean supportsLookup, boolean supportsInquiry) { |
151 | 0 | BusinessObjectRelationship relationship = null; |
152 | |
|
153 | |
|
154 | |
|
155 | 0 | if (ObjectUtils.isNestedAttribute(attributeName)) { |
156 | 0 | if (ddReference != null) { |
157 | 0 | if ((supportsLookup && getViewDictionaryService().isLookupable(ddReference.getTargetClass())) |
158 | |
|| (supportsInquiry && getViewDictionaryService().isInquirable(ddReference.getTargetClass()))) { |
159 | 0 | relationship = populateRelationshipFromDictionaryReference(dataObjectClass, ddReference, |
160 | |
attributePrefix, keysOnly); |
161 | |
|
162 | 0 | return relationship; |
163 | |
} |
164 | |
} |
165 | |
|
166 | |
|
167 | 0 | String localPrefix = StringUtils.substringBefore(attributeName, "."); |
168 | 0 | String localAttributeName = StringUtils.substringAfter(attributeName, "."); |
169 | 0 | if (dataObject == null) { |
170 | 0 | dataObject = ObjectUtils.createNewObjectFromClass(dataObjectClass); |
171 | |
} |
172 | |
|
173 | 0 | Object nestedObject = ObjectPropertyUtils.getPropertyValue(dataObject, localPrefix); |
174 | 0 | Class<?> nestedClass = null; |
175 | 0 | if (nestedObject == null) { |
176 | 0 | nestedClass = ObjectPropertyUtils.getPropertyType(dataObject, localPrefix); |
177 | |
} |
178 | |
else { |
179 | 0 | nestedClass = nestedObject.getClass(); |
180 | |
} |
181 | |
|
182 | 0 | String fullPrefix = localPrefix; |
183 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
184 | 0 | fullPrefix = attributePrefix + "." + localPrefix; |
185 | |
} |
186 | |
|
187 | 0 | relationship = getDataObjectRelationship(nestedObject, nestedClass, localAttributeName, fullPrefix, keysOnly, |
188 | |
supportsLookup, supportsInquiry); |
189 | |
|
190 | 0 | return relationship; |
191 | |
} |
192 | |
|
193 | |
|
194 | 0 | int maxSize = Integer.MAX_VALUE; |
195 | |
|
196 | |
|
197 | 0 | if (getPersistenceStructureService().isPersistable(dataObjectClass)) { |
198 | 0 | Map<String, BusinessObjectRelationship> rels = getPersistenceStructureService().getRelationshipMetadata( |
199 | |
dataObjectClass, attributeName, attributePrefix); |
200 | 0 | if (rels.size() > 0) { |
201 | 0 | for (BusinessObjectRelationship rel : rels.values()) { |
202 | 0 | if (rel.getParentToChildReferences().size() < maxSize) { |
203 | 0 | if ((supportsLookup && getViewDictionaryService().isLookupable(rel.getRelatedClass())) |
204 | |
|| (supportsInquiry && getViewDictionaryService().isInquirable(rel.getRelatedClass()))) { |
205 | 0 | maxSize = rel.getParentToChildReferences().size(); |
206 | 0 | relationship = rel; |
207 | |
} |
208 | |
} |
209 | |
} |
210 | |
} |
211 | 0 | } else { |
212 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(dataObjectClass); |
213 | 0 | if (moduleService != null && moduleService.isExternalizable(dataObjectClass)) { |
214 | 0 | relationship = getRelationshipMetadata(dataObjectClass, attributeName, attributePrefix); |
215 | 0 | if (relationship != null) { |
216 | 0 | return relationship; |
217 | |
} |
218 | |
} |
219 | |
} |
220 | |
|
221 | 0 | if (ddReference != null && ddReference.getPrimitiveAttributes().size() < maxSize) { |
222 | 0 | if ((supportsLookup && getViewDictionaryService().isLookupable(ddReference.getTargetClass())) |
223 | |
|| (supportsInquiry && getViewDictionaryService().isInquirable(ddReference.getTargetClass()))) { |
224 | 0 | relationship = populateRelationshipFromDictionaryReference(dataObjectClass, ddReference, null, keysOnly); |
225 | |
} |
226 | |
} |
227 | |
|
228 | 0 | return relationship; |
229 | |
} |
230 | |
|
231 | |
public RelationshipDefinition getDictionaryRelationship(Class<?> c, String attributeName) { |
232 | 0 | DataDictionaryEntry entryBase = getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry( |
233 | |
c.getName()); |
234 | 0 | if (entryBase == null) { |
235 | 0 | return null; |
236 | |
} |
237 | |
|
238 | 0 | RelationshipDefinition relationship = null; |
239 | |
|
240 | 0 | List<RelationshipDefinition> ddRelationships = entryBase.getRelationships(); |
241 | |
|
242 | 0 | int minKeys = Integer.MAX_VALUE; |
243 | 0 | for (RelationshipDefinition def : ddRelationships) { |
244 | |
|
245 | 0 | if (def.getPrimitiveAttributes().size() == 1) { |
246 | 0 | for (PrimitiveAttributeDefinition primitive : def.getPrimitiveAttributes()) { |
247 | 0 | if (primitive.getSourceName().equals(attributeName) |
248 | |
|| def.getObjectAttributeName().equals(attributeName)) { |
249 | 0 | relationship = def; |
250 | 0 | minKeys = 1; |
251 | 0 | break; |
252 | |
} |
253 | |
} |
254 | 0 | } else if (def.getPrimitiveAttributes().size() < minKeys) { |
255 | 0 | for (PrimitiveAttributeDefinition primitive : def.getPrimitiveAttributes()) { |
256 | 0 | if (primitive.getSourceName().equals(attributeName) |
257 | |
|| def.getObjectAttributeName().equals(attributeName)) { |
258 | 0 | relationship = def; |
259 | 0 | minKeys = def.getPrimitiveAttributes().size(); |
260 | 0 | break; |
261 | |
} |
262 | |
} |
263 | |
} |
264 | |
} |
265 | |
|
266 | 0 | if (relationship == null) { |
267 | 0 | for (RelationshipDefinition def : ddRelationships) { |
268 | 0 | if (def.hasIdentifier()) { |
269 | 0 | if (def.getIdentifier().getSourceName().equals(attributeName)) { |
270 | 0 | relationship = def; |
271 | |
} |
272 | |
} |
273 | |
} |
274 | |
} |
275 | |
|
276 | 0 | return relationship; |
277 | |
} |
278 | |
|
279 | |
protected BusinessObjectRelationship populateRelationshipFromDictionaryReference(Class<?> dataObjectClass, |
280 | |
RelationshipDefinition ddReference, String attributePrefix, boolean keysOnly) { |
281 | 0 | BusinessObjectRelationship relationship = new BusinessObjectRelationship(dataObjectClass, |
282 | |
ddReference.getObjectAttributeName(), ddReference.getTargetClass()); |
283 | |
|
284 | 0 | for (PrimitiveAttributeDefinition def : ddReference.getPrimitiveAttributes()) { |
285 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
286 | 0 | relationship.getParentToChildReferences().put(attributePrefix + "." + def.getSourceName(), |
287 | |
def.getTargetName()); |
288 | |
} else { |
289 | 0 | relationship.getParentToChildReferences().put(def.getSourceName(), def.getTargetName()); |
290 | |
} |
291 | |
} |
292 | |
|
293 | 0 | if (!keysOnly) { |
294 | 0 | for (SupportAttributeDefinition def : ddReference.getSupportAttributes()) { |
295 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
296 | 0 | relationship.getParentToChildReferences().put(attributePrefix + "." + def.getSourceName(), |
297 | |
def.getTargetName()); |
298 | 0 | if (def.isIdentifier()) { |
299 | 0 | relationship.setUserVisibleIdentifierKey(attributePrefix + "." + def.getSourceName()); |
300 | |
} |
301 | |
} else { |
302 | 0 | relationship.getParentToChildReferences().put(def.getSourceName(), def.getTargetName()); |
303 | 0 | if (def.isIdentifier()) { |
304 | 0 | relationship.setUserVisibleIdentifierKey(def.getSourceName()); |
305 | |
} |
306 | |
} |
307 | |
} |
308 | |
} |
309 | |
|
310 | 0 | return relationship; |
311 | |
} |
312 | |
|
313 | |
protected BusinessObjectRelationship getRelationshipMetadata(Class<?> dataObjectClass, String attributeName, |
314 | |
String attributePrefix) { |
315 | |
|
316 | 0 | RelationshipDefinition relationshipDefinition = getDictionaryRelationship(dataObjectClass, attributeName); |
317 | 0 | if (relationshipDefinition == null) { |
318 | 0 | return null; |
319 | |
} |
320 | |
|
321 | 0 | BusinessObjectRelationship businessObjectRelationship = new BusinessObjectRelationship( |
322 | |
relationshipDefinition.getSourceClass(), relationshipDefinition.getObjectAttributeName(), |
323 | |
relationshipDefinition.getTargetClass()); |
324 | |
|
325 | 0 | if (!StringUtils.isEmpty(attributePrefix)) { |
326 | 0 | attributePrefix += "."; |
327 | |
} |
328 | |
|
329 | 0 | List<PrimitiveAttributeDefinition> primitives = relationshipDefinition.getPrimitiveAttributes(); |
330 | 0 | for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitives) { |
331 | 0 | businessObjectRelationship.getParentToChildReferences().put( |
332 | |
attributePrefix + primitiveAttributeDefinition.getSourceName(), |
333 | |
primitiveAttributeDefinition.getTargetName()); |
334 | |
} |
335 | |
|
336 | 0 | return businessObjectRelationship; |
337 | |
} |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
@Override |
345 | |
public String getTitleAttribute(Class<?> dataObjectClass) { |
346 | 0 | String titleAttribute = null; |
347 | |
|
348 | 0 | DataObjectEntry entry = getDataObjectEntry(dataObjectClass); |
349 | 0 | if (entry != null) { |
350 | 0 | titleAttribute = entry.getTitleAttribute(); |
351 | |
} |
352 | |
|
353 | 0 | return titleAttribute; |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
protected DataObjectEntry getDataObjectEntry(Class<?> dataObjectClass) { |
364 | 0 | if (dataObjectClass == null) { |
365 | 0 | throw new IllegalArgumentException("invalid (null) dataObjectClass"); |
366 | |
} |
367 | |
|
368 | 0 | DataObjectEntry entry = getDataDictionaryService().getDataDictionary() |
369 | |
.getDataObjectEntry(dataObjectClass.getName()); |
370 | |
|
371 | 0 | return entry; |
372 | |
} |
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
protected DataDictionaryService getDataDictionaryService() { |
380 | 0 | return this.dataDictionaryService; |
381 | |
} |
382 | |
|
383 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
384 | 0 | this.dataDictionaryService = dataDictionaryService; |
385 | 0 | } |
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
protected KualiModuleService getKualiModuleService() { |
393 | 0 | return this.kualiModuleService; |
394 | |
} |
395 | |
|
396 | |
public void setKualiModuleService(KualiModuleService kualiModuleService) { |
397 | 0 | this.kualiModuleService = kualiModuleService; |
398 | 0 | } |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
protected PersistenceStructureService getPersistenceStructureService() { |
407 | 0 | return this.persistenceStructureService; |
408 | |
} |
409 | |
|
410 | |
public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) { |
411 | 0 | this.persistenceStructureService = persistenceStructureService; |
412 | 0 | } |
413 | |
|
414 | |
protected ViewDictionaryService getViewDictionaryService() { |
415 | 0 | if (this.viewDictionaryService == null) { |
416 | 0 | this.viewDictionaryService = KNSServiceLocatorWeb.getViewDictionaryService(); |
417 | |
} |
418 | 0 | return this.viewDictionaryService; |
419 | |
} |
420 | |
|
421 | |
public void setViewDictionaryService(ViewDictionaryService viewDictionaryService) { |
422 | 0 | this.viewDictionaryService = viewDictionaryService; |
423 | 0 | } |
424 | |
|
425 | |
} |