1 21 package oracle.toplink.essentials.mappings; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.descriptors.ClassDescriptor; 27 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.expressions.*; 29 import oracle.toplink.essentials.indirection.*; 30 import oracle.toplink.essentials.internal.descriptors.*; 31 import oracle.toplink.essentials.internal.expressions.*; 32 import oracle.toplink.essentials.internal.helper.*; 33 import oracle.toplink.essentials.internal.indirection.*; 34 import oracle.toplink.essentials.internal.queryframework.*; 35 import oracle.toplink.essentials.internal.sessions.*; 36 import oracle.toplink.essentials.queryframework.*; 37 import oracle.toplink.essentials.sessions.ObjectCopyingPolicy; 38 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 39 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 40 import oracle.toplink.essentials.internal.sessions.AbstractSession; 41 42 60 public abstract class DatabaseMapping implements Cloneable , Serializable { 61 62 63 protected static Vector NO_FIELDS = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(0); 64 65 66 protected static Integer NO_WEIGHT = new Integer (Integer.MAX_VALUE); 67 protected static Integer WEIGHT_1 = new Integer (1); 68 69 70 protected ClassDescriptor descriptor; 71 72 73 protected AttributeAccessor attributeAccessor; 74 75 76 protected boolean isReadOnly; 77 78 79 protected boolean isOptional; 80 81 82 protected Vector fields; 83 84 85 protected boolean isRemotelyInitialized; 86 87 88 protected Integer weight = NO_WEIGHT; 89 90 91 protected Map properties; 92 93 96 protected boolean primaryKeyMapping = false; 97 98 102 public DatabaseMapping() { 103 this.isOptional = true; 104 this.isReadOnly = false; 105 this.attributeAccessor = new InstanceVariableAttributeAccessor(); 106 } 107 108 112 public abstract void buildBackupClone(Object clone, Object backup, UnitOfWorkImpl unitOfWork); 113 114 118 public Object buildBackupCloneForPartObject(Object attributeValue, Object clone, Object backup, UnitOfWorkImpl unitOfWork) { 119 throw DescriptorException.invalidMappingOperation(this, "buildBackupCloneForPartObject"); 120 } 121 122 126 public abstract void buildClone(Object original, Object clone, UnitOfWorkImpl unitOfWork, JoinedAttributeManager joinedAttributeManager); 127 128 145 public abstract void buildCloneFromRow(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object clone, ObjectBuildingQuery sourceQuery, UnitOfWorkImpl unitOfWork, AbstractSession executionSession); 146 147 154 public void buildShallowOriginalFromRow(AbstractRecord databaseRow, Object original, ObjectBuildingQuery query, AbstractSession executionSession) { 155 return; 156 } 157 158 162 public Object buildCloneForPartObject(Object attributeValue, Object original, Object clone, UnitOfWorkImpl unitOfWork, boolean isExisting) { 163 throw DescriptorException.invalidMappingOperation(this, "buildCloneForPartObject"); 164 } 165 166 171 public void buildCopy(Object copy, Object original, ObjectCopyingPolicy policy) { 172 } 173 174 178 public Expression buildObjectJoinExpression(Expression base, Object value, AbstractSession session) { 179 throw QueryException.unsupportedMappingForObjectComparison(this, base); 180 } 181 182 186 public Expression buildObjectJoinExpression(Expression base, Expression argument, AbstractSession session) { 187 throw QueryException.unsupportedMappingForObjectComparison(this, base); 188 } 189 190 194 abstract public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects); 195 196 200 abstract public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects); 201 202 208 public void calculateDeferredChanges(ChangeRecord changeRecord, AbstractSession session){ 209 throw DescriptorException.invalidMappingOperation(this, "calculatedDeferredChanges"); 210 } 211 212 216 public void cascadeMerge(Object sourceElement, MergeManager mergeManager) { 217 throw DescriptorException.invalidMappingOperation(this, "cascadeMerge"); 218 } 219 220 224 public Object clone() { 225 DatabaseMapping mapping = null; 227 try { 228 mapping = (DatabaseMapping)super.clone(); 229 } catch (CloneNotSupportedException e) { 230 throw new InternalError (); 231 } 232 mapping.setAttributeAccessor((AttributeAccessor)attributeAccessor.clone()); 233 return mapping; 234 } 235 236 240 protected Vector cloneFields(Vector fields) { 241 Vector clonedFields = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 242 for (Enumeration fieldsEnum = fields.elements(); fieldsEnum.hasMoreElements();) { 243 clonedFields.addElement(((DatabaseField)fieldsEnum.nextElement()).clone()); 244 } 245 246 return clonedFields; 247 } 248 249 253 protected Vector collectFields() { 254 return NO_FIELDS; 255 } 256 257 262 abstract public ChangeRecord compareForChange(Object clone, Object backup, ObjectChangeSet owner, AbstractSession session); 263 264 268 public abstract boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session); 269 270 277 public void convertClassNamesToClasses(ClassLoader classLoader){}; 278 279 286 public UnitOfWorkValueHolder createUnitOfWorkValueHolder(ValueHolderInterface attributeValue, Object original, Object clone, AbstractRecord row, UnitOfWorkImpl unitOfWork, boolean buildDirectlyFromRow) { 287 throw DescriptorException.invalidMappingOperation(this, "createUnitOfWorkValueHolder"); 288 } 289 290 297 protected Vector extractNestedExpressions(List expressions, ExpressionBuilder newRoot, boolean rootExpressionsAllowed) { 298 Vector nestedExpressions = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(expressions.size()); 299 300 for (Iterator expressionsEnum = expressions.iterator(); 301 expressionsEnum.hasNext();) { 302 Expression next = (Expression)expressionsEnum.next(); 303 304 if (!next.isQueryKeyExpression()) { 307 continue; 308 } 309 QueryKeyExpression expression = (QueryKeyExpression)next; 310 QueryKeyExpression base = expression; 311 boolean afterBase = false; 312 while (!base.getBaseExpression().isExpressionBuilder()) { 313 afterBase = true; 314 base = (QueryKeyExpression)base.getBaseExpression(); 315 } 316 if (afterBase && base.getName().equals(getAttributeName())) { 317 nestedExpressions.addElement(expression.rebuildOn(base, newRoot)); 318 } else if (rootExpressionsAllowed && expression.getBaseExpression().isExpressionBuilder() && expression.getName().equals(getAttributeName())) { 319 nestedExpressions.addElement(newRoot); 320 } 321 } 322 return nestedExpressions; 323 } 324 325 331 public AttributeAccessor getAttributeAccessor() { 332 return attributeAccessor; 333 } 334 335 339 public Class getAttributeClassification() { 340 return null; 341 } 342 343 347 public String getAttributeName() { 348 return getAttributeAccessor().getAttributeName(); 349 } 350 351 355 public Object getAttributeValueFromObject(Object object) throws DescriptorException { 356 try { 357 return getAttributeAccessor().getAttributeValueFromObject(object); 358 } catch (DescriptorException exception) { 359 exception.setMapping(this); 360 throw exception; 361 } 362 } 363 364 368 public ContainerPolicy getContainerPolicy() { 369 throw DescriptorException.invalidMappingOperation(this, "getContainerPolicy"); 370 } 371 372 376 public ClassDescriptor getDescriptor() { 377 return descriptor; 378 } 379 380 387 public DatabaseField getField() { 388 return null; 389 } 390 391 397 public Class getFieldClassification(DatabaseField fieldToClassify) { 398 return null; 399 } 400 401 405 public Vector getFields() { 406 return this.fields; 407 } 408 409 414 public String getGetMethodName() { 415 if (!(getAttributeAccessor() instanceof MethodAttributeAccessor)) { 416 return null; 417 } 418 return ((MethodAttributeAccessor)getAttributeAccessor()).getGetMethodName(); 419 } 420 421 425 public Map getProperties() { 426 if (properties == null) { properties = new HashMap(5); 428 } 429 return properties; 430 } 431 432 436 public Object getProperty(Object property) { 437 if (properties == null) { 438 return null; 439 } 440 441 return getProperties().get(property); 442 } 443 444 448 public Object getRealAttributeValueFromObject(Object object, AbstractSession session) throws DescriptorException { 449 return getAttributeValueFromObject(object); 450 } 451 452 457 public Object getRealCollectionAttributeValueFromObject(Object object, AbstractSession session) throws DescriptorException { 458 throw DescriptorException.invalidMappingOperation(this, "getRealCollectionAttributeValueFromObject"); 459 } 460 461 467 public ClassDescriptor getReferenceDescriptor() { 468 return null; 469 } 470 471 476 public ClassDescriptor getReferenceClassDescriptor() { 477 ClassDescriptor desc = getReferenceDescriptor(); 478 if (desc instanceof ClassDescriptor) { 479 return (ClassDescriptor)desc; 480 } else { 481 throw ValidationException.cannotCastToClass(desc, desc.getClass(), ClassDescriptor.class); 482 } 483 } 484 485 490 public DatabaseMapping getRelationshipPartner() { 491 return null; 492 } 493 494 499 public String getSetMethodName() { 500 if (!(getAttributeAccessor() instanceof MethodAttributeAccessor)) { 501 return null; 502 } 503 return ((MethodAttributeAccessor)getAttributeAccessor()).getSetMethodName(); 504 } 505 506 511 public Integer getWeight() { 512 return this.weight; 513 } 514 515 519 public boolean hasConstraintDependency() { 520 return false; 521 } 522 523 527 public boolean isUsingMethodAccess() { 528 return getAttributeAccessor() instanceof MethodAttributeAccessor; 529 } 530 531 535 public boolean hasDependency() { 536 return isPrivateOwned(); 537 } 538 539 543 public boolean hasInverseConstraintDependency() { 544 return false; 545 } 546 547 551 public void initialize(AbstractSession session) throws DescriptorException { 552 ; 553 } 554 555 559 public boolean isAggregateCollectionMapping() { 560 return false; 561 } 562 563 567 public boolean isAggregateMapping() { 568 return false; 569 } 570 571 575 public boolean isAggregateObjectMapping() { 576 return false; 577 } 578 579 583 public boolean isCollectionMapping() { 584 return false; 585 } 586 587 590 public boolean isDatabaseMapping() { 591 return true; 592 } 593 594 598 public boolean isDirectCollectionMapping() { 599 return false; 600 } 601 602 606 public boolean isDirectMapMapping() { 607 return false; 608 } 609 610 614 public boolean isDirectToFieldMapping() { 615 return false; 616 } 617 618 622 public boolean isForeignReferenceMapping() { 623 return false; 624 } 625 626 630 public boolean isManyToManyMapping() { 631 return false; 632 } 633 634 638 public boolean isNestedTableMapping() { 639 return false; 640 } 641 642 646 public boolean isObjectReferenceMapping() { 647 return false; 648 } 649 650 654 public boolean isObjectTypeMapping() { 655 return false; 656 } 657 658 662 public boolean isOneToManyMapping() { 663 return false; 664 } 665 666 670 public boolean isOneToOneMapping() { 671 return false; 672 } 673 674 680 public boolean isOptional() { 681 return isOptional; 682 } 683 684 688 public boolean isEISMapping() { 689 return false; 690 } 691 692 696 public boolean isRelationalMapping() { 697 return false; 698 } 699 700 704 public boolean isXMLMapping() { 705 return false; 706 } 707 708 712 public boolean isAbstractDirectMapping() { 713 return false; 714 } 715 716 720 public boolean isAbstractCompositeDirectCollectionMapping() { 721 return false; 722 } 723 724 728 public boolean isAbstractCompositeObjectMapping() { 729 return false; 730 } 731 732 736 public boolean isAbstractCompositeCollectionMapping() { 737 return false; 738 } 739 743 public boolean isJoiningSupported() { 744 return false; 745 } 746 747 751 public boolean isCloningRequired() { 752 return true; 753 } 754 755 760 public boolean isPrimaryKeyMapping() { 761 return this.primaryKeyMapping; 762 } 763 768 public boolean isCascadedLockingSupported() { 769 return false; 770 } 771 772 776 public boolean isChangeTrackingSupported() { 777 return false; 778 } 779 783 public boolean isPrivateOwned() { 784 return false; 785 } 786 787 791 public boolean isReadOnly() { 792 return isReadOnly; 793 } 794 795 799 public boolean isReferenceMapping() { 800 return false; 801 } 802 803 protected boolean isRemotelyInitialized() { 804 return isRemotelyInitialized; 805 } 806 807 811 public boolean isSerializedObjectMapping() { 812 return false; 813 } 814 815 819 public boolean isStructureMapping() { 820 return false; 821 } 822 823 827 public boolean isTransformationMapping() { 828 return false; 829 } 830 831 835 public boolean isTypeConversionMapping() { 836 return false; 837 } 838 839 843 public boolean isVariableOneToOneMapping() { 844 return false; 845 } 846 847 851 public boolean isDirectToXMLTypeMapping() { 852 return false; 853 } 854 855 859 public boolean isWriteOnly() { 860 return false; 861 } 862 863 867 public abstract void iterate(DescriptorIterator iterator); 868 869 874 public void iterateOnRealAttributeValue(DescriptorIterator iterator, Object realAttributeValue) { 875 throw DescriptorException.invalidMappingOperation(this, "iterateOnRealAttributeValue"); 876 } 877 878 882 public abstract void mergeChangesIntoObject(Object target, ChangeRecord changeRecord, Object source, MergeManager mergeManager); 883 884 888 public abstract void mergeIntoObject(Object target, boolean isTargetUninitialized, Object source, MergeManager mergeManager); 889 890 895 public void performDataModificationEvent(Object [] event, AbstractSession session) throws DatabaseException, DescriptorException { 896 throw DescriptorException.invalidDataModificationEvent(this); 897 } 898 899 904 public void postDelete(DeleteObjectQuery query) throws DatabaseException { 905 return; 906 } 907 908 913 public void postInitialize(AbstractSession session) throws DescriptorException { 914 } 916 917 922 public void postInsert(WriteObjectQuery query) throws DatabaseException { 923 return; 924 } 925 926 931 public void postUpdate(WriteObjectQuery query) throws DatabaseException { 932 return; 933 } 934 935 940 public void preDelete(DeleteObjectQuery query) throws DatabaseException { 941 return; 942 } 943 944 948 public void preInitialize(AbstractSession session) throws DescriptorException { 949 try { 950 getAttributeAccessor().initializeAttributes(getDescriptor().getJavaClass()); 951 } catch (DescriptorException exception) { 952 exception.setMapping(this); 953 session.getIntegrityChecker().handleError(exception); 954 } 955 } 956 957 962 public void preInsert(WriteObjectQuery query) throws DatabaseException { 963 return; 964 } 965 966 972 public void prepareCascadeLockingPolicy() { 973 return; 974 } 975 976 981 public void preUpdate(WriteObjectQuery query) throws DatabaseException { 982 return; 983 } 984 985 991 public Object readFromRowIntoObject(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object targetObject, ObjectBuildingQuery sourceQuery) throws DatabaseException { 992 return readFromRowIntoObject(databaseRow, joinManager, targetObject, sourceQuery, sourceQuery.getSession()); 998 } 999 1000 1006 public Object readFromRowIntoObject(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object targetObject, ObjectBuildingQuery sourceQuery, AbstractSession executionSession) throws DatabaseException { 1007 Object attributeValue = valueFromRow(databaseRow, joinManager, sourceQuery, executionSession); 1008 setAttributeValueInObject(targetObject, attributeValue); 1009 return attributeValue; 1010 } 1011 1012 1018 public void readOnly() { 1019 setIsReadOnly(true); 1020 } 1021 1022 1027 public void readWrite() { 1028 setIsReadOnly(false); 1029 } 1030 1031 1037 public void rehashFieldDependancies(AbstractSession session) { 1038 } 1040 1041 1049 public void setAttributeAccessor(AttributeAccessor attributeAccessor) { 1050 String attributeName = getAttributeName(); 1051 this.attributeAccessor = attributeAccessor; 1052 if (attributeAccessor.getAttributeName() == null) { 1053 attributeAccessor.setAttributeName(attributeName); 1054 } 1055 } 1056 1057 1061 public void setAttributeName(String attributeName) { 1062 getAttributeAccessor().setAttributeName(attributeName); 1063 } 1064 1065 1069 public void setAttributeValueInObject(Object object, Object value) throws DescriptorException { 1070 try { 1072 this.attributeAccessor.setAttributeValueInObject(object, value); 1073 } catch (DescriptorException exception) { 1074 exception.setMapping(this); 1075 throw exception; 1076 } 1077 } 1078 1079 1084 public void setRealAttributeValueInObject(Object object, Object value) throws DescriptorException { 1085 try { 1086 this.setAttributeValueInObject(object, value); 1087 } catch (DescriptorException exception) { 1088 exception.setMapping(this); 1089 throw exception; 1090 } 1091 } 1092 1093 1097 public void setDescriptor(ClassDescriptor descriptor) { 1098 this.descriptor = descriptor; 1099 } 1100 1101 1105 protected void setFields(Vector fields) { 1106 this.fields = fields; 1107 } 1108 1109 1114 public void setGetMethodName(String methodName) { 1115 if (methodName == null) { 1116 return; 1117 } 1118 1119 if (getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) { 1121 String attributeName = this.attributeAccessor.getAttributeName(); 1122 setAttributeAccessor(new MethodAttributeAccessor()); 1123 getAttributeAccessor().setAttributeName(attributeName); 1124 } 1125 1126 ((MethodAttributeAccessor)getAttributeAccessor()).setGetMethodName(methodName); 1127 } 1128 1129 1134 public void setIsOptional(boolean isOptional) { 1135 this.isOptional = isOptional; 1136 } 1137 1138 1143 public void setIsPrimaryKeyMapping(boolean pkMapping) { 1144 this.primaryKeyMapping = pkMapping; 1145 } 1146 1147 1153 public void setIsReadOnly(boolean aBoolean) { 1154 isReadOnly = aBoolean; 1155 } 1156 1157 1161 public void setProperties(Map properties) { 1162 this.properties = properties; 1163 } 1164 1165 1169 public void setProperty(Object property, Object value) { 1170 getProperties().put(property, value); 1171 } 1172 1173 1179 public void setSetMethodName(String methodName) { 1180 if (methodName == null) { 1181 return; 1182 } 1183 1184 if (!(getAttributeAccessor() instanceof MethodAttributeAccessor)) { 1186 String attributeName = this.attributeAccessor.getAttributeName(); 1187 setAttributeAccessor(new MethodAttributeAccessor()); 1188 getAttributeAccessor().setAttributeName(attributeName); 1189 } 1190 1191 ((MethodAttributeAccessor)getAttributeAccessor()).setSetMethodName(methodName); 1192 } 1193 1194 1202 1203 public void setWeight(Integer newWeight) { 1205 this.weight = newWeight; 1206 } 1207 1208 1213 public void simpleAddToCollectionChangeRecord(Object referenceKey, Object changeSetToAdd, ObjectChangeSet changeSet, AbstractSession session) throws DescriptorException { 1214 throw DescriptorException.invalidMappingOperation(this, "simpleAddToCollectionChangeRecord"); 1215 } 1216 1217 1222 public void simpleRemoveFromCollectionChangeRecord(Object referenceKey, Object changeSetToAdd, ObjectChangeSet changeSet, AbstractSession session) throws DescriptorException { 1223 throw DescriptorException.invalidMappingOperation(this, "simpleRemoveFromCollectionChangeRecord"); 1224 } 1225 1226 1230 public String toString() { 1231 return getClass().getName() + "[" + getAttributeName() + "]"; 1232 } 1233 1234 1238 public void validateAfterInitialization(AbstractSession session) throws DescriptorException { 1239 } 1240 1241 1245 public void validateBeforeInitialization(AbstractSession session) throws DescriptorException { 1246 } 1247 1248 1254 public Object valueFromObject(Object anObject, DatabaseField field, AbstractSession session) { 1255 return null; 1256 } 1257 1258 1263 public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query) throws DatabaseException { 1264 return valueFromRow(row, joinManager, query, query.getSession().getExecutionSession(query)); 1265 } 1266 1267 1271 public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query, AbstractSession session) throws DatabaseException { 1272 return null; 1273 } 1274 1275 1279 public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException { 1280 return true; 1281 } 1282 1283 1288 1289 public void writeFromAttributeIntoRow(Object attribute, AbstractRecord row, AbstractSession session) 1290 { 1291 } 1293 1298 public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session) { 1299 } 1301 1302 1306 public void writeFromObjectIntoRowForShallowInsert(Object object, AbstractRecord row, AbstractSession session) { 1307 writeFromObjectIntoRow(object, row, session); 1308 } 1309 1310 1315 public void writeFromObjectIntoRowWithChangeRecord(ChangeRecord changeRecord, AbstractRecord row, AbstractSession session) { 1316 } 1318 1319 1323 public void writeFromObjectIntoRowForShallowInsertWithChangeRecord(ChangeRecord changeRecord, AbstractRecord row, AbstractSession session) { 1324 writeFromObjectIntoRowWithChangeRecord(changeRecord, row, session); 1325 } 1326 1327 1331 public void writeFromObjectIntoRowForUpdate(WriteObjectQuery query, AbstractRecord row) { 1332 writeFromObjectIntoRow(query.getObject(), row, query.getSession()); 1333 } 1334 1335 1340 public void writeFromObjectIntoRowForWhereClause(ObjectLevelModifyQuery query, AbstractRecord row) { 1341 Object object; 1342 if (query.isDeleteObjectQuery()) { 1343 object = query.getObject(); 1344 } else { 1345 object = query.getBackupClone(); 1346 } 1347 writeFromObjectIntoRow(object, row, query.getSession()); 1348 } 1349 1350 1354 public void writeInsertFieldsIntoRow(AbstractRecord databaseRow, AbstractSession session) { 1355 } 1357 1358 1363 public void writeUpdateFieldsIntoRow(AbstractRecord databaseRow, AbstractSession session) { 1364 writeInsertFieldsIntoRow(databaseRow, session); 1365 } 1366 1367 1372 public void updateChangeRecord(Object clone, Object newValue, Object oldValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) throws DescriptorException { 1373 throw DescriptorException.invalidMappingOperation(this, "updateChangeRecord"); 1374 } 1375 1376 1381 public void addToCollectionChangeRecord(Object newKey, Object newValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) throws DescriptorException { 1382 throw DescriptorException.invalidMappingOperation(this, "addToCollectionChangeRecord"); 1383 } 1384 1385 1390 public void removeFromCollectionChangeRecord(Object newKey, Object newValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) throws DescriptorException { 1391 throw DescriptorException.invalidMappingOperation(this, "removeFromCollectionChangeRecord"); 1392 } 1393 1394 1398 public ChangeRecord buildChangeRecord(Object newValue, ObjectChangeSet owner, AbstractSession session) throws DescriptorException { 1399 throw DescriptorException.invalidMappingOperation(this, "buildChangeRecord"); 1400 } 1401} 1402 | Popular Tags |