Renaming a document in velocity does not work

I am trying to rename a document (the reference, not the title) using the refactoring module. Below is my code:

#set ($source = $services.model.resolveDocument(‘spacename.page.subpage.WebHome’))
#set ($renameRequest = $services.refactoring.createRenameRequest($source, ‘sub2’))
#set ($discard = $renameRequest.setAutoRedirect(false))
$services.refactoring.rename($renameRequest)).join()

Instead of renaming subpage, it “creates” another page under subpage and then deletes the parent page.

To be clear, before the rename request, I will have the following page:
spacename.page.subpage

After the rename request, I have this:
spacename.page.subpage ← still exists in the tree layout, but when I click on it, xwiki says the page can’t be found
spacename.page.subpage.sub2

What I wanted was:
spacename.page.sub2

What am I doing wrong?

Hi,

the problem lies in this call:

#set ($renameRequest = $services.refactoring.createRenameRequest($source, ‘sub2’))

When you do that you actually perform a call to create a rename request with the following target reference: new EntityReference('sub2', $source.getType(), $source.getParent()) which in your case creates the reference: spacename.page.subpage.sub2 because the parent of spacename.page.subpage.WebHome is spacename.page.subpage.

So if you want to be sure of the location where to perform your rename I can only suggest you to manually build the target reference and use it:

#set ($source = $services.model.resolveDocument(‘spacename.page.subpage.WebHome’))
#set ($target = $services.model.resolveDocument(‘spacename.page.sub2’))
#set ($renameRequest = $services.refactoring.createRenameRequest($source, $target))

It should do the trick. Also be aware that $services.refactoring.createRenameRequest is deprecated, you should probably use directly $services.refactoring.rename($source, $target).join().

Hope that helps.

Thanks. That worked, with a caveat (see below).

Per your suggestion, I changed to code to get rid of the deprecated API call:

#set ($source = $services.model.resolveDocument(‘spacename.page.subpage.WebHome’))
#set ($target = $services.model.resolveDocument(‘xwiki:spacename.page.sub2’))
$services.refactoring.rename($source, $target).join()

The caveat is that I cannot rename the document through velocity again. For example, if I were to try to name my page back

#set ($source = $services.model.resolveDocument(‘xwiki:spacename.page.sub2.WebHome’))
#set ($target = $services.model.resolveDocument(‘xwiki:spacename.page.subpage’))
$services.refactoring.rename($source, $target).join()

The above call fails (nothing happens). I tried making the page title match the page name and that didn’t help.

If I delete the renamed page (sub2) and create it from scratch, the code to rename it does work again, but again only for the first time. My suspicion is that the velocity rename sequence is somehow tainting the page that then breaks any further renamings through velocity.

Is there anything else I need to do?

So I managed to get it working by putting “WebHome” at the end of the target address.

The next problem I’m now encountering is that if a page has a subpage and I try to rename it, it is doing the same copy-in-place thing I described in my first post.

I saw there is a “setPreserveChildren” for renameRequest API from xwiki’s github, and tried the following:

#set ($discard = $renameRequest.setPreserveChildren(true))

That did not work.

Any thoughts?