Search in sources :

Example 1 with Reference

use of org.eclipse.persistence.internal.oxm.Reference in project eclipselink by eclipse-ee4j.

the class XMLCollectionReferenceMapping method buildReference.

/**
 * INTERNAL:
 * Create (if necessary) and populate a reference object that will be used
 * during the mapping reference resolution phase after unmarshalling is
 * complete.
 */
public void buildReference(Object srcObject, XMLField xmlField, Object object, AbstractSession session, Object container, ReferenceResolver resolver) {
    if (resolver == null) {
        return;
    }
    Reference reference = resolver.getReference(this, srcObject, xmlField);
    if (reference == null) {
        // if reference is null, create a new instance and set it on the resolver
        reference = new Reference(this, srcObject, referenceClass, new HashMap(), container);
        resolver.addReference(reference);
    }
    CacheId primaryKeys;
    if (null == referenceClass || ClassConstants.OBJECT == referenceClass) {
        HashMap primaryKeyMap = reference.getPrimaryKeyMap();
        CacheId pks = (CacheId) primaryKeyMap.get(null);
        if (pks == null) {
            Object[] pkValues = new Object[1];
            pks = new CacheId(pkValues);
            primaryKeyMap.put(null, pks);
        }
        if (usesSingleNode) {
            for (StringTokenizer stok = new StringTokenizer((String) object); stok.hasMoreTokens(); ) {
                pks.add(stok.nextToken());
                reference = resolver.getReference(this, srcObject, xmlField);
                if (reference == null) {
                    // if reference is null, create a new instance and set it on the resolver
                    reference = new Reference(this, srcObject, referenceClass, new HashMap(), container);
                    resolver.addReference(reference);
                }
                primaryKeyMap = reference.getPrimaryKeyMap();
                pks = (CacheId) primaryKeyMap.get(null);
                if (pks == null) {
                    Object[] pkValues = new Object[1];
                    pks = new CacheId(pkValues);
                    primaryKeyMap.put(null, pks);
                }
            }
        } else {
            pks.add(object);
        }
    } else {
        XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(xmlField);
        String tgtXPath = tgtFld.getQualifiedName();
        HashMap primaryKeyMap = reference.getPrimaryKeyMap();
        CacheId pks = (CacheId) primaryKeyMap.get(tgtXPath);
        ClassDescriptor descriptor = session.getClassDescriptor(referenceClass);
        if (pks == null) {
            pks = new CacheId(new Object[0]);
            primaryKeyMap.put(tgtXPath, pks);
        }
        Class<?> type = descriptor.getTypedField(tgtFld).getType();
        XMLConversionManager xmlConversionManager = (XMLConversionManager) session.getDatasourcePlatform().getConversionManager();
        if (usesSingleNode) {
            for (StringTokenizer stok = new StringTokenizer((String) object); stok.hasMoreTokens(); ) {
                Object value = xmlConversionManager.convertObject(stok.nextToken(), type);
                if (value != null) {
                    pks.add(value);
                }
                reference = resolver.getReference(this, srcObject, xmlField);
                if (reference == null) {
                    // if reference is null, create a new instance and set it on the resolver
                    reference = new Reference(this, srcObject, referenceClass, new HashMap(), container);
                    resolver.addReference(reference);
                }
                primaryKeyMap = reference.getPrimaryKeyMap();
                pks = (CacheId) primaryKeyMap.get(null);
                if (pks == null) {
                    pks = new CacheId(new Object[0]);
                    primaryKeyMap.put(tgtXPath, pks);
                }
            }
        } else {
            Object value = xmlConversionManager.convertObject(object, type);
            if (value != null) {
                pks.add(value);
            }
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) StringTokenizer(java.util.StringTokenizer) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) HashMap(java.util.HashMap) Reference(org.eclipse.persistence.internal.oxm.Reference) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 2 with Reference

use of org.eclipse.persistence.internal.oxm.Reference in project eclipselink by eclipse-ee4j.

the class XMLObjectReferenceMapping method buildReference.

/**
 * INTERNAL:
 * Create (if necessary) and populate a reference object that will be used
 * during the mapping reference resolution phase after unmarshalling is
 * complete.
 */
@Override
public void buildReference(UnmarshalRecord record, XMLField xmlField, Object object, AbstractSession session) {
    ReferenceResolver resolver = record.getReferenceResolver();
    if (resolver == null) {
        return;
    }
    Object srcObject = record.getCurrentObject();
    // the order in which the primary keys are added to the vector is
    // relevant for cache lookup - it must match the ordering of the
    // reference descriptor's primary key entries
    Reference reference = resolver.getReference(this, srcObject);
    CacheId primaryKeys;
    if (null == referenceClass || ClassConstants.OBJECT == referenceClass) {
        if (reference == null) {
            // if reference is null, create a new instance and set it on the resolver
            primaryKeys = new CacheId(new Object[1]);
            reference = new Reference(this, srcObject, referenceClass, primaryKeys);
            resolver.addReference(reference);
            record.reference(reference);
        } else {
            primaryKeys = (CacheId) reference.getPrimaryKey();
        }
        primaryKeys.set(0, object);
    } else {
        Vector<String> pkFieldNames = referenceDescriptor.getPrimaryKeyFieldNames();
        // if reference is null, create a new instance and set it on the resolver
        if (reference == null) {
            primaryKeys = new CacheId(new Object[pkFieldNames.size()]);
            reference = new Reference(this, srcObject, referenceClass, primaryKeys);
            resolver.addReference(reference);
            record.reference(reference);
        } else {
            primaryKeys = (CacheId) reference.getPrimaryKey();
        }
        XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(xmlField);
        int idx = pkFieldNames.indexOf(tgtFld.getQualifiedName());
        // fix for bug# 5687430
        // need to get the actual type of the target (i.e. int, String, etc.)
        // and use the converted value when checking the cache.
        Object value = session.getDatasourcePlatform().getConversionManager().convertObject(object, referenceDescriptor.getTypedField(tgtFld).getType());
        if (value != null) {
            primaryKeys.set(idx, value);
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) Reference(org.eclipse.persistence.internal.oxm.Reference) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) ReferenceResolver(org.eclipse.persistence.internal.oxm.ReferenceResolver)

Example 3 with Reference

use of org.eclipse.persistence.internal.oxm.Reference in project eclipselink by eclipse-ee4j.

the class XMLObjectReferenceMapping method readFromRowIntoObject.

/**
 * INTERNAL:
 * Extract the primary key values from the row, then create an
 * org.eclipse.persistence.internal.oxm.Reference instance and store it
 * on the session's org.eclipse.persistence.internal.oxm.ReferenceResolver.
 */
@Override
public Object readFromRowIntoObject(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object targetObject, CacheKey parentCacheKey, ObjectBuildingQuery sourceQuery, AbstractSession executionSession, boolean isTargetProtected) throws DatabaseException {
    // the order in which the primary keys are added to the vector is
    // relevant for cache lookup - it must match the ordering of the
    // reference descriptor's primary key entries
    CacheId primaryKeys;
    ClassDescriptor descriptor = sourceQuery.getSession().getClassDescriptor(referenceClass);
    Vector pkFieldNames = null;
    if (null == descriptor) {
        primaryKeys = new CacheId(new Object[1]);
    } else {
        pkFieldNames = descriptor.getPrimaryKeyFieldNames();
        primaryKeys = new CacheId(new Object[pkFieldNames.size()]);
    }
    Iterator keyIt = sourceToTargetKeys.iterator();
    while (keyIt.hasNext()) {
        XMLField keyFld = (XMLField) keyIt.next();
        XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(keyFld);
        Object value;
        int idx = 0;
        if (null == tgtFld) {
            value = databaseRow.get(keyFld);
        } else {
            idx = pkFieldNames.indexOf(tgtFld.getXPath());
            if (idx == -1) {
                continue;
            }
            // fix for bug# 5687430
            // need to get the actual type of the target (i.e. int, String, etc.)
            // and use the converted value when checking the cache.
            value = executionSession.getDatasourcePlatform().getConversionManager().convertObject(databaseRow.get(keyFld), descriptor.getTypedField(tgtFld).getType());
        }
        if (value != null) {
            primaryKeys.set(idx, value);
        }
    }
    // store the Reference instance on the resolver for use during mapping
    // resolution phase
    ReferenceResolver resolver = ((DOMRecord) databaseRow).getReferenceResolver();
    if (resolver != null) {
        resolver.addReference(new Reference(this, targetObject, referenceClass, primaryKeys));
    }
    return null;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Reference(org.eclipse.persistence.internal.oxm.Reference) ReferenceResolver(org.eclipse.persistence.internal.oxm.ReferenceResolver)

Aggregations

CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)3 Reference (org.eclipse.persistence.internal.oxm.Reference)3 XMLField (org.eclipse.persistence.oxm.XMLField)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 ReferenceResolver (org.eclipse.persistence.internal.oxm.ReferenceResolver)2 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)1 DOMRecord (org.eclipse.persistence.oxm.record.DOMRecord)1