When working with content management systems, many times we have requirements to extract content based on certain parameters. A very generic use case might be to update the Stage or Dev systems from production based on the date the last sync had been done or the last modified date of the content on production systems. We can easily solve that problem by writing a small component by first querying the repository and using the Search result to create a package on the server.

Querying the repository

Day OOTB povides components to search the repository e.g. http://localhost:4502/libs/cq/search/content/querydebug.hml , http://localhost:4502/crx/ui/search.jsp.

One can query based on the query parameters or by specifying the XPath/SQL in a query (e.g. search the whole contents changed after April 1, 2010).

Params-based search

On http://localhost:4502/libs/cq/search/content/querydebug.html, type the params in the text area below:

type=cq:Page path=/content daterange.property=jcr:content/cq:lastModified daterange.lowerBound=2010-04-01 orderby=@jcr:content/cq:lastModified orderby.index=true orderby.sort=asc

XPath-based query

On http://localhost:5502/crx/ui/search.jsp, use XPath, as below:

/jcr:root/content//element(*, cq:Page) [ (@jcr:content/cq:lastModified > xs:dateTime('2010-04-01T00:00:00.000+05:30')) ] order by jcr:content/@cq:lastModified

We can also create our own component to search by creating a PredicateGroup based on the properties (parameters) passed and then a builder object (created with the predicateGroup and the available jcr session) can be used to create the query, as shown below:

QueryBuilder builder = resource.getResourceResolver().adaptTo(QueryBuilder.class); Session session = resource.getResourceResolver().adaptTo(Session.class); String queryParam = request.getParameter(“query”); Properties props = new Properties(); props.load(new ByteArrayInputStream(queryParam.getBytes(“ISO-8859-1″))); PredicateGroup root = PredicateConverter.createPredicates(props); // avoid slow //* queries if (!root.isEmpty()) { query = builder.createQuery(root, session); query.setHitsPerPage(0); } SearchResult result = query.getResult();

From the SearchResult object we can extract the Hits (result.getHits) and loop through the list to show the results. We can use the same list to create the packages, as explained next.

Creating packages from the list of nodes

If we have a list of nodes we can easily create a package by creating the filters and using the same for Jcr package defintion:

DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter(); for (Hit hit: hits) { PathFilterSet pathFilterSet = new PathFilterSet(hit.getPath()); filter.add(pathFilterSet); } JcrPackage jcrPackage = packMgr.create(“testGroup”,”testPackage”); JcrPackageDefinition jcrPackageDefinition = jcrPackage.getDefinition(); jcrPackageDefinition.setFilter(filter, false); PrintWriter pkgout = new PrintWriter(System.out); packMgr.assemble(jcrPackage, new DefaultProgressListener(pkgout));

On execution of assembling a package with group name "testGroup" and a package within it, "testPackage" will be created and can be accessed from the package manager.

I have created a sample component by extending the default search component that creates a package with group name testGroup and package name testPackage. If a package with the same name already exists, it ignores the request and doesn't overwrite the existing one. You can get the sample component from here.

Install the package and use it at http://servername:port/apps/packages/createPkgWithQuery/createPkg.html. You can use parameters like the ones below:

type=cq:Page path=/content daterange.property=jcr:content/cq:lastModified daterange.lowerBound=2010-04-01 orderby=@jcr:content/cq:lastModified orderby.index=true orderby.sort=asc