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

Component Cleanup BEFORE Submit

Avatar

Level 5

We have a component that allows our authors to dynamically select the number of columns and the associated widths.  Example 12, 7-5, 6-6, 4-4-4, 3-5-4, etc

Once they have selected this click save, they can add components into each of the corresponding parsys.

If the user changes their selection from 4-4-4 (3 columns) to 12 (1 column), nothing "cleans up" the orphaned nodes.

Desired Result Example:

4-4-4 - 3 Columns

columnControl

    col0

        componenta

    col1

        componentb

    col2

        component

12 - 1 column


columnControl


    col0


        componenta

Unfortunately, this is the actual result .... meaning col1 & col2 are orphans and NOT removed from the parent nodes.

Actual Result Example:

12 - 1 Column

columnControl


    col0


        componenta


    col1


        componentb


    col2


        componentc

 

 

Is there something I can do to remove the orphaned content BEFORE the new structure is saved?

Thank you for your assistance.

-Dean

1 Accepted Solution

Avatar

Correct answer by
Level 5

Here is what I added in the activate() method to resolve:

        Node currentNode = getResource().adaptTo(Node.class);
        NodeIterator ni = currentNode.getNodes();
       
        if (columns != null) {
         int columnsSize = columns.size();
         int nodeCounter = 0;
         boolean changesMade = false;
         if (ni != null) {
                while (ni.hasNext()) {
                 Node childNode = ni.nextNode();
                 if (childNode != null) {
                     String childNodeName = childNode.getName();
                     if (childNodeName != null && childNodeName.startsWith("abc")) {
                         nodeCounter++;
                         if (nodeCounter > columnsSize) {
                          childNode.remove();
                          changesMade = true;
                         }
                     }
                 }
                }
                if (changesMade) {
                 Session session = currentNode.getSession();
                 if (session != null) {
                        session.save();
                 }
                }
         }
        }
 

View solution in original post

4 Replies

Avatar

Level 10

What are you using for front end - HTL ?

Avatar

Level 5

Yes, we are using html & sling (HTL)

Avatar

Correct answer by
Level 5

Here is what I added in the activate() method to resolve:

        Node currentNode = getResource().adaptTo(Node.class);
        NodeIterator ni = currentNode.getNodes();
       
        if (columns != null) {
         int columnsSize = columns.size();
         int nodeCounter = 0;
         boolean changesMade = false;
         if (ni != null) {
                while (ni.hasNext()) {
                 Node childNode = ni.nextNode();
                 if (childNode != null) {
                     String childNodeName = childNode.getName();
                     if (childNodeName != null && childNodeName.startsWith("abc")) {
                         nodeCounter++;
                         if (nodeCounter > columnsSize) {
                          childNode.remove();
                          changesMade = true;
                         }
                     }
                 }
                }
                if (changesMade) {
                 Session session = currentNode.getSession();
                 if (session != null) {
                        session.save();
                 }
                }
         }
        }
 

Avatar

Level 10

Nice logic - thanks for sharing. This would make a nice article.