Expand my Community achievements bar.

[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-...

 

0 Replies
page footer