use of org.openrdf.repository.RepositoryException in project stanbol by apache.
the class SesameYard method store.
protected final Iterable<Representation> store(Iterable<Representation> representations, boolean allowCreate) throws IllegalArgumentException, YardException {
RepositoryConnection con = null;
try {
con = repository.getConnection();
con.begin();
ArrayList<Representation> added = new ArrayList<Representation>();
for (Representation representation : representations) {
if (representation != null) {
//reassign
Representation stored = store(con, representation, allowCreate, false);
//to check if the store was successful
if (stored != null) {
added.add(stored);
} else {
//can only be the case if allowCreate==false (update was called)
log.warn(String.format("Unable to update Representation %s in Yard %s because it is not present!", representation.getId(), getId()));
}
}
//ignore null values in the parsed Iterable!
}
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) {
}
}
}
}
use of org.openrdf.repository.RepositoryException 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) {
}
}
}
}
use of org.openrdf.repository.RepositoryException in project stanbol by apache.
the class SesameYard method findReferences.
@Override
public QueryResultList<String> findReferences(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
List<String> ids = limit > 0 ? new ArrayList<String>(limit) : new ArrayList<String>();
while (results.hasNext()) {
BindingSet result = results.next();
Value value = result.getValue(query.getRootVariableName());
if (value instanceof Resource) {
ids.add(value.stringValue());
}
}
con.commit();
return new QueryResultListImpl<String>(query, ids, String.class);
} 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 */
}
}
}
}
use of org.openrdf.repository.RepositoryException 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) {
}
}
}
}
use of org.openrdf.repository.RepositoryException 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 */
}
}
}
}
Aggregations