Search in sources :

Example 11 with RowStatementFactory

use of org.neo4j.ogm.session.request.RowStatementFactory in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToRemoveContainedRelationshipOnly.

@Test
public void shouldBeAbleToRemoveContainedRelationshipOnly() {
    // given
    Folder folder = new Folder();
    Document doc1 = new Document();
    folder.getDocuments().add(doc1);
    folder.getArchived().add(doc1);
    doc1.setFolder(folder);
    folder.setId(0L);
    doc1.setId(1L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(doc1);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "ARCHIVED", doc1.getId(), null, Folder.class, Document.class));
    // when
    folder.getDocuments().remove(doc1);
    doc1.setFolder(null);
    // then
    assertThat(folder.getDocuments()).isEmpty();
    assertThat(folder.getArchived()).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");
    mapper = new EntityGraphMapper(mappingMetadata, mappingContext);
    // There are no more changes to the graph
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).isEmpty();
}
Also used : EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) 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)

Example 12 with RowStatementFactory

use of org.neo4j.ogm.session.request.RowStatementFactory in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldNotBeAbleToCreateDuplicateRelationship.

@Test
public void shouldNotBeAbleToCreateDuplicateRelationship() {
    Folder folder = new Folder();
    Document document = new Document();
    document.setFolder(folder);
    // we try to store two identical references to the document object. Although this
    // is supported by the graph, it isn't currently supported by the OGM,
    // therefore we expect only one relationship to be persisted
    folder.getDocuments().add(document);
    folder.getDocuments().add(document);
    assertThat(folder.getDocuments()).hasSize(2);
    // save folder
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.createNodesStatements();
    List<String> createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    List<String> createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(1);
    assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    List rows = (List) statements.get(0).getParameters().get("rows");
    assertThat(rows).hasSize(1);
    // save document
    compiler = mapper.map(document).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.createNodesStatements();
    createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(1);
    assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    rows = (List) statements.get(0).getParameters().get("rows");
    assertThat(rows).hasSize(1);
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) ArrayList(java.util.ArrayList) List(java.util.List) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 13 with RowStatementFactory

use of org.neo4j.ogm.session.request.RowStatementFactory in project neo4j-ogm by neo4j.

the class CompilerTest method createSimpleRelationshipsBetweenObjects.

@Test
public void createSimpleRelationshipsBetweenObjects() {
    School waller = new School("Waller");
    Teacher mary = new Teacher("Mary");
    mary.setSchool(waller);
    waller.getTeachers().add(mary);
    Compiler compiler = mapAndCompile(waller, -1);
    compiler.useStatementFactory(new RowStatementFactory());
    assertThat(compiler.hasStatementsDependentOnNewNodes()).isTrue();
    assertThat(compiler.createNodesStatements()).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`DomainObject`:`School`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    assertThat(compiler.createRelationshipsStatements()).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`TEACHERS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Test(org.junit.Test)

Example 14 with RowStatementFactory

use of org.neo4j.ogm.session.request.RowStatementFactory in project neo4j-ogm by neo4j.

the class CompilerTest method expectNoChangesWhenDomainUnchanged.

@Test
public void expectNoChangesWhenDomainUnchanged() {
    // create
    Long wallerId = 0L;
    Long maryId = 1L;
    School waller = new School("Waller");
    waller.setId(wallerId);
    Teacher mary = new Teacher("Mary");
    mary.setId(maryId);
    // relate
    mary.setSchool(waller);
    // validate the domain model
    assertThat(mary.getSchool().equals(waller)).isTrue();
    assertThat(waller.getTeachers().contains(mary)).isTrue();
    assertThat(waller.getTeachers().size() == 1).isTrue();
    // set the mapping context accordingly
    mappingContext.addNodeEntity(mary);
    mappingContext.addNodeEntity(waller);
    mappingContext.addRelationship(new MappedRelationship(maryId, "SCHOOL", wallerId, null, Teacher.class, School.class));
    mappingContext.addRelationship(new MappedRelationship(wallerId, "TEACHERS", maryId, null, School.class, Teacher.class));
    Compiler compiler = mapAndCompile(waller, -1);
    compiler.useStatementFactory(new RowStatementFactory());
    assertThat(compiler.createNodesStatements()).isEmpty();
    assertThat(compiler.updateNodesStatements()).isEmpty();
    assertThat(compiler.createRelationshipsStatements()).isEmpty();
    assertThat(compiler.updateRelationshipStatements()).isEmpty();
    compiler = mapAndCompile(mary, -1);
    assertThat(compiler.createNodesStatements()).isEmpty();
    assertThat(compiler.updateNodesStatements()).isEmpty();
    assertThat(compiler.createRelationshipsStatements()).isEmpty();
    assertThat(compiler.updateRelationshipStatements()).isEmpty();
}
Also used : School(org.neo4j.ogm.domain.education.School) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Teacher(org.neo4j.ogm.domain.education.Teacher) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Test(org.junit.Test)

Example 15 with RowStatementFactory

use of org.neo4j.ogm.session.request.RowStatementFactory in project neo4j-ogm by neo4j.

the class CompilerTest method updateSingleObjectPropertyAndLabel.

@Test
public void updateSingleObjectPropertyAndLabel() {
    Student sheila = new Student("Sheila Smythe");
    Long sid = 0L;
    sheila.setId(sid);
    mappingContext.addNodeEntity(sheila);
    // now update the object's properties locally
    sheila.setName("Sheila Smythe-Jones");
    Compiler compiler = mapAndCompile(sheila, -1);
    assertThat(compiler.hasStatementsDependentOnNewNodes()).isFalse();
    compiler.useStatementFactory(new RowStatementFactory());
    assertThat(compiler.createNodesStatements()).isEmpty();
    assertThat(compiler.updateNodesStatements()).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (n) WHERE ID(n)=row.nodeId SET n:`DomainObject`:`Student` SET n += row.props RETURN row.nodeId as ref, ID(n) as id, $type as type");
}
Also used : RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Aggregations

RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)15 Test (org.junit.Test)12 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)9 Document (org.neo4j.ogm.domain.filesystem.Document)7 Folder (org.neo4j.ogm.domain.filesystem.Folder)7 Statement (org.neo4j.ogm.request.Statement)6 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)3 Student (org.neo4j.ogm.domain.education.Student)3 EntityMapper (org.neo4j.ogm.context.EntityMapper)2 School (org.neo4j.ogm.domain.education.School)2 Teacher (org.neo4j.ogm.domain.education.Teacher)2 MappingContext (org.neo4j.ogm.context.MappingContext)1 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)1 MetaData (org.neo4j.ogm.metadata.MetaData)1 Statements (org.neo4j.ogm.request.Statements)1