Search in sources :

Example 1 with NewRelationshipStatementBuilder

use of org.neo4j.ogm.cypher.compiler.builders.statement.NewRelationshipStatementBuilder in project neo4j-ogm by neo4j.

the class MultiStatementCypherCompiler method createRelationshipsStatements.

@Override
public List<Statement> createRelationshipsStatements() {
    assertStatementFactoryExists();
    // Group relationships by type and non-null properties
    // key: relationship type, value: Map where key=Set<Property strings>, value: Set of edges with those properties
    Map<String, Map<String, Set<Edge>>> relsByTypeAndProps = new HashMap<>();
    for (RelationshipBuilder relationshipBuilder : newRelationshipBuilders) {
        if (relationshipBuilder.edge().getStartNode() == null || relationshipBuilder.edge().getEndNode() == null) {
            // TODO this is a carry forward from the old emitters. We want to prevent this rel builder getting created or remove it
            continue;
        }
        Map<String, Set<Edge>> relsByProps = relsByTypeAndProps.computeIfAbsent(relationshipBuilder.type(), (key) -> new HashMap<>());
        RelationshipModel edge = (RelationshipModel) relationshipBuilder.edge();
        String primaryId = edge.getPrimaryIdName();
        Set<Edge> rels = relsByProps.computeIfAbsent(primaryId, (s) -> new HashSet<>());
        edge.setStartNode(context.getId(edge.getStartNode()));
        edge.setEndNode(context.getId(edge.getEndNode()));
        rels.add(edge);
    }
    List<Statement> statements = new ArrayList<>();
    // For each relationship type
    for (Map<String, Set<Edge>> edgesByProperties : relsByTypeAndProps.values()) {
        // For each set of unique property keys
        for (Set<Edge> edges : edgesByProperties.values()) {
            NewRelationshipStatementBuilder newRelationshipBuilder = new NewRelationshipStatementBuilder(edges, statementFactory);
            statements.add(newRelationshipBuilder.build());
        }
    }
    return statements;
}
Also used : Statement(org.neo4j.ogm.request.Statement) RelationshipModel(org.neo4j.ogm.response.model.RelationshipModel) NewRelationshipStatementBuilder(org.neo4j.ogm.cypher.compiler.builders.statement.NewRelationshipStatementBuilder) Edge(org.neo4j.ogm.model.Edge) DefaultRelationshipBuilder(org.neo4j.ogm.cypher.compiler.builders.node.DefaultRelationshipBuilder)

Aggregations

DefaultRelationshipBuilder (org.neo4j.ogm.cypher.compiler.builders.node.DefaultRelationshipBuilder)1 NewRelationshipStatementBuilder (org.neo4j.ogm.cypher.compiler.builders.statement.NewRelationshipStatementBuilder)1 Edge (org.neo4j.ogm.model.Edge)1 Statement (org.neo4j.ogm.request.Statement)1 RelationshipModel (org.neo4j.ogm.response.model.RelationshipModel)1