-1

I want to copy a page to a target path as follows:

Page page = "pagePath";  
PageManager pageManager = getResourceResolver().adaptTo(PageManager.class);
pageManager.copy(pagePath, pagePath + "/target/newPage", null, true, false);

This works fine, if the page "target" exists (i.e. the would-be parent of the new copy). If this page doesn't exist however, PageManager will throw an Exception.

How can I tell the PageManager that it should create target if it doesn't exist already? (I.e. similar to the -p flag on the unix mkdir programme.)

anotherdave
  • 6,656
  • 4
  • 34
  • 65
Max_Salah
  • 2,407
  • 11
  • 39
  • 68

1 Answers1

1

How would PageManager know what content to use to create "target"?

Why not just check if target exists & create it yourself if not, with whichever template you want to use?

Page page = "pagePath";  
ResourceResolver resourceResolver = getResourceResolver();
Resource parent = resourceResolver.resolve(pagePath + "/target");
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

if (parent.getResourceType().equals(Resource.RESOURCE_TYPE_NON_EXISTING) {
    pageManager.create(pagePath, "target", "SOME_TEMPLATE_NAME", "SOME_TITLE");
}

pageManager.copy(pagePath, pagePath + "/target/newPage", null, true, false);
anotherdave
  • 6,656
  • 4
  • 34
  • 65