Search in sources :

Example 1 with EntityFinderException

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;
}
Also used : SQLException(java.sql.SQLException) EntityFinderException(org.qi4j.spi.query.EntityFinderException) Connection(java.sql.Connection)

Example 2 with EntityFinderException

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);
    }
}
Also used : OrderBy(org.qi4j.api.query.grammar.OrderBy) NamedList(org.apache.solr.common.util.NamedList) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrServer(org.apache.solr.client.solrj.SolrServer) QuerySpecification(org.qi4j.api.query.grammar.QuerySpecification) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) EntityFinderException(org.qi4j.spi.query.EntityFinderException) EntityReference(org.qi4j.api.entity.EntityReference) SolrParams(org.apache.solr.common.params.SolrParams)

Example 3 with EntityFinderException

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);
    }
}
Also used : WriterRepresentation(org.restlet.representation.WriterRepresentation) EntityFinderException(org.qi4j.spi.query.EntityFinderException) EntityReference(org.qi4j.api.entity.EntityReference) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) OutputRepresentation(org.restlet.representation.OutputRepresentation) Representation(org.restlet.representation.Representation) ResourceException(org.restlet.resource.ResourceException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 4 with EntityFinderException

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);
    }
}
Also used : WriterRepresentation(org.restlet.representation.WriterRepresentation) EntityFinderException(org.qi4j.spi.query.EntityFinderException) EntityReference(org.qi4j.api.entity.EntityReference) ResourceException(org.restlet.resource.ResourceException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

EntityFinderException (org.qi4j.spi.query.EntityFinderException)4 EntityReference (org.qi4j.api.entity.EntityReference)3 PrintWriter (java.io.PrintWriter)2 Writer (java.io.Writer)2 WriterRepresentation (org.restlet.representation.WriterRepresentation)2 ResourceException (org.restlet.resource.ResourceException)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 SolrServer (org.apache.solr.client.solrj.SolrServer)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 SolrParams (org.apache.solr.common.params.SolrParams)1 NamedList (org.apache.solr.common.util.NamedList)1 OrderBy (org.qi4j.api.query.grammar.OrderBy)1 QuerySpecification (org.qi4j.api.query.grammar.QuerySpecification)1 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)1 OutputRepresentation (org.restlet.representation.OutputRepresentation)1