Workflow to Move Nodes and their child to another path [AEM6.5]
Hi Team,
I have a requirement to create a workflow where on executing on give payload it should first unpublish the node and their child nodes and
move the node and it's child node by retaining the node property to another path.
So For example I ran the workflow on path /content/project/en-us/test it should unpublish the node [including the child node] and then move this nodes [including the child node] to specific path i.e. /content/project-archive/en-us/test.
I tried multiple ways to achieve this but not able to get the exact ways to move node and their child nodes before unpublishing and also retaining the property of nodes.
String payloadPath = workflowData.getPayload().toString();
Node payloadNode = session.getNode(payloadPath);
String parentPath = payloadNode.getParent().getPath();
// Check if the payload node is published
boolean isPublished = payloadNode.isNodeType("cq:Page")
? payloadNode.getProperty("cq:lastReplicationAction").getString().equals("Activate")
: true; // assume non-page nodes are published
if (isPublished) {
//unpublish node and it;s child
}
// Get the archive node and its session
String archivePath = "/content/project-archive" + payloadPath.substring("/content".length());
Node archiveNode = null;
if (session.nodeExists(archivePath)) {
archiveNode = session.getNode(archivePath);
} else {
// If the archive node doesn't exist, create it along with its parent nodes
String[] pathParts = archivePath.split("/");
Node currentNode = session.getRootNode();
for (String part : pathParts) {
if (!part.isEmpty() && !currentNode.hasNode(part)) {
currentNode = currentNode.addNode(part);
} else {
currentNode = currentNode.getNode(part);
}
}
archiveNode = currentNode;
session.save();
}
// Move the payload node and its child nodes to the archive location
Workspace workspace = session.getWorkspace();
workspace.move(payloadPath, archiveNode.getPath() + "/" + payloadNode.getName());
session.save();
Could you please guide me the correct and optimize approach for the above requirement.
Thanks
@arunpatidar @kautuk_sahni @briankasingli @lukasz-m @anudeep_garnepudi