| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.service.impl; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.kns.bo.BusinessObject; |
| 20 | |
import org.kuali.rice.kns.bo.BusinessObjectRelationship; |
| 21 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
| 22 | |
import org.kuali.rice.kns.datadictionary.*; |
| 23 | |
import org.kuali.rice.kns.lookup.valuefinder.ValueFinder; |
| 24 | |
import org.kuali.rice.kns.service.*; |
| 25 | |
import org.kuali.rice.kns.util.ObjectUtils; |
| 26 | |
|
| 27 | |
import java.lang.reflect.InvocationTargetException; |
| 28 | |
import java.util.*; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class BusinessObjectMetaDataServiceImpl implements BusinessObjectMetaDataService { |
| 40 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger .getLogger(BusinessObjectMetaDataServiceImpl.class); |
| 41 | |
|
| 42 | |
private BusinessObjectDictionaryService businessObjectDictionaryService; |
| 43 | |
private DataDictionaryService dataDictionaryService; |
| 44 | |
private PersistenceStructureService persistenceStructureService; |
| 45 | |
private KualiModuleService kualiModuleService; |
| 46 | |
|
| 47 | |
public Collection<String> getCollectionNames(BusinessObject bo) { |
| 48 | 0 | return dataDictionaryService.getDataDictionary() |
| 49 | |
.getBusinessObjectEntry(bo.getClass().getName()) |
| 50 | |
.getCollectionNames(); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public Collection<String> getInquirableFieldNames(Class boClass, |
| 54 | |
String sectionTitle) { |
| 55 | 0 | return businessObjectDictionaryService.getInquiryFieldNames(boClass, |
| 56 | |
sectionTitle); |
| 57 | |
} |
| 58 | |
|
| 59 | |
public List<String> getLookupableFieldNames(Class boClass) { |
| 60 | 0 | return businessObjectDictionaryService.getLookupFieldNames(boClass); |
| 61 | |
} |
| 62 | |
|
| 63 | |
public String getLookupFieldDefaultValue(Class businessObjectClass, |
| 64 | |
String attributeName) { |
| 65 | 0 | return businessObjectDictionaryService.getLookupFieldDefaultValue( |
| 66 | |
businessObjectClass, attributeName); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public Class getLookupFieldDefaultValueFinderClass( |
| 70 | |
Class businessObjectClass, String attributeName) { |
| 71 | 0 | return businessObjectDictionaryService |
| 72 | |
.getLookupFieldDefaultValueFinderClass(businessObjectClass, |
| 73 | |
attributeName); |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
public String getLookupFieldQuickfinderParameterString(Class businessObjectClass, |
| 78 | |
String attributeName) { |
| 79 | 0 | return businessObjectDictionaryService.getLookupFieldQuickfinderParameterString( |
| 80 | |
businessObjectClass, attributeName); |
| 81 | |
} |
| 82 | |
|
| 83 | |
|
| 84 | |
public Class<? extends ValueFinder> getLookupFieldQuickfinderParameterStringBuilderClass( |
| 85 | |
Class businessObjectClass, String attributeName) { |
| 86 | 0 | return businessObjectDictionaryService |
| 87 | |
.getLookupFieldQuickfinderParameterStringBuilderClass(businessObjectClass, |
| 88 | |
attributeName); |
| 89 | |
} |
| 90 | |
|
| 91 | |
public boolean isAttributeInquirable(Class boClass, String attributeName, |
| 92 | |
String sectionTitle) { |
| 93 | 0 | Collection sections = businessObjectDictionaryService |
| 94 | |
.getInquirySections(boClass); |
| 95 | 0 | boolean isInquirable = true; |
| 96 | |
|
| 97 | 0 | Iterator iter = sections.iterator(); |
| 98 | |
|
| 99 | 0 | while (iter.hasNext()) { |
| 100 | 0 | InquirySectionDefinition def = (InquirySectionDefinition) iter |
| 101 | |
.next(); |
| 102 | 0 | for (FieldDefinition field : def.getInquiryFields()) { |
| 103 | 0 | if (field.getAttributeName().equalsIgnoreCase(attributeName)) { |
| 104 | 0 | isInquirable = !field.isNoInquiry(); |
| 105 | |
} |
| 106 | |
} |
| 107 | 0 | } |
| 108 | 0 | if (isInquirable) { |
| 109 | 0 | Object obj = null; |
| 110 | 0 | if (boClass != null |
| 111 | |
&& BusinessObject.class.isAssignableFrom(boClass)) { |
| 112 | 0 | obj = ObjectUtils.createNewObjectFromClass(boClass); |
| 113 | |
} |
| 114 | |
|
| 115 | 0 | if (obj != null) { |
| 116 | 0 | BusinessObject bo = (BusinessObject) obj; |
| 117 | 0 | Class clazz = getNestedBOClass(bo, attributeName); |
| 118 | 0 | if (clazz != null |
| 119 | |
&& BusinessObject.class.isAssignableFrom(clazz)) { |
| 120 | 0 | return businessObjectDictionaryService.isInquirable(clazz); |
| 121 | |
} else { |
| 122 | 0 | return false; |
| 123 | |
} |
| 124 | |
} else { |
| 125 | 0 | return false; |
| 126 | |
} |
| 127 | |
|
| 128 | |
} |
| 129 | |
|
| 130 | 0 | return isInquirable; |
| 131 | |
} |
| 132 | |
|
| 133 | |
public boolean isInquirable(Class boClass) { |
| 134 | 0 | boolean inquirable = false; |
| 135 | 0 | ModuleService moduleService = kualiModuleService |
| 136 | |
.getResponsibleModuleService(boClass); |
| 137 | 0 | if (moduleService != null && moduleService.isExternalizable(boClass)) { |
| 138 | 0 | inquirable = moduleService |
| 139 | |
.isExternalizableBusinessObjectInquirable(boClass); |
| 140 | |
} else { |
| 141 | 0 | Boolean isLookupable = businessObjectDictionaryService |
| 142 | |
.isInquirable(boClass); |
| 143 | 0 | if (isLookupable != null) { |
| 144 | 0 | inquirable = isLookupable.booleanValue(); |
| 145 | |
} |
| 146 | |
} |
| 147 | 0 | return inquirable; |
| 148 | |
} |
| 149 | |
|
| 150 | |
public boolean isAttributeLookupable(Class boClass, String attributeName) { |
| 151 | 0 | Object obj = null; |
| 152 | 0 | if (boClass != null && BusinessObject.class.isAssignableFrom(boClass)) { |
| 153 | 0 | obj = ObjectUtils.createNewObjectFromClass(boClass); |
| 154 | |
} |
| 155 | 0 | if (obj != null) { |
| 156 | 0 | BusinessObject bo = (BusinessObject) obj; |
| 157 | 0 | BusinessObjectRelationship relationship = getBusinessObjectRelationship( |
| 158 | |
bo, attributeName); |
| 159 | |
|
| 160 | 0 | if (relationship != null |
| 161 | |
&& relationship.getRelatedClass() != null |
| 162 | |
&& BusinessObject.class.isAssignableFrom(relationship |
| 163 | |
.getRelatedClass())) { |
| 164 | 0 | return isLookupable(relationship.getRelatedClass()); |
| 165 | |
} else { |
| 166 | 0 | return false; |
| 167 | |
} |
| 168 | |
} else { |
| 169 | 0 | return false; |
| 170 | |
} |
| 171 | |
} |
| 172 | |
|
| 173 | |
public boolean isLookupable(Class boClass) { |
| 174 | 0 | boolean lookupable = false; |
| 175 | 0 | ModuleService moduleService = kualiModuleService |
| 176 | |
.getResponsibleModuleService(boClass); |
| 177 | 0 | if (moduleService != null && moduleService.isExternalizable(boClass)) { |
| 178 | 0 | lookupable = moduleService |
| 179 | |
.isExternalizableBusinessObjectLookupable(boClass); |
| 180 | |
} else { |
| 181 | 0 | Boolean isLookupable = businessObjectDictionaryService |
| 182 | |
.isLookupable(boClass); |
| 183 | 0 | if (isLookupable != null) { |
| 184 | 0 | lookupable = isLookupable.booleanValue(); |
| 185 | |
} |
| 186 | |
} |
| 187 | 0 | return lookupable; |
| 188 | |
} |
| 189 | |
|
| 190 | |
public BusinessObjectRelationship getBusinessObjectRelationship( |
| 191 | |
BusinessObject bo, String attributeName) { |
| 192 | 0 | return getBusinessObjectRelationship(bo, bo.getClass(), attributeName, |
| 193 | |
"", true); |
| 194 | |
} |
| 195 | |
|
| 196 | |
public BusinessObjectRelationship getBusinessObjectRelationship( |
| 197 | |
RelationshipDefinition ddReference, BusinessObject bo, |
| 198 | |
Class boClass, String attributeName, String attributePrefix, |
| 199 | |
boolean keysOnly) { |
| 200 | |
|
| 201 | 0 | BusinessObjectRelationship relationship = null; |
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | 0 | if (ObjectUtils.isNestedAttribute(attributeName)) { |
| 206 | 0 | if (ddReference != null) { |
| 207 | 0 | relationship = new BusinessObjectRelationship(boClass, |
| 208 | |
ddReference.getObjectAttributeName(), ddReference |
| 209 | |
.getTargetClass()); |
| 210 | 0 | for (PrimitiveAttributeDefinition def : ddReference |
| 211 | |
.getPrimitiveAttributes()) { |
| 212 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
| 213 | 0 | relationship.getParentToChildReferences().put( |
| 214 | |
attributePrefix + "." + def.getSourceName(), |
| 215 | |
def.getTargetName()); |
| 216 | |
} else { |
| 217 | 0 | relationship.getParentToChildReferences().put( |
| 218 | |
def.getSourceName(), def.getTargetName()); |
| 219 | |
} |
| 220 | |
} |
| 221 | 0 | if (!keysOnly) { |
| 222 | 0 | for (SupportAttributeDefinition def : ddReference |
| 223 | |
.getSupportAttributes()) { |
| 224 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
| 225 | 0 | relationship.getParentToChildReferences() |
| 226 | |
.put( |
| 227 | |
attributePrefix + "." |
| 228 | |
+ def.getSourceName(), |
| 229 | |
def.getTargetName()); |
| 230 | 0 | if (def.isIdentifier()) { |
| 231 | 0 | relationship |
| 232 | |
.setUserVisibleIdentifierKey(attributePrefix |
| 233 | |
+ "." + def.getSourceName()); |
| 234 | |
} |
| 235 | |
} else { |
| 236 | 0 | relationship.getParentToChildReferences().put( |
| 237 | |
def.getSourceName(), def.getTargetName()); |
| 238 | 0 | if (def.isIdentifier()) { |
| 239 | 0 | relationship.setUserVisibleIdentifierKey(def |
| 240 | |
.getSourceName()); |
| 241 | |
} |
| 242 | |
} |
| 243 | |
} |
| 244 | |
} |
| 245 | 0 | return relationship; |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | 0 | String localPrefix = StringUtils |
| 250 | |
.substringBefore(attributeName, "."); |
| 251 | 0 | String localAttributeName = StringUtils.substringAfter( |
| 252 | |
attributeName, "."); |
| 253 | 0 | if (bo == null) { |
| 254 | 0 | bo = (BusinessObject) ObjectUtils |
| 255 | |
.createNewObjectFromClass(boClass); |
| 256 | |
} |
| 257 | 0 | Class nestedClass = ObjectUtils.getPropertyType(bo, localPrefix, |
| 258 | |
getPersistenceStructureService()); |
| 259 | 0 | String fullPrefix = localPrefix; |
| 260 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
| 261 | 0 | fullPrefix = attributePrefix + "." + localPrefix; |
| 262 | |
} |
| 263 | |
|
| 264 | |
try { |
| 265 | 0 | if (BusinessObject.class.isAssignableFrom(nestedClass)) { |
| 266 | 0 | relationship = getBusinessObjectRelationship(null, nestedClass, |
| 267 | |
localAttributeName, fullPrefix, keysOnly); |
| 268 | |
} |
| 269 | 0 | } catch (NullPointerException e) { |
| 270 | 0 | System.err.println("bo: " + bo + " localPrefix: " + localPrefix + " localAttributeName: " + localAttributeName + " fullPrefix: " + fullPrefix + " keysOnly: " + keysOnly); |
| 271 | 0 | } |
| 272 | |
|
| 273 | 0 | return relationship; |
| 274 | |
} |
| 275 | 0 | int maxSize = Integer.MAX_VALUE; |
| 276 | |
|
| 277 | 0 | if (PersistableBusinessObject.class.isAssignableFrom(boClass) && persistenceStructureService.isPersistable(boClass) ) { |
| 278 | 0 | Map<String, BusinessObjectRelationship> rels = persistenceStructureService |
| 279 | |
.getRelationshipMetadata(boClass, attributeName, |
| 280 | |
attributePrefix); |
| 281 | 0 | if (rels.size() > 0) { |
| 282 | 0 | for (BusinessObjectRelationship rel : rels.values()) { |
| 283 | 0 | if (rel.getParentToChildReferences().size() < maxSize |
| 284 | |
&& isLookupable(rel.getRelatedClass())) { |
| 285 | 0 | maxSize = rel.getParentToChildReferences().size(); |
| 286 | 0 | relationship = rel; |
| 287 | |
} |
| 288 | |
} |
| 289 | |
} |
| 290 | 0 | } else { |
| 291 | 0 | ModuleService moduleService = KNSServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(boClass); |
| 292 | 0 | if(moduleService!=null && moduleService.isExternalizable(boClass)){ |
| 293 | 0 | relationship = getRelationshipMetadata(boClass, attributeName, attributePrefix); |
| 294 | |
|
| 295 | 0 | if(relationship!=null) { |
| 296 | 0 | return relationship; |
| 297 | |
} |
| 298 | |
} |
| 299 | |
} |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | 0 | if (ddReference != null |
| 306 | |
&& isLookupable(ddReference.getTargetClass()) |
| 307 | |
&& bo != null |
| 308 | |
&& ddReference.getPrimitiveAttributes().size() < maxSize ) { |
| 309 | 0 | relationship = new BusinessObjectRelationship(boClass, |
| 310 | |
ddReference.getObjectAttributeName(), ddReference |
| 311 | |
.getTargetClass()); |
| 312 | 0 | for (PrimitiveAttributeDefinition def : ddReference |
| 313 | |
.getPrimitiveAttributes()) { |
| 314 | 0 | relationship.getParentToChildReferences().put( |
| 315 | |
def.getSourceName(), def.getTargetName()); |
| 316 | |
} |
| 317 | 0 | if (!keysOnly) { |
| 318 | 0 | for (SupportAttributeDefinition def : ddReference |
| 319 | |
.getSupportAttributes()) { |
| 320 | 0 | relationship.getParentToChildReferences().put( |
| 321 | |
def.getSourceName(), def.getTargetName()); |
| 322 | |
} |
| 323 | |
} |
| 324 | |
} |
| 325 | |
|
| 326 | 0 | return relationship; |
| 327 | |
|
| 328 | |
} |
| 329 | |
|
| 330 | |
public RelationshipDefinition getBusinessObjectRelationshipDefinition( |
| 331 | |
Class c, String attributeName) { |
| 332 | 0 | return getDDRelationship(c, attributeName); |
| 333 | |
} |
| 334 | |
|
| 335 | |
public RelationshipDefinition getBusinessObjectRelationshipDefinition( |
| 336 | |
BusinessObject bo, String attributeName) { |
| 337 | 0 | return getBusinessObjectRelationshipDefinition(bo.getClass(), |
| 338 | |
attributeName); |
| 339 | |
} |
| 340 | |
|
| 341 | |
public BusinessObjectRelationship getBusinessObjectRelationship( |
| 342 | |
BusinessObject bo, Class boClass, String attributeName, |
| 343 | |
String attributePrefix, boolean keysOnly) { |
| 344 | 0 | RelationshipDefinition ddReference = getBusinessObjectRelationshipDefinition( |
| 345 | |
boClass, attributeName); |
| 346 | 0 | return getBusinessObjectRelationship(ddReference, bo, boClass, |
| 347 | |
attributeName, attributePrefix, keysOnly); |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
public DataDictionaryService getDataDictionaryService() { |
| 356 | 0 | return dataDictionaryService; |
| 357 | |
} |
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
public void setDataDictionaryService( |
| 366 | |
DataDictionaryService dataDictionaryService) { |
| 367 | 0 | this.dataDictionaryService = dataDictionaryService; |
| 368 | 0 | } |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
public BusinessObjectDictionaryService getBusinessObjectDictionaryService() { |
| 376 | 0 | return businessObjectDictionaryService; |
| 377 | |
} |
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
public void setBusinessObjectDictionaryService( |
| 386 | |
BusinessObjectDictionaryService businessObjectDictionaryService) { |
| 387 | 0 | this.businessObjectDictionaryService = businessObjectDictionaryService; |
| 388 | 0 | } |
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
public PersistenceStructureService getPersistenceStructureService() { |
| 396 | 0 | return persistenceStructureService; |
| 397 | |
} |
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
public void setPersistenceStructureService( |
| 406 | |
PersistenceStructureService persistenceStructureService) { |
| 407 | 0 | this.persistenceStructureService = persistenceStructureService; |
| 408 | 0 | } |
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
private Class getNestedBOClass(BusinessObject bo, String attributeName) { |
| 419 | |
|
| 420 | 0 | String[] nestedAttributes = StringUtils.split(attributeName, "."); |
| 421 | 0 | String attributeRefName = ""; |
| 422 | 0 | Class clazz = null; |
| 423 | 0 | if (nestedAttributes.length > 1) { |
| 424 | 0 | String attributeStringSoFar = ""; |
| 425 | 0 | for (int i = 0; i < nestedAttributes.length - 1; i++) { |
| 426 | |
try { |
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | 0 | if (i != 0) { |
| 433 | 0 | attributeStringSoFar = attributeStringSoFar + "."; |
| 434 | |
} |
| 435 | 0 | attributeStringSoFar = attributeStringSoFar |
| 436 | |
+ nestedAttributes[i]; |
| 437 | 0 | clazz = ObjectUtils.easyGetPropertyType(bo, |
| 438 | |
attributeStringSoFar); |
| 439 | 0 | } catch (InvocationTargetException ite) { |
| 440 | 0 | LOG.info(ite); |
| 441 | 0 | return null; |
| 442 | 0 | } catch (NoSuchMethodException nsme) { |
| 443 | 0 | LOG.info(nsme); |
| 444 | 0 | return null; |
| 445 | 0 | } catch (IllegalAccessException iae) { |
| 446 | 0 | LOG.info(iae); |
| 447 | 0 | return null; |
| 448 | 0 | } |
| 449 | |
} |
| 450 | |
} |
| 451 | 0 | return clazz; |
| 452 | |
} |
| 453 | |
|
| 454 | |
public RelationshipDefinition getDDRelationship(Class c, |
| 455 | |
String attributeName) { |
| 456 | 0 | DataDictionaryEntry entryBase = dataDictionaryService |
| 457 | |
.getDataDictionary().getDictionaryObjectEntry(c.getName()); |
| 458 | 0 | if (entryBase == null) { |
| 459 | 0 | return null; |
| 460 | |
} |
| 461 | |
|
| 462 | 0 | List<RelationshipDefinition> ddRelationships = entryBase |
| 463 | |
.getRelationships(); |
| 464 | 0 | RelationshipDefinition relationship = null; |
| 465 | 0 | int minKeys = Integer.MAX_VALUE; |
| 466 | 0 | for (RelationshipDefinition def : ddRelationships) { |
| 467 | |
|
| 468 | 0 | if (def.getPrimitiveAttributes().size() == 1) { |
| 469 | 0 | for (PrimitiveAttributeDefinition primitive : def |
| 470 | |
.getPrimitiveAttributes()) { |
| 471 | 0 | if (primitive.getSourceName().equals(attributeName) |
| 472 | |
|| def.getObjectAttributeName().equals( |
| 473 | |
attributeName)) { |
| 474 | 0 | relationship = def; |
| 475 | 0 | minKeys = 1; |
| 476 | 0 | break; |
| 477 | |
} |
| 478 | |
} |
| 479 | 0 | } else if (def.getPrimitiveAttributes().size() < minKeys) { |
| 480 | 0 | for (PrimitiveAttributeDefinition primitive : def |
| 481 | |
.getPrimitiveAttributes()) { |
| 482 | 0 | if (primitive.getSourceName().equals(attributeName) |
| 483 | |
|| def.getObjectAttributeName().equals( |
| 484 | |
attributeName)) { |
| 485 | 0 | relationship = def; |
| 486 | 0 | minKeys = def.getPrimitiveAttributes().size(); |
| 487 | 0 | break; |
| 488 | |
} |
| 489 | |
} |
| 490 | |
} |
| 491 | |
} |
| 492 | |
|
| 493 | 0 | if (relationship == null) { |
| 494 | 0 | for (RelationshipDefinition def : ddRelationships) { |
| 495 | 0 | if (def.hasIdentifier()) { |
| 496 | 0 | if (def.getIdentifier().getSourceName().equals( |
| 497 | |
attributeName)) { |
| 498 | 0 | relationship = def; |
| 499 | |
} |
| 500 | |
} |
| 501 | |
} |
| 502 | |
} |
| 503 | 0 | return relationship; |
| 504 | |
} |
| 505 | |
|
| 506 | |
public List<BusinessObjectRelationship> getBusinessObjectRelationships( BusinessObject bo ) { |
| 507 | 0 | if ( bo == null ) { |
| 508 | 0 | return null; |
| 509 | |
} |
| 510 | 0 | return getBusinessObjectRelationships( bo.getClass() ); |
| 511 | |
} |
| 512 | |
|
| 513 | |
@SuppressWarnings("unchecked") |
| 514 | |
public List<BusinessObjectRelationship> getBusinessObjectRelationships( Class<? extends BusinessObject> boClass) { |
| 515 | 0 | if (boClass == null) { |
| 516 | 0 | return null; |
| 517 | |
} |
| 518 | |
|
| 519 | 0 | Map<String, Class> referenceClasses = null; |
| 520 | 0 | if (PersistableBusinessObject.class.isAssignableFrom( boClass ) && getPersistenceStructureService().isPersistable(boClass)) { |
| 521 | 0 | referenceClasses = getPersistenceStructureService().listReferenceObjectFields(boClass); |
| 522 | |
} |
| 523 | 0 | DataDictionaryEntry ddEntry = dataDictionaryService.getDataDictionary().getDictionaryObjectEntry(boClass.getName()); |
| 524 | 0 | List<RelationshipDefinition> ddRelationships = (ddEntry == null ? new ArrayList<RelationshipDefinition>() : ddEntry.getRelationships()); |
| 525 | 0 | List<BusinessObjectRelationship> relationships = new ArrayList<BusinessObjectRelationship>(); |
| 526 | |
|
| 527 | |
|
| 528 | 0 | if (referenceClasses != null) { |
| 529 | 0 | for (Map.Entry<String, Class> entry : referenceClasses.entrySet()) { |
| 530 | 0 | if (isLookupable(entry.getValue())) { |
| 531 | 0 | Map<String, String> fkToPkRefs = persistenceStructureService.getForeignKeysForReference(boClass, entry.getKey()); |
| 532 | 0 | BusinessObjectRelationship rel = new BusinessObjectRelationship( boClass, entry.getKey(), entry.getValue() ); |
| 533 | 0 | for (Map.Entry<String, String> ref : fkToPkRefs.entrySet()) { |
| 534 | 0 | rel.getParentToChildReferences().put(ref.getKey(), ref.getValue()); |
| 535 | |
} |
| 536 | 0 | relationships.add(rel); |
| 537 | 0 | } |
| 538 | |
} |
| 539 | |
} |
| 540 | |
|
| 541 | 0 | for (RelationshipDefinition rd : ddRelationships) { |
| 542 | 0 | if (isLookupable(rd.getTargetClass())) { |
| 543 | 0 | BusinessObjectRelationship rel = new BusinessObjectRelationship( boClass, rd.getObjectAttributeName(), rd.getTargetClass()); |
| 544 | 0 | for (PrimitiveAttributeDefinition def : rd.getPrimitiveAttributes()) { |
| 545 | 0 | rel.getParentToChildReferences().put(def.getSourceName(),def.getTargetName()); |
| 546 | |
} |
| 547 | 0 | relationships.add(rel); |
| 548 | 0 | } |
| 549 | |
} |
| 550 | |
|
| 551 | 0 | return relationships; |
| 552 | |
} |
| 553 | |
|
| 554 | |
public Map<String, Class> getReferencesForForeignKey(BusinessObject bo, |
| 555 | |
String attributeName) { |
| 556 | 0 | List<BusinessObjectRelationship> businessObjectRelationships = getBusinessObjectRelationships(bo); |
| 557 | 0 | Map<String, Class> referencesForForeignKey = new HashMap<String, Class>(); |
| 558 | 0 | for (BusinessObjectRelationship businessObjectRelationship : businessObjectRelationships) { |
| 559 | 0 | if (businessObjectRelationship != null |
| 560 | |
&& businessObjectRelationship.getParentToChildReferences() != null |
| 561 | |
&& businessObjectRelationship.getParentToChildReferences() |
| 562 | |
.containsKey(attributeName)) { |
| 563 | 0 | referencesForForeignKey.put(businessObjectRelationship |
| 564 | |
.getParentAttributeName(), businessObjectRelationship |
| 565 | |
.getRelatedClass()); |
| 566 | |
} |
| 567 | |
} |
| 568 | 0 | return referencesForForeignKey; |
| 569 | |
} |
| 570 | |
|
| 571 | |
public List listPrimaryKeyFieldNames(Class clazz) { |
| 572 | 0 | if (persistenceStructureService.isPersistable(clazz)) { |
| 573 | 0 | return persistenceStructureService.listPrimaryKeyFieldNames(clazz); |
| 574 | |
} |
| 575 | 0 | ModuleService responsibleModuleService = getKualiModuleService() |
| 576 | |
.getResponsibleModuleService(clazz); |
| 577 | 0 | if (responsibleModuleService != null |
| 578 | |
&& responsibleModuleService.isExternalizable(clazz)) |
| 579 | 0 | return responsibleModuleService.listPrimaryKeyFieldNames(clazz); |
| 580 | |
|
| 581 | |
|
| 582 | |
|
| 583 | 0 | List<String> pks = dataDictionaryService.getDataDictionary() |
| 584 | |
.getBusinessObjectEntry(clazz.getName()).getPrimaryKeys(); |
| 585 | 0 | if(pks != null && !pks.isEmpty()) |
| 586 | 0 | return pks; |
| 587 | |
|
| 588 | 0 | return new ArrayList(); |
| 589 | |
|
| 590 | |
|
| 591 | |
|
| 592 | |
|
| 593 | |
} |
| 594 | |
|
| 595 | |
public KualiModuleService getKualiModuleService() { |
| 596 | 0 | return this.kualiModuleService; |
| 597 | |
} |
| 598 | |
|
| 599 | |
public void setKualiModuleService(KualiModuleService kualiModuleService) { |
| 600 | 0 | this.kualiModuleService = kualiModuleService; |
| 601 | 0 | } |
| 602 | |
|
| 603 | |
public BusinessObjectRelationship getRelationshipMetadata( |
| 604 | |
Class businessObjectClass, String attributeName, String attributePrefix) { |
| 605 | |
|
| 606 | 0 | RelationshipDefinition relationshipDefinition = getDDRelationship(businessObjectClass, attributeName); |
| 607 | 0 | if(relationshipDefinition==null) return null; |
| 608 | 0 | BusinessObjectRelationship businessObjectRelationship = |
| 609 | |
new BusinessObjectRelationship(relationshipDefinition.getSourceClass(), |
| 610 | |
relationshipDefinition.getObjectAttributeName(), relationshipDefinition.getTargetClass()); |
| 611 | 0 | if(!StringUtils.isEmpty(attributePrefix)) |
| 612 | 0 | attributePrefix += "."; |
| 613 | 0 | List<PrimitiveAttributeDefinition> primitives = relationshipDefinition.getPrimitiveAttributes(); |
| 614 | 0 | for(PrimitiveAttributeDefinition primitiveAttributeDefinition: primitives){ |
| 615 | 0 | businessObjectRelationship.getParentToChildReferences().put( |
| 616 | |
attributePrefix+primitiveAttributeDefinition.getSourceName(), primitiveAttributeDefinition.getTargetName()); |
| 617 | |
} |
| 618 | |
|
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | 0 | return businessObjectRelationship; |
| 624 | |
} |
| 625 | |
|
| 626 | |
public String getForeignKeyFieldName(Class businessObjectClass, String attributeName, String targetName) { |
| 627 | |
|
| 628 | 0 | String fkName = ""; |
| 629 | |
|
| 630 | |
|
| 631 | 0 | RelationshipDefinition relationshipDefinition = getDDRelationship(businessObjectClass, attributeName); |
| 632 | |
|
| 633 | 0 | if(relationshipDefinition!=null){ |
| 634 | 0 | List<PrimitiveAttributeDefinition> primitives = relationshipDefinition.getPrimitiveAttributes(); |
| 635 | 0 | for(PrimitiveAttributeDefinition primitiveAttributeDefinition: primitives){ |
| 636 | 0 | if(primitiveAttributeDefinition.getTargetName().equals(targetName)){ |
| 637 | 0 | fkName = primitiveAttributeDefinition.getSourceName(); |
| 638 | 0 | break; |
| 639 | |
} |
| 640 | |
} |
| 641 | |
} |
| 642 | |
|
| 643 | |
|
| 644 | 0 | if(StringUtils.isBlank(fkName) && PersistableBusinessObject.class.isAssignableFrom(businessObjectClass) && getPersistenceStructureService().isPersistable(businessObjectClass)) { |
| 645 | 0 | fkName = |
| 646 | |
getPersistenceStructureService().getForeignKeyFieldName(businessObjectClass, attributeName, targetName); |
| 647 | |
} |
| 648 | 0 | return fkName; |
| 649 | |
} |
| 650 | |
} |