use of org.qi4j.spi.query.EntityFinderException in project qi4j-sdk by Qi4j.
the class SQLEntityFinder method performQuery.
// Helper method to perform SQL queries and handle things if/when something happens
private <ReturnType> ReturnType performQuery(DoQuery<ReturnType> doQuery) throws EntityFinderException {
ReturnType result = null;
Connection connection = null;
try {
connection = this._dataSource.getConnection();
connection.setReadOnly(true);
result = doQuery.doIt(connection);
} catch (SQLException sqle) {
throw new EntityFinderException(sqle);
} finally {
SQLUtil.closeQuietly(connection);
}
return result;
}
use of org.qi4j.spi.query.EntityFinderException in project qi4j-sdk by Qi4j.
the class SolrEntityQueryMixin method findEntities.
@Override
public Iterable<EntityReference> findEntities(Class<?> resultType, @Optional Specification<Composite> whereClause, @Optional OrderBy[] orderBySegments, @Optional Integer firstResult, @Optional Integer maxResults, Map<String, Object> variables) throws EntityFinderException {
try {
QuerySpecification expr = (QuerySpecification) whereClause;
SolrServer server = solr.solrServer();
NamedList<Object> list = new NamedList<Object>();
list.add("q", expr.query());
list.add("rows", maxResults != 0 ? maxResults : 10000);
list.add("start", firstResult);
if (orderBySegments != null && orderBySegments.length > 0) {
for (OrderBy orderBySegment : orderBySegments) {
String propName = ((Member) orderBySegment.property().accessor()).getName() + "_for_sort";
String order = orderBySegment.order() == OrderBy.Order.ASCENDING ? "asc" : "desc";
list.add("sort", propName + " " + order);
}
}
SolrParams solrParams = SolrParams.toSolrParams(list);
logger.debug("Search:" + list.toString());
QueryResponse query = server.query(solrParams);
SolrDocumentList results = query.getResults();
List<EntityReference> references = new ArrayList<EntityReference>(results.size());
for (SolrDocument result : results) {
references.add(EntityReference.parseEntityReference(result.getFirstValue("id").toString()));
}
return references;
} catch (SolrServerException e) {
throw new EntityFinderException(e);
}
}
use of org.qi4j.spi.query.EntityFinderException in project qi4j-sdk by Qi4j.
the class EntitiesResource method representHtml.
private Representation representHtml() throws ResourceException {
try {
final Iterable<EntityReference> query = entityFinder.findEntities(EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap());
Representation representation = new WriterRepresentation(MediaType.TEXT_HTML) {
@Override
public void write(Writer buf) throws IOException {
PrintWriter out = new PrintWriter(buf);
out.println("<html><head><title>All entities</title></head><body><h1>All entities</h1><ul>");
for (EntityReference entity : query) {
out.println("<li><a href=\"" + getRequest().getResourceRef().clone().addSegment(entity.identity() + ".html") + "\">" + entity.identity() + "</a></li>");
}
out.println("</ul></body></html>");
}
};
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;
} catch (EntityFinderException e) {
throw new ResourceException(e);
}
}
use of org.qi4j.spi.query.EntityFinderException in project qi4j-sdk by Qi4j.
the class EntitiesResource method representRdf.
private Representation representRdf() throws ResourceException {
try {
final Iterable<EntityReference> query = entityFinder.findEntities(EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap());
WriterRepresentation representation = new WriterRepresentation(MediaType.APPLICATION_RDF_XML) {
@Override
public void write(Writer writer) throws IOException {
PrintWriter out = new PrintWriter(writer);
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<rdf:RDF\n" + "\txmlns=\"urn:qi4j:\"\n" + "\txmlns:qi4j=\"http://www.qi4j.org/rdf/model/1.0/\"\n" + "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" + "\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">");
for (EntityReference qualifiedIdentity : query) {
out.println("<qi4j:entity rdf:about=\"" + getRequest().getResourceRef().getPath() + "/" + qualifiedIdentity.identity() + ".rdf\"/>");
}
out.println("</rdf:RDF>");
}
};
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;
} catch (EntityFinderException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
}
}
Aggregations