Search in sources :

Example 1 with EntityhubBackend

use of org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend in project stanbol by apache.

the class EntityhubRootResource method executeLDPathQuery.

/**
     * Execute a Query that uses LDPath to process results.
     * @param query the query
     * @param mediaType the mediaType for the response
     * @param headers the http headers of the request
     * @return the response
     */
private Response executeLDPathQuery(Entityhub entityhub, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
    QueryResultList<Representation> result;
    ValueFactory vf = new RdfValueFactory(new IndexedGraph());
    EntityhubBackend backend = new EntityhubBackend(entityhub);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    //copy the selected fields, because we might need to delete some during
    //the preparation phase
    Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
    //first prepare (only execute the query if the parameters are valid)
    Program<Object> program;
    try {
        program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
    } catch (LDPathParseException e) {
        log.warn("Unable to parse LDPath program used as select for Query:");
        log.warn("FieldQuery: \n {}", query);
        log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
        log.warn("Exception:", e);
        return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
    } catch (IllegalStateException e) {
        log.warn("parsed LDPath program is not compatible with parsed Query!", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //2. execute the query
    Iterator<Representation> resultIt;
    try {
        // go directly to the yard and query there for Representations
        resultIt = entityhub.getYard().findRepresentation(query).iterator();
    } catch (EntityhubException e) {
        String message = String.format("Exception while performing the " + "FieldQuery on the EntityHub (message: %s)", e.getMessage());
        log.error(message, e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //process the results
    Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
    result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
    ResponseBuilder rb = Response.ok(result);
    rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : EntityhubBackend(org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)1 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)1 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)1 EntityhubBackend (org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend)1 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)1 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)1 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)1