Search in sources :

Example 1 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class SaveEventDelegate method deregisterOutgoingRelationship.

private void deregisterOutgoingRelationship(Long id, String relationshipType, Class endNodeType) {
    Iterator<MappedRelationship> iterator = this.registeredRelationships.iterator();
    while (iterator.hasNext()) {
        MappedRelationship mappedRelationship = iterator.next();
        if (mappedRelationship.getStartNodeId() == id && mappedRelationship.getRelationshipType().equals(relationshipType) && endNodeType.equals(mappedRelationship.getEndNodeType())) {
            deletedRelationships.add(mappedRelationship);
            iterator.remove();
        }
    }
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship)

Example 2 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class SaveEventDelegate method mapInstance.

// creates a MappedRelationship between the parent object and the reference. In the case that the reference
// object is a RE, the relationship is created from the start node and the end node of the RE.
// a MappedRelationship therefore represents a directed edge between two nodes in the graph.
private void mapInstance(Set<MappedRelationship> mappedRelationships, ClassInfo parentInfo, long parentId, FieldInfo reader, Object reference) {
    String type = reader.relationshipType();
    Direction direction = reader.relationshipDirection();
    ClassInfo referenceInfo = this.session.metaData().classInfo(reference);
    if (referenceInfo == null) {
        return;
    }
    if (referenceInfo.isRelationshipEntity()) {
        // The relationship entity might just get created and therefore we must be careful not to
        // trigger the creation of it's id place holder, otherwise we can't check wether it's new or not.
        Optional<Long> optionalReferenceId = session.context().optionalNativeId(reference);
        // graph relationship is transitive across the RE domain object
        Object startNode = referenceInfo.getStartNodeReader().read(reference);
        ClassInfo startNodeInfo = this.session.metaData().classInfo(startNode);
        Long startNodeId = session.context().nativeId(startNode);
        Object endNode = referenceInfo.getEndNodeReader().read(reference);
        ClassInfo endNodeInfo = this.session.metaData().classInfo(endNode);
        Long endNodeId = session.context().nativeId(endNode);
        MappedRelationship edge = new MappedRelationship(startNodeId, type, endNodeId, optionalReferenceId.orElse(null), startNodeInfo.getUnderlyingClass(), endNodeInfo.getUnderlyingClass());
        mappedRelationships.add(edge);
    } else {
        // We assume the existence of the reference here
        Long referenceId = session.context().nativeId(reference);
        if (direction == Direction.OUTGOING) {
            MappedRelationship edge = new MappedRelationship(parentId, type, referenceId, null, parentInfo.getUnderlyingClass(), referenceInfo.getUnderlyingClass());
            mappedRelationships.add(edge);
        } else {
            MappedRelationship edge = new MappedRelationship(referenceId, type, parentId, null, referenceInfo.getUnderlyingClass(), parentInfo.getUnderlyingClass());
            mappedRelationships.add(edge);
        }
    }
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Direction(org.neo4j.ogm.annotation.Relationship.Direction) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 3 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class SaveEventDelegate method touched.

private Set<Object> touched() {
    Set<Object> touched = new HashSet<>();
    for (MappedRelationship added : addedRelationships) {
        Object src = session.context().getNodeEntity(added.getStartNodeId());
        Object tgt = session.context().getNodeEntity(added.getEndNodeId());
        if (src != null) {
            touched.add(src);
        }
        if (tgt != null) {
            touched.add(tgt);
        }
    }
    return touched;
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) HashSet(java.util.HashSet)

Example 4 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class SaveEventDelegate method unreachable.

private Set<Object> unreachable() {
    Set<Object> unreachable = new HashSet<>();
    for (MappedRelationship mappedRelationship : deletedRelationships) {
        logger.debug("unreachable start {} end {}", mappedRelationship.getStartNodeId(), mappedRelationship.getEndNodeId());
        addUnreachable(unreachable, mappedRelationship.getStartNodeId());
        addUnreachable(unreachable, mappedRelationship.getEndNodeId());
    }
    return unreachable;
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) HashSet(java.util.HashSet)

Example 5 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToRemoveAnyRegisteredRelationship.

@Test
public void shouldBeAbleToRemoveAnyRegisteredRelationship() {
    // given
    Folder folder = new Folder();
    Document doc1 = new Document();
    Document doc2 = new Document();
    folder.getDocuments().add(doc1);
    folder.getDocuments().add(doc2);
    doc1.setFolder(folder);
    doc2.setFolder(folder);
    folder.setId(0L);
    doc1.setId(1L);
    doc2.setId(2L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(doc1);
    mappingContext.addNodeEntity(doc2);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    // when
    doc2.setFolder(null);
    folder.getDocuments().remove(doc2);
    // then
    assertThat(folder.getDocuments()).hasSize(1);
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc2).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Aggregations

MappedRelationship (org.neo4j.ogm.context.MappedRelationship)18 Test (org.junit.Test)11 Statement (org.neo4j.ogm.request.Statement)9 Teacher (org.neo4j.ogm.domain.education.Teacher)4 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)3 School (org.neo4j.ogm.domain.education.School)3 Document (org.neo4j.ogm.domain.filesystem.Document)3 Folder (org.neo4j.ogm.domain.filesystem.Folder)3 HashSet (java.util.HashSet)2 Course (org.neo4j.ogm.domain.education.Course)2 Student (org.neo4j.ogm.domain.education.Student)2 Forum (org.neo4j.ogm.domain.forum.Forum)2 ForumTopicLink (org.neo4j.ogm.domain.forum.ForumTopicLink)2 Topic (org.neo4j.ogm.domain.forum.Topic)2 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 Direction (org.neo4j.ogm.annotation.Relationship.Direction)1 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)1