public void deleteNode( long nodeId ) throws NodeNotFoundException, ConstraintViolationException { Node node = node( nodeId ); if ( node.hasRelationship() ) { throw new ConstraintViolationException( String.format( "The node with id %d cannot be deleted. Check that the node is orphaned before deletion.", nodeId ) ); } node.delete(); }
@Test public void shouldImportOnlyNodes() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath() ); // no relationships // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { int nodeCount = 0; for ( Node node : db.getAllNodes() ) { assertTrue( node.hasProperty( "name" ) ); nodeCount++; assertFalse( node.hasRelationship() ); } assertEquals( NODE_COUNT, nodeCount ); tx.success(); } }
@Test @Graph( "Joe knows Sara" ) @Documented( "Upon successful creation of a relationship, the new relationship is returned." ) @Title( "Create a relationship with properties" ) public void create_a_relationship_with_properties() { String jsonString = "{\"to\" : \"" + getDataUri() + "node/" + getNode( "Sara" ).getId() + "\", \"type\" : \"LOVES\", \"data\" : {\"foo\" : \"bar\"}}"; Node i = getNode( "Joe" ); gen.get().expectedStatus( Status.CREATED.getStatusCode() ).payload( jsonString ).post( getNodeUri( i ) + "/relationships" ); try ( Transaction tx = graphdb().beginTx() ) { assertTrue( i.hasRelationship( RelationshipType.withName( "LOVES" ) ) ); } }
@PluginTarget( Node.class ) public Path pathToReference( @Source Node me ) { PathFinder<Path> finder = GraphAlgoFactory.shortestPath( PathExpanders.allTypesAndDirections(), 6 ); try ( Transaction tx = me.getGraphDatabase().beginTx() ) { Node other; if ( me.hasRelationship( RelationshipType.withName( "friend" ) ) ) { ResourceIterable<Relationship> relationships = (ResourceIterable<Relationship>) me.getRelationships( RelationshipType.withName( "friend" ) ); try ( ResourceIterator<Relationship> resourceIterator = relationships.iterator() ) { other = resourceIterator.next().getOtherNode( me ); } } else { other = me.getGraphDatabase().createNode(); } Path path = finder.findSinglePath( other, me ); tx.success(); return path; } }
@Test @Documented( "Upon successful creation of a relationship, the new relationship is returned." ) @Title( "Create relationship" ) @Graph( "Joe knows Sara" ) public void create_relationship() throws Exception { String jsonString = "{\"to\" : \"" + getDataUri() + "node/" + getNode( "Sara" ).getId() + "\", \"type\" : \"LOVES\"}"; Node i = getNode( "Joe" ); String entity = gen.get().expectedStatus( Status.CREATED.getStatusCode() ).payload( jsonString ) .post( getNodeUri( i ) + "/relationships" ).entity(); try ( Transaction tx = graphdb().beginTx() ) { assertTrue( i.hasRelationship( RelationshipType.withName( "LOVES" ) ) ); } assertProperRelationshipRepresentation( JsonHelper.jsonToMap( entity ) ); }
@UserFunction("apoc.node.relationship.types") @Description("apoc.node.relationship.types(node, rel-direction-pattern) - returns a list of distinct relationship types") public List<String> relationshipTypes(@Name("node") Node node, @Name(value = "types",defaultValue = "") String types) { if (node==null) return null; List<String> relTypes = Iterables.asList(Iterables.map(RelationshipType::name, node.getRelationshipTypes())); if (types == null || types.isEmpty()) return relTypes; List<String> result = new ArrayList<>(relTypes.size()); for (Pair<RelationshipType, Direction> p : parse(types)) { String name = p.first().name(); if (relTypes.contains(name) && node.hasRelationship(p.first(),p.other())) { result.add(name); } } return result; }
@Override public boolean hasRelationship( Direction direction, RelationshipType... types ) { return actual.hasRelationship( direction, types ); }
@Override public boolean hasRelationship( RelationshipType... types ) { return actual.hasRelationship( types ); }
public boolean hasPrevious() { if (currentElement == underlyingNode) { return false; } else { return currentElement.hasRelationship(RelTypes.NEXT_ELEMENT, Direction.INCOMING); } }
@UserFunction("apoc.node.relationship.exists") @Description("apoc.node.relationship.exists(node, rel-direction-pattern) - returns true when the node has the relationships of the pattern") public boolean hasRelationship(@Name("node") Node node, @Name(value = "types", defaultValue = "") String types) { if (types == null || types.isEmpty()) return node.hasRelationship(); long id = node.getId(); try ( NodeCursor nodeCursor = ktx.cursors().allocateNodeCursor()) {
/** * {@inheritDoc} */ @Override public boolean hasRelationship(Direction direction, RelationshipType... types) { return getPropertyContainer().hasRelationship(direction, types); }
/** * {@inheritDoc} */ @Override public boolean hasRelationship(Direction dir) { return getPropertyContainer().hasRelationship(dir); }
@Override public Evaluation evaluate(Path path) { if (!path.endNode().hasRelationship(Direction.OUTGOING) || path.endNode().hasRelationship(CommitRelationshipTypes.TOROOT)) { return Evaluation.INCLUDE_AND_PRUNE; } return Evaluation.EXCLUDE_AND_CONTINUE; }
public boolean isReturnableNode( TraversalPosition position ) { Node currentNode = position.currentNode(); return currentNode != null && !currentNode.hasRelationship( RelTypes.TIMELINE_INSTANCE, Direction.INCOMING ); } }, RelTypes.TIMELINE_INSTANCE, Direction.INCOMING );
public boolean isReturnableNode( TraversalPosition position ) { Node currentNode = position.currentNode(); return currentNode != null && !currentNode.hasRelationship( RelTypes.TIMELINE_INSTANCE, Direction.INCOMING ); } }, RelTypes.TIMELINE_INSTANCE, Direction.INCOMING );
@Override public boolean hasNext() { if (currentElement.getId() == underlyingNode.getId()) { return currentElement.hasRelationship(RelTypes.LAST_ELEMENT, Direction.OUTGOING); } else { return currentElement.hasRelationship(RelTypes.NEXT_ELEMENT, Direction.INCOMING); } }
private void initIndexRoot() { Node layerNode = getRootNode(); if (!layerNode.hasRelationship(RTreeRelationshipTypes.RTREE_ROOT, Direction.OUTGOING)) { // index initialization Node root = database.createNode(); layerNode.createRelationshipTo(root, RTreeRelationshipTypes.RTREE_ROOT); } }
private static Node getNodeByName(DatabaseService db, String name) { Node typeSubRef = getOrCreateTypeSubRef(db); RelationshipType relType = DynamicRelationshipType.withName(name); if (typeSubRef.hasRelationship(relType, Direction.OUTGOING)) { return typeSubRef .getSingleRelationship(relType, Direction.OUTGOING) .getEndNode(); } else { return null; } }
private static Node getOrCreateTypeSubRef(DatabaseService db) { Node refNode = db.getReferenceNode(); RelationshipType relType = RelTypes.ORG_NEO4J_COLLECTIONS_GRAPHDB_TYPE_SUBREF; if (refNode.hasRelationship(relType, Direction.OUTGOING)) { return refNode.getSingleRelationship(relType, Direction.OUTGOING) .getEndNode(); } else { Node n = db.createNode(); refNode.createRelationshipTo(n, relType); return n; } }
private static Node getOrCreateTypeSubRef(DatabaseService db) { Node refNode = ReferenceNodes.getReferenceNode(db); RelationshipType relType = RelTypes.ORG_NEO4J_COLLECTIONS_GRAPHDB_TYPE_SUBREF; if (refNode.hasRelationship(relType, Direction.OUTGOING)) { return refNode.getSingleRelationship(relType, Direction.OUTGOING) .getEndNode(); } else { Node n = db.createNode(); refNode.createRelationshipTo(n, relType); return n; } }