Query Builder | Community
Skip to main content
amoghd64765494
New Participant
February 4, 2016
Solved

Query Builder

  • February 4, 2016
  • 17 replies
  • 5652 views

I want to search pages which are having node 'contactform' and inside that property name called 'id' whose value is empty/null.

Can some one please help me with this to write a query? I struggled much to get this done.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kunal_Gaba_

Here is the JCR SQL2 query for this use case- 

SELECT parent.* FROM [cq:Page] AS parent INNER JOIN [nt:unstructured] AS child ON ISDESCENDANTNODE(child,parent) WHERE ISDESCENDANTNODE(parent, [/content/xxx]) AND  LOCALNAME(child) = 'contactform' AND  child.[id] IS NULL 

17 replies

amoghd64765494
New Participant
February 6, 2016

Thanks Guys..For all the replies. My problem is resolved now smiley

smacdonald2008
New Participant
February 5, 2016

Good call - when setting up the nodes for testing - that is exactly what happened. Modified the Java code in above response. Nice pickup! 

Kunal_Gaba_
New Participant
February 5, 2016

May be while testing you might have added the contactform node directly under cq:Page node rather than under cq:PageContent node - 

/content/geometrixx-outdoors/en/TestPage/contactform

 
smacdonald2008
New Participant
February 5, 2016

Interesting - when i use this:

SELECT parent.* FROM [cq:PageContent]

It give me a 0 result set.

When I use cq:Page - it give me the result set I am looking for. 

Kunal_Gaba_
New Participant
February 5, 2016

Also, It will be good to fire this query on cq:PageContent node type to limit the number of results returned- 

SELECT parent.* FROM [cq:PageContent] AS parent INNER JOIN [nt:unstructured] AS child ON isdescendantnode(child,parent) where isdescendantnode(parent, [/content/geometrixx-outdoors]) AND  LOCALNAME(child) = 'contactform' AND  (child.[id]='' OR child.[id] IS NULL) 
smacdonald2008
New Participant
February 5, 2016

Also - ** When using this query, you need to get results via getRows() instead of getNodes() since queries with joins can eventually return multiple different node types.

Here is the a Java class that returns the data you want.  You have more control using JCR SQL2 than QUeryBuilder with respect to join statements. 

This works....

import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Node;
import javax.jcr.query.Query;
import java.util.List ;
import java.util.ArrayList ;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.TransientRepository;


public class TestJCR {

    
    
    private static ArrayList<String> myList = null; 
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    
    
        try {
             
            String aemUrl = "http://localhost:4502/crx/server" ;
            //Create a connection to the CQ repository running on local host
            Repository repository = JcrUtils.getRepository(aemUrl);

 

       
            javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));
 
            //Obtain the query manager for the session ...
            javax.jcr.query.QueryManager queryManager = session.getWorkspace().getQueryManager();
 
    
            
            String sqlStatement = "SELECT parent.* FROM [cq:PageContent]
AS parent INNER JOIN [nt:unstructured] AS child ON isdescendantnode(child,parent) where isdescendantnode(parent, [/content/geometrixx-outdoors]) AND  LOCALNAME(child) = 'contactform' AND  (child.[id]='' OR child.[id] IS NULL) ";
                   
             javax.jcr.query.Query query = queryManager.createQuery(sqlStatement,"JCR-SQL2");
 
            //Execute the query and get the results ...
            javax.jcr.query.QueryResult result = query.execute();
 
            //Iterate over the nodes in the results ...
            javax.jcr.query.RowIterator nodeIter = result.getRows() ;
 
            long mySize = nodeIter.getSize();
            
            myList = new ArrayList(); //allocate mem to the list
 
            while ( nodeIter.hasNext() ) {
 
                //For each node-- get the path of the node
                String myPath = nodeIter.nextRow().getPath("parent");
                System.out.println("@@@@ THE path is "+myPath);    
 
               
           }

                       
           session.logout();
           
          
 
           System.out.println("@@@@ THE END");
           
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
        

}

Kunal_Gaba_
New Participant
February 5, 2016

One more point to add IS NULL check returns true only if the property does not exists on that node. So if the property does exist on the node and we need to check the value as blank then following condition should be used-

child.[id] IS NULL OR child.[id]=''
smacdonald2008
New Participant
February 5, 2016

I am going to test this and report back!!

Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Participant
February 5, 2016

Here is the JCR SQL2 query for this use case- 

SELECT parent.* FROM [cq:Page] AS parent INNER JOIN [nt:unstructured] AS child ON ISDESCENDANTNODE(child,parent) WHERE ISDESCENDANTNODE(parent, [/content/xxx]) AND  LOCALNAME(child) = 'contactform' AND  child.[id] IS NULL 
smacdonald2008
New Participant
February 5, 2016

If you have hundreds of pages under /content - that is true. If you can narrow your search using a more specific JCR path under /content- it will reduce the result set to iterate over. However - if you need all pages under /content - then you cannot do this. 

Have you tired the suggestion by himanshusinghal?  

Also - what do you need this for. Are you using this for a report of pages and you will only run this every so often?

You are not using this logic in a web page or AEM app are you? I mean the performace will not effect any AEM app. If you need it for a report - i would do JCR SQL - it may be slower - but it will be the best way.