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);
}
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();
}
}
Aggregations