Expand my Community achievements bar.

SOLVED

[AEM6.5] ItemExistException while moving the existing parent and parent of page through workflow

Avatar

Level 6

Hi All, 
I am facing an issue :
I have created a workflow to move/archive a node from one loaction to another location, but in same hirearchy, if hirearchy is not available it will create the same hirearchy.
example if we run workflow for /content/project/website/en-us/parent/child. it will create the same hirearchy under /content/archived and move the "child" node. /content/archived/website/en-us/parent/child
****
The problem I am facing here let say If I pass a payload /content/we-retail/us/en/experience/arctic-surfing-in-lofoten/Child-1 and run workflow it will successfully move node [child] from one loaction to another, and create the same hirearchy under /content/archived/us/en/experience/arctic-surfing-in-lofoten/Child-1

And Now if I try to move the /content/we-retail/us/en/experience page to archive i.e. /content/archived it shows me an exception that ItemExistException.
Because it has a child of child page [i.e. Child-1] already archived so not allowing me to check and move the child of child when we're moving the grand parent page. including the parent and siblin nodes,

tushaar_srivastava_0-1681294911101.png


This is the approach that I written

 

 

@Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        String payload = workItem.getWorkflowData().getPayload().toString();
        ResourceResolver resourceResolver = resourceResolverUtil.getResourceResolver();
        Session session = resourceResolver.adaptTo(Session.class);
        Page rootPage = Objects.requireNonNull(resourceResolver.getResource(payload)).adaptTo(Page.class);
        if (rootPage != null && payload.startsWith("/content/project")) {
            List<String> list = new ArrayList<>();
            list.add(payload);
            Iterator<Page> childPages;
            childPages = rootPage.listChildren(new PublishedPageFilter(), true);
            if (childPages != null) {
                while (childPages.hasNext()) {
                    Page page = childPages.next();
                    list.add(page.getPath());
                }
            }
            String[] paths = list.toArray(new String[0]);
            try {
                Node payloadNode;
                if (session != null) {
                    payloadNode = session.getNode(payload);
                    String payloadName;
                    if (payloadNode != null) {
                        payloadName = payloadNode.getName();
                        if (StringUtils.isNoneBlank(payload)) {
                            String archivePath = StringUtils.substringAfter(payload, "/content/project");
                            archivePath = StringUtils.substringBeforeLast(archivePath, "/");
                            if (StringUtils.isNoneBlank(archivePath)) {
                                archivePath = "/content/archived" + archivePath;
                                Node archived = JcrUtils.getOrCreateByPath(archivePath, "cq:Page", session);
                                //TODO check if payload node exist, if yes iterate all child node including jcr:content and move and remove from existing source.
                                session.move(payloadNode.getPath(), archived.getPath() + "/" + payloadName);
                            }
                        }
                    }
                    session.save();
                }

            } catch (RepositoryException e) {
                LOG.error("RepositoryException exception occurred at :: {}", Arrays.toString(paths));
            } finally {
                if (resourceResolver.isLive()) {
                    resourceResolver.close();
                }
            }
        } else {
            LOG.error("Workflow Terminated ");
        }
    }

 

 

in order to move child nodes when parent node is present,

 

 

String targetPath = archived.getPath() + "/" + payloadName;
if (session.nodeExists(targetPath)) {
    NodeIterator nodesIterator = payloadNode.getNodes();
    while (nodesIterator.hasNext()) {
        Node childNode = nodesIterator.nextNode();
        if (!targetPath.equals(childNode.getPath())) {
            childNodes.add(childNode);
        }
    }
    if (!childNodes.isEmpty()) {
        for (Node childNode : childNodes) {
            session.move(childNode.getPath(), targetPath + DELIMITER_SLASH + childNode.getName());
        }
    }
    session.removeItem(payload);
} else {
    session.move(payloadNode.getPath(), archived.getPath() + DELIMITER_SLASH + payloadName);
    }

 

 


How to handle the situation when we move the parent of parent node and move it's child pages including the child of child pages.


Thanks

@arunpatidar  @lukasz-m  @kautuk_sahni 

Extension of : https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-itemexistexception-...

 

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @kautuk_sahni  and Everyone,
Thank you for you help, through this community Question I got the approach. so bascially when I am moving a node and creating the same hirearchy, through 

 

 Node archived = JcrUtils.getOrCreateByPath(archivePath, "cq:Page", session);

 

So, the same hirearchy is created in the destination path and in order to achive the resolution. we can set the flag if we want to move only Parent or Parent including child.

 

If in first attempt only parent to be moved then we can write a method for furter movement recursively and move page and at the end move the jcr:content of the existing page in destination hirearchy.
Hence recursively we can use the method in order to move parent or parent of parent or any relation within hirearchy.

View solution in original post

3 Replies

Avatar

Administrator

@SivakumarKanoori @Fanindra_Surat @Jagadeesh_Prakash @krati_garg @iamnjain @somen-sarkar @bajancherry @sunil_kumar_ @shelly-goel @Ganthimathi_R @davidjgonzalezzzz @krishna_garikapati  @Shubham_borole @milind_bachani @krishna_sai @manjunathdj  

I kindly request your attention to this AEM question. Your expertise and guidance would be greatly appreciated. Thank you in advance for your support.



Kautuk Sahni

Avatar

Correct answer by
Level 6

Hi @kautuk_sahni  and Everyone,
Thank you for you help, through this community Question I got the approach. so bascially when I am moving a node and creating the same hirearchy, through 

 

 Node archived = JcrUtils.getOrCreateByPath(archivePath, "cq:Page", session);

 

So, the same hirearchy is created in the destination path and in order to achive the resolution. we can set the flag if we want to move only Parent or Parent including child.

 

If in first attempt only parent to be moved then we can write a method for furter movement recursively and move page and at the end move the jcr:content of the existing page in destination hirearchy.
Hence recursively we can use the method in order to move parent or parent of parent or any relation within hirearchy.

Avatar

Administrator

@tushaar_srivastava, Thank you for sharing the solution with the community. This would in posterity. 



Kautuk Sahni