{
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 );
}
}
// NamedAssociations
for( AssociationDescriptor namedAssocDesc : entityType.state().namedAssociations() )
{
if( namedAssocDesc.queryable() )
{
String key = namedAssocDesc.qualifiedName().name();
JSONArray array = new JSONArray();
NamedAssociationState associateds = state.namedAssociationValueOf( namedAssocDesc.qualifiedName() );
for( String name : associateds )
{
if( namedAssocDesc.isAggregated() || support.indexNonAggregatedAssociations() )
{
String identity = associateds.get( name ).identity();
if( newStates.containsKey( identity ) )
{
JSONObject obj = new JSONObject( toJSON( newStates.get( identity ), newStates, uow ) );
obj.put( "_named", name );
array.put( obj );
}
else
{
EntityState assocState = uow.entityStateOf( EntityReference.parseEntityReference( identity ) );
JSONObject obj = new JSONObject( toJSON( assocState, newStates, uow ) );
obj.put( "_named", name );
array.put( obj );
}
}
else
{
JSONObject obj = new JSONObject();
obj.put( "_named", name );
obj.put( "identity", associateds.get( name ).identity() );
array.put( obj );
}
}
json.put( key, array );
}