Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Errors on saving to JCR

Avatar

Level 6

In my local jcr I have

content/item

In a bundle I have the following code fragment : On saving or creating nodes I get the following results from the string I created, debugmessage

 

debugMessage = Server Response = line 127:: got root node doesSpecialsExist it.hasNext() = true line142:: content/specials does exist so specialsRoot = content.getNode("specials"); Just finnished session.save();1 line 127:: got root node doesSpecialsExist it.hasNext() = true line142:: content/specials does exist so specialsRoot = content.getNode("specials"); line 152 :: Inside for (Items item : itemsArrayList) Failed on specialsRoot.addNode Failed to resolve path TimeStamp1417390151359Alla07\12\201 /content/specials line 152 :: Inside for (Items item : itemsArrayList) Failed on specialsRoot.addNode Failed to resolve path TimeStamp1417390151359Alla07\12/specials Just

    private int saveSpecials() {
        Map<String, Object> param = new HashMap<String, Object>();

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

        ResourceResolver resolver = null;
        int specialsId = 0;
        try {
            resolver = resolverFactory.getServiceResourceResolver(param);
            // Create a node that represents the root node
            session = resolver.adaptTo(Session.class);
            javax.jcr.Node root = session.getRootNode();
            setDebugMessage(getDebugMessage() + " saveSpecials: got root node ");
            // Get the content node in the JCR
            javax.jcr.Node content = root.getNode("content");
            javax.jcr.Node specialsRoot = null;
            // Determine if the content/specials node exists
            int specialsRec = doesSpecialsExist(content);
            // -1 means that content/customer does not exist
            if (specialsRec == -1) {
                // content/specials does not exist -- create it
                specialsRoot = content.addNode("\\specials", "nt:unstructured");
                setDebugMessage(getDebugMessage()
                        + " content/specials does not exist so specialsRoot = content.addNode(\"specials\", \"nt:unstructured\");   ");
            } else {
                // content/specials does exist -- retrieve it
                specialsRoot = content.getNode("\\specials");
                setDebugMessage(getDebugMessage()
                        + " content/specials does exist so specialsRoot = content.getNode(\"specials\");");
            }
            specialsId = specialsRec + 1;

            // Store content from the client JSP in the JCR
            Long timeStamp = System.currentTimeMillis();
            String strTimeStamp = Long.toString(timeStamp);

            for (Items item : itemsArrayList) {
                setDebugMessage(getDebugMessage() + " Inside for (Items item : itemsArrayList)");
                String productCategory = item.getProductCategory();
                String productDescription = item.getProductDescription();
                String productExpiryDate = item.getProductExpiryDate();
                String productImage = item.getProductImage();
                String productName = item.getProductName();
                String productPrice = item.getProductPrice();
                String terms = item.getTerms();
                try {
                    javax.jcr.Node specialsNode = specialsRoot.addNode(
                            "TimeStamp" + strTimeStamp + productCategory
                                    + productDescription + productExpiryDate
                                    + productImage + productName + productPrice
                                    + terms, "nt:unstructured");
                    // make sure name of node is unique
                    specialsNode.setProperty("id", specialsId);
                    specialsNode.setProperty("TimeStamp", strTimeStamp);
                    specialsNode
                            .setProperty("ProductCategory", productCategory);
                    specialsNode.setProperty("ProductDescription",
                            productDescription);
                    specialsNode.setProperty("ProductExpiryDate",
                            productExpiryDate);
                    specialsNode.setProperty("ProductImage", productImage);
                    specialsNode.setProperty("ProductName", productName);
                    specialsNode.setProperty("ProductPrice", productPrice);
                    specialsNode.setProperty("Terms", terms);
                    setDebugMessage(getDebugMessage()
                            + " specialsRoot.addNode(\"TimeStamp"
                            + strTimeStamp + productCategory
                            + productDescription + productExpiryDate
                            + productImage + productName + productPrice + terms
                            + ", nt:unstructured\");");
                } catch (Exception e) {
                    setDebugMessage(getDebugMessage()
                            + " Failed on specialsRoot.addNode "
                            + e.getMessage());
                }

            }
            // Save the session changes and log out
            session.save();
            setDebugMessage(getDebugMessage()
                    + " Just finnished session.save();");
            session.logout();

        } catch (LoginException e) {
            setDebugMessage(" LoginException : " + getDebugMessage()
                    + e.getMessage());
        } catch (RepositoryException e1) {
            setDebugMessage(getDebugMessage() + " PersistenceException : "
                    + e1.getMessage() + "  :::  ");
        }
        setDebugMessage(getDebugMessage() + specialsId);
        return specialsId;
    }

    private int doesSpecialsExist(javax.jcr.Node content) {
        try {
            int index = 0;
            int childRecs = 0;

            java.lang.Iterable<javax.jcr.Node> custNode = JcrUtils
                    .getChildNodes(content, "specials");
            Iterator it = custNode.iterator();

            // only going to be 1 content/specials node if it exists
            if (it.hasNext()) {
                setDebugMessage(getDebugMessage()
                        + " doesSpecialsExist it.hasNext() = true");
                // Count the number of child nodes to specials
                javax.jcr.Node specialsRoot = content.getNode("specials");
                Iterable itSpecials = JcrUtils.getChildNodes(specialsRoot);
                Iterator childNodeIt = itSpecials.iterator();

                // Count the number of customer child nodes
                while (childNodeIt.hasNext()) {
                    childRecs++;
                    childNodeIt.next();
                }
                return childRecs;
            } else
                setDebugMessage(getDebugMessage()
                        + " content/specials does not exist");
            return -1; // content/specials does not exist

        } catch (Exception e) {
            setDebugMessage(getDebugMessage()
                    + " Failed on readService "
                    + e.getMessage());
        }
        return 0;
    }

1 Accepted Solution

Avatar

Correct answer by
Level 10

Looking at your code -- the issue looks like its around naming of the node:

TimeStamp1417390151359Alla07\12\201

In the persisting article - that you based this one:

Persisting CQ data in the Java Content Repository

we named the nodes based on simple string concatenations. For example, firstname, last name, etc. 

We do not use any other chars in the node name.  Try renaming the nodes with simple strings. See:

http://stackoverflow.com/questions/672551/jcr-170-data-modeling-node-names

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Looking at your code -- the issue looks like its around naming of the node:

TimeStamp1417390151359Alla07\12\201

In the persisting article - that you based this one:

Persisting CQ data in the Java Content Repository

we named the nodes based on simple string concatenations. For example, firstname, last name, etc. 

We do not use any other chars in the node name.  Try renaming the nodes with simple strings. See:

http://stackoverflow.com/questions/672551/jcr-170-data-modeling-node-names

Avatar

Level 6

Thank you,

Your advice was perfect. Thank you for the link about naming.

 

Best regards

 

Clive