Search in sources :

Example 6 with LDPathSelect

use of org.apache.stanbol.entityhub.ldpath.query.LDPathSelect in project stanbol by apache.

the class FieldQueryToJsonUtils method toJSON.

/**
     * Converts a {@link FieldQuery} to it's JSON representation
     *
     * @param query the Query
     * @return the {@link JSONObject}
     * @throws JSONException
     */
public static JSONObject toJSON(FieldQuery query, NamespacePrefixService nsPrefixService) throws JSONException {
    JSONObject jQuery = new JSONObject();
    jQuery.put("selected", new JSONArray(query.getSelectedFields()));
    JSONArray constraints = new JSONArray();
    jQuery.put("constraints", constraints);
    for (Entry<String, Constraint> fieldConstraint : query) {
        JSONObject jFieldConstraint = convertConstraintToJSON(fieldConstraint.getValue(), nsPrefixService);
        //add the field
        jFieldConstraint.put("field", fieldConstraint.getKey());
        //write the boost if present
        Double boost = fieldConstraint.getValue().getBoost();
        if (boost != null) {
            jFieldConstraint.put("boost", boost);
        }
        //add fieldConstraint
        constraints.put(jFieldConstraint);
    }
    if (query.getLimit() != null) {
        jQuery.put("limit", query.getLimit());
    }
    //if(query.getOffset() != 0){
    jQuery.put("offset", query.getOffset());
    //}
    if (query instanceof LDPathSelect && ((LDPathSelect) query).getLDPathSelect() != null && !((LDPathSelect) query).getLDPathSelect().isEmpty()) {
        jQuery.put("ldpath", ((LDPathSelect) query).getLDPathSelect());
    }
    return jQuery;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) JSONArray(org.codehaus.jettison.json.JSONArray) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)

Example 7 with LDPathSelect

use of org.apache.stanbol.entityhub.ldpath.query.LDPathSelect in project stanbol by apache.

the class ReferencedSiteRootResource method executeQuery.

/**
     * Executes the query parsed by {@link #queryEntities(String, File, HttpHeaders)} or created based
     * {@link #findEntity(String, String, String, int, int, HttpHeaders)}
     * 
     * @param query
     *            The query to execute
     * @param headers the request headers
     * @return the response (results of error)
     */
private Response executeQuery(Site site, FieldQuery query, HttpHeaders headers) throws WebApplicationException {
    MediaType mediaType = getAcceptableMediaType(headers, ENTITY_SUPPORTED_MEDIA_TYPES, APPLICATION_JSON_TYPE);
    if (query instanceof LDPathSelect && ((LDPathSelect) query).getLDPathSelect() != null) {
        //use the LDPath variant to process this query
        return executeLDPathQuery(site, query, ((LDPathSelect) query).getLDPathSelect(), mediaType, headers);
    } else {
        //use the default query execution
        QueryResultList<Representation> result;
        try {
            result = site.find(query);
        } catch (SiteException e) {
            String message = String.format("Unable to Query Site '%s' (message: %s)", site.getId(), e.getMessage());
            log.error(message, e);
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, mediaType).build();
        }
        ResponseBuilder rb = Response.ok(result);
        rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Aggregations

LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)7 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 HashSet (java.util.HashSet)3 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)3 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)3 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)3 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)3 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)3 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)2 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)2 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)2 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)2 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)2 MediaType (javax.ws.rs.core.MediaType)1 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)1 EntityhubBackend (org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend)1 SiteBackend (org.apache.stanbol.entityhub.ldpath.backend.SiteBackend)1 SiteManagerBackend (org.apache.stanbol.entityhub.ldpath.backend.SiteManagerBackend)1 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)1