}
Unqualified unqualified = valueDescriptor.metaInfo( Unqualified.class );
// Iterable<? extends PropertyDescriptor> properties = valueDescriptor.state().properties();
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 = module.newValueBuilderWithState(
valueType,
new Function<PropertyDescriptor, Object>()
{
@Override
public Object map( PropertyDescriptor descriptor )
{
try
{
return associationState.propertyFor( descriptor.accessor() ).get();
}
catch( IllegalArgumentException e )
{
AssociationStateDescriptor entityState = entityDescriptor.state();
String associationName = descriptor.qualifiedName().name();
if( descriptor.valueType().mainType().equals( String.class ) )
{
// Find Association and convert to string
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityState.getAssociationByName( associationName );
}
catch( IllegalArgumentException e1 )
{
return null;
}
AccessibleObject associationMethod = associationDescriptor.accessor();
Object entity = associationState.associationFor( associationMethod ).get();
if( entity != null )
{
return ( (Identity) entity ).identity().get();
}
else
{
return null;
}
}
else if( descriptor.valueType() instanceof CollectionType
&& ( (CollectionType) descriptor.valueType() ).collectedType()
.mainType()
.equals( String.class ) )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityState.getManyAssociationByName( associationName );
}
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;
}
else if( descriptor.valueType() instanceof MapType
&& ( (MapType) descriptor.valueType() ).keyType().mainType().equals( String.class )
&& ( (MapType) descriptor.valueType() ).valueType().mainType().equals( String.class ) )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityState.getNamedAssociationByName( associationName );
}
catch( IllegalArgumentException e1 )
{
return Collections.emptyMap();
}
NamedAssociation<?> state = associationState.namedAssociationFor( associationDescriptor.accessor() );
Map<String, String> entities = new LinkedHashMap<>( state.count() );
for( String name : state )
{
entities.put( name, ( (Identity) state.get( name ) ).identity().get() );
}
return entities;
}
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 associationDescriptor )
{
NamedAssociation<?> assoc = associationState.namedAssociationFor( associationDescriptor.accessor() );
Map<String, EntityReference> refs = new LinkedHashMap<>( assoc.count() );
for( String name : assoc )
{
refs.put( name, EntityReference.entityReferenceFor( assoc.get( name ) ) );
}
return refs;
}
} );
}
else
{
builder = module.newValueBuilderWithState(
valueType,
new Function<PropertyDescriptor, Object>()
{
@Override
public Object map( final PropertyDescriptor descriptor )
{
AssociationStateDescriptor entityState = entityDescriptor.state();
String propertyName = descriptor.qualifiedName().name();
try
{
PropertyDescriptor propertyDescriptor = entityState.findPropertyModelByName( propertyName );
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 = entityState.getAssociationByName( propertyName );
}
catch( IllegalArgumentException e1 )
{
return null;
}
AccessibleObject associationMethod = associationDescriptor.accessor();
Object entity = associationState.associationFor( associationMethod ).get();
if( entity != null )
{
return ( (Identity) entity ).identity().get();
}
else
{
return null;
}
}
else if( descriptor.valueType() instanceof CollectionType
&& ( (CollectionType) descriptor.valueType() ).collectedType().mainType().equals( String.class ) )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityState.getManyAssociationByName( propertyName );
}
catch( IllegalArgumentException e1 )
{
return null;
}
AccessibleObject associationMethod = associationDescriptor.accessor();
ManyAssociation<?> state = associationState.manyAssociationFor( associationMethod );
List<String> entities = new ArrayList<>( state.count() );
for( Object entity : state )
{
entities.add( ( (Identity) entity ).identity().get() );
}
return entities;
}
else if( descriptor.valueType() instanceof MapType
&& ( (MapType) descriptor.valueType() ).keyType().mainType().equals( String.class )
&& ( (MapType) descriptor.valueType() ).valueType().mainType().equals( String.class ) )
{
AssociationDescriptor associationDescriptor;
try
{
associationDescriptor = entityState.getNamedAssociationByName( propertyName );
}
catch( IllegalArgumentException e1 )
{
return null;
}
AccessibleObject associationMethod = associationDescriptor.accessor();
NamedAssociation<?> state = associationState.namedAssociationFor( associationMethod );
Map<String, String> entities = new LinkedHashMap<>( state.count() );
for( String name : state )
{
entities.put( name, ( (Identity) state.get( name ) ).identity().get() );
}
return entities;
}
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;
}
AccessibleObject associationMethod = associationDescriptor.accessor();
Association<Object> association = associationState.associationFor( associationMethod );
return EntityReference.entityReferenceFor( association.get() );
}
},
new Function<AssociationDescriptor, Iterable<EntityReference>>()
{
@Override
public Iterable<EntityReference> map( final AssociationDescriptor descriptor )
{
AssociationDescriptor associationDescriptor;
try
{
String associationName = descriptor.qualifiedName().name();
AssociationStateDescriptor entityState = entityDescriptor.state();
associationDescriptor = entityState.getManyAssociationByName( associationName );
}
catch( IllegalArgumentException e )
{
return Iterables.empty();
}
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 descriptor )
{
AssociationDescriptor associationDescriptor;
try
{
String associationName = descriptor.qualifiedName().name();
AssociationStateDescriptor entityState = entityDescriptor.state();
associationDescriptor = entityState.getNamedAssociationByName( associationName );
}
catch( IllegalArgumentException e )
{
return Collections.emptyMap();
}
AccessibleObject associationMethod = associationDescriptor.accessor();
NamedAssociation<Object> assoc = associationState.namedAssociationFor( associationMethod );
Map<String, EntityReference> refs = new LinkedHashMap<>( assoc.count() );
for( String name : assoc )
{
refs.put( name, EntityReference.entityReferenceFor( assoc.get( name ) ) );
}