3

I need to copy an AEM JCR node source to destination. The following code is working but if that node already exists in the destination I'm getting an error:

String sourcePath="/content/dam/assets/content";
String destinationPath="/content/dam/Marketing/content";
Session session = resourceResolver.adaptTo(Session.class);
Workspace workspace = session.getWorkspace();
workspace.copy(sourcePath, destinationPath);
session.logout();

But the problem is that the content node already exists inside the folder "Marketing" so that it's only working on the first time. Once the content node is created I'm not able to copy/update the node but I need to replace each time without deleting the source node.

António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
Asna Raheem
  • 55
  • 1
  • 7

2 Answers2

2

If you want to operate on a whole node you can always use workspace move/copy operations (workspace.copy(String srcAbsPath, String destAbsPath) to the destination path, where the destination node should not be present/exist in JCR, if it present/exist you will end up with

javax.jcr.ItemExistsException: /jcr/repository/path/nodepresent

and for the workspace clone (workspace.clone(String srcWorkspace, String srcAbsPath, String destAbsPath, boolean removeExisting) usage you need to operate with two different workspaces, if you use the clone operation with the same workspace you will end up with

javax.jcr.RepositoryException: crx.default: illegal workspace (same as current)

As i can see your requirement is to verify the destination path and update operation, you need to handle it with custom code which can verify destpath and have the NodeIterations and perform update operation.

Also have a look at copy-aem-node-tree-in-jcr which might help for your use case.

VAr
  • 2,551
  • 1
  • 27
  • 40
1

Use clone instead of copy.because clone has a removexisting boolean argument. Refer https://docs.adobe.com/docs/en/spec/jsr170/javadocs/jcr-1.0/javax/jcr/Workspace.html#clone

Thanga
  • 7,811
  • 3
  • 19
  • 38