Search in sources :

Example 26 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYard method remove.

@Override
public final void remove(Iterable<String> ids) throws IllegalArgumentException, YardException {
    if (ids == null) {
        throw new IllegalArgumentException("The parsed Iterable over the IDs to remove MUST NOT be NULL!");
    }
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        for (String id : ids) {
            if (id != null) {
                remove(con, sesameFactory.createURI(id));
            }
        }
        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 27 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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 28 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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 29 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYard method isRepresentation.

@Override
public boolean isRepresentation(String id) throws YardException {
    if (id == null) {
        throw new IllegalArgumentException("The parsed id MUST NOT be NULL!");
    }
    if (id.isEmpty()) {
        throw new IllegalArgumentException("The parsed id MUST NOT be EMPTY!");
    }
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        boolean state = isRepresentation(con, sesameFactory.createURI(id));
        con.commit();
        return state;
    } catch (RepositoryException e) {
        throw new YardException("Unable to check 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 30 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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)

Aggregations

RepositoryConnection (org.openrdf.repository.RepositoryConnection)71 TupleQuery (org.openrdf.query.TupleQuery)27 URI (org.openrdf.model.URI)24 RepositoryException (org.openrdf.repository.RepositoryException)20 Test (org.junit.Test)15 Statement (org.openrdf.model.Statement)11 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Literal (org.openrdf.model.Literal)9 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 Update (org.openrdf.query.Update)6 InputStream (java.io.InputStream)5 Value (org.openrdf.model.Value)5 Repository (org.openrdf.repository.Repository)5 SailRepository (org.openrdf.repository.sail.SailRepository)5 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)4 Resource (org.openrdf.model.Resource)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3