Search in sources :

Example 6 with OptimisticLockingConfig

use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.

the class ExistingRelationshipStatementBuilder method build.

@Override
public Statement build() {
    final Map<String, Object> parameters = new HashMap<>();
    final StringBuilder queryBuilder = new StringBuilder();
    Edge firstEdge = edges.iterator().next();
    if (edges.size() > 0) {
        queryBuilder.append("UNWIND $rows AS row MATCH ()-[r]->() WHERE ID(r) = row.relId ");
        if (firstEdge.hasVersionProperty()) {
            queryBuilder.append(OptimisticLockingUtils.getFragmentForExistingNodesAndRelationships(firstEdge, "r", dirtyEdges ? 1 : 0));
        }
        queryBuilder.append(firstEdge.createPropertyRemovalFragment("r"));
        queryBuilder.append("SET r += row.props ");
        queryBuilder.append("RETURN ID(r) as ref, ID(r) as id, $type as type");
        List<Map> rows = new ArrayList<>();
        for (Edge edge : edges) {
            Map<String, Object> rowMap = new HashMap<>();
            rowMap.put("relId", edge.getId());
            Map<String, Object> props = new HashMap<>();
            for (Property property : edge.getPropertyList()) {
                if (!property.equals(edge.getVersion())) {
                    props.put((String) property.getKey(), property.getValue());
                }
            }
            rowMap.put("props", props);
            if (edge.hasVersionProperty()) {
                Property version = edge.getVersion();
                rowMap.put((String) version.getKey(), version.getValue());
            }
            rows.add(rowMap);
        }
        parameters.put("rows", rows);
        parameters.put("type", "rel");
        if (firstEdge.hasVersionProperty()) {
            OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), new String[] { firstEdge.getType() }, firstEdge.getVersion().getKey());
            return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
        }
    }
    return statementFactory.statement(queryBuilder.toString(), parameters);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) Edge(org.neo4j.ogm.model.Edge) Map(java.util.Map) HashMap(java.util.HashMap) Property(org.neo4j.ogm.model.Property)

Example 7 with OptimisticLockingConfig

use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.

the class NewNodeStatementBuilder method build.

@Override
public Statement build() {
    final Map<String, Object> parameters = new HashMap<>();
    final StringBuilder queryBuilder = new StringBuilder();
    if (newNodes != null && newNodes.size() > 0) {
        Node firstNode = newNodes.iterator().next();
        queryBuilder.append("UNWIND $rows as row ");
        boolean hasPrimaryIndex = firstNode.getPrimaryIndex() != null;
        if (hasPrimaryIndex) {
            queryBuilder.append("MERGE (n");
        } else {
            queryBuilder.append("CREATE (n");
        }
        for (String label : firstNode.getLabels()) {
            queryBuilder.append(":`").append(label).append("`");
        }
        if (hasPrimaryIndex) {
            String propertiesToMergeOn = Arrays.stream(firstNode.getPrimaryIndex().split(PROPERTY_SEPARATOR)).map(p -> p + ": row.props." + p).collect(joining(",", "{", "}"));
            queryBuilder.append(propertiesToMergeOn);
        }
        // Closing MERGE or CREATE
        queryBuilder.append(") ");
        if (firstNode.hasVersionProperty() && hasPrimaryIndex) {
            queryBuilder.append(getFragmentForNewOrExistingNodes(firstNode, "n"));
        }
        queryBuilder.append("SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
        List<Map> rows = newNodes.stream().map(node -> node.toRow("nodeRef")).collect(toList());
        parameters.put("type", "node");
        parameters.put("rows", rows);
        if (firstNode.hasVersionProperty()) {
            OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), firstNode.getLabels(), firstNode.getVersion().getKey());
            return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
        }
    }
    return statementFactory.statement(queryBuilder.toString(), parameters);
}
Also used : Arrays(java.util.Arrays) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) OptimisticLockingUtils(org.neo4j.ogm.cypher.compiler.builders.statement.OptimisticLockingUtils) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) NodeQueryStatements(org.neo4j.ogm.session.request.strategy.impl.NodeQueryStatements) Statement(org.neo4j.ogm.request.Statement) StatementFactory(org.neo4j.ogm.request.StatementFactory) List(java.util.List) CypherStatementBuilder(org.neo4j.ogm.cypher.compiler.CypherStatementBuilder) Map(java.util.Map) Node(org.neo4j.ogm.model.Node) HashMap(java.util.HashMap) Node(org.neo4j.ogm.model.Node) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)7 OptimisticLockingConfig (org.neo4j.ogm.request.OptimisticLockingConfig)7 Map (java.util.Map)5 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 CypherStatementBuilder (org.neo4j.ogm.cypher.compiler.CypherStatementBuilder)2 DefaultRowModelRequest (org.neo4j.ogm.cypher.query.DefaultRowModelRequest)2 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)2 Edge (org.neo4j.ogm.model.Edge)2 Node (org.neo4j.ogm.model.Node)2 Property (org.neo4j.ogm.model.Property)2 Statement (org.neo4j.ogm.request.Statement)2 StatementFactory (org.neo4j.ogm.request.StatementFactory)2 Arrays (java.util.Arrays)1 OptimisticLockingUtils (org.neo4j.ogm.cypher.compiler.builders.statement.OptimisticLockingUtils)1 OptimisticLockingException (org.neo4j.ogm.exception.OptimisticLockingException)1 RowModel (org.neo4j.ogm.model.RowModel)1 NodeQueryStatements (org.neo4j.ogm.session.request.strategy.impl.NodeQueryStatements)1