@Test public void shuldCreateRelation() { context.setDocumentKey("key"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(parent.getId(), rel.getStartNode().getId()); Assert.assertEquals(child.getId(), rel.getEndNode().getId()); Assert.assertEquals("key", rel.getType().name()); }
@Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String documentKey = context.getDocumentKey(); if(StringUtils.isBlank(documentKey)) { return null; } RelationshipType relationType = RelationshipType.withName( documentKey ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,relationType); List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); Relationship relationship; if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, relationType); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } return relationship; }
DocumentRelationBuilderByKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String documentKey = context.getDocumentKey(); if(StringUtils.isBlank(documentKey)) { return null; } RelationshipType relationType = RelationshipType.withName( documentKey ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,relationType); List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); Relationship relationship; if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, relationType); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } return relationship; } }
DocumentRelationBuilderByKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String documentKey = context.getDocumentKey(); if(StringUtils.isBlank(documentKey)) { return null; } RelationshipType relationType = RelationshipType.withName( documentKey ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,relationType); List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); Relationship relationship; if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, relationType); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } return relationship; }  }
DocumentRelationBuilderByKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String documentKey = context.getDocumentKey(); if(StringUtils.isBlank(documentKey)) { return null; } RelationshipType relationType = RelationshipType.withName( documentKey ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,relationType); List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); Relationship relationship; if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, relationType); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } return relationship; }  void setDb(GraphDatabaseService db); void setLog(Log log); @Override Relationship buildRelation(Node parent, Node child, DocumentRelationContext context); @Override Set<Node> deleteRelations(DocumentRelationContext context); }
DocumentRelationBuilderByKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String documentKey = context.getDocumentKey(); if(StringUtils.isBlank(documentKey)) { return null; } RelationshipType relationType = RelationshipType.withName( documentKey ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,relationType); List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); Relationship relationship; if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, relationType); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } return relationship; }  void setDb(GraphDatabaseService db); void setLog(Log log); @Override Relationship buildRelation(Node parent, Node child, DocumentRelationContext context); @Override Set<Node> deleteRelations(DocumentRelationContext context);  }