Search in sources :

Example 1 with FieldTransformer

use of org.eclipse.persistence.mappings.transformers.FieldTransformer in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method initializeFieldToTransformers.

/**
 * INTERNAL:
 * Convert the field names and their corresponding method names to
 * DatabaseFields and Methods.
 */
protected void initializeFieldToTransformers(AbstractSession session) throws DescriptorException {
    for (Object[] pair : this.fieldToTransformers) {
        pair[0] = getDescriptor().buildField(((DatabaseField) pair[0]));
        ((FieldTransformer) pair[1]).initialize(this);
    }
    for (FieldTransformation transformation : getFieldTransformations()) {
        DatabaseField field = getDescriptor().buildField(transformation.getField());
        String transformerClassName = "MethodBasedFieldTransformer";
        FieldTransformer transformer = null;
        try {
            transformer = transformation.buildTransformer();
        } catch (ConversionException ex) {
            if (transformation instanceof TransformerBasedFieldTransformation) {
                transformerClassName = ((TransformerBasedFieldTransformation) transformation).getTransformerClassName();
            }
            throw DescriptorException.fieldTransformerClassNotFound(transformerClassName, this, ex);
        } catch (Exception ex) {
            if (transformation instanceof TransformerBasedFieldTransformation) {
                transformerClassName = ((TransformerBasedFieldTransformation) transformation).getTransformerClassName();
            }
            throw DescriptorException.fieldTransformerClassInvalid(transformerClassName, this, ex);
        }
        transformer.initialize(this);
        // Attempt to ensure a type is set on the field.
        if (field.getType() == null) {
            if (transformer instanceof MethodBasedFieldTransformer) {
                field.setType(((MethodBasedFieldTransformer) transformer).getFieldType());
            } else if (field.getColumnDefinition() != null) {
                // Search for the type for this field definition.
                if (session.getDatasourcePlatform() instanceof DatabasePlatform) {
                    Iterator<Map.Entry<Class<?>, FieldTypeDefinition>> iterator = session.getPlatform().getFieldTypes().entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<Class<?>, FieldTypeDefinition> entry = iterator.next();
                        if (entry.getValue().getName().equals(field.getColumnDefinition())) {
                            field.setType(entry.getKey());
                            break;
                        }
                    }
                }
            }
        }
        Object[] fieldToTransformer = new Object[2];
        fieldToTransformer[0] = field;
        fieldToTransformer[1] = transformer;
        this.fieldToTransformers.add(fieldToTransformer);
    }
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) PrivilegedActionException(java.security.PrivilegedActionException) MethodBasedFieldTransformer(org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer) FieldTypeDefinition(org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition) MethodBasedFieldTransformer(org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer) FieldTransformer(org.eclipse.persistence.mappings.transformers.FieldTransformer) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Iterator(java.util.Iterator) DescriptorIterator(org.eclipse.persistence.internal.descriptors.DescriptorIterator) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) Map(java.util.Map) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) FieldTransformation(org.eclipse.persistence.internal.descriptors.FieldTransformation) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)

Example 2 with FieldTransformer

use of org.eclipse.persistence.mappings.transformers.FieldTransformer in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method buildPhantomRowFrom.

/**
 * INTERNAL
 * Build a phantom row that contains only the fields
 * for the mapping, populated with the values generated by
 * invoking the field methods on the specified object.
 */
protected AbstractRecord buildPhantomRowFrom(Object domainObject, AbstractSession session) {
    AbstractRecord row = new DatabaseRecord(this.fieldToTransformers.size());
    for (Object[] pair : this.fieldToTransformers) {
        DatabaseField field = (DatabaseField) pair[0];
        FieldTransformer transformer = (FieldTransformer) pair[1];
        Object fieldValue = this.invokeFieldTransformer(field, transformer, domainObject, session);
        row.put(field, fieldValue);
    }
    return row;
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) MethodBasedFieldTransformer(org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer) FieldTransformer(org.eclipse.persistence.mappings.transformers.FieldTransformer) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord)

Example 3 with FieldTransformer

use of org.eclipse.persistence.mappings.transformers.FieldTransformer in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method writeFromObjectIntoRow.

/**
 * INTERNAL:
 * Get a value from the object and set that in the respective field of the row.
 */
@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) {
    if (isReadOnly()) {
        return;
    }
    for (Object[] pair : this.fieldToTransformers) {
        DatabaseField field = (DatabaseField) pair[0];
        FieldTransformer transformer = (FieldTransformer) pair[1];
        Object fieldValue = invokeFieldTransformer(field, transformer, object, session);
        row.put(field, fieldValue);
    }
}
Also used : DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) MethodBasedFieldTransformer(org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer) FieldTransformer(org.eclipse.persistence.mappings.transformers.FieldTransformer)

Example 4 with FieldTransformer

use of org.eclipse.persistence.mappings.transformers.FieldTransformer in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method compareObjects.

/**
 * INTERNAL:
 * Compare the attributes belonging to this mapping for the objects.
 */
@Override
public boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session) {
    if (!isWriteOnly()) {
        // PERF: Checks if attribute values are equal first before apply field translation.
        Object firstValue = getRealAttributeValueFromObject(firstObject, session);
        Object secondValue = getRealAttributeValueFromObject(secondObject, session);
        if (firstValue == secondValue) {
            return true;
        }
        if ((firstValue == null) || (secondValue == null)) {
            return false;
        }
        if (firstValue.equals(secondValue)) {
            return true;
        }
    }
    for (Object[] pair : this.fieldToTransformers) {
        DatabaseField field = (DatabaseField) pair[0];
        FieldTransformer transformer = (FieldTransformer) pair[1];
        Object firstFieldValue = invokeFieldTransformer(field, transformer, firstObject, session);
        Object secondFieldValue = invokeFieldTransformer(field, transformer, secondObject, session);
        if (firstFieldValue == secondFieldValue) {
            // skip this iteration, go to the next one
            continue;
        }
        if ((firstFieldValue == null) || (secondFieldValue == null)) {
            return false;
        }
        if (!firstFieldValue.equals(secondFieldValue)) {
            if (!Helper.comparePotentialArrays(firstFieldValue, secondFieldValue)) {
                return false;
            }
        }
    }
    return true;
}
Also used : DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) MethodBasedFieldTransformer(org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer) FieldTransformer(org.eclipse.persistence.mappings.transformers.FieldTransformer)

Example 5 with FieldTransformer

use of org.eclipse.persistence.mappings.transformers.FieldTransformer in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method compareForChange.

/**
 * INTERNAL:
 * Compare the attributes belonging to this mapping for the objects.
 */
@Override
public ChangeRecord compareForChange(Object clone, Object backUp, ObjectChangeSet owner, AbstractSession session) {
    if (isReadOnly() || isWriteOnly()) {
        return null;
    }
    Object cloneAttribute = getAttributeValueFromObject(clone);
    Object backUpAttribute = null;
    if ((cloneAttribute != null) && (!this.indirectionPolicy.objectIsInstantiated(cloneAttribute))) {
        return null;
    }
    boolean difference = false;
    Object backupValue = null;
    if (owner.isNew()) {
        difference = true;
    } else {
        if (backUp != null) {
            backUpAttribute = getAttributeValueFromObject(backUp);
            backupValue = this.indirectionPolicy.getRealAttributeValueFromObject(backUp, backUpAttribute);
        }
        boolean backUpIsInstantiated = ((backUpAttribute == null) || (this.indirectionPolicy.objectIsInstantiated(backUpAttribute)));
        Object cloneValue = this.indirectionPolicy.getRealAttributeValueFromObject(clone, cloneAttribute);
        if (backUpIsInstantiated) {
            if (cloneValue == backupValue) {
                return null;
            }
            if (((cloneValue != null && (backupValue != null)) && cloneValue.equals(backupValue))) {
                return null;
            }
        }
        for (Object[] pair : this.fieldToTransformers) {
            DatabaseField field = (DatabaseField) pair[0];
            FieldTransformer transformer = (FieldTransformer) pair[1];
            Object cloneFieldValue = null;
            Object backUpFieldValue = null;
            if (clone != null) {
                cloneFieldValue = invokeFieldTransformer(field, transformer, clone, session);
            }
            if ((backUpIsInstantiated) && (backUp != null)) {
                backUpFieldValue = invokeFieldTransformer(field, transformer, backUp, session);
            }
            if (cloneFieldValue == backUpFieldValue) {
                // skip this iteration, go to the next one
                continue;
            }
            if ((cloneFieldValue == null) || (backUpFieldValue == null)) {
                difference = true;
                // There is a difference.
                break;
            }
            if (cloneFieldValue.equals(backUpFieldValue)) {
                // skip this iteration, go to the next one
                continue;
            }
            if (Helper.comparePotentialArrays(cloneFieldValue, backUpFieldValue)) {
                // skip this iteration, go to the next one
                continue;
            }
            difference = true;
            // There is a difference.
            break;
        }
    }
    if (difference) {
        return internalBuildChangeRecord(clone, backupValue, owner, session);
    }
    return null;
}
Also used : DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) MethodBasedFieldTransformer(org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer) FieldTransformer(org.eclipse.persistence.mappings.transformers.FieldTransformer)

Aggregations

DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)5 FieldTransformer (org.eclipse.persistence.mappings.transformers.FieldTransformer)5 MethodBasedFieldTransformer (org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer)5 PrivilegedActionException (java.security.PrivilegedActionException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)1 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)1 ValidationException (org.eclipse.persistence.exceptions.ValidationException)1 DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)1 FieldTypeDefinition (org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition)1 DescriptorIterator (org.eclipse.persistence.internal.descriptors.DescriptorIterator)1 FieldTransformation (org.eclipse.persistence.internal.descriptors.FieldTransformation)1 MethodBasedFieldTransformation (org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)1 TransformerBasedFieldTransformation (org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation)1 PrivilegedNewInstanceFromClass (org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass)1 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)1 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)1