{
throw new NoSuchValueException( valueType.getName(), module.name() );
}
Unqualified unqualified = valueDescriptor.metaInfo( Unqualified.class );
final EntityComposite composite = (EntityComposite) entity;
final EntityDescriptor entityDescriptor = spi.entityDescriptorFor( composite );
final AssociationStateHolder associationState = spi.stateOf( composite );
ValueBuilder<?> builder;
if( unqualified == null || !unqualified.value() )
{
// Copy state using qualified names
builder = vbf.newValueBuilderWithState( valueType, new Function<PropertyDescriptor, Object>()
{
@Override
public Object map( PropertyDescriptor descriptor )
{
try
{
return associationState.propertyFor( descriptor.accessor() ).get();
}
catch( IllegalArgumentException e )
{
if( descriptor.valueType().mainType().equals( String.class ) )
{
// Find Association and convert to string
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityDescriptor.state()
.getAssociationByName( descriptor.qualifiedName().name() );
}
catch( IllegalArgumentException e1 )
{
return null;
}
Object entity = associationState.associationFor( associationDescriptor.accessor() ).get();
if( entity != null )
{
return ( (Identity) entity ).identity().get();
}
return null;
}
else if( descriptor.valueType() instanceof CollectionType
&& ( (CollectionType) descriptor.valueType() ).collectedType().mainType().equals( String.class ) )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityDescriptor.state()
.getManyAssociationByName( descriptor.qualifiedName().name() );
}
catch( IllegalArgumentException e1 )
{
return Collections.emptyList();
}
ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() );
List<String> entities = new ArrayList<>( state.count() );
for( Object entity : state )
{
entities.add( ( (Identity) entity ).identity().get() );
}
return entities;
}
// No NamedAssociation support
return null;
}
}
}, new Function<AssociationDescriptor, EntityReference>()
{
@Override
public EntityReference map( AssociationDescriptor associationDescriptor )
{
return EntityReference.entityReferenceFor(
associationState.associationFor( associationDescriptor.accessor() ).get() );
}
}, new Function<AssociationDescriptor, Iterable<EntityReference>>()
{
@Override
public Iterable<EntityReference> map( AssociationDescriptor associationDescriptor )
{
ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() );
List<EntityReference> refs = new ArrayList<>( state.count() );
for( Object entity : state )
{
refs.add( EntityReference.entityReferenceFor( entity ) );
}
return refs;
}
}, new Function<AssociationDescriptor, Map<String, EntityReference>>()
{
@Override
public Map<String, EntityReference> map( AssociationDescriptor from )
{
// No NamedAssociation support
return Collections.emptyMap();
}
} );
}
else
{
builder = vbf.newValueBuilderWithState( valueType, new Function<PropertyDescriptor, Object>()
{
@Override
public Object map( PropertyDescriptor descriptor )
{
try
{
PropertyDescriptor propertyDescriptor = entityDescriptor.state()
.findPropertyModelByName( descriptor.qualifiedName().name() );
return associationState.propertyFor( propertyDescriptor.accessor() ).get();
}
catch( IllegalArgumentException e )
{
if( descriptor.valueType().mainType().equals( String.class ) )
{
// Find Association and convert to string
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityDescriptor.state()
.getAssociationByName( descriptor.qualifiedName().name() );
}
catch( IllegalArgumentException e1 )
{
return null;
}
Object entity = associationState.associationFor( associationDescriptor.accessor() ).get();
if( entity != null )
{
return ( (Identity) entity ).identity().get();
}
return null;
}
else if( descriptor.valueType() instanceof CollectionType
&& ( (CollectionType) descriptor.valueType() ).collectedType().mainType().equals( String.class ) )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityDescriptor.state()
.getManyAssociationByName( descriptor.qualifiedName().name() );
}
catch( IllegalArgumentException e1 )
{
return null;
}
ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() );
List<String> entities = new ArrayList<>( state.count() );
for( Object entity : state )
{
entities.add( ( (Identity) entity ).identity().get() );
}
return entities;
}
// No NamedAssociation support
// DTO
Class<?> type = descriptor.valueType().mainType();
if( DTO.class.isAssignableFrom( type ) )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityDescriptor.state()
.getAssociationByName( descriptor.qualifiedName().name() );
}
catch( IllegalArgumentException e1 )
{
return null;
}
Object entity = associationState.associationFor( associationDescriptor.accessor() ).get();
if( entity != null )
{
return convert( type, entity );
}
}
return null;
}
}
}, new Function<AssociationDescriptor, EntityReference>()
{
@Override
public EntityReference map( AssociationDescriptor descriptor )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityDescriptor.state()
.getAssociationByName( descriptor.qualifiedName().name() );
}
catch( IllegalArgumentException e )
{
return null;
}
return EntityReference.entityReferenceFor( associationState
.associationFor( associationDescriptor.accessor() ).get() );
}
}, new Function<AssociationDescriptor, Iterable<EntityReference>>()
{
@Override
public Iterable<EntityReference> map( AssociationDescriptor descriptor )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityDescriptor.state()
.getManyAssociationByName( descriptor.qualifiedName().name() );
}
catch( IllegalArgumentException e )
{
return Iterables.empty();