Skip to content

Commit 9cfe51e

Browse files
Repo resource order before
1 parent 58afb36 commit 9cfe51e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

core/src/main/java/dev/vml/es/acm/core/repo/RepoResource.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.function.Function;
1010
import java.util.stream.Stream;
1111
import javax.jcr.Node;
12+
import javax.jcr.NodeIterator;
1213
import javax.jcr.Property;
1314
import javax.jcr.RepositoryException;
1415
import org.apache.commons.io.IOUtils;
@@ -403,6 +404,50 @@ public RepoResource moveInPlace(RepoResource target, boolean replace) {
403404
return target;
404405
}
405406

407+
// AEM 6.5.0 has no 'resourceResolver.orderBefore' so adding own based on:
408+
// https://github.com/apache/sling-org-apache-sling-jcr-resource/commit/3b8f01d226124417bdfdba4ca2086114a73a7c5d
409+
public boolean orderBefore(String siblingName) {
410+
Node parentNode = parent().requireNode();
411+
String name = getName();
412+
try {
413+
long existingNodePosition = -1;
414+
long siblingNodePosition = -1;
415+
long index = 0;
416+
417+
NodeIterator nodeIterator = parentNode.getNodes();
418+
while (nodeIterator.hasNext()) {
419+
Node childNode = nodeIterator.nextNode();
420+
String childName = childNode.getName();
421+
422+
if (childName.equals(name)) {
423+
existingNodePosition = index;
424+
}
425+
if (siblingName != null && childName.equals(siblingName)) {
426+
siblingNodePosition = index;
427+
} else if (siblingName == null && childName.equals(name)) {
428+
if (existingNodePosition == nodeIterator.getSize() - 1) {
429+
return false;
430+
}
431+
}
432+
index++;
433+
}
434+
435+
if (siblingName != null
436+
&& existingNodePosition >= 0
437+
&& siblingNodePosition >= 0
438+
&& existingNodePosition == siblingNodePosition - 1) {
439+
return false;
440+
}
441+
442+
parentNode.orderBefore(name, siblingName);
443+
repo.commit(String.format("reordering resource '%s' before '%s'", path, siblingName));
444+
repo.getLogger().info("Reordered resource '{}' before '{}'", path, siblingName);
445+
return true;
446+
} catch (RepositoryException e) {
447+
throw new RepoException(String.format("Cannot reorder resource '%s' before '%s'", path, siblingName), e);
448+
}
449+
}
450+
406451
public RepoResource parent() {
407452
String parentPath = parentPath();
408453
if (parentPath == null) {

0 commit comments

Comments
 (0)