Search in sources :

Example 1 with RestModel

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);
}
Also used : QueryResultModel(org.neo4j.ogm.response.model.QueryResultModel) RestStatisticsModel(org.neo4j.ogm.context.RestStatisticsModel) DefaultRestModelRequest(org.neo4j.ogm.cypher.query.DefaultRestModelRequest) RestModelRequest(org.neo4j.ogm.request.RestModelRequest) DefaultRestModelRequest(org.neo4j.ogm.cypher.query.DefaultRestModelRequest) RestModel(org.neo4j.ogm.model.RestModel) RestModelMapper(org.neo4j.ogm.context.RestModelMapper)

Example 2 with RestModel

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;
}
Also used : Response(org.neo4j.ogm.response.Response) GraphModel(org.neo4j.ogm.model.GraphModel) DefaultGraphModel(org.neo4j.ogm.response.model.DefaultGraphModel) RestModel(org.neo4j.ogm.model.RestModel)

Example 3 with RestModel

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);
    }
}
Also used : NodeModel(org.neo4j.ogm.response.model.NodeModel) RestModel(org.neo4j.ogm.model.RestModel) Test(org.junit.Test)

Aggregations

RestModel (org.neo4j.ogm.model.RestModel)3 Test (org.junit.Test)1 RestModelMapper (org.neo4j.ogm.context.RestModelMapper)1 RestStatisticsModel (org.neo4j.ogm.context.RestStatisticsModel)1 DefaultRestModelRequest (org.neo4j.ogm.cypher.query.DefaultRestModelRequest)1 GraphModel (org.neo4j.ogm.model.GraphModel)1 RestModelRequest (org.neo4j.ogm.request.RestModelRequest)1 Response (org.neo4j.ogm.response.Response)1 DefaultGraphModel (org.neo4j.ogm.response.model.DefaultGraphModel)1 NodeModel (org.neo4j.ogm.response.model.NodeModel)1 QueryResultModel (org.neo4j.ogm.response.model.QueryResultModel)1