Search in sources :

Example 56 with ContainerPolicy

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

the class XMLVariableXPathCollectionMapping method writeFromObjectIntoRow.

@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) throws DescriptorException {
    if (this.isReadOnly()) {
        return;
    }
    Object attributeValue = this.getAttributeValueFromObject(object);
    ContainerPolicy cp = this.getContainerPolicy();
    Object iter = cp.iteratorFor(attributeValue);
    if (null != iter) {
        while (cp.hasNext(iter)) {
            Object element = cp.next(iter, session);
            // convert the value - if necessary
            element = convertObjectValueToDataValue(element, session, ((XMLRecord) row).getMarshaller());
            if (element != null) {
                XMLField variableField = new XMLField();
                XMLRecord xmlRow = (XMLRecord) row;
                // variableField.setXPathFragment(getXPathFragmentForValue(element,(XMLRecord)row));
                variableField.setXPathFragment(getXPathFragmentForValue(element, xmlRow.getNamespaceResolver(), xmlRow.isNamespaceAware(), xmlRow.getNamespaceSeparator()));
                row.put(variableField, buildCompositeRow(variableField, element, session, row, writeType));
            }
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) MapContainerPolicy(org.eclipse.persistence.internal.queries.MapContainerPolicy) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 57 with ContainerPolicy

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

the class EISOneToManyMapping method postInsert.

/**
 * INTERNAL:
 * Insert the reference objects.
 */
@Override
public void postInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (isForeignKeyRelationship()) {
        return;
    }
    if (!this.shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    // only cascade dependents in UOW
    if (query.shouldCascadeOnlyDependentParts()) {
        return;
    }
    Object objects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
    // insert each object one by one
    ContainerPolicy cp = this.getContainerPolicy();
    for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
        Object object = cp.next(iter, query.getSession());
        if (this.isPrivateOwned()) {
            // no need to set changeSet as insert is a straight copy
            InsertObjectQuery insertQuery = new InsertObjectQuery();
            insertQuery.setIsExecutionClone(true);
            insertQuery.setObject(object);
            insertQuery.setCascadePolicy(query.getCascadePolicy());
            query.getSession().executeQuery(insertQuery);
        } else {
            // Avoid cycles by checking commit manager, this is allowed because there is no dependency.
            if (!query.getSession().getCommitManager().isCommitInPreModify(object)) {
                WriteObjectQuery writeQuery = new WriteObjectQuery();
                writeQuery.setIsExecutionClone(true);
                writeQuery.setObject(object);
                writeQuery.setCascadePolicy(query.getCascadePolicy());
                query.getSession().executeQuery(writeQuery);
            }
        }
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) WriteObjectQuery(org.eclipse.persistence.queries.WriteObjectQuery) InsertObjectQuery(org.eclipse.persistence.queries.InsertObjectQuery)

Example 58 with ContainerPolicy

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

the class EISOneToManyMapping method preInsert.

/**
 * INTERNAL:
 * Insert privately owned parts
 */
@Override
public void preInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!this.isForeignKeyRelationship()) {
        return;
    }
    if (!this.shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    // only cascade dependents in UOW
    if (query.shouldCascadeOnlyDependentParts()) {
        return;
    }
    Object objects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
    // insert each object one by one
    ContainerPolicy cp = this.getContainerPolicy();
    for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
        Object object = cp.next(iter, query.getSession());
        if (this.isPrivateOwned()) {
            // no need to set changeset here as insert is just a copy of the object anyway
            InsertObjectQuery insertQuery = new InsertObjectQuery();
            insertQuery.setIsExecutionClone(true);
            insertQuery.setObject(object);
            insertQuery.setCascadePolicy(query.getCascadePolicy());
            query.getSession().executeQuery(insertQuery);
        } else {
            // Avoid cycles by checking commit manager, this is allowed because there is no dependency.
            if (!query.getSession().getCommitManager().isCommitInPreModify(object)) {
                WriteObjectQuery writeQuery = new WriteObjectQuery();
                writeQuery.setIsExecutionClone(true);
                if (query.getSession().isUnitOfWork()) {
                    UnitOfWorkChangeSet uowChangeSet = (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet();
                    if (uowChangeSet != null) {
                        writeQuery.setObjectChangeSet((ObjectChangeSet) uowChangeSet.getObjectChangeSetForClone(object));
                    }
                }
                writeQuery.setObject(object);
                writeQuery.setCascadePolicy(query.getCascadePolicy());
                query.getSession().executeQuery(writeQuery);
            }
        }
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) WriteObjectQuery(org.eclipse.persistence.queries.WriteObjectQuery) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) InsertObjectQuery(org.eclipse.persistence.queries.InsertObjectQuery)

Example 59 with ContainerPolicy

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

the class EISOneToManyMapping method valueFromRow.

/**
 * INTERNAL:
 * Return the value of the reference attribute or a value holder.
 * Check whether the mapping's attribute should be optimized through batch and joining.
 */
@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
    if (this.descriptor.getCachePolicy().isProtectedIsolation()) {
        if (this.isCacheable && isTargetProtected && cacheKey != null) {
            // cachekey will be null when isolating to uow
            // used cached collection
            Object cached = cacheKey.getObject();
            if (cached != null) {
                if (wasCacheUsed != null) {
                    wasCacheUsed[0] = Boolean.TRUE;
                }
                // the indirection object is responsible for cloning the value.
                return getAttributeValueFromObject(cached);
            }
        } else if (!this.isCacheable && !isTargetProtected && cacheKey != null) {
            return this.indirectionPolicy.buildIndirectObject(new ValueHolder<>(null));
        }
    }
    if (((EISDescriptor) this.getDescriptor()).isXMLFormat()) {
        ((XMLRecord) row).setSession(executionSession);
    }
    ReadQuery targetQuery = getSelectionQuery();
    if (!this.isForeignKeyRelationship) {
        // if the source query is cascading then the target query must use the same settings
        if (targetQuery.isObjectLevelReadQuery() && (sourceQuery.shouldCascadeAllParts() || (sourceQuery.shouldCascadePrivateParts() && isPrivateOwned()) || (sourceQuery.shouldCascadeByMapping() && this.cascadeRefresh))) {
            targetQuery = (ObjectLevelReadQuery) targetQuery.clone();
            ((ObjectLevelReadQuery) targetQuery).setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
            targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
            // CR #4365
            targetQuery.setQueryId(sourceQuery.getQueryId());
            // For queries that have turned caching off, such as aggregate collection, leave it off.
            if (targetQuery.shouldMaintainCache()) {
                targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
            }
        }
        return getIndirectionPolicy().valueFromQuery(targetQuery, row, sourceQuery.getSession());
    } else {
        if (getIndirectionPolicy().usesIndirection()) {
            EISOneToManyQueryBasedValueHolder valueholder = new EISOneToManyQueryBasedValueHolder(this, targetQuery, row, sourceQuery.getSession());
            return getIndirectionPolicy().buildIndirectObject(valueholder);
        } else {
            Vector<AbstractRecord> subRows = getForeignKeyRows(row, executionSession);
            if (subRows == null) {
                return null;
            }
            ContainerPolicy cp = this.getContainerPolicy();
            Object results = cp.containerInstance(subRows.size());
            for (int i = 0; i < subRows.size(); i++) {
                XMLRecord subRow = (XMLRecord) subRows.elementAt(i);
                subRow.setSession(executionSession);
                Object object = getIndirectionPolicy().valueFromQuery(targetQuery, subRow, sourceQuery.getSession());
                if (object instanceof Collection) {
                    java.util.Iterator<?> iter = ((Collection<?>) object).iterator();
                    while (iter.hasNext()) {
                        cp.addInto(iter.next(), results, executionSession);
                    }
                } else if (object instanceof java.util.Map) {
                    java.util.Iterator<?> iter = ((java.util.Map<?, ?>) object).values().iterator();
                    while (iter.hasNext()) {
                        cp.addInto(iter.next(), results, executionSession);
                    }
                } else {
                    cp.addInto(object, results, executionSession);
                }
            }
            if (cp.sizeFor(results) == 0) {
                return null;
            }
            return results;
        }
    }
}
Also used : AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) EISOneToManyQueryBasedValueHolder(org.eclipse.persistence.internal.indirection.EISOneToManyQueryBasedValueHolder) EISDescriptor(org.eclipse.persistence.eis.EISDescriptor) EISOneToManyQueryBasedValueHolder(org.eclipse.persistence.internal.indirection.EISOneToManyQueryBasedValueHolder) ValueHolder(org.eclipse.persistence.indirection.ValueHolder) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Map(java.util.Map) Iterator(java.util.Iterator) Collection(java.util.Collection) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ReadQuery(org.eclipse.persistence.queries.ReadQuery) HashMap(java.util.HashMap) Map(java.util.Map)

Example 60 with ContainerPolicy

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

the class EISOneToManyMapping 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 = this.readPrivateOwnedForObject(query);
    // delete all these objects one by one
    ContainerPolicy cp = this.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