Search in sources :

Example 16 with YardException

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

the class SesameYard method findRepresentation.

@Override
public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        //execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, false);
        //parse the results and generate the Representations
        //create an own valueFactors so that all the data of the query results
        //are added to the same Sesame Model
        Model model = new TreeModel();
        RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
        List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof URI) {
                //copy all data to the model and create the representation
                RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI) value);
                //link the result with the query result
                model.add(queryRoot, queryResult, value);
                representations.add(rep);
            }
        //ignore non URI results
        }
        con.commit();
        return new SesameQueryResultList(model, query, representations);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            //close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException) URI(org.openrdf.model.URI) TreeModel(org.openrdf.model.impl.TreeModel) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)

Example 17 with YardException

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

the class SesameYard method remove.

@Override
public void remove(String id) throws YardException, IllegalArgumentException {
    if (id == null) {
        throw new IllegalArgumentException("The parsed Representation id MUST NOT be NULL!");
    }
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        remove(con, sesameFactory.createURI(id));
        con.commit();
    } catch (RepositoryException e) {
        throw new YardException("Unable to remove for Representation " + id, e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) RepositoryException(org.openrdf.repository.RepositoryException)

Example 18 with YardException

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

the class SesameYard method removeAll.

@Override
public final void removeAll() throws YardException {
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        //removes everything
        con.clear(contexts);
        con.commit();
    } catch (RepositoryException e) {
        throw new YardException("Unable to remove parsed Representations", e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) RepositoryException(org.openrdf.repository.RepositoryException)

Example 19 with YardException

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

the class SesameYard method store.

/**
     * Generic store method used by store and update methods
     * @param representation the representation to store/update
     * @param allowCreate if new representation are allowed to be created
     * @param canNotCreateIsError if updates to existing one are allowed
     * @return the representation as added to the yard
     * @throws IllegalArgumentException
     * @throws YardException
     */
protected final Representation store(Representation representation, boolean allowCreate, boolean canNotCreateIsError) throws IllegalArgumentException, YardException {
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        Representation added = store(con, representation, allowCreate, canNotCreateIsError);
        con.commit();
        return added;
    } catch (RepositoryException e) {
        throw new YardException("Unable to remove parsed Representations", e);
    } catch (IllegalArgumentException e) {
        try {
            //to avoid Exception logs in case store(..) throws an Exception
            //in the case allowCreate and canNotCreateIsError do not allow
            //the store operation
            con.rollback();
        } catch (RepositoryException ignore) {
        }
        throw e;
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException)

Example 20 with YardException

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

the class EntityPersisterRunnable method store.

/**
     * Stores the parsed Representations to the {@link #yard} and 
     * {@link #sendError(String, String, Exception)} for entities that could
     * not be stored!
     * @param toStore the Representations to store. This method removes all
     * Elements of this map while doing the work
     */
private Set<QueueItem<Representation>> store(Map<String, QueueItem<Representation>> toStore) {
    String errorMsg;
    YardException yardException = null;
    Set<QueueItem<Representation>> stored = new HashSet<QueueItem<Representation>>();
    Collection<Representation> reps = new ArrayList<Representation>(toStore.size());
    for (QueueItem<Representation> item : toStore.values()) {
        reps.add(item.getItem());
    }
    try {
        for (Representation r : yard.store(reps)) {
            QueueItem<Representation> old = toStore.remove(r.getId());
            //create a new QueueItem and copy the metadata of the old one
            stored.add(new QueueItem<Representation>(r, old));
        }
        errorMsg = "Entity %s was not indexed by the Yard %s";
    } catch (YardException e) {
        errorMsg = "Unable to store Entity %s to Yard %s because of an YardException";
        yardException = e;
    }
    //the remaining Items in to store have some errors
    for (QueueItem<Representation> entry : toStore.values()) {
        sendError(entry.getItem().getId(), entry, String.format(errorMsg, entry.getItem().getId(), yard.getId()), yardException);
    }
    //clear the
    toStore.clear();
    return stored;
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ArrayList(java.util.ArrayList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) HashSet(java.util.HashSet)

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