Home > Blog > [Q&A Friday] SELECT Clause in Nuxeo NXQL Queries - Part 2

[Q&A Friday] SELECT Clause in Nuxeo NXQL Queries – Part 2

SELECT clause in NXQL queries - part 2

SELECT clause in NXQL queries – part 2

Hi everyone, here’s a follow up on the last Q&A post about select clause in NXQL. I’m going to implement an operation that uses CoreSession.queryAndFetch and show you how to manipulate the result in Studio using MVEL scripting.

First, create an operation. This is quite simple using the operation wizard with Nuxeo IDE. I’ve chosen no input and output. Yes, the operation won’t return anything, instead it will assign the search result to a context variable. Take a look at the code:

[java]
package org.nuxeo.sample;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.nuxeo.ecm.automation.OperationContext;
import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.IterableQueryResult;
import org.nuxeo.ecm.core.query.sql.NXQL;

/**
* @author ldoguin
*/
@Operation(id=QueryAndFetch.ID, category=Constants.CAT_DOCUMENT, label="QueryAndFetch", description="Query example using queryAndFetch to have specific select clause.")
public class QueryAndFetch {

public static final String ID = "QueryAndFetch";

@Context
CoreSession coreSession;

@Context
OperationContext operationContext;

@Param(name = "query", required = true)
String query;

/*
* OperationContext variable name.
*/
@Param(name = "name", required = true)
String name;

@Param(name = "language", required = false, widget = Constants.W_OPTION, values = {
NXQL.NXQL, "CMISQL" })
protected String lang = NXQL.NXQL;

@OperationMethod
public void run() throws ClientException {
// Simply execute the query
IterableQueryResult itqr = coreSession.queryAndFetch(query, lang, null);
List<Map<String, Serializable>> results = new ArrayList<Map<String, Serializable>>();
Iterator<Map<String, Serializable>> it = itqr.iterator();
// put the result in a simpler object like a list
while (it.hasNext()) {
results.add(it.next());
}
// put the result in the operation context
operationContext.put(name, results);
// always close the iterableQueryResult
itqr.close();
}

}
[/java]

This is a rough implementation. You might want to add pagination or at least a limit for the query result. Now, I’m going to write a simple chain that will display the query result as an information message on the web app. I know it’s not particularly useful, but it makes a good example :)

[xml]
<extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent" point="chains">
<chain id="testQueryAndFetch">
<operation id="QueryAndFetch">
<param type="string" name="query">select dc:title From Document</param>
<param type="string" name="name">queryResult</param>
<param type="string" name="language">NXQL</param>
</operation>
<operation id="Context.RunScript">
<param type="string" name="script">
String titles = ‘titles: ‘;
foreach (res : Context[‘queryResult’]) {
titles = titles + res.get(‘dc:title’)
}
Context[‘titles’] = titles;
</param>
</operation>
<operation id="Seam.AddInfoMessage">
<param type="string" name="message">expr:Context["titles"]</param>
</operation>
</chain>
</extension>
[/xml]

The most important operation here is Context.RunScript. It first instantiates a titles String, then loop into our queryResult variable using a forech. In this loop, we simply concatenate the document titles with the titles object. Then, once it’s done, we assign it to a new variable using Context[‘titles’] = titles;. It’s then easy to retrieve in the following operations. As you can see MVEL scripting can be quite powerful :)

Nuxeo Document Management
Document
Management
with the
Nuxeo Platform Watch the webinar >
img
img
img
img
  • http://www.cuclapita2.com i thought about this

    I just want to tell you that I am just newbie to blogging and honestly savored you’re blog. Very likely I’m planning to bookmark your site . You definitely come with fabulous writings. Thank you for sharing your web site.

  • http://www.2014PandaSeo.com/ seo plugin

    Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there’s even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That’s right AUTOMATICALLY, just watch this 4minute video for more information at. Seo Plugin