Search in sources :

Example 1 with Jacksonisable

use of ai.grakn.engine.Jacksonisable in project grakn by graknlabs.

the class ConceptController method getConceptCollection.

private <X extends ai.grakn.concept.Concept> String getConceptCollection(Request request, Response response, String key, Function<GraknTx, X> getter, Function<X, Stream<Jacksonisable>> collector) throws JsonProcessingException {
    response.type(APPLICATION_JSON);
    Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
    try (GraknTx tx = factory.tx(keyspace, READ);
        Timer.Context context = labelGetTimer.time()) {
        X concept = getter.apply(tx);
        // If the concept was not found return;
        if (concept == null) {
            response.status(SC_NOT_FOUND);
            return "[]";
        }
        List<Jacksonisable> list = collector.apply(concept).collect(Collectors.toList());
        Link link = Link.create(request.pathInfo());
        ListResource<Jacksonisable> listResource = ListResource.create(link, key, list);
        return objectMapper.writeValueAsString(listResource);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) Jacksonisable(ai.grakn.engine.Jacksonisable) Link(ai.grakn.engine.controller.response.Link)

Aggregations

GraknTx (ai.grakn.GraknTx)1 Keyspace (ai.grakn.Keyspace)1 Jacksonisable (ai.grakn.engine.Jacksonisable)1 Link (ai.grakn.engine.controller.response.Link)1 Timer (com.codahale.metrics.Timer)1