for( AssociationDescriptor assocDesc : entityType.state().associations() )
{
if( assocDesc.queryable() )
{
String key = assocDesc.qualifiedName().name();
EntityReference associated = state.associationValueOf( assocDesc.qualifiedName() );
Object value;
if( associated == null )
{
value = null;
}
else
{
if( assocDesc.isAggregated() || support.indexNonAggregatedAssociations() )
{
if( newStates.containsKey( associated.identity() ) )
{
value = new JSONObject( toJSON( newStates.get( associated.identity() ), newStates, uow ) );
}
else
{
EntityState assocState = uow.entityStateOf( EntityReference.parseEntityReference( associated.identity() ) );
value = new JSONObject( toJSON( assocState, newStates, uow ) );
}
}
else
{
value = new JSONObject( Collections.singletonMap( "identity", associated.identity() ) );
}
}
json.put( key, value );
}
}
// ManyAssociations
for( AssociationDescriptor manyAssocDesc : entityType.state().manyAssociations() )
{
if( manyAssocDesc.queryable() )
{
String key = manyAssocDesc.qualifiedName().name();
JSONArray array = new JSONArray();
ManyAssociationState associateds = state.manyAssociationValueOf( manyAssocDesc.qualifiedName() );
for( EntityReference associated : associateds )
{
if( manyAssocDesc.isAggregated() || support.indexNonAggregatedAssociations() )
{
if( newStates.containsKey( associated.identity() ) )
{
array.put( new JSONObject( toJSON( newStates.get( associated.identity() ), newStates, uow ) ) );
}
else
{
EntityState assocState = uow.entityStateOf( EntityReference.parseEntityReference( associated.identity() ) );
array.put( new JSONObject( toJSON( assocState, newStates, uow ) ) );
}
}
else
{
array.put( new JSONObject( Collections.singletonMap( "identity", associated.identity() ) ) );
}
}
json.put( key, array );
}
}