1 21 package oracle.toplink.essentials.mappings; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.expressions.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.internal.queryframework.JoinedAttributeManager; 29 import oracle.toplink.essentials.internal.sessions.*; 30 import oracle.toplink.essentials.internal.descriptors.ObjectBuilder; 31 import oracle.toplink.essentials.sessions.DatabaseRecord; 32 import oracle.toplink.essentials.queryframework.*; 33 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 34 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 35 import oracle.toplink.essentials.internal.sessions.AbstractSession; 36 import oracle.toplink.essentials.descriptors.ClassDescriptor; 37 38 49 public class AggregateObjectMapping extends AggregateMapping implements RelationalMapping { 50 51 59 protected boolean isNullAllowed; 60 61 62 protected transient Map aggregateToSourceFieldNames; 63 64 67 public AggregateObjectMapping() { 68 aggregateToSourceFieldNames = new HashMap(5); 69 isNullAllowed = true; 70 } 71 72 75 public boolean isRelationalMapping() { 76 return true; 77 } 78 79 84 public void addFieldNameTranslation(String sourceFieldName, String aggregateFieldName) { 85 String unQualifiedAggregateFieldName = aggregateFieldName.substring(aggregateFieldName.lastIndexOf('.') + 1); getAggregateToSourceFieldNames().put(unQualifiedAggregateFieldName, sourceFieldName); 87 } 88 89 94 protected boolean allAggregateFieldsAreNull(AbstractRecord databaseRow) { 95 for (Enumeration fieldsEnum = getReferenceFields().elements(); 96 fieldsEnum.hasMoreElements();) { 97 DatabaseField field = (DatabaseField)fieldsEnum.nextElement(); 98 Object value = databaseRow.get(field); 99 if (value != null) { 100 return false; 101 } 102 } 103 return true; 104 } 105 106 118 public void allowNull() { 119 setIsNullAllowed(true); 120 } 121 122 127 protected boolean backupAttributeValueIsNull(WriteObjectQuery query) { 128 if (query.getSession().isUnitOfWork()) { 129 Object backupAttributeValue = getAttributeValueFromObject(query.getBackupClone()); 130 if (backupAttributeValue == null) { 131 return true; 132 } 133 } 134 return false; 135 } 136 137 144 protected Object buildAggregateFromRow(AbstractRecord databaseRow, Object targetObject, JoinedAttributeManager joinManager, boolean buildShallowOriginal, AbstractSession session) throws DatabaseException { 145 if (isNullAllowed() && allAggregateFieldsAreNull(databaseRow)) { 147 return null; 148 } 149 150 Object aggregate = null; 153 ClassDescriptor descriptor = getReferenceDescriptor(); 154 boolean refreshing = true; 155 if (descriptor.hasInheritance()) { 156 Class newAggregateClass = descriptor.getInheritancePolicy().classFromRow(databaseRow, session); 157 descriptor = getReferenceDescriptor(newAggregateClass, session); 158 159 if (joinManager.getBaseQuery().shouldRefreshIdentityMapResult()) { 160 aggregate = getMatchingAttributeValueFromObject(databaseRow, targetObject, session, descriptor); 161 if ((aggregate != null) && (aggregate.getClass() != newAggregateClass)) { 162 aggregate = descriptor.getObjectBuilder().buildNewInstance(); 165 refreshing = false; 166 } 167 } 168 } else { 169 if (joinManager.getBaseQuery().shouldRefreshIdentityMapResult()) { 170 aggregate = getMatchingAttributeValueFromObject(databaseRow, targetObject, session, descriptor); 171 } 172 } 173 174 if (aggregate == null) { 175 aggregate = descriptor.getObjectBuilder().buildNewInstance(); 176 refreshing = false; 177 } 178 179 ObjectBuildingQuery nestedQuery = joinManager.getBaseQuery(); 180 nestedQuery.setSession(session); if (joinManager.getBaseQuery().isObjectLevelReadQuery()){ 182 if (joinManager.isAttributeJoined(getDescriptor(), getAttributeName()) ){ 183 nestedQuery = (ObjectLevelReadQuery)((ObjectLevelReadQuery)joinManager.getBaseQuery()).deepClone(); 185 ((ObjectLevelReadQuery)nestedQuery).getJoinedAttributeManager().setJoinedAttributeExpressions_(extractNestedExpressions(joinManager.getJoinedAttributeExpressions(), joinManager.getBaseExpressionBuilder(), false)); 187 nestedQuery.setDescriptor(descriptor); 188 } 189 } 190 if (buildShallowOriginal) { 191 descriptor.getObjectBuilder().buildAttributesIntoShallowObject(aggregate, databaseRow, nestedQuery); 192 } else if (session.isUnitOfWork()) { 193 descriptor.getObjectBuilder().buildAttributesIntoWorkingCopyClone(aggregate, nestedQuery, joinManager, databaseRow, (UnitOfWorkImpl)session, refreshing); 194 } else { 195 descriptor.getObjectBuilder().buildAttributesIntoObject(aggregate, databaseRow, nestedQuery, joinManager, refreshing); 196 } 197 return aggregate; 198 } 199 200 205 protected AbstractRecord buildNullReferenceRow() { 206 AbstractRecord result = new DatabaseRecord(getReferenceFields().size()); 207 for (Enumeration stream = getReferenceFields().elements(); stream.hasMoreElements();) { 208 result.put((DatabaseField)stream.nextElement(), null); 209 } 210 return result; 211 } 212 213 219 public Expression buildObjectJoinExpression(Expression expression, Object value, AbstractSession session) { 220 Expression attributeByAttributeComparison = null; 221 Expression join = null; 222 Object attributeValue = null; 223 224 ClassDescriptor referenceDescriptor = getReferenceDescriptor(); 227 if ((value != null) && !referenceDescriptor.getJavaClass().isInstance(value)) { 228 throw QueryException.incorrectClassForObjectComparison(expression, value, this); 229 } 230 Enumeration mappings = referenceDescriptor.getMappings().elements(); 231 for (; mappings.hasMoreElements();) { 232 DatabaseMapping mapping = (DatabaseMapping)mappings.nextElement(); 233 if (value == null) { 234 attributeValue = null; 235 } else { 236 attributeValue = mapping.getAttributeValueFromObject(value); 237 } 238 join = expression.get(mapping.getAttributeName()).equal(attributeValue); 239 if (attributeByAttributeComparison == null) { 240 attributeByAttributeComparison = join; 241 } else { 242 attributeByAttributeComparison = attributeByAttributeComparison.and(join); 243 } 244 } 245 return attributeByAttributeComparison; 246 } 247 248 252 public Expression buildObjectJoinExpression(Expression expression, Expression argument, AbstractSession session) { 253 Expression attributeByAttributeComparison = null; 254 255 Enumeration mappingsEnum = getReferenceDescriptor().getMappings().elements(); 257 for (; mappingsEnum.hasMoreElements();) { 258 DatabaseMapping mapping = (DatabaseMapping)mappingsEnum.nextElement(); 259 String attributeName = mapping.getAttributeName(); 260 Expression join = expression.get(attributeName).equal(argument.get(attributeName)); 261 if (attributeByAttributeComparison == null) { 262 attributeByAttributeComparison = join; 263 } else { 264 attributeByAttributeComparison = attributeByAttributeComparison.and(join); 265 } 266 } 267 return attributeByAttributeComparison; 268 } 269 270 275 protected AbstractRecord buildRowFromAggregate(Object object, Object attributeValue, AbstractSession session) throws DescriptorException { 276 return buildRowFromAggregate(object, attributeValue, session, false); 277 } 278 279 284 protected AbstractRecord buildRowFromAggregate(Object object, Object attributeValue, AbstractSession session, boolean forceWriteOfReadOnlyClasses) throws DescriptorException { 285 if (attributeValue == null) { 286 if (isNullAllowed()) { 287 return buildNullReferenceRow(); 288 } else { 289 throw DescriptorException.nullForNonNullAggregate(object, this); 290 } 291 } else { 292 if ((!forceWriteOfReadOnlyClasses) && (session.isClassReadOnly(attributeValue.getClass()))) { 293 return new DatabaseRecord(1); 294 } else { 295 return getObjectBuilder(attributeValue, session).buildRow(attributeValue, session); 296 } 297 } 298 } 299 300 305 protected AbstractRecord buildRowFromAggregateWithChangeRecord(ChangeRecord changeRecord, ObjectChangeSet objectChangeSet, AbstractSession session) throws DescriptorException { 306 return buildRowFromAggregateWithChangeRecord(changeRecord, objectChangeSet, session, false); 307 } 308 309 314 protected AbstractRecord buildRowFromAggregateWithChangeRecord(ChangeRecord changeRecord, ObjectChangeSet objectChangeSet, AbstractSession session, boolean forceWriteOfReadOnlyClasses) throws DescriptorException { 315 if (objectChangeSet == null) { 316 if (isNullAllowed()) { 317 return buildNullReferenceRow(); 318 } else { 319 Object object = ((ObjectChangeSet)changeRecord.getOwner()).getUnitOfWorkClone(); 320 throw DescriptorException.nullForNonNullAggregate(object, this); 321 } 322 } else { 323 if ((!forceWriteOfReadOnlyClasses) && (session.isClassReadOnly(objectChangeSet.getClassType(session)))) { 324 return new DatabaseRecord(1); 325 } else { 326 return getReferenceDescriptor(objectChangeSet.getClassType(session), session).getObjectBuilder().buildRowWithChangeSet(objectChangeSet, session); 327 } 328 } 329 } 330 331 336 protected AbstractRecord buildRowFromAggregateForUpdate(WriteObjectQuery query, Object attributeValue) throws DescriptorException { 337 if (attributeValue == null) { 338 if (isNullAllowed()) { 339 if (backupAttributeValueIsNull(query)) { 340 return new DatabaseRecord(1); } else { 342 return buildNullReferenceRow(); 343 } 344 } else { 345 throw DescriptorException.nullForNonNullAggregate(query.getObject(), this); 346 } 347 } else if ((query.getBackupClone() != null) && ((getMatchingBackupAttributeValue(query, attributeValue) == null) || !(attributeValue.getClass().equals(getMatchingBackupAttributeValue(query, attributeValue).getClass())))) { 348 return getObjectBuilder(attributeValue, query.getSession()).buildRow(attributeValue, query.getSession()); 349 } else { 350 if (query.getSession().isClassReadOnly(attributeValue.getClass())) { 351 return new DatabaseRecord(1); 352 } 353 WriteObjectQuery clonedQuery = (WriteObjectQuery)query.clone(); 354 clonedQuery.setObject(attributeValue); 355 if (query.getSession().isUnitOfWork()) { 356 Object backupAttributeValue = getMatchingBackupAttributeValue(query, attributeValue); 357 if (backupAttributeValue == null) { 358 backupAttributeValue = getObjectBuilder(attributeValue, query.getSession()).buildNewInstance(); 359 } 360 clonedQuery.setBackupClone(backupAttributeValue); 361 } 362 return getObjectBuilder(attributeValue, query.getSession()).buildRowForUpdate(clonedQuery); 363 } 364 } 365 366 370 public void buildClone(Object original, Object clone, UnitOfWorkImpl unitOfWork, JoinedAttributeManager joinedAttributeManager) { 371 Object attributeValue = getAttributeValueFromObject(original); 372 Object aggregateClone = buildClonePart(original, attributeValue, unitOfWork); 373 374 if (aggregateClone != null) { 375 ClassDescriptor descriptor = getReferenceDescriptor(aggregateClone, unitOfWork); 376 descriptor.getObjectChangePolicy().setAggregateChangeListener(clone, aggregateClone, unitOfWork, descriptor, getAttributeName()); 377 } 378 379 setAttributeValueInObject(clone, aggregateClone); 380 } 381 382 399 public void buildCloneFromRow(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object clone, ObjectBuildingQuery sourceQuery, UnitOfWorkImpl unitOfWork, AbstractSession executionSession) { 400 Object clonedAttributeValue = buildAggregateFromRow(databaseRow, clone, joinManager, false, executionSession); 405 ClassDescriptor descriptor = getReferenceDescriptor(clonedAttributeValue, unitOfWork); 406 descriptor.getObjectChangePolicy().setAggregateChangeListener(clone, clonedAttributeValue, unitOfWork, descriptor, getAttributeName()); 407 setAttributeValueInObject(clone, clonedAttributeValue); 408 return; 409 } 410 411 418 public void buildShallowOriginalFromRow(AbstractRecord databaseRow, Object original, JoinedAttributeManager joinManager, AbstractSession executionSession) { 419 Object aggregate = buildAggregateFromRow(databaseRow, original, joinManager, true, executionSession); setAttributeValueInObject(original, aggregate); 421 } 422 423 428 protected AbstractRecord buildTemplateInsertRow(AbstractSession session) { 429 AbstractRecord result = getReferenceDescriptor().getObjectBuilder().buildTemplateInsertRow(session); 430 List processedMappings = (List)getReferenceDescriptor().getMappings().clone(); 431 if (getReferenceDescriptor().hasInheritance()) { 432 Enumeration children = getReferenceDescriptor().getInheritancePolicy().getChildDescriptors().elements(); 433 while (children.hasMoreElements()) { 434 Enumeration mappings = ((ClassDescriptor)children.nextElement()).getMappings().elements(); 435 while (mappings.hasMoreElements()) { 436 DatabaseMapping mapping = (DatabaseMapping)mappings.nextElement(); 437 438 if (!processedMappings.contains(mapping)) { 440 mapping.writeInsertFieldsIntoRow(result, session); 441 processedMappings.add(mapping); 442 } 443 } 444 } 445 } 446 return result; 447 } 448 449 453 public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects){ 454 Object objectReferenced = getRealAttributeValueFromObject(object, uow); 457 if ((objectReferenced == null)){ 458 return ; 459 } 460 if ( ! visitedObjects.contains(objectReferenced)){ 461 visitedObjects.put(objectReferenced, objectReferenced); 462 ObjectBuilder builder = getReferenceDescriptor(objectReferenced.getClass(), uow).getObjectBuilder(); 463 builder.cascadePerformRemove(objectReferenced, uow, visitedObjects); 464 } 465 466 } 467 468 472 public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects){ 473 Object objectReferenced = getRealAttributeValueFromObject(object, uow); 475 if ( (objectReferenced == null) ){ 476 return ; 477 } 478 if ( ! visitedObjects.contains(objectReferenced)){ 479 visitedObjects.put(objectReferenced, objectReferenced); 480 ObjectBuilder builder = getReferenceDescriptor(objectReferenced.getClass(), uow).getObjectBuilder(); 481 builder.cascadeRegisterNewForCreate(objectReferenced, uow, visitedObjects); 482 } 483 } 484 485 489 protected Vector collectFields() { 490 return getReferenceFields(); 491 } 492 493 505 public void dontAllowNull() { 506 setIsNullAllowed(false); 507 } 508 509 513 public Vector getAggregateToSourceFieldNameAssociations() { 514 Vector associations = new Vector(getAggregateToSourceFieldNames().size()); 515 Iterator aggregateEnum = getAggregateToSourceFieldNames().keySet().iterator(); 516 Iterator sourceEnum = getAggregateToSourceFieldNames().values().iterator(); 517 while (aggregateEnum.hasNext()) { 518 associations.addElement(new Association(aggregateEnum.next(), sourceEnum.next())); 519 } 520 521 return associations; 522 } 523 524 528 public Map getAggregateToSourceFieldNames() { 529 return aggregateToSourceFieldNames; 530 } 531 532 537 public Class getFieldClassification(DatabaseField fieldToClassify) { 538 DatabaseMapping mapping = getReferenceDescriptor().getObjectBuilder().getMappingForField(fieldToClassify); 539 if (mapping == null) { 540 return null; } 542 return mapping.getFieldClassification(fieldToClassify); 543 } 544 545 551 protected Object getMatchingAttributeValueFromObject(AbstractRecord row, Object targetObject, AbstractSession session, ClassDescriptor descriptor) { 552 return getAttributeValueFromObject(targetObject); 553 } 554 555 561 protected Object getMatchingBackupAttributeValue(WriteObjectQuery query, Object attributeValue) { 562 return getAttributeValueFromObject(query.getBackupClone()); 563 } 564 565 569 protected ClassDescriptor getReferenceDescriptor(Class theClass, AbstractSession session) { 570 if (getReferenceDescriptor().getJavaClass().equals(theClass)) { 571 return getReferenceDescriptor(); 572 } 573 574 ClassDescriptor subclassDescriptor = getReferenceDescriptor().getInheritancePolicy().getSubclassDescriptor(theClass); 575 if (subclassDescriptor == null) { 576 throw DescriptorException.noSubClassMatch(theClass, this); 577 } else { 578 return subclassDescriptor; 579 } 580 } 581 582 586 protected Vector getReferenceFields() { 587 return getReferenceDescriptor().getAllFields(); 588 } 589 590 594 public boolean hasDependency() { 595 return getReferenceDescriptor().hasDependencyOnParts(); 596 } 597 598 607 public void initialize(AbstractSession session) throws DescriptorException { 608 super.initialize(session); 609 610 ClassDescriptor clonedDescriptor = (ClassDescriptor)getReferenceDescriptor().clone(); 611 if (clonedDescriptor.isChildDescriptor()) { 612 ClassDescriptor parentDescriptor = session.getDescriptor(clonedDescriptor.getInheritancePolicy().getParentClass()); 613 initializeParentInheritance(parentDescriptor, clonedDescriptor, session); 614 } 615 setReferenceDescriptor(clonedDescriptor); 616 617 initializeReferenceDescriptor(clonedDescriptor); 618 clonedDescriptor.preInitialize(session); 619 clonedDescriptor.initialize(session); 620 translateFields(clonedDescriptor, session); 621 622 if (clonedDescriptor.hasInheritance() && clonedDescriptor.getInheritancePolicy().hasChildren()) { 623 initializeChildInheritance(clonedDescriptor, session); 625 } 626 627 setFields(collectFields()); 628 } 629 630 639 public void initializeChildInheritance(ClassDescriptor parentDescriptor, AbstractSession session) throws DescriptorException { 640 if (parentDescriptor.getInheritancePolicy().hasChildren()) { 642 Vector childDescriptors = parentDescriptor.getInheritancePolicy().getChildDescriptors(); 644 Vector cloneChildDescriptors = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 645 for (Enumeration enumtr = childDescriptors.elements(); enumtr.hasMoreElements();) { 646 ClassDescriptor clonedChildDescriptor = (ClassDescriptor)((ClassDescriptor)enumtr.nextElement()).clone(); 647 clonedChildDescriptor.getInheritancePolicy().setParentDescriptor(parentDescriptor); 648 initializeReferenceDescriptor(clonedChildDescriptor); 649 clonedChildDescriptor.preInitialize(session); 650 clonedChildDescriptor.initialize(session); 651 translateFields(clonedChildDescriptor, session); 652 cloneChildDescriptors.addElement(clonedChildDescriptor); 653 initializeChildInheritance(clonedChildDescriptor, session); 654 } 655 parentDescriptor.getInheritancePolicy().setChildDescriptors(cloneChildDescriptors); 656 } 657 } 658 659 668 public void initializeParentInheritance(ClassDescriptor parentDescriptor, ClassDescriptor childDescriptor, AbstractSession session) throws DescriptorException { 669 ClassDescriptor clonedParentDescriptor = (ClassDescriptor)parentDescriptor.clone(); 670 671 if (clonedParentDescriptor.getInheritancePolicy().isChildDescriptor()) { 673 ClassDescriptor parentToParentDescriptor = session.getDescriptor(clonedParentDescriptor.getJavaClass()); 674 initializeParentInheritance(parentToParentDescriptor, parentDescriptor, session); 675 } 676 677 initializeReferenceDescriptor(clonedParentDescriptor); 678 Vector children = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1); 679 children.addElement(childDescriptor); 680 clonedParentDescriptor.getInheritancePolicy().setChildDescriptors(children); 681 clonedParentDescriptor.preInitialize(session); 682 clonedParentDescriptor.initialize(session); 683 translateFields(clonedParentDescriptor, session); 684 } 685 686 690 protected void initializeReferenceDescriptor(ClassDescriptor clonedDescriptor) { 691 clonedDescriptor.setDefaultTable(getDescriptor().getDefaultTable()); 693 clonedDescriptor.setTables(getDescriptor().getTables()); 694 clonedDescriptor.setPrimaryKeyFields(getDescriptor().getPrimaryKeyFields()); 695 } 696 697 701 public boolean isAggregateObjectMapping() { 702 return true; 703 } 704 705 709 public boolean isChangeTrackingSupported() { 710 return false; 711 } 712 713 717 public boolean isCascadedLockingSupported() { 718 return true; 719 } 720 721 725 public boolean isNullAllowed() { 726 return isNullAllowed; 727 } 728 729 738 public void postInitialize(AbstractSession session) throws DescriptorException { 739 super.postInitialize(session); 740 741 if (getReferenceDescriptor() != null) { 742 getReferenceDescriptor().postInitialize(session); 743 } 744 } 745 746 753 public Object readFromReturnRowIntoObject(AbstractRecord row, Object targetObject, ReadObjectQuery query, Collection handledMappings) throws DatabaseException { 754 Object aggregate = getAttributeValueFromObject(targetObject); 755 if (aggregate == null) { 756 aggregate = readFromRowIntoObject(row, null, targetObject, query); 757 handledMappings.add(this); 758 return aggregate; 759 } 760 761 for (int i = 0; i < getReferenceFields().size(); i++) { 762 DatabaseField field = (DatabaseField)getReferenceFields().elementAt(i); 763 if (row.containsKey(field)) { 764 getObjectBuilder(aggregate, query.getSession()).assignReturnValueForField(aggregate, query, row, field, handledMappings); 765 } 766 } 767 768 if (isNullAllowed()) { 769 boolean allAttributesNull = true; 770 for (int i = 0; (i < getReferenceFields().size()) && allAttributesNull; i++) { 771 DatabaseField field = (DatabaseField)fields.elementAt(i); 772 if (row.containsKey(field)) { 773 allAttributesNull = row.get(field) == null; 774 } else { 775 Object fieldValue = valueFromObject(targetObject, field, query.getSession()); 776 if (fieldValue == null) { 777 Object baseValue = getDescriptor().getObjectBuilder().getBaseValueForField(field, targetObject); 778 if (baseValue != null) { 779 DatabaseMapping baseMapping = getDescriptor().getObjectBuilder().getBaseMappingForField(field); 780 if (baseMapping.isForeignReferenceMapping()) { 781 ForeignReferenceMapping refMapping = (ForeignReferenceMapping)baseMapping; 782 if (refMapping.usesIndirection()) { 783 allAttributesNull = refMapping.getIndirectionPolicy().objectIsInstantiated(baseValue); 784 } 785 } 786 } 787 } else { 788 allAttributesNull = false; 789 } 790 } 791 } 792 if (allAttributesNull) { 793 aggregate = null; 794 setAttributeValueInObject(targetObject, aggregate); 795 } 796 } 797 handledMappings.add(this); 798 return aggregate; 799 } 800 801 806 public Object readFromRowIntoObject(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object targetObject, ObjectBuildingQuery sourceQuery, AbstractSession executionSession) throws DatabaseException { 807 Object aggregate = buildAggregateFromRow(databaseRow, targetObject, joinManager, false, executionSession); setAttributeValueInObject(targetObject, aggregate); 809 return aggregate; 810 } 811 812 817 public void rehashFieldDependancies(AbstractSession session) { 818 getReferenceDescriptor().rehashFieldDependancies(session); 819 } 820 821 825 public void setAggregateToSourceFieldNameAssociations(Vector fieldAssociations) { 826 Hashtable fieldNames = new Hashtable(fieldAssociations.size() + 1); 827 for (Enumeration associationsEnum = fieldAssociations.elements(); 828 associationsEnum.hasMoreElements();) { 829 Association association = (Association)associationsEnum.nextElement(); 830 fieldNames.put(association.getKey(), association.getValue()); 831 } 832 833 setAggregateToSourceFieldNames(fieldNames); 834 } 835 836 840 protected void setAggregateToSourceFieldNames(Map aggregateToSource) { 841 aggregateToSourceFieldNames = aggregateToSource; 842 } 843 844 848 public void setIsNullAllowed(boolean aBoolean) { 849 isNullAllowed = aBoolean; 850 } 851 852 858 protected void translateFields(ClassDescriptor clonedDescriptor, AbstractSession session) { 859 for (Enumeration entry = clonedDescriptor.getFields().elements(); entry.hasMoreElements();) { 860 DatabaseField field = (DatabaseField)entry.nextElement(); 861 String nameInAggregate = field.getName(); 862 String nameInSource = (String )getAggregateToSourceFieldNames().get(nameInAggregate); 863 864 if (nameInSource != null) { 866 DatabaseField fieldInSource = new DatabaseField(nameInSource); 867 868 if (fieldInSource.getName().equals(nameInSource)) { 870 field.setName(nameInSource); 872 } else { 873 field.setName(fieldInSource.getName()); 875 field.setTable(clonedDescriptor.getTable(fieldInSource.getTable().getName())); 876 } 877 } 878 } 879 880 clonedDescriptor.rehashFieldDependancies(session); 881 } 882 883 888 889 public void writeFromAttributeIntoRow(Object attribute, AbstractRecord row, AbstractSession session) 890 { 891 AbstractRecord targetRow = buildRowFromAggregate(null, attribute, session); 892 for (Enumeration stream = targetRow.keys(); stream.hasMoreElements(); ) { 893 DatabaseField field = (DatabaseField) stream.nextElement(); 894 Object value = targetRow.get(field); 895 row.put(field, value); 896 } 897 } 898 899 903 public Object valueFromObject(Object object, DatabaseField field, AbstractSession session) throws DescriptorException { 904 Object attributeValue = getAttributeValueFromObject(object); 905 if (attributeValue == null) { 906 if (isNullAllowed()) { 907 return null; 908 } else { 909 throw DescriptorException.nullForNonNullAggregate(object, this); 910 } 911 } else { 912 return getObjectBuilder(attributeValue, session).extractValueFromObjectForField(attributeValue, field, session); 913 } 914 } 915 916 921 public void writeFromObjectIntoRow(Object object, AbstractRecord databaseRow, AbstractSession session) throws DescriptorException { 922 if (isReadOnly()) { 923 return; 924 } 925 AbstractRecord targetRow = buildRowFromAggregate(object, getAttributeValueFromObject(object), session); 926 for (Enumeration stream = targetRow.keys(); stream.hasMoreElements();) { 927 DatabaseField field = (DatabaseField)stream.nextElement(); 928 Object value = targetRow.get(field); 929 databaseRow.add(field, value); 930 } 931 } 932 933 938 public void writeFromObjectIntoRowWithChangeRecord(ChangeRecord changeRecord, AbstractRecord databaseRow, AbstractSession session) throws DescriptorException { 939 if (isReadOnly()) { 940 return; 941 } 942 AbstractRecord targetRow = buildRowFromAggregateWithChangeRecord(changeRecord, (ObjectChangeSet)((AggregateChangeRecord)changeRecord).getChangedObject(), session); 943 for (Enumeration stream = targetRow.keys(); stream.hasMoreElements();) { 944 DatabaseField field = (DatabaseField)stream.nextElement(); 945 Object value = targetRow.get(field); 946 databaseRow.add(field, value); 947 } 948 } 949 950 955 public void writeFromObjectIntoRowForUpdate(WriteObjectQuery query, AbstractRecord databaseRow) throws DescriptorException { 956 if (isReadOnly()) { 957 return; 958 } 959 AbstractRecord targetRow = buildRowFromAggregateForUpdate(query, getAttributeValueFromObject(query.getObject())); 960 for (Enumeration stream = targetRow.keys(); stream.hasMoreElements();) { 961 DatabaseField field = (DatabaseField)stream.nextElement(); 962 Object value = targetRow.get(field); 963 databaseRow.add(field, value); 964 } 965 } 966 967 971 public void writeInsertFieldsIntoRow(AbstractRecord databaseRow, AbstractSession session) { 972 if (isReadOnly()) { 973 return; 974 } 975 976 AbstractRecord targetRow = buildTemplateInsertRow(session); 977 for (Enumeration keyEnum = targetRow.keys(); keyEnum.hasMoreElements();) { 978 DatabaseField field = (DatabaseField)keyEnum.nextElement(); 979 Object value = targetRow.get(field); 980 981 databaseRow.add(field, value); 983 } 984 } 985 } 986 | Popular Tags |