How-to execute a query from an ESP script?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How-to execute a query from an ESP script?

gertv
L.S.,

The subject basically covers the entire question: I would like to
execute a JCR query from within an ESP page to get a set of nodes in the
order I want (descending by date created).  How can I do this?

Thanks for any help,

Gert

Reply | Threaded
Open this post in threaded view
|

Re: How-to execute a query from an ESP script?

Felix Meschberger-2
Hi Gert,

Am Montag, den 14.04.2008, 16:22 +0200 schrieb Gert Vanthienen:
> L.S.,
>
> The subject basically covers the entire question: I would like to
> execute a JCR query from within an ESP page to get a set of nodes in the
> order I want (descending by date created).  How can I do this?

I suggest you use the ResolverResolver.findResources(String query,
String language) method. This returns an Iterator<Resource> according to
the query. In ESP this would be something like:

        <%
        var iter = request.resourceResolver.findResources("..xpath..",
                            Packages.javax.jcr.query.Query.XPATH);
        while (iter.hasNext()) {
           var resource = iter.next();
           .. do whatever you like ..
        }
        %>

If are more interested in the result rows than in the resources found,
you would call the ResourceResolver.queryResources(String query, String
language) method which returns you an Iteratory<Map<String, Object>>,
that is an iterator on the rows represented as maps indexed by column
names.

Hope this helps.

Regards
Felix


Reply | Threaded
Open this post in threaded view
|

Re: How-to execute a query from an ESP script?

Bertrand Delacretaz
In reply to this post by gertv
Hi Gert,

On Mon, Apr 14, 2008 at 4:22 PM, Gert Vanthienen
<[hidden email]> wrote:
> ... The subject basically covers the entire question: I would like to execute a
> JCR query from within an ESP page to get a set of nodes in the order I want
> (descending by date created).  How can I do this?...

We don't have an example yet, but that would look something like

 var statement = ...jackrabbit query statement
 var language = ...jackrabbit query language selection

 var resolver = request.getResourceResolver();
 results = resolver.queryResources(statement, language);

 while(results.hasNext()) {
    count++;
    var row = results.next();
    var path = row.get('jcr:path');
    var title = row.get("title");
...
  }

-Bertrand

Reply | Threaded
Open this post in threaded view
|

Re: How-to execute a query from an ESP script?

David Nuescheler-3
In reply to this post by Felix Meschberger-2
Hi Guys,

here is my take (using JCR of course ;) ):

<%
var qm=currentNode.getSession().getWorkspace().getQueryManager();
var q=qm.createQuery("SELECT * FROM nt:resource ORDER BY
jcr:lastModified", "sql");
var res=q.execute().getNodes();

while (res.hasNext) {
        hit=res.nextNode();
        %><%= hit.getPath() %><br><%
}
%>

However, I think this really should be a lot simpler...
looking the the Sling.getContent() on the client side, I think for
javascript on the client
side we should have something like a Sling.query() that returns a
collection of nodes.
Since think that we in the long run it would be great to offer scripts
on the server
side the same level of convience i think the resulting script (both on
the client
and on the server) should look something like this.

<%
var res=Sling.query("SELECT * FROM nt:resource ORDER BY jcr:lastModified");

for (var a in res) {
        hit=res[a];
        %><%= hit.getPath() %><br><%
}
%>


regards,
david

Reply | Threaded
Open this post in threaded view
|

Re: How-to execute a query from an ESP script?

Felix Meschberger-2
Hi,

Am Montag, den 14.04.2008, 16:59 +0200 schrieb David Nuescheler:

> Hi Guys,
>
> here is my take (using JCR of course ;) ):
>
> <%
> var qm=currentNode.getSession().getWorkspace().getQueryManager();
> var q=qm.createQuery("SELECT * FROM nt:resource ORDER BY
> jcr:lastModified", "sql");
> var res=q.execute().getNodes();
>
> while (res.hasNext) {
> hit=res.nextNode();
> %><%= hit.getPath() %><br><%
> }
> %>
>
> However, I think this really should be a lot simpler...

This is why we have ResourceResolver.findResources()
and .queryResources() ;-)

Regards
Felix


Reply | Threaded
Open this post in threaded view
|

Re: How-to execute a query from an ESP script?

David Nuescheler-3
>  This is why we have ResourceResolver.findResources()
>  and .queryResources() ;-)
I think that I agree that this may be functionally simpler, however I think that
it is not exactly "intuitive" (which for me makes up the the biggest
part of "Simple").

If I want to "execute a JCR query" as Gert put it I don't understand what
business I have with a thing called the "ResourceResolver".
If I have a really simple problem, I think we should offer a really
simple solution with having some first understand the internal architecture
of Sling.

regards,
david



--
Visit: http://dev.day.com/ - Day JCR Cup 08 - Win a MacBook Pro