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 */
}
}
}
}
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) {
}
}
}
}
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) {
}
}
}
}
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) {
}
}
}
}
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;
}
Aggregations