Search in sources :

Example 21 with YardException

use of org.apache.stanbol.entityhub.servicesapi.yard.YardException in project stanbol by apache.

the class ReferencedSiteImpl method getEntity.

@Override
public Entity getEntity(String id) throws SiteException {
    Representation rep = null;
    Boolean cachedVersion = Boolean.FALSE;
    long start = System.currentTimeMillis();
    if (cache != null) {
        try {
            rep = cache.getRepresentation(id);
            if (rep == null) {
                if (siteConfiguration.getCacheStrategy() == CacheStrategy.all) {
                    // do no remote lookups on CacheStrategy.all!!
                    return null;
                }
            } else {
                cachedVersion = Boolean.TRUE;
            }
        } catch (YardException e) {
            if (dereferencer == null) {
                throw new SiteException(String.format("Unable to get Represetnation %s form Cache %s", id, siteConfiguration.getCacheId()), e);
            } else {
                log.warn(String.format("Unable to get Represetnation %s form Cache %s. Will dereference from remote site %s", id, siteConfiguration.getCacheId(), siteConfiguration.getAccessUri()), e);
            }
        }
    }
    if (rep == null && dereferencer != null) {
        try {
            rep = dereferencer.dereference(id);
        } catch (IOException e) {
            throw new SiteException(String.format("Unable to load Representation for entity %s form remote site %s with dereferencer %s", id, siteConfiguration.getAccessUri(), siteConfiguration.getEntityDereferencerType()), e);
        }
        // representation loaded from remote site and cache is available
        if (rep != null && cache != null) {
            // -> cache the representation
            try {
                start = System.currentTimeMillis();
                // return the the cached version
                rep = cache.store(rep);
                cachedVersion = Boolean.TRUE;
                log.debug("  - cached Representation {} in {} ms", id, (System.currentTimeMillis() - start));
            } catch (YardException e) {
                log.warn(String.format("Unable to cache Represetnation %s in Cache %s! Representation not cached!", id, siteConfiguration.getCacheId()), e);
            }
        }
    }
    if (rep != null) {
        Entity entity = new EntityImpl(getId(), rep, null);
        initEntityMetadata(entity, siteMetadata, singletonMap(RdfResourceEnum.isChached.getUri(), (Object) cachedVersion));
        return entity;
    } else {
        return null;
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 22 with YardException

use of org.apache.stanbol.entityhub.servicesapi.yard.YardException in project stanbol by apache.

the class CacheImpl method setAdditionalMappings.

/**
     * Internally used in the initialisation to be able to parse the Yard instance
     *
     * @param yard the yard used to set the configured additional mappings
     * @param fieldMapper the configuration
     * @throws YardException on any error while accessing the yard
     */
protected void setAdditionalMappings(Yard yard, FieldMapper fieldMapper) throws YardException {
    FieldMapper old = this.additionalMapper;
    this.additionalMapper = fieldMapper;
    try {
        CacheUtils.storeAdditionalMappingsConfiguration(yard, additionalMapper);
    } catch (YardException e) {
        this.additionalMapper = old;
        throw e;
    }
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)

Example 23 with YardException

use of org.apache.stanbol.entityhub.servicesapi.yard.YardException in project stanbol by apache.

the class SesameYard method executeSparqlFieldQuery.

/**
     * Returns the SPARQL result set for a given {@link SparqlFieldQuery} that
     * was executed on this yard
     * @param con the repository connection to use
     * @param fieldQuery the SparqlFieldQuery instance
     * @param limit the maximum number of results
     * @return the results of the SPARQL query in the {@link #contexts} of the
     * Sesame Repository 
     * @throws RepositoryException on any error while using the parsed connection
     * @throws QueryEvaluationException  on any error while executing the query
     * @throws YardException if the SPARQL query created for the parsed FieldQuery
     * was illegal formatted or if the {@link #repository} does not support 
     * SPARQL.
     */
private TupleQueryResult executeSparqlFieldQuery(RepositoryConnection con, final SparqlFieldQuery fieldQuery, int limit, boolean select) throws RepositoryException, YardException, QueryEvaluationException {
    log.debug("> execute FieldQuery: {}", fieldQuery);
    String sparqlQueryString = SparqlQueryUtils.createSparqlSelectQuery(fieldQuery, select, limit, SparqlEndpointTypeEnum.Sesame);
    log.debug(" - SPARQL Query: {}", sparqlQueryString);
    TupleQuery sparqlOuery;
    try {
        sparqlOuery = con.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQueryString);
    } catch (MalformedQueryException e) {
        log.error("Unable to pparse SPARQL Query generated for a FieldQuery");
        log.error("FieldQuery: {}", fieldQuery);
        log.error("SPARQL Query: {}", sparqlQueryString);
        log.error("Exception ", e);
        throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
    } catch (UnsupportedQueryTypeException e) {
        String message = "The Sesame Repository '" + repository + "'(class: " + repository.getClass().getName() + ") does not support SPARQL!";
        log.error(message, e);
        throw new YardException(message, e);
    }
    if (dataset != null) {
        //respect the configured contexts
        sparqlOuery.setDataset(dataset);
    }
    return sparqlOuery.evaluate();
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) MalformedQueryException(org.openrdf.query.MalformedQueryException) UnsupportedQueryTypeException(org.apache.stanbol.entityhub.servicesapi.query.UnsupportedQueryTypeException) TupleQuery(org.openrdf.query.TupleQuery)

Example 24 with YardException

use of org.apache.stanbol.entityhub.servicesapi.yard.YardException in project stanbol by apache.

the class ClerezzaYard method executeSparqlFieldQuery.

/**
     * Returns the SPARQL result set for a given {@link SparqlFieldQuery} that
     * was executed on this yard
     * @param query the SparqlFieldQuery instance
     * @return the results of the SPARQL query in the yard
     * @throws YardException in case the generated SPARQL query could not be parsed
     * or the generated Query is not an SPARQL SELECT query.
     */
private ResultSet executeSparqlFieldQuery(final SparqlFieldQuery query) throws YardException {
    int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
    SelectQuery sparqlQuery;
    String sparqlQueryString = SparqlQueryUtils.createSparqlSelectQuery(query, false, limit, EndpointTypeEnum.Standard);
    try {
        sparqlQuery = (SelectQuery) QueryParser.getInstance().parse(sparqlQueryString);
    } catch (ParseException e) {
        log.error("ParseException for SPARQL Query in findRepresentation");
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
    } catch (ClassCastException e) {
        log.error("ClassCastExeption because parsed SPARQL Query is not of Type " + SelectQuery.class);
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to parse SPARQL SELECT query generated for the parse FieldQuery", e);
    }
    return tcManager.executeSparqlQuery(sparqlQuery, graph);
}
Also used : SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException)

Example 25 with YardException

use of org.apache.stanbol.entityhub.servicesapi.yard.YardException in project stanbol by apache.

the class YardSite method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws ManagedSiteException {
    QueryResultList<Representation> results;
    try {
        results = getYard().findRepresentation(query);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
    return new QueryResultListImpl<Entity>(results.getQuery(), new AdaptingIterator<Representation, Entity>(results.iterator(), new AdaptingIterator.Adapter<Representation, Entity>() {

        private final String siteId = config.getId();

        @Override
        public Entity adapt(Representation value, Class<Entity> type) {
            Entity entity = new EntityImpl(siteId, value, null);
            SiteUtils.initEntityMetadata(entity, siteMetadata, null);
            return entity;
        }
    }, Entity.class), Entity.class);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Aggregations

YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)31 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)15 RepositoryConnection (org.openrdf.repository.RepositoryConnection)10 RepositoryException (org.openrdf.repository.RepositoryException)10 IOException (java.io.IOException)9 SolrServerException (org.apache.solr.client.solrj.SolrServerException)6 PrivilegedActionException (java.security.PrivilegedActionException)5 ArrayList (java.util.ArrayList)5 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)5 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)5 EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)4 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)4 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)4 HashSet (java.util.HashSet)3 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)3 SolrDocument (org.apache.solr.common.SolrDocument)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3 FieldMapper (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)3 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)3