@Test public void shuldUpdateRelation() { context.setDocumentKey("key1"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel1 = this.docrel.buildRelation(parent, child, context); context.setDocumentKey("key2"); Relationship rel2 = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(rel1.getId(), rel2.getId()); String[] keys = (String[]) rel2.getProperty("docKeys"); Assert.assertEquals(2, keys.length); Assert.assertEquals("key1", keys[0]); Assert.assertEquals("key2", keys[1]); }
@Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String relationName = buildRelationName(parent, child, context); RelationshipType type = RelationshipType.withName( relationName ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,type); Relationship relationship; List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, type); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } String[] keys = new String[0]; if(relationship.getAllProperties().containsKey(DOC_KEYS)) { keys = (String[]) relationship.getProperty(DOC_KEYS); } String documentKey = context.getDocumentKey(); if(! ArrayUtils.contains(keys, documentKey)) { keys = ArrayUtils.add(keys, documentKey); relationship.setProperty(DOC_KEYS, keys); } return relationship; }
DocumentRelationBuilderTypeArrayKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String relationName = buildRelationName(parent, child, context); RelationshipType type = RelationshipType.withName( relationName ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,type); Relationship relationship; List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, type); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } String[] keys = new String[0]; if(relationship.getAllProperties().containsKey(DOC_KEYS)) { keys = (String[]) relationship.getProperty(DOC_KEYS); } String documentKey = context.getDocumentKey(); if(! ArrayUtils.contains(keys, documentKey)) { keys = ArrayUtils.add(keys, documentKey); relationship.setProperty(DOC_KEYS, keys); } return relationship; } }
DocumentRelationBuilderTypeArrayKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String relationName = buildRelationName(parent, child, context); RelationshipType type = RelationshipType.withName( relationName ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,type); Relationship relationship; List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, type); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } String[] keys = new String[0]; if(relationship.getAllProperties().containsKey(DOC_KEYS)) { keys = (String[]) relationship.getProperty(DOC_KEYS); } String documentKey = context.getDocumentKey(); if(! ArrayUtils.contains(keys, documentKey)) { keys = ArrayUtils.add(keys, documentKey); relationship.setProperty(DOC_KEYS, keys); } return relationship; }  }
DocumentRelationBuilderTypeArrayKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String relationName = buildRelationName(parent, child, context); RelationshipType type = RelationshipType.withName( relationName ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,type); Relationship relationship; List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, type); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } String[] keys = new String[0]; if(relationship.getAllProperties().containsKey(DOC_KEYS)) { keys = (String[]) relationship.getProperty(DOC_KEYS); } String documentKey = context.getDocumentKey(); if(! ArrayUtils.contains(keys, documentKey)) { keys = ArrayUtils.add(keys, documentKey); relationship.setProperty(DOC_KEYS, keys); } 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); }
DocumentRelationBuilderTypeArrayKey implements DocumentRelationBuilder { @Override public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) { String relationName = buildRelationName(parent, child, context); RelationshipType type = RelationshipType.withName( relationName ); Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,type); Relationship relationship; List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false) .filter(rel -> rel.getStartNode().getId() == parent.getId()) .collect(Collectors.toList()); if(rels.isEmpty()) { relationship = parent.createRelationshipTo(child, type); if(log.isDebugEnabled()) log.debug("Create new Relation "+relationship); }else { relationship = rels.get(0); if(log.isDebugEnabled()) log.debug("Update Relation "+relationship); } String[] keys = new String[0]; if(relationship.getAllProperties().containsKey(DOC_KEYS)) { keys = (String[]) relationship.getProperty(DOC_KEYS); } String documentKey = context.getDocumentKey(); if(! ArrayUtils.contains(keys, documentKey)) { keys = ArrayUtils.add(keys, documentKey); relationship.setProperty(DOC_KEYS, keys); } 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);  }