kwin commented on a change in pull request #16:
URL:
https://github.com/apache/sling-org-apache-sling-servlets-post/pull/16#discussion_r744156929
##########
File path:
src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
##########
@@ -313,6 +311,102 @@ protected String getResourcePath(SlingHttpServletRequest
request) {
return request.getResource().getPath();
}
+
+ /**
+ * Orders the given node according to the specified command. The following
+ * syntax is supported: <xmp> | first | before all child nodes |
before A |
+ * before child node A | after A | after child node A | last | after all
+ * nodes | N | at a specific position, N being an integer </xmp>
+ *
+ * @param request The http request
+ * @param resource the resource to order
+ * @param changes the list of modifications
+ * @throws PersistenceException in case the operation is not successful
+ */
+ protected void orderNode(final SlingHttpServletRequest 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 node 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) {
Review comment:
This logic hasn't changed and should stay in the context of this PR. We
can do cleanup in a separate issue and commit. IMHO currently just using the
parameter `:order` without a value leads to reordering the node to the end of
the sibling list (although undocumented)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]