Search in sources :

Example 6 with Node

use of org.neo4j.ogm.model.Node 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)

Example 7 with Node

use of org.neo4j.ogm.model.Node in project neo4j-ogm by neo4j.

the class JsonGraphResponseTest method shouldParseDataInLoadByIdsGraphResponseCorrectly.

@Test
public void shouldParseDataInLoadByIdsGraphResponseCorrectly() throws IOException {
    when(entity.getContent()).thenReturn(loadByIdsGraphResults());
    try (Response<GraphModel> rsp = new GraphModelResponse(response)) {
        GraphModel graphModel = rsp.next();
        assertThat(graphModel).isNotNull();
        Collection<Node> nodes = graphModel.getNodes();
        assertThat(nodes).hasSize(1);
        assertThat(nodes.iterator().next().getPropertyList().get(0).getValue()).isEqualTo("adam");
        assertThat(graphModel.getRelationships()).isEmpty();
        graphModel = rsp.next();
        assertThat(graphModel).isNotNull();
        nodes = graphModel.getNodes();
        assertThat(nodes).hasSize(2);
        Iterator<Node> nodeIterator = nodes.iterator();
        // skip adam
        nodeIterator.next();
        assertThat(nodeIterator.next().getPropertyList().get(0).getValue()).isEqualTo("GraphAware");
        assertThat(graphModel.getRelationships()).hasSize(1);
        assertThat(graphModel.getRelationships().iterator().next().getType()).isEqualTo("EMPLOYED_BY");
        for (int i = 0; i < 4; i++) {
            assertThat(rsp.next()).isNotNull();
        }
        assertThat(rsp.next()).isNull();
    }
}
Also used : GraphModel(org.neo4j.ogm.model.GraphModel) Node(org.neo4j.ogm.model.Node) Test(org.junit.Test)

Aggregations

Node (org.neo4j.ogm.model.Node)7 Statement (org.neo4j.ogm.request.Statement)4 Collectors (java.util.stream.Collectors)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 EndNode (org.neo4j.ogm.annotation.EndNode)2 StartNode (org.neo4j.ogm.annotation.StartNode)2 CypherStatementBuilder (org.neo4j.ogm.cypher.compiler.CypherStatementBuilder)2 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 GraphModel (org.neo4j.ogm.model.GraphModel)2 OptimisticLockingConfig (org.neo4j.ogm.request.OptimisticLockingConfig)2 StatementFactory (org.neo4j.ogm.request.StatementFactory)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 Arrays (java.util.Arrays)1 BiFunction (java.util.function.BiFunction)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1