Search in sources :

Example 6 with TransformerBasedFieldTransformation

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

the class NonJDBCTestHelper method buildWorkbenchXMLProject.

public static ObjectPersistenceWorkbenchXMLProject buildWorkbenchXMLProject() {
    ObjectPersistenceWorkbenchXMLProject workbenchXMLProject = null;
    try {
        workbenchXMLProject = new ObjectPersistenceWorkbenchXMLProject();
        XMLTransformationMapping versionMapping = (XMLTransformationMapping) workbenchXMLProject.getDescriptor(Project.class).getMappings().firstElement();
        TransformerBasedFieldTransformation versionTransformer = (TransformerBasedFieldTransformation) versionMapping.getFieldTransformations().get(0);
        Field transformerField = TransformerBasedFieldTransformation.class.getDeclaredField("transformer");
        transformerField.setAccessible(true);
        ConstantTransformer constantTransformer = (ConstantTransformer) transformerField.get(versionTransformer);
        constantTransformer.setValue(CONSTANT_PROJECT_BUILD_VERSION);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return workbenchXMLProject;
}
Also used : Project(org.eclipse.persistence.sessions.Project) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) Field(java.lang.reflect.Field) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) ConstantTransformer(org.eclipse.persistence.mappings.transformers.ConstantTransformer)

Example 7 with TransformerBasedFieldTransformation

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

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

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

the class AbstractTransformationMapping method addFieldTransformer.

/**
 * PUBLIC:
 * Add the field and the transformer
 * that returns the value to be placed in the field
 * when the object is written to the database.
 */
public void addFieldTransformer(DatabaseField field, FieldTransformer transformer) {
    TransformerBasedFieldTransformation transformation = new TransformerBasedFieldTransformation(transformer);
    transformation.setField(field);
    getFieldTransformations().add(transformation);
}
Also used : TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation)

Example 10 with TransformerBasedFieldTransformation

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

the class PLSQLrecordTestHelper method buildWorkbenchXMLProject.

public static ObjectPersistenceWorkbenchXMLProject buildWorkbenchXMLProject() {
    ObjectPersistenceWorkbenchXMLProject workbenchXMLProject = null;
    try {
        workbenchXMLProject = new ObjectPersistenceWorkbenchXMLProject();
        XMLTransformationMapping versionMapping = (XMLTransformationMapping) workbenchXMLProject.getDescriptor(Project.class).getMappings().firstElement();
        TransformerBasedFieldTransformation versionTransformer = (TransformerBasedFieldTransformation) versionMapping.getFieldTransformations().get(0);
        Field transformerField = TransformerBasedFieldTransformation.class.getDeclaredField("transformer");
        transformerField.setAccessible(true);
        ConstantTransformer constantTransformer = (ConstantTransformer) transformerField.get(versionTransformer);
        constantTransformer.setValue(CONSTANT_PROJECT_BUILD_VERSION);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return workbenchXMLProject;
}
Also used : Project(org.eclipse.persistence.sessions.Project) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) Field(java.lang.reflect.Field) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) TransformerBasedFieldTransformation(org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) ConstantTransformer(org.eclipse.persistence.mappings.transformers.ConstantTransformer)

Aggregations

TransformerBasedFieldTransformation (org.eclipse.persistence.internal.descriptors.TransformerBasedFieldTransformation)11 Field (java.lang.reflect.Field)5 ObjectPersistenceWorkbenchXMLProject (org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject)5 ConstantTransformer (org.eclipse.persistence.mappings.transformers.ConstantTransformer)5 XMLTransformationMapping (org.eclipse.persistence.oxm.mappings.XMLTransformationMapping)5 Project (org.eclipse.persistence.sessions.Project)5 FieldTransformation (org.eclipse.persistence.internal.descriptors.FieldTransformation)4 MethodBasedFieldTransformation (org.eclipse.persistence.internal.descriptors.MethodBasedFieldTransformation)4 StringReader (java.io.StringReader)2 PrivilegedActionException (java.security.PrivilegedActionException)2 SQLException (java.sql.SQLException)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 XMLContext (org.eclipse.persistence.oxm.XMLContext)2 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)2 BeforeClass (org.junit.BeforeClass)2 Document (org.w3c.dom.Document)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Statement (java.sql.Statement)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1