Search in sources :

Example 1 with FieldTransformation

use of org.eclipse.persistence.internal.descriptors.FieldTransformation in project eclipselink by eclipse-ee4j.

the class ProjectClassGenerator method addTransformationMappingLines.

protected void addTransformationMappingLines(NonreflectiveMethodDefinition method, String mappingName, TransformationMapping mapping) {
    if (!mapping.isWriteOnly()) {
        if (mapping.getAttributeMethodName() != null) {
            method.addLine(mappingName + ".setAttributeTransformation(\"" + mapping.getAttributeMethodName() + "\");");
        } else {
            method.addLine(mappingName + ".setAttributeTransformer(new " + mapping.getAttributeTransformerClassName() + "());");
        }
    }
    Iterator<FieldTransformation> fieldTransformations = mapping.getFieldTransformations().iterator();
    while (fieldTransformations.hasNext()) {
        FieldTransformation trans = fieldTransformations.next();
        String fieldName = trans.getFieldName();
        if (trans instanceof MethodBasedFieldTransformation) {
            String methodName = ((MethodBasedFieldTransformation) trans).getMethodName();
            method.addLine(mappingName + ".addFieldTransformation(\"" + fieldName + "\", \"" + methodName + "\");");
        } else {
            String transformerClass = ((TransformerBasedFieldTransformation) trans).getTransformerClassName();
            method.addLine(mappingName + ".addFieldTransformer(\"" + fieldName + "\", new " + transformerClass + "());");
        }
    }
    IndirectionPolicy policy = mapping.getIndirectionPolicy();
    if (policy instanceof ContainerIndirectionPolicy) {
        String containerClassName = ((ContainerIndirectionPolicy) policy).getContainerClassName();
        method.addLine(mappingName + ".useContainerIndirection(" + containerClassName + ".class);");
    } else if (policy instanceof BasicIndirectionPolicy) {
        method.addLine(mappingName + ".useBasicIndirection();");
    }
    method.addLine(mappingName + ".setIsMutable(" + mapping.isMutable() + ");");
}
Also used : TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation) ContainerIndirectionPolicy(org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy) ProxyIndirectionPolicy(org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy) ContainerIndirectionPolicy(org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy) NoIndirectionPolicy(org.eclipse.persistence.internal.indirection.NoIndirectionPolicy) TransparentIndirectionPolicy(org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy) InMemoryQueryIndirectionPolicy(org.eclipse.persistence.queries.InMemoryQueryIndirectionPolicy) IndirectionPolicy(org.eclipse.persistence.internal.indirection.IndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) BasicIndirectionPolicy(org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) FieldTransformation(org.eclipse.persistence.internal.descriptors.FieldTransformation) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)

Example 2 with FieldTransformation

use of org.eclipse.persistence.internal.descriptors.FieldTransformation 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 3 with FieldTransformation

use of org.eclipse.persistence.internal.descriptors.FieldTransformation in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method convertClassNamesToClasses.

/**
 * INTERNAL:
 * Convert all the class-name-based settings in this mapping to actual class-based
 * settings
 */
@Override
public void convertClassNamesToClasses(ClassLoader classLoader) {
    super.convertClassNamesToClasses(classLoader);
    if (attributeTransformerClassName != null) {
        Class<?> attributeTransformerClass = null;
        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                try {
                    attributeTransformerClass = AccessController.doPrivileged(new PrivilegedClassForName<>(attributeTransformerClassName, true, classLoader));
                } catch (PrivilegedActionException exception) {
                    throw ValidationException.classNotFoundWhileConvertingClassNames(attributeTransformerClassName, exception.getException());
                }
            } else {
                attributeTransformerClass = PrivilegedAccessHelper.getClassForName(attributeTransformerClassName, true, classLoader);
            }
        } catch (ClassNotFoundException exc) {
            throw ValidationException.classNotFoundWhileConvertingClassNames(attributeTransformerClassName, exc);
        }
        this.setAttributeTransformerClass(attributeTransformerClass);
    }
    for (FieldTransformation transformation : getFieldTransformations()) {
        if (transformation instanceof TransformerBasedFieldTransformation) {
            TransformerBasedFieldTransformation transformer = (TransformerBasedFieldTransformation) transformation;
            String transformerClassName = transformer.getTransformerClassName();
            if (transformerClassName == null) {
                return;
            }
            Class<?> transformerClass = null;
            try {
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                    try {
                        transformerClass = AccessController.doPrivileged(new PrivilegedClassForName<>(transformerClassName, true, classLoader));
                    } catch (PrivilegedActionException exception) {
                        throw ValidationException.classNotFoundWhileConvertingClassNames(transformerClassName, exception.getException());
                    }
                } else {
                    transformerClass = PrivilegedAccessHelper.getClassForName(transformerClassName, true, classLoader);
                }
            } catch (ClassNotFoundException exc) {
                throw ValidationException.classNotFoundWhileConvertingClassNames(transformerClassName, exc);
            }
            transformer.setTransformerClass(transformerClass);
        }
    }
}
Also used : TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) FieldTransformation(org.eclipse.persistence.internal.descriptors.FieldTransformation) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)

Example 4 with FieldTransformation

use of org.eclipse.persistence.internal.descriptors.FieldTransformation in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method getFieldNameToMethodNameAssociations.

/**
 * INTERNAL:
 * Needed for backwards compatibility
 */
public Vector getFieldNameToMethodNameAssociations() {
    Vector associations = new Vector();
    for (Iterator<FieldTransformation> source = getFieldTransformations().iterator(); source.hasNext(); ) {
        FieldTransformation tf = source.next();
        if (tf instanceof MethodBasedFieldTransformation) {
            Association ass = new Association();
            ass.setKey(tf.getField().getQualifiedName());
            ass.setValue(((MethodBasedFieldTransformation) tf).getMethodName());
            associations.addElement(ass);
        }
    }
    return associations;
}
Also used : Association(org.eclipse.persistence.mappings.Association) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation) Vector(java.util.Vector) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) FieldTransformation(org.eclipse.persistence.internal.descriptors.FieldTransformation) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)

Example 5 with FieldTransformation

use of org.eclipse.persistence.internal.descriptors.FieldTransformation in project eclipselink by eclipse-ee4j.

the class AbstractTransformationMapping method getFieldNameToMethodNames.

/**
 * INTERNAL:
 * Required for reverse compatibility and test cases:
 * @return a hash table containing the fieldName and their respective method names
 */
public Hashtable getFieldNameToMethodNames() {
    Hashtable table = new Hashtable(getFieldTransformations().size());
    Iterator<FieldTransformation> transformations = getFieldTransformations().iterator();
    while (transformations.hasNext()) {
        FieldTransformation transformation = transformations.next();
        if (transformation instanceof MethodBasedFieldTransformation) {
            table.put(transformation.getField().getQualifiedName(), ((MethodBasedFieldTransformation) transformation).getMethodName());
        }
    }
    return table;
}
Also used : MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation) Hashtable(java.util.Hashtable) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) FieldTransformation(org.eclipse.persistence.internal.descriptors.FieldTransformation) MethodBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)

Aggregations

FieldTransformation (org.eclipse.persistence.internal.descriptors.FieldTransformation)6 MethodBasedFieldTransformation (org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)6 TransformerBasedFieldTransformation (org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation)6 PrivilegedActionException (java.security.PrivilegedActionException)2 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Vector (java.util.Vector)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 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 BasicIndirectionPolicy (org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy)1 ContainerIndirectionPolicy (org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy)1 IndirectionPolicy (org.eclipse.persistence.internal.indirection.IndirectionPolicy)1 NoIndirectionPolicy (org.eclipse.persistence.internal.indirection.NoIndirectionPolicy)1