in src/main/java/org/apache/sling/servlets/post/AbstractPostOperation.java [428:511]
protected void orderNode(SlingHttpServletRequest request, Item item, List<Modification> changes)
throws RepositoryException {
String command = request.getParameter(SlingPostConstants.RP_ORDER);
if (command == null || command.length() == 0) {
// nothing to do
return;
}
if (!item.isNode()) {
return;
}
Node parent = item.getParent();
String next = null;
if (command.equals(SlingPostConstants.ORDER_FIRST)) {
next = parent.getNodes().nextNode().getName();
} else if (command.equals(SlingPostConstants.ORDER_LAST)) {
next = "";
} else if (command.startsWith(SlingPostConstants.ORDER_BEFORE)) {
next = command.substring(SlingPostConstants.ORDER_BEFORE.length());
} else if (command.startsWith(SlingPostConstants.ORDER_AFTER)) {
String name = command.substring(SlingPostConstants.ORDER_AFTER.length());
NodeIterator iter = parent.getNodes();
while (iter.hasNext()) {
Node n = iter.nextNode();
if (n.getName().equals(name)) {
if (iter.hasNext()) {
next = iter.nextNode().getName();
} else {
next = "";
}
}
}
} else {
// check for integer
try {
// 01234
// abcde move a -> 2 (above 3)
// bcade move a -> 1 (above 1)
// bacde
int newPos = Integer.parseInt(command);
next = "";
NodeIterator iter = parent.getNodes();
while (iter.hasNext() && newPos >= 0) {
Node n = iter.nextNode();
if (n.getName().equals(item.getName())) {
// if old node is found before index, need to
// inc index
newPos++;
}
if (newPos == 0) {
next = n.getName();
break;
}
newPos--;
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("provided node ordering command is invalid: " + command);
}
}
if (next != null) {
if (next.equals("")) {
next = null;
}
parent.orderBefore(item.getName(), next);
changes.add(Modification.onOrder(item.getPath(), next));
if (log.isDebugEnabled()) {
log.debug("Node {} moved '{}'", item.getPath(), command);
}
} else {
throw new IllegalArgumentException("provided node ordering command is invalid: " + command);
}
}