Query Builder | Community
Skip to main content
amoghd64765494
Level 3
February 4, 2016
Solved

Query Builder

  • February 4, 2016
  • 17 replies
  • 5663 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

Kunal_Gaba_
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
Level 10
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_
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
Level 10
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_
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
Level 10
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! 

amoghd64765494
Level 3
February 6, 2016

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