Search in sources :

Example 1 with OrmAttributeAccessor

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());
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) OrmAttributeAccessor(org.eclipse.persistence.internal.oxm.accessor.OrmAttributeAccessor) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

CoreAttributeAccessor (org.eclipse.persistence.core.mappings.CoreAttributeAccessor)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 OrmAttributeAccessor (org.eclipse.persistence.internal.oxm.accessor.OrmAttributeAccessor)1 CompositeCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping)1 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)1 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)1 ObjectReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)1