} else {
/* An existing object to be updated */
if (target.getFromRelationsFrom(element.getId()) == null) {
// Element is not in target yet, read it from the store and add to target
Relation original = relationDao.retrieve(element.getId());
Relation updated = relationDtoTranslator.fromDto(element,
original, translated);
target.addToRelationsFrom(updated);
} else {
// Element is in target already, use this object. No need to add to the collection
relationDtoTranslator.fromDto(element, target
.getFromRelationsFrom(element.getId()), translated);
}
}
}
/*
* Synchronize RelationsTo association.
*/
Set<Relation> relationsToToBeRemoved = new HashSet<Relation>();
/* Avoid changing the collection underneath an active iterator. */
for (Relation element : target.getRelationsTo()) {
if (source.getFromRelationsTo(element.getId()) == null) {
relationsToToBeRemoved.add(element);
}
}
/* Objects to be removed */
for (Relation element : relationsToToBeRemoved) {
target.removeFromRelationsTo(element);
}
for (RelationDto element : source.getRelationsTo()) {
if (element.getId() == null) {
/* A new object to be added */
target.addToRelationsTo(relationDtoTranslator.fromDto(element,
translated));
} else {
/* An existing object to be updated */
if (target.getFromRelationsTo(element.getId()) == null) {
// Element is not in target yet, read it from the store and add to target
Relation original = relationDao.retrieve(element.getId());
Relation updated = relationDtoTranslator.fromDto(element,
original, translated);
target.addToRelationsTo(updated);
} else {
// Element is in target already, use this object. No need to add to the collection
relationDtoTranslator.fromDto(element, target