use of org.eclipse.persistence.internal.oxm.accessor.OrmAttributeAccessor in project eclipselink by eclipse-ee4j.
the class XMLContext method applyORMMetadata.
/**
* ADVANCED:
* Adjust the OXM metadata to take into account ORM mapping metadata,
*/
public void applyORMMetadata(AbstractSession ormSession) {
// Iterate over the ORM descriptors and check for matching OXM descriptors
Iterator<ClassDescriptor> ormDescriptors = ormSession.getDescriptors().values().iterator();
while (ormDescriptors.hasNext()) {
ClassDescriptor ormDescriptor = ormDescriptors.next();
Class<?> javaClass = ormDescriptor.getJavaClass();
AbstractSession oxmSession = null;
try {
oxmSession = this.getSession(javaClass);
} catch (XMLMarshalException ex) {
// if we couldn't find a session for this class, we
// don't have an OX descriptor for it.
}
if (oxmSession != null) {
ClassDescriptor oxmDescriptor = oxmSession.getDescriptor(javaClass);
// If we have an oxmDescriptor for this ORM descriptor, iterate over
// mappings, and update the required OXM mappings attribute accessors
Iterator<DatabaseMapping> ormMappings = ormDescriptor.getMappings().iterator();
while (ormMappings.hasNext()) {
DatabaseMapping ormMapping = ormMappings.next();
Mapping oxmMapping = (Mapping) oxmDescriptor.getMappingForAttributeName(ormMapping.getAttributeName());
if (oxmMapping != null) {
CoreAttributeAccessor oxmAccessor = oxmMapping.getAttributeAccessor();
OrmAttributeAccessor newAccessor = new OrmAttributeAccessor(ormMapping.getAttributeAccessor(), oxmAccessor);
if (ormMapping.isOneToOneMapping() && ((OneToOneMapping) ormMapping).usesIndirection()) {
newAccessor.setValueHolderProperty(true);
}
newAccessor.setChangeTracking(ormDescriptor.getObjectChangePolicy().isAttributeChangeTrackingPolicy());
oxmMapping.setAttributeAccessor(newAccessor);
// check to see if we need to deal with containerAccessor
CoreAttributeAccessor containerAccessor = null;
Class<?> containerClass = null;
if (oxmMapping.isAbstractCompositeObjectMapping()) {
containerAccessor = ((CompositeObjectMapping) oxmMapping).getInverseReferenceMapping().getAttributeAccessor();
containerClass = ((CompositeObjectMapping) oxmMapping).getReferenceClass();
} else if (oxmMapping.isAbstractCompositeCollectionMapping()) {
containerAccessor = ((CompositeCollectionMapping) oxmMapping).getInverseReferenceMapping().getAttributeAccessor();
containerClass = ((CompositeCollectionMapping) oxmMapping).getReferenceClass();
}
if (containerAccessor != null) {
ClassDescriptor containerDescriptor = ormSession.getDescriptor(containerClass);
if (containerDescriptor != null) {
DatabaseMapping ormContainerMapping = containerDescriptor.getMappingForAttributeName(containerAccessor.getAttributeName());
if (ormContainerMapping != null) {
// Check for indirection on the container mapping
OrmAttributeAccessor ormAccessor = new OrmAttributeAccessor(ormContainerMapping.getAttributeAccessor(), containerAccessor);
ormAccessor.setChangeTracking(containerDescriptor.getObjectChangePolicy().isAttributeChangeTrackingPolicy());
ormAccessor.setValueHolderProperty(ormContainerMapping instanceof OneToOneMapping && ((OneToOneMapping) ormContainerMapping).usesIndirection());
if (oxmMapping.isAbstractCompositeObjectMapping()) {
((CompositeObjectMapping) oxmMapping).getInverseReferenceMapping().setAttributeAccessor(ormAccessor);
} else if (oxmMapping.isAbstractCompositeCollectionMapping()) {
((CompositeCollectionMapping) oxmMapping).getInverseReferenceMapping().setAttributeAccessor(ormAccessor);
}
}
}
}
}
}
Iterator<DatabaseMapping> oxmMappingsIterator = oxmDescriptor.getMappings().iterator();
while (oxmMappingsIterator.hasNext()) {
// iterate over the oxm mappings. Any ReferenceMappings that have a
// collection as a backpointer, check to see if the container policy
// needs to be matched with the ORM project
DatabaseMapping nextMapping = oxmMappingsIterator.next();
if (nextMapping instanceof ObjectReferenceMapping) {
ObjectReferenceMapping refMapping = (ObjectReferenceMapping) nextMapping;
if (refMapping.getInverseReferenceMapping().getAttributeAccessor() != null && refMapping.getInverseReferenceMapping().getContainerPolicy() != null) {
ClassDescriptor refDescriptor = ormSession.getClassDescriptor(refMapping.getReferenceClass());
if (refDescriptor != null) {
DatabaseMapping backpointerMapping = refDescriptor.getMappingForAttributeName(refMapping.getInverseReferenceMapping().getAttributeName());
if (backpointerMapping != null && backpointerMapping.isCollectionMapping()) {
refMapping.getInverseReferenceMapping().getContainerPolicy().setContainerClass(backpointerMapping.getContainerPolicy().getContainerClass());
}
}
}
}
}
}
}
}
Aggregations