Search in sources :

Example 11 with ContainerPolicy

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

the class EISOneToManyMapping method writeFromObjectIntoRow.

/**
 * INTERNAL:
 * Get the appropriate attribute value from the object
 * and put it in the appropriate field of the database row.
 * Loop through the reference objects and extract the
 * primary keys and put them in the vector of "nested" rows.
 */
@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) {
    if (!isForeignKeyRelationship) {
        return;
    }
    if (((getSourceForeignKeysToTargetKeys()) == null) || (getSourceForeignKeysToTargetKeys().size() == 0)) {
        return;
    }
    if (this.isReadOnly()) {
        return;
    }
    AbstractRecord referenceRow = this.getIndirectionPolicy().extractReferenceRow(this.getAttributeValueFromObject(object));
    if (referenceRow != null) {
        // the reference objects have not been instantiated - use the value from the original row
        if (getForeignKeyGroupingElement() != null) {
            row.put(this.getForeignKeyGroupingElement(), referenceRow.getValues(this.getForeignKeyGroupingElement()));
        } else if (getSourceForeignKeyFields().size() > 0) {
            DatabaseField foreignKeyField = getSourceForeignKeyFields().get(0);
            row.put(foreignKeyField, referenceRow.getValues(foreignKeyField));
        }
        return;
    }
    ContainerPolicy cp = this.getContainerPolicy();
    // extract the keys from the objects
    Object attributeValue = this.getRealCollectionAttributeValueFromObject(object, session);
    if (getForeignKeyGroupingElement() != null) {
        Vector<AbstractRecord> nestedRows = new Vector<>(cp.sizeFor(attributeValue));
        for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
            AbstractRecord nestedRow = extractKeyRowFromReferenceObject(cp.next(iter, session), session, row);
            nestedRows.add(nestedRow);
        }
        row.add(this.getForeignKeyGroupingElement(), nestedRows);
    } else {
        DatabaseField singleField = getSourceForeignKeyFields().get(0);
        DatabaseField pkField = getSourceForeignKeysToTargetKeys().get(singleField);
        List<Object> foreignKeys = new ArrayList<>(cp.sizeFor(attributeValue));
        for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
            Object singleValue = getReferenceDescriptor().getObjectBuilder().extractValueFromObjectForField(cp.next(iter, session), pkField, session);
            foreignKeys.add(singleValue);
        }
        row.add(singleField, foreignKeys);
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ArrayList(java.util.ArrayList) Vector(java.util.Vector)

Example 12 with ContainerPolicy

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

the class EISOneToManyMapping method postDelete.

/**
 * INTERNAL:
 * Delete the reference objects.
 */
@Override
public void postDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!isForeignKeyRelationship()) {
        return;
    }
    if (!this.shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    Object referenceObjects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
    // otherwise, delete the reference objects one by one
    if (this.hasCustomDeleteAllQuery()) {
        this.deleteAll(query, referenceObjects);
    } else {
        ContainerPolicy cp = this.getContainerPolicy();
        for (Object iter = cp.iteratorFor(referenceObjects); 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);
        }
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery)

Example 13 with ContainerPolicy

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

the class EISOneToManyMapping method verifyDelete.

/**
 * INTERNAL:
 * Used to verify whether the specified object is deleted or not.
 */
@Override
public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException {
    if (this.isPrivateOwned()) {
        Object objects = this.getRealCollectionAttributeValueFromObject(object, session);
        ContainerPolicy containerPolicy = getContainerPolicy();
        for (Object iter = containerPolicy.iteratorFor(objects); containerPolicy.hasNext(iter); ) {
            if (!session.verifyDelete(containerPolicy.next(iter, session))) {
                return false;
            }
        }
    }
    return true;
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy)

Example 14 with ContainerPolicy

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

the class EISOneToManyQueryBasedValueHolder method instantiate.

@Override
protected T instantiate(AbstractSession session) throws DatabaseException {
    Vector<AbstractRecord> rows = this.mapping.getForeignKeyRows(this.getRow(), session);
    int size = rows.size();
    ContainerPolicy cp = ((ReadAllQuery) this.getQuery()).getContainerPolicy();
    @SuppressWarnings({ "unchecked" }) T returnValue = (T) cp.containerInstance(size);
    for (int i = 0; i < size; i++) {
        AbstractRecord nextRow = rows.get(i);
        Object results = session.executeQuery(getQuery(), nextRow);
        if (results instanceof Collection) {
            Iterator<?> iter = ((Collection<?>) results).iterator();
            while (iter.hasNext()) {
                cp.addInto(iter.next(), returnValue, session);
            }
        } else if (results instanceof java.util.Map) {
            Iterator<?> iter = ((java.util.Map<?, ?>) results).values().iterator();
            while (iter.hasNext()) {
                cp.addInto(iter.next(), returnValue, session);
            }
        } else {
            cp.addInto(results, returnValue, session);
        }
    }
    return returnValue;
}
Also used : AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Iterator(java.util.Iterator) Collection(java.util.Collection)

Example 15 with ContainerPolicy

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

the class CollectionChangeRecord method internalRecreateOriginalCollection.

/**
 * Recreates the original state of currentCollection.
 */
@Override
public void internalRecreateOriginalCollection(Object currentCollection, AbstractSession session) {
    ContainerPolicy cp = this.mapping.getContainerPolicy();
    if (orderedChangeObjectList == null || orderedChangeObjectList.isEmpty()) {
        if (this.removeObjectList != null) {
            Iterator<ObjectChangeSet> it = this.removeObjectList.keySet().iterator();
            while (it.hasNext()) {
                ObjectChangeSet changeSet = it.next();
                cp.addInto(changeSet.getUnitOfWorkClone(), currentCollection, session);
            }
        }
        if (this.addObjectList != null) {
            Iterator<ObjectChangeSet> it = this.addObjectList.keySet().iterator();
            while (it.hasNext()) {
                ObjectChangeSet changeSet = it.next();
                cp.removeFrom(changeSet.getUnitOfWorkClone(), currentCollection, session);
            }
        }
    } else {
        List originalList = (List) currentCollection;
        for (int i = this.orderedChangeObjectList.size() - 1; i >= 0; i--) {
            OrderedChangeObject orderedChange = this.orderedChangeObjectList.get(i);
            Object obj = orderedChange.getAddedOrRemovedObject();
            Integer index = orderedChange.getIndex();
            int changeType = orderedChange.getChangeType();
            if (changeType == CollectionChangeEvent.ADD) {
                // the object was added - remove the corresponding index
                if (index == null) {
                    originalList.remove(originalList.size() - 1);
                } else {
                    originalList.remove(index.intValue());
                }
            } else if (changeType == CollectionChangeEvent.REMOVE) {
                // the object was removed - add its index in the new list
                if (index == null) {
                    throw ValidationException.collectionRemoveEventWithNoIndex(getMapping());
                } else {
                    originalList.add(index, obj);
                }
            }
        }
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) List(java.util.List) ArrayList(java.util.ArrayList)

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