Search in sources :

Example 1 with AbstractTransformationMapping

use of org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method assignReturnValueToMapping.

/**
 * INTERNAL:
 * Assign values from objectRow to the object through the mapping.
 * If not null changeSet must correspond to object. changeSet is updated with all of the field values in the row.
 */
protected void assignReturnValueToMapping(Object object, ReadObjectQuery query, AbstractRecord row, DatabaseField field, DatabaseMapping mapping, Collection handledMappings, ObjectChangeSet changeSet) {
    if ((handledMappings != null) && handledMappings.contains(mapping)) {
        return;
    }
    if (mapping.isAbstractDirectMapping()) {
        if (changeSet != null && (!changeSet.isNew() || (query.getDescriptor() != null && query.getDescriptor().shouldUseFullChangeSetsForNewObjects()))) {
            DirectToFieldChangeRecord changeRecord = (DirectToFieldChangeRecord) changeSet.getChangesForAttributeNamed(mapping.getAttributeName());
            Object oldAttributeValue = null;
            if (changeRecord == null) {
                oldAttributeValue = mapping.getAttributeValueFromObject(object);
            }
            // use null cachekey to ensure we build directly into the attribute
            Object attributeValue = mapping.readFromRowIntoObject(row, null, object, null, query, query.getSession(), true);
            if (changeRecord == null) {
                // Don't use ObjectChangeSet.updateChangeRecordForAttributeWithMappedObject to avoid unnecessary conversion - attributeValue is already converted.
                changeRecord = (DirectToFieldChangeRecord) ((AbstractDirectMapping) mapping).internalBuildChangeRecord(attributeValue, oldAttributeValue, changeSet);
                changeSet.addChange(changeRecord);
            } else {
                changeRecord.setNewValue(attributeValue);
            }
        } else {
            mapping.readFromRowIntoObject(row, null, object, null, query, query.getSession(), true);
        }
    } else if (mapping.isAggregateObjectMapping()) {
        ((AggregateObjectMapping) mapping).readFromReturnRowIntoObject(row, object, query, handledMappings, changeSet);
    } else if (mapping.isTransformationMapping()) {
        ((AbstractTransformationMapping) mapping).readFromReturnRowIntoObject(row, object, query, handledMappings, changeSet);
    } else {
        query.getSession().log(SessionLog.FINEST, SessionLog.QUERY, "field_for_unsupported_mapping_returned", field, this.descriptor);
    }
}
Also used : AbstractTransformationMapping(org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping) AbstractDirectMapping(org.eclipse.persistence.mappings.foundation.AbstractDirectMapping) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) DirectToFieldChangeRecord(org.eclipse.persistence.internal.sessions.DirectToFieldChangeRecord)

Example 2 with AbstractTransformationMapping

use of org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping in project eclipselink by eclipse-ee4j.

the class AggregateObjectMapping method readFromReturnRowIntoObject.

/**
 * INTERNAL:
 * Build an aggregate object from the specified return row and put it
 * in the specified target object.
 * Return row is merged into object after execution of insert or update call
 * according to ReturningPolicy.
 * If not null changeSet must correspond to targetObject. changeSet is updated with all of the field values in the row.
 */
public Object readFromReturnRowIntoObject(AbstractRecord row, Object targetObject, ReadObjectQuery query, Collection handledMappings, ObjectChangeSet changeSet) throws DatabaseException {
    Object aggregate = getAttributeValueFromObject(targetObject);
    ObjectChangeSet aggregateChangeSet = null;
    if (aggregate == null) {
        aggregate = readFromRowIntoObject(row, null, targetObject, null, query, query.getSession(), true);
    } else {
        if (changeSet != null && (!changeSet.isNew() || (query.getDescriptor() != null && query.getDescriptor().shouldUseFullChangeSetsForNewObjects()))) {
            aggregateChangeSet = getReferenceDescriptor(aggregate, query.getSession()).getObjectBuilder().createObjectChangeSet(aggregate, (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet(), true, query.getSession());
        }
        AbstractRecord aggregateRow = new DatabaseRecord();
        int size = row.size();
        List<DatabaseField> fields = row.getFields();
        List values = row.getValues();
        List<DatabaseField> aggregateFields = getReferenceFields();
        for (int i = 0; i < size; i++) {
            DatabaseField field = fields.get(i);
            if (aggregateFields.contains(field)) {
                aggregateRow.add(field, values.get(i));
            }
        }
        getObjectBuilder(aggregate, query.getSession()).assignReturnRow(aggregate, query.getSession(), aggregateRow, aggregateChangeSet);
    }
    if (aggregate != null && isNullAllowed()) {
        boolean allAttributesNull = true;
        int nAggregateFields = this.fields.size();
        for (int i = 0; (i < nAggregateFields) && allAttributesNull; i++) {
            DatabaseField field = this.fields.elementAt(i);
            if (row.containsKey(field)) {
                allAttributesNull = row.get(field) == null;
            } else {
                Object fieldValue = valueFromObject(targetObject, field, query.getSession());
                if (fieldValue == null) {
                    Object baseValue = getDescriptor().getObjectBuilder().getBaseValueForField(field, targetObject);
                    if (baseValue != null) {
                        DatabaseMapping baseMapping = getDescriptor().getObjectBuilder().getBaseMappingForField(field);
                        if (baseMapping.isForeignReferenceMapping()) {
                            ForeignReferenceMapping refMapping = (ForeignReferenceMapping) baseMapping;
                            if (refMapping.usesIndirection()) {
                                allAttributesNull = refMapping.getIndirectionPolicy().objectIsInstantiated(baseValue);
                            }
                        } else if (baseMapping.isTransformationMapping()) {
                            AbstractTransformationMapping transMapping = (AbstractTransformationMapping) baseMapping;
                            if (transMapping.usesIndirection()) {
                                allAttributesNull = transMapping.getIndirectionPolicy().objectIsInstantiated(baseValue);
                            }
                        }
                    }
                } else {
                    allAttributesNull = false;
                }
            }
        }
        if (allAttributesNull) {
            aggregate = null;
            setAttributeValueInObject(targetObject, aggregate);
        }
    }
    if (changeSet != null && (!changeSet.isNew() || (query.getDescriptor() != null && query.getDescriptor().shouldUseFullChangeSetsForNewObjects()))) {
        AggregateChangeRecord record = (AggregateChangeRecord) changeSet.getChangesForAttributeNamed(getAttributeName());
        if (aggregate == null) {
            if (record != null) {
                record.setChangedObject(null);
            }
        } else {
            if (record == null) {
                record = new AggregateChangeRecord(changeSet);
                record.setAttribute(getAttributeName());
                record.setMapping(this);
                changeSet.addChange(record);
            }
            if (aggregateChangeSet == null) {
                // the old aggregate value was null
                aggregateChangeSet = getReferenceDescriptor(aggregate, query.getSession()).getObjectBuilder().createObjectChangeSet(aggregate, (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet(), true, query.getSession());
            }
            record.setChangedObject(aggregateChangeSet);
        }
    }
    if (handledMappings != null) {
        handledMappings.add(this);
    }
    return aggregate;
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) AbstractTransformationMapping(org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) List(java.util.List) ArrayList(java.util.ArrayList) AggregateChangeRecord(org.eclipse.persistence.internal.sessions.AggregateChangeRecord)

Aggregations

AbstractTransformationMapping (org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)1 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)1 AggregateChangeRecord (org.eclipse.persistence.internal.sessions.AggregateChangeRecord)1 DirectToFieldChangeRecord (org.eclipse.persistence.internal.sessions.DirectToFieldChangeRecord)1 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)1 UnitOfWorkChangeSet (org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet)1 AbstractDirectMapping (org.eclipse.persistence.mappings.foundation.AbstractDirectMapping)1 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)1