Rename Object

Last modified by Vincent Massol on 2021/03/18 11:28

cogFix Objects after renaming a page containing a Class
Type
Category
Developed by

Vincent Massol

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Right now when you rename a page defining a Class (a.k.a XClass) then all pages having an Object (a.k.a XObject) of this Class are broken since the Objects are not renamed automatically (See http://jira.xwiki.org/browse/XWIKI-13721).

In the meantime here's a workaround script (modify the first 2 lines to suite your needs):

{{velocity}}
#set ($originalXClass = 'ReleaseNotes.Code.ChangesClass')
#set ($newXClass = 'ReleaseNotes2.Code.ChangesClass')
#foreach ($item in $services.query.xwql("from doc.object($originalXClass) as changes").execute())
  * Processing [$item]...
 #set ($itemDoc = $xwiki.getDocument($item))
 #set ($originalXObject = $itemDoc.getObject($originalXClass))
 #if ("$!itemDoc.getObject($newXClass)" == '')
   #set ($newXobject = $itemDoc.newObject($newXClass))
   #foreach ($property in $originalXObject.getProperties())
     #set ($discard = $newXobject.set($property.name, $property.value))
      ** Setting property [$property.name] to [{{{$property.value}}}]...
   #end
   #set ($discard = $itemDoc.removeObject($originalXObject))
   #set ($discard = $itemDoc.save("Renaming [$originalXClass] object into [$newXClass]", true))
 #else
    ** New object already exist, doing nothing...
 #end
#end
{{/velocity}}
Tags:
     

Get Connected