}
@Override
public void set(final SecurityContext securityContext, final NodeInterface sourceNode, final Iterable<T> collection) throws FrameworkException {
final App app = StructrApp.getInstance(securityContext);
final Set<T> toBeDeleted = new LinkedHashSet<>(Iterables.toList(get(securityContext, sourceNode, null)));
final Set<T> toBeCreated = new LinkedHashSet<>();
if (collection != null) {
Iterables.addAll(toBeCreated, collection);
}
// create intersection of both sets
final Set<T> intersection = new LinkedHashSet<>(toBeCreated);
intersection.retainAll(toBeDeleted);
// intersection needs no change
toBeCreated.removeAll(intersection);
toBeDeleted.removeAll(intersection);
// remove existing relationships
for (T targetNode : toBeDeleted) {
for (AbstractRelationship rel : sourceNode.getOutgoingRelationships()) {
final String relTypeName = rel.getRelType().name();
final String desiredRelType = relation.name();
if (relTypeName.equals(desiredRelType) && rel.getTargetNode().equals(targetNode)) {
app.delete(rel);
}
}
}
// test: obtain properties from notion
// create new relationships
for (T targetNode : toBeCreated) {
relation.ensureCardinality(securityContext, sourceNode, targetNode);
app.create(sourceNode, targetNode, relation.getClass(), getNotionProperties(securityContext, relation.getClass(), targetNode.getUuid()));
}
}