Search in sources :

Example 1 with DatasetImpl

use of org.openrdf.query.impl.DatasetImpl in project qi4j-sdk by Qi4j.

the class SPARQLResource method getQuery.

private Query getQuery(Repository repository, RepositoryConnection repositoryCon, String queryStr) throws ResourceException {
    Form form = getRequest().getResourceRef().getQueryAsForm();
    Query result;
    // default query language is SPARQL
    QueryLanguage queryLn = QueryLanguage.SPARQL;
    // determine if inferred triples should be included in query evaluation
    boolean includeInferred = true;
    // build a dataset, if specified
    String[] defaultGraphURIs = form.getValuesArray(DEFAULT_GRAPH_PARAM_NAME);
    String[] namedGraphURIs = form.getValuesArray(NAMED_GRAPH_PARAM_NAME);
    DatasetImpl dataset = null;
    if (defaultGraphURIs.length > 0 || namedGraphURIs.length > 0) {
        dataset = new DatasetImpl();
        if (defaultGraphURIs.length > 0) {
            for (String defaultGraphURI : defaultGraphURIs) {
                try {
                    URI uri = repository.getValueFactory().createURI(defaultGraphURI);
                    dataset.addDefaultGraph(uri);
                } catch (IllegalArgumentException e) {
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Illegal URI for default graph: " + defaultGraphURI);
                }
            }
        }
        if (namedGraphURIs.length > 0) {
            for (String namedGraphURI : namedGraphURIs) {
                try {
                    URI uri = repository.getValueFactory().createURI(namedGraphURI);
                    dataset.addNamedGraph(uri);
                } catch (IllegalArgumentException e) {
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Illegal URI for named graph: " + namedGraphURI);
                }
            }
        }
    }
    try {
        result = repositoryCon.prepareQuery(queryLn, queryStr);
        result.setIncludeInferred(includeInferred);
        if (dataset != null) {
            result.setDataset(dataset);
        }
        // determine if any variable bindings have been set on this query.
        @SuppressWarnings("unchecked") Enumeration<String> parameterNames = Collections.enumeration(form.getValuesMap().keySet());
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            if (parameterName.startsWith(BINDING_PREFIX) && parameterName.length() > BINDING_PREFIX.length()) {
                String bindingName = parameterName.substring(BINDING_PREFIX.length());
                Value bindingValue = parseValueParam(repository, form, parameterName);
                result.setBinding(bindingName, bindingValue);
            }
        }
    } catch (UnsupportedQueryLanguageException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage());
    } catch (MalformedQueryException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage());
    } catch (RepositoryException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
    return result;
}
Also used : Form(org.restlet.data.Form) RepositoryException(org.openrdf.repository.RepositoryException) DatasetImpl(org.openrdf.query.impl.DatasetImpl) URI(org.openrdf.model.URI) Value(org.openrdf.model.Value) ResourceException(org.restlet.resource.ResourceException)

Aggregations

URI (org.openrdf.model.URI)1 Value (org.openrdf.model.Value)1 DatasetImpl (org.openrdf.query.impl.DatasetImpl)1 RepositoryException (org.openrdf.repository.RepositoryException)1 Form (org.restlet.data.Form)1 ResourceException (org.restlet.resource.ResourceException)1