@Override public Iterator columnAs(final String name) { final Iterator<Value> it = result.list(Records.column(name)).iterator(); return Iterables.map( new Function<Value, Object>() { @Override public Object apply(final Value t) { return wrapper.apply(t.asObject()); } }, it); }
@Override public Iterable<S> get( final SecurityContext securityContext, final NodeInterface node, final Predicate<GraphObject> predicate) { final NodeFactory<S> nodeFactory = new NodeFactory<>(securityContext); final Iterable<Relationship> rels = getRawSource(securityContext, node.getNode(), predicate); if (rels != null) { return Iterables.map( new Function<Relationship, S>() { @Override public S apply(Relationship from) throws RuntimeException { return nodeFactory.instantiate(from.getStartNode(), from); } }, sort(rels)); } return null; }
@Override public Object set( final SecurityContext securityContext, final NodeInterface targetNode, final Iterable<S> collection) throws FrameworkException { final App app = StructrApp.getInstance(securityContext); final PropertyMap properties = new PropertyMap(); final NodeInterface actualTargetNode = (NodeInterface) unwrap(securityContext, relation.getClass(), targetNode, properties); final Set<S> toBeDeleted = new LinkedHashSet<>(Iterables.toList(get(securityContext, actualTargetNode, null))); final Set<S> toBeCreated = new LinkedHashSet<>(); if (collection != null) { Iterables.addAll(toBeCreated, collection); } // create intersection of both sets final Set<S> intersection = new HashSet<>(toBeCreated); intersection.retainAll(toBeDeleted); // intersection needs no change toBeCreated.removeAll(intersection); toBeDeleted.removeAll(intersection); // remove existing relationships for (S sourceNode : toBeDeleted) { for (AbstractRelationship rel : actualTargetNode.getIncomingRelationships()) { final String relTypeName = rel.getRelType().name(); final String desiredRelType = relation.name(); if (sourceNode.equals(actualTargetNode)) { logger.warn( "Preventing deletion of self relationship {}-[{}]->{}. If you experience issue with this, please report to [email protected].", new Object[] {sourceNode, rel.getRelType(), actualTargetNode}); // skip self relationships continue; } if (relTypeName.equals(desiredRelType) && rel.getSourceNode().equals(sourceNode)) { app.delete(rel); } } } final List<Relation> createdRelationship = new LinkedList<>(); // create new relationships for (S sourceNode : toBeCreated) { if (sourceNode != null && actualTargetNode != null) { properties.clear(); final S actualSourceNode = (S) unwrap(securityContext, relation.getClass(), sourceNode, properties); final PropertyMap notionProperties = getNotionProperties( securityContext, relation.getClass(), actualSourceNode.getName() + relation.name() + actualTargetNode.getName()); if (notionProperties != null) { properties.putAll(notionProperties); } relation.ensureCardinality(securityContext, actualSourceNode, actualTargetNode); createdRelationship.add( app.create(actualSourceNode, actualTargetNode, relation.getClass(), properties)); } } return createdRelationship; }