| 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 java.beans.PropertyDescriptor; |
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.Collection; |
| 21 | |
import java.util.HashMap; |
| 22 | |
import java.util.Iterator; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.Map; |
| 25 | |
import java.util.Set; |
| 26 | |
|
| 27 | |
import javax.persistence.CascadeType; |
| 28 | |
|
| 29 | |
import org.apache.commons.beanutils.PropertyUtils; |
| 30 | |
import org.apache.commons.lang.StringUtils; |
| 31 | |
import org.kuali.rice.core.jpa.metadata.EntityDescriptor; |
| 32 | |
import org.kuali.rice.core.jpa.metadata.JoinColumnDescriptor; |
| 33 | |
import org.kuali.rice.core.jpa.metadata.MetadataManager; |
| 34 | |
import org.kuali.rice.core.jpa.metadata.ObjectDescriptor; |
| 35 | |
import org.kuali.rice.kns.bo.BusinessObjectRelationship; |
| 36 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
| 37 | |
import org.kuali.rice.kim.bo.Person; |
| 38 | |
import org.kuali.rice.kns.exception.ObjectNotABusinessObjectRuntimeException; |
| 39 | |
import org.kuali.rice.kns.exception.ReferenceAttributeDoesntExistException; |
| 40 | |
import org.kuali.rice.kns.exception.ReferenceAttributeNotAnOjbReferenceException; |
| 41 | |
import org.kuali.rice.kns.service.PersistenceStructureService; |
| 42 | |
import org.kuali.rice.kns.util.ForeignKeyFieldsPopulationState; |
| 43 | |
import org.kuali.rice.kns.util.spring.Cached; |
| 44 | |
|
| 45 | 0 | public class PersistenceStructureServiceJpaImpl extends PersistenceServiceImplBase implements PersistenceStructureService { |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 0 | public static Map<Class, Class> referenceConversionMap = new HashMap<Class, Class>(); |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
@Cached |
| 68 | |
public boolean isPersistable(Class clazz) { |
| 69 | 0 | boolean isPersistable = false; |
| 70 | |
|
| 71 | 0 | if (MetadataManager.getEntityDescriptor(clazz) != null) { |
| 72 | 0 | isPersistable = true; |
| 73 | |
} |
| 74 | |
|
| 75 | 0 | return isPersistable; |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
@Cached |
| 82 | |
public List getPrimaryKeys(Class clazz) { |
| 83 | 0 | List pkList = new ArrayList(); |
| 84 | |
|
| 85 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz); |
| 86 | 0 | for (org.kuali.rice.core.jpa.metadata.FieldDescriptor field : descriptor.getPrimaryKeys()) { |
| 87 | 0 | pkList.add(field.getName()); |
| 88 | |
} |
| 89 | |
|
| 90 | 0 | return pkList; |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
@Cached |
| 97 | |
public List listFieldNames(Class clazz) { |
| 98 | 0 | List fieldNames = new ArrayList(); |
| 99 | |
|
| 100 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz); |
| 101 | 0 | for (org.kuali.rice.core.jpa.metadata.FieldDescriptor field : descriptor.getFields()) { |
| 102 | 0 | fieldNames.add(field.getName()); |
| 103 | |
} |
| 104 | |
|
| 105 | 0 | return fieldNames; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public Object clearPrimaryKeyFields(Object persistableObject) { |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | 0 | return persistableObject; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
@Cached |
| 142 | |
|
| 143 | |
|
| 144 | |
public List listPersistableSubclasses(Class superclazz) { |
| 145 | 0 | List persistableSubclasses = new ArrayList(); |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | 0 | return persistableSubclasses; |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
@Cached |
| 168 | |
public Map<String, BusinessObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName, String attributePrefix) { |
| 169 | 0 | if (persistableClass == null) { |
| 170 | 0 | throw new IllegalArgumentException("invalid (null) persistableClass"); |
| 171 | |
} |
| 172 | 0 | if (StringUtils.isBlank(attributeName)) { |
| 173 | 0 | throw new IllegalArgumentException("invalid (blank) attributeName"); |
| 174 | |
} |
| 175 | |
|
| 176 | 0 | Map<String, BusinessObjectRelationship> relationships = new HashMap<String, BusinessObjectRelationship>(); |
| 177 | |
|
| 178 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableClass); |
| 179 | 0 | for (ObjectDescriptor objectDescriptor : descriptor.getObjectRelationships()) { |
| 180 | 0 | List<String> fks = objectDescriptor.getForeignKeyFields(); |
| 181 | 0 | if (fks.contains(attributeName) || objectDescriptor.getAttributeName().equals(attributeName)) { |
| 182 | 0 | Map<String, String> fkToPkRefs = getForeignKeysForReference(persistableClass, objectDescriptor.getAttributeName()); |
| 183 | 0 | BusinessObjectRelationship rel = new BusinessObjectRelationship(persistableClass, objectDescriptor.getAttributeName(), objectDescriptor.getTargetEntity()); |
| 184 | 0 | for (Map.Entry<String, String> ref : fkToPkRefs.entrySet()) { |
| 185 | 0 | if (StringUtils.isBlank(attributePrefix)) { |
| 186 | 0 | rel.getParentToChildReferences().put(ref.getKey(), ref.getValue()); |
| 187 | |
} else { |
| 188 | 0 | rel.getParentToChildReferences().put(attributePrefix + "." + ref.getKey(), ref.getValue()); |
| 189 | |
} |
| 190 | |
} |
| 191 | 0 | relationships.put(objectDescriptor.getAttributeName(), rel); |
| 192 | |
} |
| 193 | 0 | } |
| 194 | |
|
| 195 | 0 | return relationships; |
| 196 | |
} |
| 197 | |
|
| 198 | |
@Cached |
| 199 | |
|
| 200 | |
public Map<String, BusinessObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName) { |
| 201 | 0 | return getRelationshipMetadata(persistableClass, attributeName, null); |
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
@Cached |
| 209 | |
public String getForeignKeyFieldName(Class persistableObjectClass, String attributeName, String pkName) { |
| 210 | 0 | String fkName = null; |
| 211 | |
|
| 212 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass); |
| 213 | 0 | ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(attributeName); |
| 214 | 0 | if (objectDescriptor == null) { |
| 215 | 0 | throw new RuntimeException("Attribute name " + attributeName + " is not a valid reference to class " + persistableObjectClass.getName()); |
| 216 | |
} |
| 217 | 0 | List<org.kuali.rice.core.jpa.metadata.FieldDescriptor> matches = new ArrayList<org.kuali.rice.core.jpa.metadata.FieldDescriptor>(); |
| 218 | 0 | for (org.kuali.rice.core.jpa.metadata.FieldDescriptor field : descriptor.getFields()) { |
| 219 | 0 | String column = field.getColumn(); |
| 220 | 0 | for (JoinColumnDescriptor join : objectDescriptor.getJoinColumnDescriptors()) { |
| 221 | 0 | if (column != null && column.equals(join.getName())) { |
| 222 | 0 | matches.add(field); |
| 223 | |
} |
| 224 | |
} |
| 225 | 0 | } |
| 226 | |
|
| 227 | 0 | if (matches.size() == 1) { |
| 228 | 0 | fkName = matches.get(0).getName(); |
| 229 | |
} else { |
| 230 | 0 | throw new RuntimeException("Implement me!"); |
| 231 | |
} |
| 232 | |
|
| 233 | 0 | return fkName; |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
@Cached |
| 241 | |
public Map getReferencesForForeignKey(Class persistableObjectClass, String attributeName) { |
| 242 | 0 | Map referenceClasses = new HashMap(); |
| 243 | |
|
| 244 | 0 | if (PersistableBusinessObject.class.isAssignableFrom(persistableObjectClass)) { |
| 245 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass); |
| 246 | 0 | for (ObjectDescriptor objectDescriptor : descriptor.getObjectRelationships()) { |
| 247 | 0 | List<String> refFkNames = objectDescriptor.getForeignKeyFields(); |
| 248 | 0 | for (String fk : refFkNames) { |
| 249 | 0 | if (fk.equals(attributeName)) { |
| 250 | 0 | referenceClasses.put(objectDescriptor.getAttributeName(), objectDescriptor.getTargetEntity()); |
| 251 | |
} |
| 252 | |
} |
| 253 | 0 | } |
| 254 | |
} |
| 255 | |
|
| 256 | 0 | return referenceClasses; |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
@Cached |
| 276 | |
public Map getForeignKeysForReference(Class clazz, String attributeName) { |
| 277 | |
|
| 278 | |
|
| 279 | 0 | if (clazz == null) { |
| 280 | 0 | throw new IllegalArgumentException("The Class passed in for the clazz argument was null."); |
| 281 | |
} |
| 282 | 0 | if (attributeName == null) { |
| 283 | 0 | throw new IllegalArgumentException("The String passed in for the attributeName argument was null."); |
| 284 | |
} |
| 285 | |
|
| 286 | |
|
| 287 | 0 | Class attributeClass = getBusinessObjectAttributeClass(clazz, attributeName); |
| 288 | 0 | if (attributeClass == null) { |
| 289 | 0 | throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + attributeName + "' does not exist on class: '" + clazz.getName() + "'."); |
| 290 | |
} |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | 0 | if (!PersistableBusinessObject.class.isAssignableFrom(attributeClass)) { |
| 295 | 0 | throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + attributeName + ") is of class: '" + attributeClass.getName() + "' and is not a descendent of BusinessObject. Only descendents of BusinessObject can be used."); |
| 296 | |
} |
| 297 | |
|
| 298 | 0 | return determineFkMap(clazz, attributeName, attributeClass); |
| 299 | |
} |
| 300 | |
|
| 301 | |
private Map determineFkMap(Class clazz, String attributeName, Class attributeClass) { |
| 302 | 0 | Map fkMap = new HashMap(); |
| 303 | 0 | EntityDescriptor entityDescriptor = MetadataManager.getEntityDescriptor(clazz); |
| 304 | 0 | ObjectDescriptor objectDescriptor = entityDescriptor.getObjectDescriptorByName(attributeName); |
| 305 | 0 | if (objectDescriptor == null) { |
| 306 | 0 | throw new ReferenceAttributeNotAnOjbReferenceException("Attribute requested (" + attributeName + ") is not defined in JPA annotations for class: '" + clazz.getName() + "'"); |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | 0 | if (!attributeClass.equals(objectDescriptor.getTargetEntity())) { |
| 315 | 0 | if (referenceConversionMap.containsKey(attributeClass)) { |
| 316 | 0 | attributeClass = referenceConversionMap.get(attributeClass); |
| 317 | |
} else { |
| 318 | 0 | throw new RuntimeException("The Class of the Java member [" + attributeClass.getName() + "] '" + attributeName + "' does not match the class of the reference [" + objectDescriptor.getTargetEntity().getName() + "]. " + "This is an unhandled special case for which special code needs to be written in this class."); |
| 319 | |
} |
| 320 | |
} |
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | 0 | List<String> fkFields = objectDescriptor.getForeignKeyFields(); |
| 325 | 0 | Iterator<String> fkIterator = fkFields.iterator(); |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | 0 | List pkFields = getPrimaryKeys(attributeClass); |
| 330 | 0 | Iterator pkIterator = pkFields.iterator(); |
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | 0 | if (pkFields.size() != fkFields.size()) { |
| 335 | 0 | throw new RuntimeException("KualiPersistenceStructureService Error: The number of " + "foreign keys doesnt match the number of primary keys."); |
| 336 | |
} |
| 337 | |
|
| 338 | |
|
| 339 | 0 | while (fkIterator.hasNext()) { |
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | 0 | if (!pkIterator.hasNext()) { |
| 344 | 0 | throw new RuntimeException("The number of foriegn keys dont match the number of primary keys for the reference '" + attributeName + "', on BO of type '" + clazz.getName() + "'. " + "This should never happen under normal circumstances."); |
| 345 | |
} |
| 346 | |
|
| 347 | |
|
| 348 | 0 | String fkFieldName = (String) fkIterator.next(); |
| 349 | 0 | String pkFieldName = (String) pkIterator.next(); |
| 350 | |
|
| 351 | |
|
| 352 | 0 | fkMap.put(fkFieldName, pkFieldName); |
| 353 | 0 | } |
| 354 | 0 | return fkMap; |
| 355 | |
} |
| 356 | |
|
| 357 | |
@Cached |
| 358 | |
public Map<String, String> getInverseForeignKeysForCollection(Class boClass, String collectionName) { |
| 359 | |
|
| 360 | 0 | if (boClass == null) { |
| 361 | 0 | throw new IllegalArgumentException("The Class passed in for the boClass argument was null."); |
| 362 | |
} |
| 363 | 0 | if (collectionName == null) { |
| 364 | 0 | throw new IllegalArgumentException("The String passed in for the attributeName argument was null."); |
| 365 | |
} |
| 366 | |
|
| 367 | 0 | PropertyDescriptor propertyDescriptor = null; |
| 368 | |
|
| 369 | |
|
| 370 | |
Object classInstance; |
| 371 | |
try { |
| 372 | 0 | classInstance = boClass.newInstance(); |
| 373 | 0 | } catch (Exception e) { |
| 374 | 0 | throw new RuntimeException(e); |
| 375 | 0 | } |
| 376 | |
|
| 377 | |
|
| 378 | |
try { |
| 379 | 0 | propertyDescriptor = PropertyUtils.getPropertyDescriptor(classInstance, collectionName); |
| 380 | 0 | } catch (Exception e) { |
| 381 | 0 | throw new RuntimeException(e); |
| 382 | 0 | } |
| 383 | 0 | if (propertyDescriptor == null) { |
| 384 | 0 | throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + collectionName + "' does not exist " + "on class: '" + boClass.getName() + "'. GFK"); |
| 385 | |
} |
| 386 | |
|
| 387 | |
|
| 388 | 0 | Class attributeClass = propertyDescriptor.getPropertyType(); |
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | 0 | if (!Collection.class.isAssignableFrom(attributeClass)) { |
| 393 | 0 | throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + collectionName + ") is of class: " + "'" + attributeClass.getName() + "' and is not a " + "descendent of Collection"); |
| 394 | |
} |
| 395 | |
|
| 396 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 397 | 0 | org.kuali.rice.core.jpa.metadata.CollectionDescriptor cd = descriptor.getCollectionDescriptorByName(collectionName); |
| 398 | 0 | List<String> childPrimaryKeys = cd.getForeignKeyFields(); |
| 399 | |
|
| 400 | 0 | List parentForeignKeys = getPrimaryKeys(boClass); |
| 401 | |
|
| 402 | 0 | if (parentForeignKeys.size() != childPrimaryKeys.size()) { |
| 403 | 0 | throw new RuntimeException("The number of keys in the class descriptor and the inverse foreign key mapping for the collection descriptors do not match."); |
| 404 | |
} |
| 405 | |
|
| 406 | 0 | Map<String, String> fkToPkMap = new HashMap<String, String>(); |
| 407 | 0 | Iterator pFKIter = parentForeignKeys.iterator(); |
| 408 | 0 | Iterator cPKIterator = childPrimaryKeys.iterator(); |
| 409 | |
|
| 410 | 0 | while (pFKIter.hasNext()) { |
| 411 | 0 | String parentForeignKey = (String) pFKIter.next(); |
| 412 | 0 | String childPrimaryKey = (String) cPKIterator.next(); |
| 413 | |
|
| 414 | 0 | fkToPkMap.put(parentForeignKey, childPrimaryKey); |
| 415 | 0 | } |
| 416 | 0 | return fkToPkMap; |
| 417 | |
} |
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
@Cached |
| 423 | |
public Map getNestedForeignKeyMap(Class persistableObjectClass) { |
| 424 | 0 | Map fkMap = new HashMap(); |
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass); |
| 430 | 0 | for (ObjectDescriptor objectReferenceDescriptor : descriptor.getObjectRelationships()) { |
| 431 | 0 | EntityDescriptor referenceDescriptor = MetadataManager.getEntityDescriptor(objectReferenceDescriptor.getTargetEntity()); |
| 432 | 0 | List<String> fkFields = objectReferenceDescriptor.getForeignKeyFields(); |
| 433 | 0 | Set<org.kuali.rice.core.jpa.metadata.FieldDescriptor> pkFields = referenceDescriptor.getPrimaryKeys(); |
| 434 | 0 | int i = 0; |
| 435 | 0 | for (org.kuali.rice.core.jpa.metadata.FieldDescriptor fd : pkFields) { |
| 436 | 0 | fkMap.put(objectReferenceDescriptor.getAttributeName() + "." + fd.getName(), fkFields.get(i)); |
| 437 | 0 | i++; |
| 438 | |
} |
| 439 | 0 | } |
| 440 | |
|
| 441 | 0 | return fkMap; |
| 442 | |
} |
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
public boolean hasPrimaryKeyFieldValues(Object persistableObject) { |
| 448 | 0 | Map keyFields = getPrimaryKeyFieldValues(persistableObject); |
| 449 | |
|
| 450 | 0 | boolean emptyField = false; |
| 451 | 0 | for (Iterator i = keyFields.entrySet().iterator(); !emptyField && i.hasNext();) { |
| 452 | 0 | Map.Entry e = (Map.Entry) i.next(); |
| 453 | |
|
| 454 | 0 | Object fieldValue = e.getValue(); |
| 455 | 0 | if (fieldValue == null) { |
| 456 | 0 | emptyField = true; |
| 457 | 0 | } else if (fieldValue instanceof String) { |
| 458 | 0 | if (StringUtils.isEmpty((String) fieldValue)) { |
| 459 | 0 | emptyField = true; |
| 460 | |
} else { |
| 461 | 0 | emptyField = false; |
| 462 | |
} |
| 463 | |
} |
| 464 | 0 | } |
| 465 | |
|
| 466 | 0 | return !emptyField; |
| 467 | |
} |
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
public ForeignKeyFieldsPopulationState getForeignKeyFieldsPopulationState(PersistableBusinessObject bo, String referenceName) { |
| 474 | |
|
| 475 | 0 | boolean allFieldsPopulated = true; |
| 476 | 0 | boolean anyFieldsPopulated = false; |
| 477 | 0 | List<String> unpopulatedFields = new ArrayList<String>(); |
| 478 | |
|
| 479 | |
|
| 480 | 0 | if (bo == null) { |
| 481 | 0 | throw new IllegalArgumentException("The Class passed in for the BusinessObject argument was null."); |
| 482 | |
} |
| 483 | 0 | if (StringUtils.isBlank(referenceName)) { |
| 484 | 0 | throw new IllegalArgumentException("The String passed in for the referenceName argument was null or empty."); |
| 485 | |
} |
| 486 | |
|
| 487 | 0 | PropertyDescriptor propertyDescriptor = null; |
| 488 | |
|
| 489 | |
|
| 490 | |
try { |
| 491 | 0 | propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName); |
| 492 | 0 | } catch (Exception e) { |
| 493 | 0 | throw new RuntimeException(e); |
| 494 | 0 | } |
| 495 | 0 | if (propertyDescriptor == null) { |
| 496 | 0 | throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'."); |
| 497 | |
} |
| 498 | |
|
| 499 | |
|
| 500 | 0 | Class referenceClass = propertyDescriptor.getPropertyType(); |
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | 0 | if (!PersistableBusinessObject.class.isAssignableFrom(referenceClass)) { |
| 505 | 0 | throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of BusinessObject. Only descendents of BusinessObject " + "can be used."); |
| 506 | |
} |
| 507 | |
|
| 508 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(bo.getClass()); |
| 509 | 0 | ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(referenceName); |
| 510 | |
|
| 511 | 0 | if (objectDescriptor == null) { |
| 512 | 0 | throw new ReferenceAttributeNotAnOjbReferenceException("Attribute requested (" + referenceName + ") is not listed " + "in OJB as a reference-descriptor for class: '" + bo.getClass().getName() + "'"); |
| 513 | |
} |
| 514 | |
|
| 515 | 0 | List<String> fkFields = objectDescriptor.getForeignKeyFields(); |
| 516 | 0 | Iterator fkIterator = fkFields.iterator(); |
| 517 | |
|
| 518 | |
|
| 519 | 0 | while (fkIterator.hasNext()) { |
| 520 | |
|
| 521 | |
|
| 522 | 0 | String fkFieldName = (String) fkIterator.next(); |
| 523 | |
|
| 524 | |
|
| 525 | 0 | Object fkFieldValue = null; |
| 526 | |
try { |
| 527 | 0 | fkFieldValue = PropertyUtils.getSimpleProperty(bo, fkFieldName); |
| 528 | |
} |
| 529 | |
|
| 530 | |
|
| 531 | 0 | catch (Exception e) { |
| 532 | 0 | throw new RuntimeException(e); |
| 533 | 0 | } |
| 534 | |
|
| 535 | |
|
| 536 | 0 | if (fkFieldValue == null) { |
| 537 | 0 | allFieldsPopulated = false; |
| 538 | 0 | unpopulatedFields.add(fkFieldName); |
| 539 | 0 | } else if (fkFieldValue instanceof String) { |
| 540 | 0 | if (StringUtils.isBlank((String) fkFieldValue)) { |
| 541 | 0 | allFieldsPopulated = false; |
| 542 | 0 | unpopulatedFields.add(fkFieldName); |
| 543 | |
} else { |
| 544 | 0 | anyFieldsPopulated = true; |
| 545 | |
} |
| 546 | |
} else { |
| 547 | 0 | anyFieldsPopulated = true; |
| 548 | |
} |
| 549 | 0 | } |
| 550 | |
|
| 551 | |
|
| 552 | |
|
| 553 | 0 | if (allFieldsPopulated) { |
| 554 | 0 | if (!unpopulatedFields.isEmpty()) { |
| 555 | 0 | throw new RuntimeException("The flag is set that indicates all fields are populated, but there " + "are fields present in the unpopulatedFields list. This should never happen, and indicates " + "that the logic in this method is broken."); |
| 556 | |
} |
| 557 | |
} |
| 558 | |
|
| 559 | 0 | return new ForeignKeyFieldsPopulationState(allFieldsPopulated, anyFieldsPopulated, unpopulatedFields); |
| 560 | |
} |
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | |
|
| 565 | |
@Cached |
| 566 | |
public Map<String, Class> listReferenceObjectFields(Class boClass) { |
| 567 | |
|
| 568 | 0 | if (boClass == null) { |
| 569 | 0 | throw new IllegalArgumentException("Class specified in the parameter was null."); |
| 570 | |
} |
| 571 | 0 | if (!PersistableBusinessObject.class.isAssignableFrom(boClass)) { |
| 572 | 0 | throw new IllegalArgumentException("Class specified [" + boClass.getName() + "] must be a class that " + "inherits from BusinessObject."); |
| 573 | |
} |
| 574 | |
|
| 575 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 576 | 0 | Map<String, Class> references = new HashMap(); |
| 577 | 0 | for (org.kuali.rice.core.jpa.metadata.ObjectDescriptor od : descriptor.getObjectRelationships()) { |
| 578 | 0 | references.put(od.getAttributeName(), od.getTargetEntity()); |
| 579 | |
} |
| 580 | |
|
| 581 | 0 | return references; |
| 582 | |
} |
| 583 | |
|
| 584 | |
@Cached |
| 585 | |
public Map<String, Class> listCollectionObjectTypes(Class boClass) { |
| 586 | 0 | if (boClass == null) { |
| 587 | 0 | throw new IllegalArgumentException("Class specified in the parameter was null."); |
| 588 | |
} |
| 589 | |
|
| 590 | 0 | Map<String, Class> references = new HashMap(); |
| 591 | |
|
| 592 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 593 | 0 | if (descriptor == null) { |
| 594 | 0 | return references; |
| 595 | |
} |
| 596 | |
|
| 597 | 0 | for (org.kuali.rice.core.jpa.metadata.CollectionDescriptor cd : descriptor.getCollectionRelationships()) { |
| 598 | 0 | references.put(cd.getAttributeName(), cd.getTargetEntity()); |
| 599 | |
} |
| 600 | |
|
| 601 | 0 | return references; |
| 602 | |
} |
| 603 | |
|
| 604 | |
public Map<String, Class> listCollectionObjectTypes(PersistableBusinessObject bo) { |
| 605 | |
|
| 606 | 0 | if (bo == null) { |
| 607 | 0 | throw new IllegalArgumentException("BO specified in the parameter was null."); |
| 608 | |
} |
| 609 | 0 | if (!(bo instanceof PersistableBusinessObject)) { |
| 610 | 0 | throw new IllegalArgumentException("BO specified [" + bo.getClass().getName() + "] must be a class that " + "inherits from BusinessObject."); |
| 611 | |
} |
| 612 | |
|
| 613 | 0 | return listCollectionObjectTypes(bo.getClass()); |
| 614 | |
} |
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
public Map<String, Class> listReferenceObjectFields(PersistableBusinessObject bo) { |
| 620 | |
|
| 621 | 0 | if (bo == null) { |
| 622 | 0 | throw new IllegalArgumentException("BO specified in the parameter was null."); |
| 623 | |
} |
| 624 | 0 | if (!(bo instanceof PersistableBusinessObject)) { |
| 625 | 0 | throw new IllegalArgumentException("BO specified [" + bo.getClass().getName() + "] must be a class that " + "inherits from BusinessObject."); |
| 626 | |
} |
| 627 | |
|
| 628 | 0 | return listReferenceObjectFields(bo.getClass()); |
| 629 | |
} |
| 630 | |
|
| 631 | |
@Cached |
| 632 | |
public boolean isReferenceUpdatable(Class boClass, String referenceName) { |
| 633 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 634 | 0 | return descriptor.getObjectDescriptorByName(referenceName).isUpdateable(); |
| 635 | |
|
| 636 | |
|
| 637 | |
|
| 638 | |
|
| 639 | |
|
| 640 | |
|
| 641 | |
} |
| 642 | |
|
| 643 | |
@Cached |
| 644 | |
public boolean isCollectionUpdatable(Class boClass, String collectionName) { |
| 645 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 646 | 0 | return descriptor.getCollectionDescriptorByName(collectionName).isUpdateable(); |
| 647 | |
|
| 648 | |
|
| 649 | |
|
| 650 | |
|
| 651 | |
|
| 652 | |
|
| 653 | |
} |
| 654 | |
|
| 655 | |
@Cached |
| 656 | |
public boolean hasCollection(Class boClass, String collectionName) { |
| 657 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 658 | 0 | return descriptor.getCollectionDescriptorByName(collectionName) != null; |
| 659 | |
} |
| 660 | |
|
| 661 | |
@Cached |
| 662 | |
public boolean hasReference(Class boClass, String referenceName) { |
| 663 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 664 | 0 | return descriptor.getObjectDescriptorByName(referenceName) != null; |
| 665 | |
} |
| 666 | |
|
| 667 | |
|
| 668 | |
|
| 669 | |
|
| 670 | |
@Cached |
| 671 | |
public String getTableName(Class<? extends PersistableBusinessObject> boClass) { |
| 672 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
| 673 | 0 | return descriptor.getTable(); |
| 674 | |
} |
| 675 | |
} |
| 676 | |
|