Search in sources :

Example 16 with ContainerPolicy

use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.

the class DeferrableChangeRecord method getOldValue.

/**
 * ADVANCED:
 * If the owning UnitOfWork has shouldChangeRecordKeepOldValue set to true,
 * then return the old value of the attribute represented by this ChangeRecord.
 */
@Override
public Object getOldValue() {
    if (this.originalCollection != null) {
        return this.originalCollection;
    } else {
        if (getOwner() != null) {
            Object obj = ((org.eclipse.persistence.internal.sessions.ObjectChangeSet) getOwner()).getUnitOfWorkClone();
            AbstractSession session = ((org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) getOwner().getUOWChangeSet()).getSession();
            if (obj != null && session != null) {
                Object currentCollection = this.mapping.getAttributeValueFromObject(obj);
                ContainerPolicy cp = this.mapping.getContainerPolicy();
                Object cloneCurrentCollection = cp.containerInstance(cp.sizeFor(currentCollection));
                for (Object valuesIterator = cp.iteratorFor(currentCollection); cp.hasNext(valuesIterator); ) {
                    Object member = cp.next(valuesIterator, session);
                    cp.addInto(cp.keyFromIterator(valuesIterator), member, cloneCurrentCollection, session);
                }
                return getOldValue(cloneCurrentCollection, session);
            }
        }
        return null;
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy)

Example 17 with ContainerPolicy

use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.

the class DirectMapChangeRecord method internalRecreateOriginalCollection.

/**
 * Recreates the original state of the collection.
 */
@Override
public void internalRecreateOriginalCollection(Object currentMap, AbstractSession session) {
    ContainerPolicy cp = this.mapping.getContainerPolicy();
    if (this.removeObjectsList != null) {
        Iterator it = this.removeObjectsList.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            cp.addInto(entry.getKey(), entry.getValue(), currentMap, session);
        }
    }
    if (this.addObjectsList != null) {
        Iterator it = this.addObjectsList.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            cp.removeFrom(entry.getKey(), entry.getValue(), currentMap, session);
        }
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy)

Example 18 with ContainerPolicy

use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.

the class XPathObjectBuilder method buildRow.

@Override
public XMLRecord buildRow(XMLRecord record, Object object, CoreAbstractSession session, Marshaller marshaller, XPathFragment rootFragment) {
    lazyInitialize();
    XPathNode textNode = rootXPathNode.getTextNode();
    List<XPathNode> nonAttributeChildren = rootXPathNode.getNonAttributeChildren();
    if (null == textNode && null == nonAttributeChildren) {
        return record;
    }
    Descriptor xmlDescriptor = (Descriptor) descriptor;
    XPathNode node = rootXPathNode;
    MarshalRecord marshalRecord = (MarshalRecord) record;
    QName schemaType = null;
    if (marshalRecord.getCycleDetectionStack().contains(object, marshaller.isEqualUsingIdenity())) {
        if (cycleRecoverableClass == null) {
            initCycleRecoverableClasses();
        }
        if (cycleRecoverableClass != null && cycleRecoverableClass.isAssignableFrom(object.getClass())) {
            try {
                Object jaxbMarshaller = marshaller.getProperty(Constants.JAXB_MARSHALLER);
                // Create a proxy instance of CycleRecoverable$Context, a parameter to
                // the onCycleDetected method
                Object contextProxy = CycleRecoverableContextProxy.getProxy(cycleRecoverableContextClass, jaxbMarshaller);
                // Invoke onCycleDetected method, passing in proxy, and reset
                // 'object' to the returned value
                Method onCycleDetectedMethod = object.getClass().getMethod(ON_CYCLE_DETECTED, cycleRecoverableContextClass);
                object = PrivilegedAccessHelper.invokeMethod(onCycleDetectedMethod, object, new Object[] { contextProxy });
            } catch (Exception e) {
                throw XMLMarshalException.marshalException(e);
            }
            // Returned object might have a different descriptor
            xmlDescriptor = (Descriptor) session.getDescriptor(object.getClass());
            if (xmlDescriptor != null) {
                node = ((ObjectBuilder) xmlDescriptor.getObjectBuilder()).getRootXPathNode();
            } else {
                node = null;
            }
            // Push new object
            marshalRecord.getCycleDetectionStack().push(object);
            // Write xsi:type if onCycleDetected returned an object of a type different than the one mapped
            if (xmlDescriptor != descriptor) {
                if (xmlDescriptor == null) {
                    schemaType = record.getConversionManager().schemaType(object.getClass());
                } else {
                    schemaType = xmlDescriptor.getSchemaReference().getSchemaContextAsQName();
                }
                marshalRecord.writeXsiTypeAttribute(xmlDescriptor, schemaType.getNamespaceURI(), schemaType.getLocalPart(), schemaType.getPrefix(), false);
            }
        } else {
            // Push the duplicate object anyway, so that we can get the complete cycle string
            marshalRecord.getCycleDetectionStack().push(object);
            throw XMLMarshalException.objectCycleDetected(marshalRecord.getCycleDetectionStack().getCycleString());
        }
    } else {
        marshalRecord.getCycleDetectionStack().push(object);
    }
    NamespaceResolver namespaceResolver = null;
    if (xmlDescriptor != null) {
        namespaceResolver = xmlDescriptor.getNamespaceResolver();
    }
    MarshalContext marshalContext = null;
    if (xmlDescriptor != null && xmlDescriptor.isSequencedObject()) {
        SequencedObject sequencedObject = (SequencedObject) object;
        marshalContext = new SequencedMarshalContext(sequencedObject.getSettings());
    } else {
        marshalContext = ObjectMarshalContext.getInstance();
    }
    if (null == nonAttributeChildren) {
        textNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext, rootFragment);
    } else {
        if (node == null) {
            // No descriptor for this object, so manually create a MappingNodeValue and marshal it
            XPathNode n = new XPathNode();
            CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, org.eclipse.persistence.oxm.record.XMLRecord> m = new XMLCompositeObjectMapping();
            m.setXPath(".");
            XMLCompositeObjectMappingNodeValue nv = new XMLCompositeObjectMappingNodeValue(m);
            n.setMarshalNodeValue(nv);
            nv.marshalSingleValue(new XPathFragment("."), marshalRecord, null, object, session, namespaceResolver, marshalContext);
        } else {
            for (int x = 0, size = marshalContext.getNonAttributeChildrenSize(node); x < size; x++) {
                XPathNode xPathNode = (XPathNode) marshalContext.getNonAttributeChild(x, node);
                xPathNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext.getMarshalContext(x), rootFragment);
            }
        }
    }
    marshalRecord.getCycleDetectionStack().pop();
    return record;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) AbstractMarshalRecord(org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord) MarshalRecord(org.eclipse.persistence.internal.oxm.record.MarshalRecord) SequencedObject(org.eclipse.persistence.oxm.sequenced.SequencedObject) Converter(org.eclipse.persistence.mappings.converters.Converter) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession) QName(javax.xml.namespace.QName) SequencedMarshalContext(org.eclipse.persistence.internal.oxm.record.SequencedMarshalContext) Method(java.lang.reflect.Method) XMLRecord(org.eclipse.persistence.internal.oxm.record.XMLRecord) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) ObjectMarshalContext(org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext) SequencedMarshalContext(org.eclipse.persistence.internal.oxm.record.SequencedMarshalContext) MarshalContext(org.eclipse.persistence.internal.oxm.record.MarshalContext) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) SequencedObject(org.eclipse.persistence.oxm.sequenced.SequencedObject) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)

Example 19 with ContainerPolicy

use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.

the class OneToManyMapping method updateTargetForeignKeyPostUpdateSource_ObjectAdded.

/**
 * INTERNAL:
 * Update target foreign key after a target object was added to the source. This follows following steps.
 * <p>- Extract primary key and its value from the source object.
 * <p>- Extract target key and its value from the target object.
 * <p>- Construct an update statement with above fields and values for target table.
 * <p>- execute the statement.
 */
public void updateTargetForeignKeyPostUpdateSource_ObjectAdded(ObjectLevelModifyQuery query, Object objectAdded, Map extraData) throws DatabaseException {
    if (isReadOnly() || addTargetQuery == null) {
        return;
    }
    ContainerPolicy cp = getContainerPolicy();
    prepareTranslationRow(query.getTranslationRow(), query.getObject(), query.getDescriptor(), query.getSession());
    AbstractRecord databaseRow = buildKeyRowForTargetUpdate(query);
    // Extract target field and its value. Construct insert statement and execute it
    int size = targetPrimaryKeyFields.size();
    for (int index = 0; index < size; index++) {
        DatabaseField targetPrimaryKey = targetPrimaryKeyFields.get(index);
        Object targetKeyValue = getReferenceDescriptor().getObjectBuilder().extractValueFromObjectForField(cp.unwrapIteratorResult(objectAdded), targetPrimaryKey, query.getSession());
        databaseRow.put(targetPrimaryKey, targetKeyValue);
    }
    ContainerPolicy.copyMapDataToRow(cp.getKeyMappingDataForWriteQuery(objectAdded, query.getSession()), databaseRow);
    if (listOrderField != null && extraData != null) {
        databaseRow.put(listOrderField, extraData.get(listOrderField));
    }
    query.getSession().executeQuery(addTargetQuery, databaseRow);
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord)

Example 20 with ContainerPolicy

use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.

the class OneToManyMapping method deleteReferenceObjectsLeftOnDatabase.

/**
 *    This method will make sure that all the records privately owned by this mapping are
 * actually removed. If such records are found then those are all read and removed one
 * by one along with their privately owned parts.
 */
protected void deleteReferenceObjectsLeftOnDatabase(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    Object objects = readPrivateOwnedForObject(query);
    // Delete all these object one by one.
    ContainerPolicy cp = getContainerPolicy();
    for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
        query.getSession().deleteObject(cp.next(iter, query.getSession()));
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy)

Aggregations

ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)119 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)28 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)17 XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)17 List (java.util.List)14 Vector (java.util.Vector)14 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)14 XMLField (org.eclipse.persistence.oxm.XMLField)14 ArrayList (java.util.ArrayList)13 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)13 Collection (java.util.Collection)12 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)10 CollectionContainerPolicy (org.eclipse.persistence.internal.queries.CollectionContainerPolicy)9 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)8 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)8 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)8 HashMap (java.util.HashMap)7 VirtualAttributeAccessor (org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor)7 CustomAccessorAttributeAccessor (org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor)7 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)7