0 Votes

Wiki source code of Macros

Last modified by Ryan C on 2025/06/28 00:21

Show last authors
1 {{velocity output='false'}}
2 #*
3 * Sort a list of XWiki objects using a property as comparator.
4 *
5 * @param inputObjects the list of XWiki objects to sort.
6 * @param propertyName the name of the XWiki object property to sort the inputObjects by. Multiple values for the same
7 * property are allowed.
8 * @param outputObjects the list where to append the sorted objects. This list must not be null and should be empty
9 * before calling the macro.
10 *#
11 #macro (sortXWikiObjectsList $inputObjects $propertyName $outputObjects)
12 #set ($objectsMap = {})
13 #foreach ($object in $inputObjects)
14 #set ($key = $object.getValue($propertyName))
15 #set ($sameKeyObjects = $objectsMap.get($key))
16 #if (!$sameKeyObjects)
17 #set ($sameKeyObjects = [])
18 #set ($discard = $objectsMap.put($key, $sameKeyObjects))
19 #end
20 #set ($discard = $sameKeyObjects.add($object))
21 #end
22 #foreach ($key in $properSortTool.sort($objectsMap.keySet()))
23 #set ($sameKeyObjects = $objectsMap.get($key))
24 #foreach ($sameKeyObject in $sameKeyObjects)
25 #set ($discard = $outputObjects.add($sameKeyObject))
26 #end
27 #end
28 #end
29
30 #macro (defineSortTool $sortToolVariable)
31 #set ($checkedExtension = "org.xwiki.commons:xwiki-commons-velocity")
32 ## Because of an API collision, $collectionstool has been replaced by $collectiontool in XCOMMONS-1861
33 #if ($services.extension.core.getCoreExtension($checkedExtension).id.version.compareTo("12.1-SNAPSHOT") >= 0)
34 #setVariable ($sortToolVariable, $collectiontool)
35 ## With the upgrade of Velocity $sorttool has been replaced by $collectionstool (XCOMMONS-1296)
36 #elseif ($services.extension.core.getCoreExtension($checkedExtension).id.version.compareTo("12.0-SNAPSHOT") >= 0)
37 #setVariable ($sortToolVariable, $collectionstool)
38 ## Old API for sorting was $sorttool
39 #else
40 #setVariable ($sortToolVariable, $sorttool)
41 #end
42 #end
43
44 #defineSortTool("$properSortTool")
45 {{/velocity}}