|
9 | 9 | import java.util.function.Function; |
10 | 10 | import java.util.stream.Stream; |
11 | 11 | import javax.jcr.Node; |
| 12 | +import javax.jcr.NodeIterator; |
12 | 13 | import javax.jcr.Property; |
13 | 14 | import javax.jcr.RepositoryException; |
14 | 15 | import org.apache.commons.io.IOUtils; |
@@ -403,6 +404,50 @@ public RepoResource moveInPlace(RepoResource target, boolean replace) { |
403 | 404 | return target; |
404 | 405 | } |
405 | 406 |
|
| 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 | + |
406 | 451 | public RepoResource parent() { |
407 | 452 | String parentPath = parentPath(); |
408 | 453 | if (parentPath == null) { |
|
0 commit comments