2 Replies Latest reply on Feb 17, 2013 6:05 PM by Sham HC

    List Component - Query

    cqlearner Level 1

      Hi all,

       

      I have a list component and I have set up initial query in advance search, which generates the listing of the articles.

       

      Now I am planning to add a search box and upon providing the new search text I would like to search again and utilizing same list component I would like to display new result list.

       

      I looked a the init.jsp and found the place where I think it executes the initial query?

      I tried to override source and query string but it is not taking an effect.  Also I am not able to find the jar for "com.day.cq.wcm.foundation.List", I wanted to decompile and wanted to see how querys are executed.

       

      List list = new List(slingRequest, new PageFilter());

       

      list.setQuery("Test");

      list.setSource("search");

       

      Any pointers would be helpful!

       

       

      Thanks in advance.

        • 1. Re: List Component - Query
          smacdonald2008 Adobe Employee

          I am workiing on a similiar workflow for an upcoming artilce that dicusses how to query the AEM CQ JCR: http://scottsdigitalcommunity.blogspot.ca/2013/02/querying-adobe-experience-manager-data.h tml .

           

          This sample app queries the JCR for customer data, supports filtering, and displays the result set in a grid contol located in the client:

           

          datagrid2.png

           

          Although this article will not be posted until Feb 23 --I can provide a few pointers. Like most workflows in computer science -- you can  perform a task in more than 1 way. I find that you get more power when you wrap the JCR Query Manager API within an OSGi component and develop the service to expose an operation that queries the JCR and returns the result set.

           

          You can return the result set to the client and display the data in a web page - similiar to the pic above. Of course -- you can do the same workflow using sling -- its a matter of preferece IMHO. I prefer to develop OSGi services to do the heavy lifting and let the client call the service and display the data.

           

          For example - to call the OSGI service from a client:

           

          com.adobe.aem.CustomerService cs = sling.getService(com.adobe.aem.CustomerService .class);

          String XML = cs.getCustomerData(filter) ;

           

          In this workflow -- the query results are stored in an XML DOM and returned to the client -- where the data is parsed and displayed. The complete artilce will be posted next Fri.

           

          Here is a snippet of the backend Java logic that is wrapped in the OSGi and queries the JCR that is called from the client:

           

          public Document getCustomerData(String filter) {

           

                              Customer cust = null;

           

                               List<Customer> custList = new ArrayList<Customer>();

                              try {

           

           

                                 Session session = this.repository.loginAdministrative(null);

           

                                 // Obtain the query manager for the session ...

                           javax.jcr.query.QueryManager queryManager = session.getWorkspace().getQueryManager();

           

                           //Setup the query based on user input -- 3 options are   All Customers,

                           //Active Customer or Past Customer

                           String sqlStatement="";

           

                                     //Setup the query to get all corresponding customer nodes -- which are of node type nt:unstructured

                           if (filter.equals("All Customers"))

                                     sqlStatement = "SELECT * FROM [nt:unstructured] WHERE CONTAINS(desc, 'Customer')";

                           else if(filter.equals("Active Customer"))

                                               sqlStatement = "SELECT * FROM [nt:unstructured] WHERE CONTAINS(desc, 'Active')";

                           else if(filter.equals("Past Customer"))

                                              sqlStatement = "SELECT * FROM [nt:unstructured] WHERE CONTAINS(desc, 'Past')";

           

                   //Execute the query and get the results ...

                           javax.jcr.query.Query query = queryManager.createQuery(sqlStatement,"JCR-SQL2");

           

                           javax.jcr.query.QueryResult result = query.execute();

           

                           //Iterate over the nodes in the results ...

                           javax.jcr.NodeIterator nodeIter = result.getNodes();

           

                           while ( nodeIter.hasNext() ) {

           

           

                                                    //For each node-- create a customer instance

                                                    cust = new Customer();

           

                                                    javax.jcr.Node node = nodeIter.nextNode();

           

                                                    //Set all Customer object fields

                                                    cust.setCustFirst(node.getProperty("firstName").getString());

                                                    cust.setCustLast(node.getProperty("lastName").getString());

                                                    cust.setCustAddress(node.getProperty("address").getString());

                                                    cust.setCustDescription(node.getProperty("desc").getString());

           

                                                    //Push Customer to the list

                                                    custList.add(cust);

           

                          }

           

                           // Save the session changes and log out

                                 session.save();

                                 session.logout();

           

               //Return the customer data within an XML DOM

                                 return toXml(custList);

           

           

           

                              }

                        catch(Exception e)

                        {

                                   e.printStackTrace();

                        }

                        return null;

                    }

           

           

           

           

          Hope this helps..

          • 2. Re: List Component - Query
            Sham HC Level 7

            The source should be at   /libs/foundation/src/impl/src/main/java/com/day/cq/wcm/foundation/List.java