Search in sources :

Example 6 with ContainerPolicy

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

the class JAXBValueStore method unsetDeclaredProperty.

/**
 * For isMany=false properties set the value to null. For isMany=true set
 * the value to an empty container of the appropriate type.
 */
@Override
public void unsetDeclaredProperty(int propertyIndex) {
    SDOProperty declaredProperty = (SDOProperty) dataObject.getType().getDeclaredProperties().get(propertyIndex);
    Mapping mapping = this.getJAXBMappingForProperty(declaredProperty);
    if (declaredProperty.isMany()) {
        ContainerMapping containerMapping = (ContainerMapping) mapping;
        ContainerPolicy containerPolicy = containerMapping.getContainerPolicy();
        // OLD VALUE
        if (mapping.isAbstractCompositeCollectionMapping()) {
            XMLCompositeCollectionMapping compositeMapping = (XMLCompositeCollectionMapping) mapping;
            XMLInverseReferenceMapping inverseReferenceMapping = compositeMapping.getInverseReferenceMapping();
            if (inverseReferenceMapping != null && inverseReferenceMapping.getAttributeAccessor() != null) {
                Object oldContainer = mapping.getAttributeValueFromObject(entity);
                if (oldContainer != null) {
                    AbstractSession session = ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
                    Object iterator = containerPolicy.iteratorFor(oldContainer);
                    while (containerPolicy.hasNext(iterator)) {
                        Object oldValue = containerPolicy.next(iterator, session);
                        inverseReferenceMapping.getAttributeAccessor().setAttributeValueInObject(oldValue, null);
                    }
                }
            }
        }
        // NEW VALUE
        Object container = containerPolicy.containerInstance();
        mapping.getAttributeAccessor().setAttributeValueInObject(entity, container);
    } else {
        // OLD VALUE
        Object oldValue = mapping.getAttributeAccessor().getAttributeValueFromObject(entity);
        if (mapping.isAbstractCompositeObjectMapping()) {
            XMLCompositeObjectMapping compositeMapping = (XMLCompositeObjectMapping) mapping;
            final XMLInverseReferenceMapping inverseReferenceMapping = compositeMapping.getInverseReferenceMapping();
            if (inverseReferenceMapping != null && inverseReferenceMapping.getAttributeAccessor() != null) {
                if (oldValue != null) {
                    inverseReferenceMapping.getAttributeAccessor().setAttributeValueInObject(oldValue, null);
                }
            }
        }
        // NEW VALUE
        mapping.getAttributeAccessor().setAttributeValueInObject(entity, null);
    }
}
Also used : CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) 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) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) ContainerMapping(org.eclipse.persistence.mappings.ContainerMapping) SDOProperty(org.eclipse.persistence.sdo.SDOProperty) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 7 with ContainerPolicy

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

the class EISOneToManyMapping method preDelete.

/**
 * INTERNAL:
 * Delete the reference objects.
 */
@Override
public void preDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (isForeignKeyRelationship()) {
        return;
    }
    if (!this.shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    Object objects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
    ContainerPolicy cp = this.getContainerPolicy();
    // else delete everything in one shot
    if (this.mustDeleteReferenceObjectsOneByOne()) {
        for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
            DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
            deleteQuery.setIsExecutionClone(true);
            deleteQuery.setObject(cp.next(iter, query.getSession()));
            deleteQuery.setCascadePolicy(query.getCascadePolicy());
            query.getSession().executeQuery(deleteQuery);
        }
        if (!query.getSession().isUnitOfWork()) {
            // This deletes any objects on the database, as the collection in memory may have been changed.
            // This is not required for unit of work, as the update would have already deleted these objects,
            // and the backup copy will include the same objects causing double deletes.
            this.deleteReferenceObjectsLeftOnDatabase(query);
        }
    } else {
        this.deleteAll(query);
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery)

Example 8 with ContainerPolicy

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

the class EISOneToManyMappingHelper method mergeChangesIntoObjectWithOrder.

/**
 * Merge changes from the source to the target object.
 * Simply replace the entire target collection.
 */
private void mergeChangesIntoObjectWithOrder(Object target, ChangeRecord changeRecord, Object source, MergeManager mergeManager, AbstractSession targetSession) {
    ContainerPolicy cp = this.getContainerPolicy();
    AbstractSession session = mergeManager.getSession();
    List changes = ((EISOrderedCollectionChangeRecord) changeRecord).getNewCollection();
    Object targetCollection = cp.containerInstance(changes.size());
    for (Object changed : changes) {
        Object targetElement = buildAddedElementFromChangeSet(changed, mergeManager, targetSession);
        cp.addInto(targetElement, targetCollection, session);
    }
    // reset the attribute to allow for set method to re-morph changes if the collection is not being stored directly
    this.setRealAttributeValueInObject(target, targetCollection);
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) List(java.util.List) EISOrderedCollectionChangeRecord(org.eclipse.persistence.eis.EISOrderedCollectionChangeRecord) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 9 with ContainerPolicy

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

the class EISOneToManyMappingHelper method mergeIntoObject.

/**
 * INTERNAL:
 * Merge changes from the source to the target object.
 * Simply replace the entire target collection.
 */
public void mergeIntoObject(Object target, boolean isTargetUnInitialized, Object source, MergeManager mergeManager, AbstractSession targetSession) {
    ContainerPolicy cp = this.getContainerPolicy();
    AbstractSession session = mergeManager.getSession();
    Object sourceCollection = this.getRealCollectionAttributeValueFromObject(source, session);
    Object targetCollection = cp.containerInstance(cp.sizeFor(sourceCollection));
    for (Object iter = cp.iteratorFor(sourceCollection); cp.hasNext(iter); ) {
        Object targetElement = this.buildElementFromElement(cp.next(iter, session), mergeManager, targetSession);
        cp.addInto(targetElement, targetCollection, session);
    }
    // reset the attribute to allow for set method to re-morph changes if the collection is not being stored directly
    setRealAttributeValueInObject(target, targetCollection);
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 10 with ContainerPolicy

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

the class EISOneToManyMappingHelper method compareAttributeValuesForChangeWithOrder.

/**
 * Build and return the change record that results
 * from comparing the two collection attributes.
 * The order of the elements is significant.
 */
private ChangeRecord compareAttributeValuesForChangeWithOrder(Object cloneCollection, Object backupCollection, ObjectChangeSet owner, AbstractSession session) {
    ContainerPolicy cp = this.getContainerPolicy();
    // convert it to a Vector so we can preserve the order and use indexes
    List cloneVector = cp.vectorFor(cloneCollection, session);
    // "clone" it so we can clear out the slots
    List backupVector = cp.vectorFor(backupCollection, session);
    EISOrderedCollectionChangeRecord changeRecord = new EISOrderedCollectionChangeRecord(owner, this.getAttributeName(), this.getDatabaseMapping());
    for (int i = 0; i < cloneVector.size(); i++) {
        Object cloneElement = cloneVector.get(i);
        boolean found = false;
        for (int j = 0; j < backupVector.size(); j++) {
            if (this.compareElementsForChange(cloneElement, backupVector.get(j), session)) {
                // the clone element was found in the backup collection
                found = true;
                // clear out the matching backup element
                backupVector.set(j, XXX);
                changeRecord.addMovedChangeSet(this.buildChangeSet(cloneElement, owner, session), j, i);
                // matching backup element found - skip the rest of them
                break;
            }
        }
        if (!found) {
            // the clone element was not found, so it must have been added
            changeRecord.addAddedChangeSet(this.buildChangeSet(cloneElement, owner, session), i);
        }
    }
    for (int i = 0; i < backupVector.size(); i++) {
        Object backupElement = backupVector.get(i);
        if (backupElement != XXX) {
            // the backup element was not in the clone collection, so it must have been removed
            changeRecord.addRemovedChangeSet(this.buildChangeSet(backupElement, owner, session), i);
        }
    }
    if (changeRecord.hasChanges()) {
        return changeRecord;
    } else {
        return null;
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) List(java.util.List) EISOrderedCollectionChangeRecord(org.eclipse.persistence.eis.EISOrderedCollectionChangeRecord)

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