Search in sources :

Example 1 with MethodBasedFieldTransformer

use of org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer 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)

Aggregations

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 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 PrivilegedNewInstanceFromClass (org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass)1 FieldTransformer (org.eclipse.persistence.mappings.transformers.FieldTransformer)1 MethodBasedFieldTransformer (org.eclipse.persistence.mappings.transformers.MethodBasedFieldTransformer)1