Skip to content

programmatic transformation

stemey edited this page Oct 25, 2012 · 2 revisions

The TypeTransformationBuilder makes it possible to create a Transformation between two EntityTypes. The feature really improves developer productivity if one of the types is a dynamic type. In that case the definition of the transformation also defines the dynamic type. Here is an example:

// the target type of the transformation is created in the given dynamic sub repository
EntityTypeBuilder<ObjetNode> targetTypeBuilder = this.jsonRepository.createEntityTypeBuilder("json:person");
TypeTransformationBuilder builder = TypeTransformationBuilder.create(Person.class,targetTypeBuilder);
// create the field "firstName " in the target type and map it to the same field in the source type
builder.transform().from("firstName");
// create field "street" in the target type and map it to a path
builder.transform().from("address.street".to("street");
// build the tranformation.
EntityTypeTransformation<Person,ObjectNode> transformation = builder.buildTransformation();
// use the transformation to create an instanceof "personview" for an instance of Person.class.
ObjectNode personView=transformation.getAB().create(person);

The EntityTypeTransformation has sub transformations and a supertransformation. It atains a set of AttributeTransformations.

mapping

Mapping is done by matching two attributes by name. The name of the target attribute will be used to create an attribute on the target type.

``builder.transform().from("address.street").to("street");``
  • single primitive attribute
    A single primitive attribute in the target type will be created with the same type unless the primitive type is not supported by the target repository. Then you need to specifiy a converter that converts into a supported type.

    builder.transform().from("address.street").to("street").converter(EnumToStringConverter.class);

  • single associative attribute
    During the transformation the data is merged to the target entity if it exists and is of the correct type by usig transformation.getAB().merge(a,b). Otherwise the target entity is created from the soure entity by using transformation.getAB().create(a). If the type is a primitive then the first form is used.

    builder.transform().from("address").converter(addressTransformation);

  • associative collection attribute

    builder.transformCollection().from("adresses").converter(addressTransformation).

  • map attribute

    builder.transformMap().from("addresses").converter(..).keyConverter(...)

  • custom mapping (the example embeds attributes of associated type in main type)

    builder.transformCustom(Embed.class).from("address").transform(addressTransformation)

  • generic mapping (mapping n attributes in source tyype to m attributes in target type)

    builder.transformCustom(GenericTransformationBuilder.class).from("street").from("name")..transform(new JavaTransformation(){...}).to().addSingleAttribute("propertyA",String.class).addSingleAttribute("propertyB",String.class)

to create a custom transformation implement the Interface CustomAttributeTransformationBuilder:

public interface CustomAttributeTransformationBuilder extends AttributeTrasformationBuilder{
	void setSourceType(EntityType entityType);
	void set...
	/**
	* add the attribute associated with this transformation to the target type.
	*/	
	build(EntityTypeBuilder targetBuilder);
	/**
	* create the AttributeTransformation that will convert the source attribute into the target attribute
	*/
	AttributeTransformation create(EntityType targetType);
}

###Configuration

the basic component is the transformationBuilderFactory. It has a component ConverterFactory, which provides default converters In the following example converter for date and enum to string are provided.

<bean id="atem-utility-transformationBuilderFactory"
	class="org.atemsource.atem.utility.transform.api.TransformationBuilderFactory">
	<property name="converterFactory">
		<bean
			class="org.atemsource.atem.utility.transform.impl.converter.ConverterFactoryImpl">
			<property name="javaConverters">
				<list>
					<ref bean="atem-dateConverter" />
				</list>
			</property>
			<property name="factories">
				<list>
					<bean
						class="org.atemsource.atem.utility.transform.impl.converter.EnumToStringConverterFactory" />
				</list>
			</property>
		</bean>
	</property>
</bean>

<bean id="atem-dateConverter"
	class="org.atemsource.atem.utility.transform.impl.converter.DateConverter">
	<property name="pattern" value="yyyy.MM.dd" />
</bean>
  • classpath:/meta/utility/transform.xml : classpath scanning
  • classpath:/meta/utility/transform-example.xml : defines component "meta-utility-transformationBuilderFactory"
  • classpath:/meta/utility/transform-complete.xml : defines component "meta-utility-transformationBuilderFactory" including standard converter for enum and date to string(useful for java to json and java to soydata transformations). All meta data is also included.

Clone this wiki locally