in src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java [344:424]
protected void orderResource(
final SlingJakartaHttpServletRequest request, final Resource resource, final List<Modification> changes)
throws PersistenceException {
final String command = request.getParameter(SlingPostConstants.RP_ORDER);
if (command == null || command.length() == 0) {
// nothing to do
return;
}
final Resource parent = resource.getParent();
String next = null;
if (command.equals(SlingPostConstants.ORDER_FIRST)) {
next = parent.listChildren().next().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());
Iterator<Resource> iter = parent.listChildren();
while (iter.hasNext()) {
Resource r = iter.next();
if (r.getName().equals(name)) {
if (iter.hasNext()) {
next = iter.next().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 = "";
Iterator<Resource> iter = parent.listChildren();
while (iter.hasNext() && newPos >= 0) {
Resource r = iter.next();
if (r.getName().equals(resource.getName())) {
// if old resource is found before index, need to
// inc index
newPos++;
}
if (newPos == 0) {
next = r.getName();
break;
}
newPos--;
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("provided node ordering command is invalid: " + command);
}
}
if (next != null) {
if (next.equals("")) {
next = null;
}
resource.getResourceResolver().orderBefore(parent, resource.getName(), next);
changes.add(Modification.onOrder(resource.getPath(), next));
if (log.isDebugEnabled()) {
log.debug("Resource {} ordered '{}'", resource.getPath(), command);
}
} else {
throw new IllegalArgumentException("provided resource ordering command is invalid: " + command);
}
}