Search in sources :

Example 1 with CoreAttributeAccessor

use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor in project eclipselink by eclipse-ee4j.

the class JAXBValueStore method copy.

@Override
public ValueStore copy() {
    AbstractSession session = ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
    Object originalEntity = entity;
    entity = descriptor.getInstantiationPolicy().buildNewInstance();
    for (SDOProperty sdoProperty : (List<SDOProperty>) dataObject.getType().getProperties()) {
        if (!sdoProperty.getType().isChangeSummaryType()) {
            Mapping mapping = getJAXBMappingForProperty(sdoProperty);
            CoreAttributeAccessor attributeAccessor = mapping.getAttributeAccessor();
            Object attributeValue = attributeAccessor.getAttributeValueFromObject(originalEntity);
            if (mapping.isCollectionMapping()) {
                Object containerCopy = null;
                SDOChangeSummary sdoChangeSummary = dataObject.getChangeSummary();
                if (null != sdoChangeSummary) {
                    List list = listWrappers.get(sdoProperty);
                    containerCopy = sdoChangeSummary.getOriginalElements().get(list);
                }
                if (null == containerCopy) {
                    CoreContainerPolicy containerPolicy = mapping.getContainerPolicy();
                    containerCopy = containerPolicy.containerInstance();
                    if (null != attributeValue) {
                        Object attributeValueIterator = containerPolicy.iteratorFor(attributeValue);
                        while (containerPolicy.hasNext(attributeValueIterator)) {
                            containerPolicy.addInto(containerPolicy.nextEntry(attributeValueIterator), containerCopy, session);
                        }
                    }
                }
                attributeAccessor.setAttributeValueInObject(entity, containerCopy);
            } else {
                attributeAccessor.setAttributeValueInObject(entity, attributeValue);
            }
        }
    }
    return new JAXBValueStore(jaxbHelperContext, originalEntity, descriptor, dataObject, listWrappers);
}
Also used : CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) SDOChangeSummary(org.eclipse.persistence.sdo.SDOChangeSummary) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) List(java.util.List) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) ContainerMapping(org.eclipse.persistence.mappings.ContainerMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) SDOProperty(org.eclipse.persistence.sdo.SDOProperty) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 2 with CoreAttributeAccessor

use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor in project eclipselink by eclipse-ee4j.

the class ReferenceResolver method perform.

/**
 * Add java doc if you understand this code.
 */
private void perform(final CoreAbstractSession session, final IDResolver userSpecifiedResolver, final ErrorHandler handler, final Reference reference) {
    final Object referenceSourceObject = reference.getSourceObject();
    if (reference.getMapping() instanceof CollectionReferenceMapping) {
        final CollectionReferenceMapping mapping = (CollectionReferenceMapping) reference.getMapping();
        final CoreContainerPolicy cPolicy = mapping.getContainerPolicy();
        // container should never be null
        final Object container = reference.getContainer();
        // create vectors of primary key values - one vector per reference instance
        createPKVectorsFromMap(reference, mapping);
        // if the we could not generate the primary key for the reference, it will not resolve - skip it
        if (reference.getPrimaryKey() == null) {
            return;
        }
        // loop over each pk vector and get object from cache - then add to collection and set on object
        Object value = null;
        if (!mapping.isWriteOnly()) {
            for (Object o : ((Vector) reference.getPrimaryKey())) {
                final CacheId primaryKey = (CacheId) o;
                if (userSpecifiedResolver != null) {
                    final Callable c;
                    try {
                        if (primaryKey.getPrimaryKey().length > 1) {
                            final Map<String, Object> idWrapper = new HashMap<>();
                            for (int y = 0; y < primaryKey.getPrimaryKey().length; y++) {
                                final ObjectReferenceMapping refMapping = (ObjectReferenceMapping) reference.getMapping();
                                final String idName = (String) refMapping.getReferenceDescriptor().getPrimaryKeyFieldNames().get(y);
                                final Object idValue = primaryKey.getPrimaryKey()[y];
                                idWrapper.put(idName, idValue);
                            }
                            c = userSpecifiedResolver.resolve(idWrapper, reference.getTargetClass());
                        } else {
                            c = userSpecifiedResolver.resolve(primaryKey.getPrimaryKey()[0], reference.getTargetClass());
                        }
                        if (c != null) {
                            value = c.call();
                        }
                    } catch (Exception e) {
                        throw XMLMarshalException.unmarshalException(e);
                    }
                } else {
                    value = getValue(session, reference, primaryKey, handler);
                }
                if (value != null) {
                    cPolicy.addInto(value, container, session);
                }
            }
        }
        // for each reference, get the source object and add it to the container policy
        // when finished, set the policy on the mapping
        mapping.setAttributeValueInObject(referenceSourceObject, container);
        final InverseReferenceMapping inverseReferenceMapping = mapping.getInverseReferenceMapping();
        if (inverseReferenceMapping != null && value != null) {
            final CoreAttributeAccessor backpointerAccessor = inverseReferenceMapping.getAttributeAccessor();
            final CoreContainerPolicy backpointerContainerPolicy = inverseReferenceMapping.getContainerPolicy();
            if (backpointerContainerPolicy == null) {
                backpointerAccessor.setAttributeValueInObject(value, referenceSourceObject);
            } else {
                Object backpointerContainer = backpointerAccessor.getAttributeValueFromObject(value);
                if (backpointerContainer == null) {
                    backpointerContainer = backpointerContainerPolicy.containerInstance();
                    backpointerAccessor.setAttributeValueInObject(value, backpointerContainer);
                }
                backpointerContainerPolicy.addInto(referenceSourceObject, backpointerContainer, session);
            }
        }
    } else if (reference.getMapping() instanceof ObjectReferenceMapping) {
        final CacheId primaryKey = (CacheId) reference.getPrimaryKey();
        Object value = null;
        if (userSpecifiedResolver != null) {
            final Callable c;
            try {
                if (primaryKey.getPrimaryKey().length > 1) {
                    final Map<String, Object> idWrapper = new HashMap<>();
                    for (int y = 0; y < primaryKey.getPrimaryKey().length; y++) {
                        final ObjectReferenceMapping refMapping = (ObjectReferenceMapping) reference.getMapping();
                        final String idName = (String) refMapping.getReferenceDescriptor().getPrimaryKeyFieldNames().get(y);
                        final Object idValue = primaryKey.getPrimaryKey()[y];
                        idWrapper.put(idName, idValue);
                    }
                    c = userSpecifiedResolver.resolve(idWrapper, reference.getTargetClass());
                } else {
                    c = userSpecifiedResolver.resolve(primaryKey.getPrimaryKey()[0], reference.getTargetClass());
                }
                if (c != null) {
                    value = c.call();
                }
            } catch (Exception e) {
                throw XMLMarshalException.unmarshalException(e);
            }
        } else {
            value = getValue(session, reference, primaryKey, handler);
        }
        ObjectReferenceMapping mapping = (ObjectReferenceMapping) reference.getMapping();
        if (value != null) {
            mapping.setAttributeValueInObject(reference.getSourceObject(), value);
        }
        if (null != reference.getSetting()) {
            reference.getSetting().setValue(value);
        }
        InverseReferenceMapping inverseReferenceMapping = mapping.getInverseReferenceMapping();
        if (inverseReferenceMapping != null) {
            CoreAttributeAccessor backpointerAccessor = inverseReferenceMapping.getAttributeAccessor();
            CoreContainerPolicy backpointerContainerPolicy = inverseReferenceMapping.getContainerPolicy();
            if (backpointerContainerPolicy == null) {
                backpointerAccessor.setAttributeValueInObject(value, referenceSourceObject);
            } else {
                Object backpointerContainer = backpointerAccessor.getAttributeValueFromObject(value);
                if (backpointerContainer == null) {
                    backpointerContainer = backpointerContainerPolicy.containerInstance();
                    backpointerAccessor.setAttributeValueInObject(value, backpointerContainer);
                }
                backpointerContainerPolicy.addInto(reference.getSourceObject(), backpointerContainer, session);
            }
        }
    }
}
Also used : InverseReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) Callable(java.util.concurrent.Callable) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 3 with CoreAttributeAccessor

use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor 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)

Example 4 with CoreAttributeAccessor

use of org.eclipse.persistence.core.mappings.CoreAttributeAccessor in project eclipselink by eclipse-ee4j.

the class XMLVariableXPathMappingNodeValue method setXPathInObject.

public void setXPathInObject(String uri, String localName, Object childObject) {
    CoreAttributeAccessor variableAttributeAccessor = this.getMapping().getVariableAttributeAccessor();
    if (!variableAttributeAccessor.isWriteOnly()) {
        Object value = null;
        if (getMapping().getVariableAttributeAccessor().getAttributeClass() == CoreClassConstants.QNAME) {
            if (uri != null && uri.length() > 0) {
                value = new QName(uri, localName);
            } else {
                value = new QName(localName);
            }
        } else {
            value = localName;
        }
        variableAttributeAccessor.setAttributeValueInObject(childObject, value);
    }
}
Also used : CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) QName(javax.xml.namespace.QName)

Aggregations

CoreAttributeAccessor (org.eclipse.persistence.core.mappings.CoreAttributeAccessor)4 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)2 CoreContainerPolicy (org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)2 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)2 ObjectReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping)2 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)2 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)2 DataObject (commonj.sdo.DataObject)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 QName (javax.xml.namespace.QName)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)1 OrmAttributeAccessor (org.eclipse.persistence.internal.oxm.accessor.OrmAttributeAccessor)1 CollectionReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping)1 CompositeCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping)1