XWiki Object Sort

Last modified by crocket on 2021/03/17 21:47

cogIt sorts XWiki Objects(com.xpn.xwiki.api.Object) by their properties.
TypeJAR
Category
Developed by

xwiki:XWiki.crocket

Rating
0 Votes
LicenseDo What The Fuck You Want To Public License 2
Compatibility

It was tested only on 4.3-milestone-1, but it is high likely to be compatible with XWiki > 2.3 since it uses only XWiki components and ScriptService.

Description

It has a component, ObjectSort(org.xwiki.contrib.objectsort.ObjectSort) that can be injected into other components.
You can inject ObjectSort as follows

import org.xwiki.contrib.objectsort.ObjectSort;

...
...

@Inject
private ObjectSort objectSort;

ObjectSort has five methods that help sort XWiki Objects(com.xpn.xwiki.api.Object).

  1. void sort(List<Object> list, String prop) throws Exception;
    • It sorts XWiki Objects by one of their properties. The 3 other methods use this method.

    • The valid syntax for prop is "property[:asc|:desc]" where the part in [] is optional.
      • :asc = ascending order. If you don't specify :asc or :desc, :asc is assumed.
      • :desc = descending order.
      • It throws an exception when you pass a prop value that doesn't conform to the property syntax.
  2. List<Object> sortCopy(List<Object> list, String prop) throws Exception;
    • It returns a new list of XWiki Objects sorted by one of their properties
  3. void sortByProps(List<Object> list, List<String> props) throws Exception;
    • It sorts XWiki Objects by one or more of their properties. It basically calls sort(List<Object> list, String prop) for each String object of props in the reverse order. If it didn't invoke sort for each String in props in the reverse order, users wouldn't get what they wanted.
  4. List<Object> sortCopyByProps(List<Object> list, List<String> props) throws Exception;
    • It returns a new list of XWiki Objects sorted by their properties.
  5. Comparator<Object> newComparator(String prop);
    • It returns a new Comparator that compares an XWiki object by one of its properties. sort(List<Object> list, String prop) uses this comparator internally to sort XWiki Objects by one of their properties. If you want to use this comparator directly, invoke this method.

It also has a script service named "objectsort" which has only one method.

  1. ObjectSort getInstance()
    • This method returns the one and only one instance of ObjectSort.

You can invoke methods of ObjecSort as below in a velocity script template.

  • $services.objectsort.getInstance().sort($doc.getObjects("Class"), "port")
  • $services.objectsort.getInstance().sort($doc.getObjects("Class"), "port:asc")
  • $services.objectsort.getInstance().sort($doc.getObjects("Class"), "port:desc")
  • $services.objectsort.getInstance().sortByProps($doc.getObjects("Class"), ["port", "day:asc", "hour:desc"])

Prerequisites & Installation Instructions

Just put the jar file( xwiki-contrib-objectsort-4.3-milestone-1.jar ) in $CATALINA_BASE/webapps/xwiki/WEB-INF/lib and reload xwiki.

Tags:
    

Get Connected