use of org.neo4j.ogm.model.RestModel in project neo4j-ogm by neo4j.
the class ExecuteQueriesDelegate method query.
public Result query(String cypher, Map<String, ?> parameters, boolean readOnly) {
validateQuery(cypher, parameters, readOnly);
if (mayBeReadWrite(cypher)) {
// While an update query may not return objects, it has enough changes
// to modify all entities in the context, so we must flush it either way.
session.clear();
}
RestModelRequest request = new DefaultRestModelRequest(cypher, parameters);
RestModelMapper mapper = new RestModelMapper(session.metaData(), session.context(), session.getEntityInstantiator());
return session.doInTransaction(() -> {
try (Response<RestModel> response = session.requestHandler().execute(request)) {
RestStatisticsModel restStatisticsModel = mapper.map(response);
if (readOnly) {
return new QueryResultModel(restStatisticsModel.getResult(), null);
} else {
return new QueryResultModel(restStatisticsModel.getResult(), restStatisticsModel.getStatistics());
}
}
}, readOnly ? Transaction.Type.READ_ONLY : Transaction.Type.READ_WRITE);
}
use of org.neo4j.ogm.model.RestModel in project neo4j-ogm by neo4j.
the class RestModelMapper method map.
public RestStatisticsModel map(Response<RestModel> response) {
List<ResultRowBuilder> resultRowBuilders = new ArrayList<>();
Response<GraphModel> graphModelResponse = new Response<GraphModel>() {
@Override
public GraphModel next() {
RestModel model = response.next();
if (model == null) {
return null;
}
ResultRowBuilder resultRowBuilder = new ResultRowBuilder(RestModelMapper.this::getEntityOrNodeModel, mappingContext::getRelationshipEntity);
resultRowBuilders.add(resultRowBuilder);
model.getRow().forEach(resultRowBuilder::handle);
return resultRowBuilder.buildGraphModel();
}
@Override
public void close() {
response.close();
}
@Override
public String[] columns() {
return response.columns();
}
};
// Run the actual mapping
delegate.map(Object.class, graphModelResponse);
// Recreate the original structure
RestStatisticsModel restStatisticsModel = new RestStatisticsModel();
response.getStatistics().ifPresent(restStatisticsModel::setStatistics);
restStatisticsModel.setResult(resultRowBuilders.stream().map(ResultRowBuilder::finish).collect(Collectors.toList()));
return restStatisticsModel;
}
use of org.neo4j.ogm.model.RestModel in project neo4j-ogm by neo4j.
the class JsonRestResponseTest method shouldParseDataInRowResponseCorrectly.
@Test
public void shouldParseDataInRowResponseCorrectly() throws IOException {
when(entity.getContent()).thenReturn(rowResultsAndNoErrors());
try (Response<RestModel> rsp = new RestModelResponse(response)) {
RestModel restModel = rsp.next();
assertThat(restModel).isNotNull();
Map<String, Object> rows = restModel.getRow();
assertThat(rows.entrySet()).hasSize(3);
assertThat(rows.get("count")).isEqualTo(1L);
NodeModel data = (NodeModel) rows.get("director");
assertThat(data.property("born")).isEqualTo(1931L);
data = (NodeModel) rows.get("movie");
assertThat(data.property("title")).isEqualTo("The Birdcage");
assertThat(data.getId().longValue()).isEqualTo(395L);
restModel = rsp.next();
rows = restModel.getRow();
assertThat(rows.entrySet()).hasSize(3);
assertThat(rows.get("count")).isEqualTo(1L);
data = (NodeModel) rows.get("director");
assertThat(data.property("born")).isEqualTo(1931L);
data = (NodeModel) rows.get("movie");
assertThat(data.property("released")).isEqualTo(2007L);
}
}
Aggregations