Zest™
Introduction
Tutorials
Javadoc
Samples
Core
Libraries
Extensions
Tools
Glossary 

Conversion - DEPRECATED

code

docs

tests

The Conversion Library provides support for converting composite types.

Table 28. Artifact

Group IDArtifact IDVersion

org.qi4j.library

org.qi4j.library.conversion

2.1


DEPRECATED

This functionality is now present in UnitOfWork as the two methods toEntity() and toValue(). Since this library was written assocations of all kinds are now fully supported in Values.

Entities to Values

To convert Entities to Values, use the EntityToValueService. It is easily assembled:

new EntityToValueAssembler().assemble( module );

Let’s say we have an interface defining state:

public interface PersonState
{

    Property<String> firstName();

    Property<String> lastName();

    Property<Date> dateOfBirth();

}

An EntityComposite using the state as a Private Mixin:

@Mixins( PersonMixin.class )
public interface PersonEntity
    extends EntityComposite
{

    String firstName();

    String lastName();

    Integer age();

    @Optional
    Association<PersonEntity> spouse();

    ManyAssociation<PersonEntity> children();

}
  [...snip...]

public static abstract class PersonMixin
    implements PersonEntity
{

    @This
    private PersonState state;
      [...snip...]

}

And a ValueComposite extending this very same state;

public interface PersonValue
    extends PersonState, ValueComposite
{

    @Optional
    Property<String> spouse();

    @Optional
    Property<List<String>> children();

}

Here is how to convert an EntityComposite to a ValueComposite:

EntityToValueService conversion = module.findService( EntityToValueService.class ).get();
PersonValue value = conversion.convert( PersonValue.class, entity );

Values to Entities

Using the ValueToEntity service one can create new Entities or update existing ones from Values. It is easy assembled:

new ValueToEntityAssembler().assemble( module );

Let’s say we have the exact same model as described above.

Here is how to create an EntityComposite from a ValueComposite:

ValueToEntity conversion = module.findService( ValueToEntity.class ).get();
PersonEntity edEntity = conversion.create( PersonEntity.class, edValue );

Here is how to update an EntityComposite from a ValueComposite:

ValueToEntity conversion = module.findService( ValueToEntity.class ).get();
conversion.update( rickyEntity, rickyNewStateValue );

Associations are converted to Identity strings

If your Entities and Values cannot use the same state type, you can annotate the Value that is the target of the conversion with the @Unqualified annotation. Then, the lookup of the Value Property will be performed using the unqualified name only, and not via the default of the full qualified name. In other words, this means that the Property may be declared in the different interfaces and still be matched.

Here is an example:

@Unqualified
public interface PersonValue2
    extends ValueComposite
{

    Property<String> firstName();

    Property<String> lastName();

    Property<Date> dateOfBirth();

    @Optional
    Property<String> spouse();

    @Optional
    Property<List<String>> children();

}