{
// Synchronize old and new association
int index = 0;
while( ( identity = bufferedReader.readLine() ) != null )
{
EntityReference reference = new EntityReference( identity );
if( manyAssociation.count() < index && manyAssociation.get( index ).equals( reference ) )
{
continue;
}
try
{
unitOfWork.entityStateOf( reference );
manyAssociation.remove( reference );
manyAssociation.add( index++, reference );
}
catch( EntityNotFoundException e )
{
// Ignore this entity - doesn't exist
}
}
// Remove "left-overs"
while( manyAssociation.count() > index )
{
manyAssociation.remove( manyAssociation.get( index ) );
}
}
catch( IOException e )
{
// Ignore
}
}
for( AssociationDescriptor associationType : descriptor.state().namedAssociations() )
{
String newStringAssociation = form.getFirstValue( associationType.qualifiedName().name() );
NamedAssociationState namedAssociation = entity.namedAssociationValueOf( associationType.qualifiedName() );
if( newStringAssociation == null )
{
// Remove "left-overs"
for( String name : namedAssociation )
{
namedAssociation.remove( name );
}
continue;
}
Set<String> names = new HashSet<>();
BufferedReader bufferedReader = new BufferedReader( new StringReader( newStringAssociation ) );
String line;
try
{
while( ( line = bufferedReader.readLine() ) != null )
{
String name = line;
line = bufferedReader.readLine();
if( line == null )
{
break;
}
String identity = line;
EntityReference reference = new EntityReference( identity );
try
{
unitOfWork.entityStateOf( reference );
namedAssociation.remove( name );