Expand my Community achievements bar.

Elevate your expertise and be recognized as a true influencer! Nominations for the exclusive Adobe Community Advisor program 2023 are now OPEN.
SOLVED

AEM CRUD operation on the JCR

Avatar

Level 6

I have got my C = Create to work in inserting nodes.

At this point I do not need to Update any nodes.

My node represents a saved data object where I use the nodes properties to store each name value pair from my object. This creates a set of nodes with the correct properties from each object from my ArrayList<object>.

 

In CRXDE lite I run the following SQL2 query and it correctly returns the results of each node I want to read

SELECT * from [nt:unstructured] AS t WHERE ISDESCENDANTNODE([/content/specials])

In my bundle to read these values I use  the following code

        List<Items> specialsList = new ArrayList<Items>();
        // Invoke the adaptTo method to create a Session used to create a
        Map<String, Object> param = new HashMap<String, Object>();

        param.put(ResourceResolverFactory.SUBSERVICE, "readService");

        ResourceResolver resolver = null;
        try {
            resolver = resolverFactory.getServiceResourceResolver(param);
            session = resolver.adaptTo(Session.class);.
            javax.jcr.query.QueryManager queryManager = session.getWorkspace()
                    .getQueryManager();

            String sqlStatement = "SELECT * from [nt:unstructured] AS t WHERE ISDESCENDANTNODE([/content/specials])";

            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.NodeIterator nodeIter = result.getNodes();
            Long length = nodeIter.getSize(); // IN DEBUG MODE I HAVE A NULL VALUE HERE.
            while (nodeIter.hasNext()) {
                // For each node-- create a customer instance
                i++;
                Items item = new Items();
                javax.jcr.Node node = nodeIter.nextNode();
                item.setProductCategory(node.getProperty("productCategory")
                        .getString()); // IN DEBUG THIS IMMEDIATELY JUMPS TO
                item.setProductName(node.getProperty("productName")
                        .getString());
                // AND SO ON FOR EACH PROPERTY

                // Push ITEM to the list
                specialsList.add(item);
            }

            // Log out
            session.logout();

        } catch (Exception e) {
            // LAND HERE STRAIGHT AFTER   ---setProductCategory(node.getProperty("productCategory") AND e IS NULL
        }
        return specialsList;

 

 

Finally, how does one DELETE a set of nodes returned from a SQL2 query.

 

Rgeards

 

Clive Stewart

1 Accepted Solution

Avatar

Correct answer by
Level 10

Here is a great StackOverflow topic that talks about all sorts of JCR SQL2 operations:

http://stackoverflow.com/questions/tagged/jcr-sql2

4 Replies

Avatar

Correct answer by
Level 10

Here is a great StackOverflow topic that talks about all sorts of JCR SQL2 operations:

http://stackoverflow.com/questions/tagged/jcr-sql2

Avatar

Level 6

Hi I looked through them.

 

I could not find a method for deleting.

 

Also I have the following error. I have been adapting code from the following tutorial

http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

String sqlStatement = "SELECT * from [nt:unstructured] AS t WHERE ISDESCENDANTNODE([/content/specials])";

            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.NodeIterator nodeIter = result.getNodes();
            Long length = nodeIter.getSize();
            setDebugMessage(getDebugMessage() + " nodeIter.getSize() = " + length);            
            while (nodeIter.hasNext()) {
                // For each node-- create a customer instance
                Items item = new Items();
                javax.jcr.Node node = nodeIter.nextNode();

 

The length = 20, exactly as many nt:unstructured nodes as I have under /content/specials.

When I get to

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

The loop immediately terminates and finnishes.

I have tested "SELECT * from [nt:unstructured] AS t WHERE ISDESCENDANTNODE([/content/specials])"; on the crxde-lite query form for SQL2 and it returns exactly 20 results.

I really do not understand why. It is as if I had a node Itterator with 0 results.

Please can anyone help.

 

Regards

Clive Stewart

Avatar

Level 6

Thank you for another helpful reply

 

Regards

Clive Stewart

Avatar

Level 6

I solved it.

 

I was using

                javax.jcr.Node node = nodeIter.nextNode();             
                item.setProductCategory(node.getProperty("ProductCategory").getString(););

I changed it to

            javax.jcr.Node node = nodeIter.nextNode();
                String productCategory = node.getProperty("ProductCategory").getString();
                item.setProductCategory(productCategory);