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);
}
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations