createComparator(HttpServletRequest request);
+
+ }
diff --git a/bundle/src/main/java/com/adobe/acs/commons/sorter/SortNodesOperation.java b/bundle/src/main/java/com/adobe/acs/commons/sorter/SortNodesOperation.java
index 3e6fcd8d1a..e9fd29af72 100755
--- a/bundle/src/main/java/com/adobe/acs/commons/sorter/SortNodesOperation.java
+++ b/bundle/src/main/java/com/adobe/acs/commons/sorter/SortNodesOperation.java
@@ -19,13 +19,15 @@
*/
package com.adobe.acs.commons.sorter;
-import com.day.cq.commons.JcrLabeledResource;
-import org.apache.jackrabbit.JcrConstants;
+import com.adobe.acs.commons.sorter.impl.NodeNameSorter;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.servlets.post.PostOperation;
import org.apache.sling.servlets.post.PostResponse;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -35,8 +37,11 @@
import javax.servlet.http.HttpServletResponse;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Comparator;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import static org.apache.sling.servlets.post.PostOperation.PROP_OPERATION_NAME;
@@ -49,7 +54,7 @@
*
*
*
- * You can use optional {@link #RP_CASE_SENSITIVE} and {@link #RP_SORT_BY_TITLE} parameters to control whether to sort by
+ * You can use optional {@link NodeNameSorter#RP_CASE_SENSITIVE} and {@link #RP_SORTER_NAME} parameters to control whether to sort by
* by node name (default) or by jcr:title and whether the sort should be case-sensitive:
*
* curl -u admin:admin -F":operation=acs-commons:sortNodes" \
@@ -65,7 +70,7 @@ public class SortNodesOperation implements PostOperation {
/**
* Name of the sort operation.
- * The acs-commons: prefix is to void name clash with other PostOperations .
+ * The acs-commons: prefix is to avoid name clash with other PostOperations .
*/
public static final String OPERATION_SORT = "acs-commons:sortNodes";
@@ -74,50 +79,33 @@ public class SortNodesOperation implements PostOperation {
*
* If this request parameter is missing then nodes will be sorted by node name.
*/
- public static final String RP_SORT_BY_TITLE = ":byTitle";
+ public static final String RP_SORTER_NAME = ":sorterName";
- /**
- * Name of the request parameter indicating whether the sort should be case sensitive
- *
- * If this request parameter is missing then the sort will be case insensitive
- */
- public static final String RP_CASE_SENSITIVE = ":caseSensitive";
+ public static final String DEFAULT_SORTER_NAME = NodeNameSorter.SORTER_NAME;
- /**
- * Name of the request parameter indicating whether the sort should move non-hierarchy nodes to the top
- *
- * The default value is true which means jcr:content, rep:policy and such will be sorted first by their node names
- * followed by other,hierarchy nodes, e.g :
- *
- * + /parent
- * - jcr:content
- * - rep:policy
- * - a
- * - b
- * - c
- * - p
- *
- * If user forces it to false
then nodes will be sorted regardless of their hierarchy type:
- *
- * + /parent
- * - a
- * - b
- * - c
- * - jcr:content
- * - p
- * - rep:policy
- *
- */
- public static final String RP_NOT_HIERARCHY_FIRST = ":nonHierarchyFirst";
+
+ private final Map nodeSorters = Collections.synchronizedMap(new LinkedHashMap<>());
+
+ @Reference(service = NodeSorter.class,
+ cardinality = ReferenceCardinality.MULTIPLE,
+ policy = ReferencePolicy.DYNAMIC)
+ protected void bindNodeSorter(NodeSorter sorter, Map properties){
+ if (sorter != null ) {
+ String sorterName = sorter.getName();
+ log.debug("registering node sorter: {} -> {}", sorterName, sorter.getClass().getName());
+ nodeSorters.put(sorterName, sorter);
+ }
+ }
+
+ protected void unbindNodeSorter(NodeSorter sorter, Map properties){
+ String sorterName = sorter.getName();
+ log.debug("un-registering node sorter: {} -> {}", sorterName, sorter.getClass().getName());
+ nodeSorters.remove(sorterName);
+ }
@Override
public void run(SlingHttpServletRequest slingRequest, PostResponse response, SlingPostProcessor[] processors) {
try {
- final long t0 = System.currentTimeMillis();
- final boolean byTitle = Boolean.parseBoolean(slingRequest.getParameter(RP_SORT_BY_TITLE));
- final boolean caseSensitive = Boolean.parseBoolean(slingRequest.getParameter(RP_CASE_SENSITIVE));
- final boolean nonHierarchyFirst = slingRequest.getParameter(RP_NOT_HIERARCHY_FIRST) == null || Boolean.parseBoolean(slingRequest.getParameter(RP_NOT_HIERARCHY_FIRST));
-
Node targetNode = slingRequest.getResource().adaptTo(Node.class);
if (targetNode == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND,"Missing target node to sort: " + slingRequest.getResource().getPath());
@@ -129,7 +117,8 @@ public void run(SlingHttpServletRequest slingRequest, PostResponse response, Sli
response.setParentLocation(targetNode.getParent().getPath());
}
- Comparator comparator = createComparator(nonHierarchyFirst, byTitle, caseSensitive);
+ long t0 = System.currentTimeMillis();
+ Comparator comparator = getNodeSorter(slingRequest);
List children = getSortedNodes(targetNode, comparator);
Node prev = null;
@@ -137,18 +126,16 @@ public void run(SlingHttpServletRequest slingRequest, PostResponse response, Sli
Node n = children.get(children.size() - 1 - i);
if (prev != null) {
log.trace("orderBefore: {}, {}", n.getName(), prev.getName());
-
targetNode.orderBefore(n.getName(), prev.getName());
}
response.onChange("ordered", n.getPath(), prev == null ? "" : prev.getName());
prev = n;
}
targetNode.getSession().save();
-
response.setTitle("Content sorted " + response.getPath());
log.info("{} nodes sorted in {} ms", children.size(), System.currentTimeMillis()-t0 );
- } catch (RepositoryException e) {
+ } catch (RepositoryException | IllegalArgumentException e) {
response.setError(e);
}
}
@@ -162,6 +149,7 @@ public void run(SlingHttpServletRequest slingRequest, PostResponse response, Sli
* @throws RepositoryException if something went wrong
*/
List getSortedNodes(Node node, Comparator comparator) throws RepositoryException {
+
List children = new ArrayList<>();
NodeIterator it = node.getNodes();
while (it.hasNext()) {
@@ -173,60 +161,17 @@ List getSortedNodes(Node node, Comparator comparator) throws Reposit
return children;
}
- /**
- * Create a comparator to sort nodes
- *
- * @param nonHierarchyFirst whether non-hierarchy nodes should go first
- * @param byTitle whether to sort by jcr:title. If false
then sort by node name.
- * @param caseSensitive whether comparison should be case-sensitive
- * @return comparator
- */
- static Comparator createComparator(boolean nonHierarchyFirst, boolean byTitle, boolean caseSensitive) {
-
- Comparator comparator = (n1, n2) -> 0;
-
- if(nonHierarchyFirst){
- comparator = comparator.thenComparing((n1, n2) -> {
- try {
- return Boolean.compare(
- n1.isNodeType(JcrConstants.NT_HIERARCHYNODE),
- n2.isNodeType(JcrConstants.NT_HIERARCHYNODE));
- } catch (RepositoryException e) {
- return 0;
- }
- });
+ Comparator getNodeSorter(SlingHttpServletRequest slingRequest){
+ String sorterId = slingRequest.getParameter(RP_SORTER_NAME);
+ if(sorterId == null){
+ sorterId = DEFAULT_SORTER_NAME;
}
-
- if (byTitle) {
- comparator = comparator.thenComparing((n1, n2) -> {
- try {
- String title1 = new JcrLabeledResource(n1).getTitle();
- if (title1 == null) {
- title1 = n1.getName();
- }
- String title2 = new JcrLabeledResource(n2).getTitle();
- if (title2 == null) {
- title2 = n2.getName();
- }
- return caseSensitive ? title1.compareTo(title2) :
- title1.compareToIgnoreCase(title2);
- } catch (RepositoryException e) {
- return 0;
- }
- });
- } else {
- comparator = comparator.thenComparing((n1, n2) -> {
- try {
- String name1 = n1.getName();
- String name2 = n2.getName();
- return caseSensitive ? name1.compareTo(name2) :
- name1.compareToIgnoreCase(name2);
- } catch (RepositoryException e) {
- return 0;
- }
- });
+ NodeSorter sorter = nodeSorters.get(sorterId);
+ if(sorter == null){
+ String msg = "NodeSorter was not found: " + sorterId +
+ ". Available sorters are: " + nodeSorters.keySet().toString();
+ throw new IllegalArgumentException(msg);
}
- return comparator;
+ return sorter.createComparator(slingRequest);
}
-
}
diff --git a/bundle/src/main/java/com/adobe/acs/commons/sorter/SortersPojo.java b/bundle/src/main/java/com/adobe/acs/commons/sorter/SortersPojo.java
new file mode 100755
index 0000000000..a7603888cc
--- /dev/null
+++ b/bundle/src/main/java/com/adobe/acs/commons/sorter/SortersPojo.java
@@ -0,0 +1,45 @@
+/*
+ * #%L
+ * ACS AEM Commons Bundle
+ * %%
+ * Copyright (C) 2013 Adobe
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.adobe.acs.commons.sorter;
+
+import com.adobe.cq.sightly.WCMUsePojo;
+import org.osgi.annotation.versioning.ProviderType;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Produce a list of available sorters to populate the drop-down on the sort-nodes.html page.
+ */
+@ProviderType
+public class SortersPojo extends WCMUsePojo {
+
+ @Override
+ public void activate(){
+
+ }
+
+ public Collection getAvailableSorters(){
+ NodeSorter[] sorters = getSlingScriptHelper().getServices(NodeSorter.class, null);
+ return sorters == null ? Collections.emptyList() : Arrays.asList(sorters);
+ }
+
+}
diff --git a/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/HierarchyNodeComparator.java b/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/HierarchyNodeComparator.java
new file mode 100755
index 0000000000..b1559215f8
--- /dev/null
+++ b/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/HierarchyNodeComparator.java
@@ -0,0 +1,45 @@
+/*
+ * #%L
+ * ACS AEM Commons Bundle
+ * %%
+ * Copyright (C) 2013 Adobe
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.adobe.acs.commons.sorter.impl;
+
+import org.apache.jackrabbit.JcrConstants;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import java.util.Comparator;
+
+class HierarchyNodeComparator implements Comparator {
+ public static final String RP_NOT_HIERARCHY_FIRST = ":nonHierarchyFirst";
+
+ public static HierarchyNodeComparator INSTANCE = new HierarchyNodeComparator();
+
+ private HierarchyNodeComparator(){}
+
+ @Override
+ public int compare(Node n1, Node n2) {
+ try {
+ return Boolean.compare(
+ n1.isNodeType(JcrConstants.NT_HIERARCHYNODE),
+ n2.isNodeType(JcrConstants.NT_HIERARCHYNODE));
+ } catch (RepositoryException e) {
+ return 0;
+ }
+ }
+}
diff --git a/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/NodeNameSorter.java b/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/NodeNameSorter.java
new file mode 100755
index 0000000000..740a885f09
--- /dev/null
+++ b/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/NodeNameSorter.java
@@ -0,0 +1,63 @@
+/*
+ * #%L
+ * ACS AEM Commons Bundle
+ * %%
+ * Copyright (C) 2013 Adobe
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.adobe.acs.commons.sorter.impl;
+
+import com.adobe.acs.commons.sorter.NodeSorter;
+import org.osgi.service.component.annotations.Component;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.servlet.http.HttpServletRequest;
+import java.util.Comparator;
+
+import static com.adobe.acs.commons.sorter.impl.HierarchyNodeComparator.RP_NOT_HIERARCHY_FIRST;
+
+@Component
+public class NodeNameSorter implements NodeSorter {
+ public static final String SORTER_NAME = "byName";
+ public static final String RP_CASE_SENSITIVE = ":caseSensitive";
+
+ public String getName(){
+ return SORTER_NAME;
+ }
+
+ public String getLabel(){
+ return "By Node Name";
+ }
+
+ @Override
+ public Comparator createComparator(HttpServletRequest request) {
+ boolean caseSensitive = Boolean.parseBoolean(request.getParameter(RP_CASE_SENSITIVE));
+ boolean nonHierarchyFirst = request.getParameter(RP_NOT_HIERARCHY_FIRST) == null ||
+ Boolean.parseBoolean(request.getParameter(RP_NOT_HIERARCHY_FIRST));
+ Comparator parentComparator = nonHierarchyFirst ? HierarchyNodeComparator.INSTANCE : (n1, n2) -> 0;
+ return parentComparator.thenComparing((n1, n2) -> {
+ try {
+ String name1 = n1.getName();
+ String name2 = n2.getName();
+ return caseSensitive ? name1.compareTo(name2) :
+ name1.compareToIgnoreCase(name2);
+ } catch (RepositoryException e) {
+ return 0;
+ }
+ });
+ }
+
+}
diff --git a/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/NodeTitleSorter.java b/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/NodeTitleSorter.java
new file mode 100755
index 0000000000..41323dc55f
--- /dev/null
+++ b/bundle/src/main/java/com/adobe/acs/commons/sorter/impl/NodeTitleSorter.java
@@ -0,0 +1,70 @@
+/*
+ * #%L
+ * ACS AEM Commons Bundle
+ * %%
+ * Copyright (C) 2013 Adobe
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+package com.adobe.acs.commons.sorter.impl;
+
+import com.adobe.acs.commons.sorter.NodeSorter;
+import com.day.cq.commons.JcrLabeledResource;
+import org.osgi.service.component.annotations.Component;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.servlet.http.HttpServletRequest;
+import java.util.Comparator;
+
+import static com.adobe.acs.commons.sorter.impl.HierarchyNodeComparator.RP_NOT_HIERARCHY_FIRST;
+
+@Component
+public class NodeTitleSorter implements NodeSorter {
+
+ public static final String SORTER_NAME = "byTitle";
+ public static final String RP_CASE_SENSITIVE = ":caseSensitive";
+
+ public String getName(){
+ return SORTER_NAME;
+ }
+
+ public String getLabel(){
+ return "By Node Title";
+ }
+
+ @Override
+ public Comparator createComparator(HttpServletRequest request) {
+ boolean caseSensitive = Boolean.parseBoolean(request.getParameter(RP_CASE_SENSITIVE));
+ boolean nonHierarchyFirst = request.getParameter(RP_NOT_HIERARCHY_FIRST) == null ||
+ Boolean.parseBoolean(request.getParameter(RP_NOT_HIERARCHY_FIRST));
+ Comparator parentComparator = nonHierarchyFirst ? HierarchyNodeComparator.INSTANCE : (n1, n2) -> 0;
+ return parentComparator.thenComparing((n1, n2) -> {
+ try {
+ String title1 = new JcrLabeledResource(n1).getTitle();
+ if (title1 == null) {
+ title1 = n1.getName();
+ }
+ String title2 = new JcrLabeledResource(n2).getTitle();
+ if (title2 == null) {
+ title2 = n2.getName();
+ }
+ return caseSensitive ? title1.compareTo(title2) :
+ title1.compareToIgnoreCase(title2);
+ } catch (RepositoryException e) {
+ return 0;
+ }
+ });
+ }
+}
diff --git a/bundle/src/main/java/com/adobe/acs/commons/sorter/package-info.java b/bundle/src/main/java/com/adobe/acs/commons/sorter/package-info.java
new file mode 100755
index 0000000000..4375bc2176
--- /dev/null
+++ b/bundle/src/main/java/com/adobe/acs/commons/sorter/package-info.java
@@ -0,0 +1,26 @@
+/*
+ * #%L
+ * ACS AEM Commons Bundle
+ * %%
+ * Copyright (C) 2013 Adobe
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+/**
+ * Miscellaneous Utilities.
+ */
+@Version("1.0.0")
+
+package com.adobe.acs.commons.sorter;
+import org.osgi.annotation.versioning.Version;
diff --git a/bundle/src/test/java/com/adobe/acs/commons/sorter/SortNodesOperationTest.java b/bundle/src/test/java/com/adobe/acs/commons/sorter/SortNodesOperationTest.java
index 2b05e33cc9..9927bd4ed6 100755
--- a/bundle/src/test/java/com/adobe/acs/commons/sorter/SortNodesOperationTest.java
+++ b/bundle/src/test/java/com/adobe/acs/commons/sorter/SortNodesOperationTest.java
@@ -19,6 +19,8 @@
*/
package com.adobe.acs.commons.sorter;
+import com.adobe.acs.commons.sorter.impl.NodeNameSorter;
+import com.adobe.acs.commons.sorter.impl.NodeTitleSorter;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.servlets.post.PostResponse;
@@ -36,12 +38,15 @@
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
+import static com.adobe.acs.commons.sorter.SortNodesOperation.RP_SORTER_NAME;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -54,6 +59,8 @@ public class SortNodesOperationTest {
@Before
public void setUp(){
+ sorter.bindNodeSorter(new NodeNameSorter(), Collections.emptyMap());
+ sorter.bindNodeSorter(new NodeTitleSorter(), Collections.emptyMap());
context.build()
.resource("/sortable", JCR_PRIMARYTYPE, "cq:Page")
.resource("/sortable/page2", JCR_PRIMARYTYPE, "cq:Page")
@@ -66,12 +73,14 @@ public void setUp(){
.resource("/sortable/rep:policy", JCR_PRIMARYTYPE, "rep:ACL");
}
+
/**
* by node name, case insensitive
*/
@Test
public void testSortByNameCaseInsensitive() throws RepositoryException {
- Comparator comparator = SortNodesOperation.createComparator(true,false, false);
+ MockSlingHttpServletRequest request = context.request();
+ Comparator comparator = sorter.getNodeSorter(request);
Node node = context.resourceResolver().getResource("/sortable").adaptTo(Node.class);
List list = sorter.getSortedNodes(node, comparator);
@@ -84,7 +93,9 @@ public void testSortByNameCaseInsensitive() throws RepositoryException {
*/
@Test
public void testDisableNonHierarchyFirst() throws RepositoryException {
- Comparator comparator = SortNodesOperation.createComparator(false,false, false);
+ MockSlingHttpServletRequest request = context.request();
+ request.addRequestParameter(":nonHierarchyFirst", "false");
+ Comparator comparator = sorter.getNodeSorter(request);
Node node = context.resourceResolver().getResource("/sortable").adaptTo(Node.class);
List list = sorter.getSortedNodes(node, comparator);
@@ -97,7 +108,9 @@ public void testDisableNonHierarchyFirst() throws RepositoryException {
*/
@Test
public void testSortByNameCaseSensitive() throws RepositoryException {
- Comparator comparator = SortNodesOperation.createComparator(true,false, true);
+ MockSlingHttpServletRequest request = context.request();
+ request.addRequestParameter(NodeTitleSorter.RP_CASE_SENSITIVE, "true");
+ Comparator comparator = sorter.getNodeSorter(request);
Node node = context.resourceResolver().getResource("/sortable").adaptTo(Node.class);
List list = sorter.getSortedNodes(node, comparator);
@@ -110,7 +123,8 @@ public void testSortByNameCaseSensitive() throws RepositoryException {
*/
@Test
public void testSortByTitleCaseInsensitive() throws RepositoryException {
- Comparator comparator = SortNodesOperation.createComparator(true,true, false);
+ MockSlingHttpServletRequest request = context.request();
+ Comparator comparator = sorter.getNodeSorter(request);
Node node = context.resourceResolver().getResource("/sortable").adaptTo(Node.class);
List list = sorter.getSortedNodes(node, comparator);
@@ -123,7 +137,10 @@ public void testSortByTitleCaseInsensitive() throws RepositoryException {
*/
@Test
public void testSortByTitleCaseSensitive() throws RepositoryException {
- Comparator comparator = SortNodesOperation.createComparator(true,true, true);
+ MockSlingHttpServletRequest request = context.request();
+ request.addRequestParameter(RP_SORTER_NAME, NodeTitleSorter.SORTER_NAME);
+ request.addRequestParameter(NodeTitleSorter.RP_CASE_SENSITIVE, "true");
+ Comparator comparator = sorter.getNodeSorter(request);
Node node = context.resourceResolver().getResource("/sortable").adaptTo(Node.class);
List list = sorter.getSortedNodes(node, comparator);
@@ -143,21 +160,6 @@ public void testDefaultRequestParameters() {
assertSortOrder(Arrays.asList("jcr:content", "rep:policy", "page1", "page2", "Page3"), children);
}
- @Test
- public void testRequestParameters() {
- Resource resource = context.resourceResolver().getResource("/sortable");
- MockSlingHttpServletRequest request = context.request();
- request.addRequestParameter(SortNodesOperation.RP_SORT_BY_TITLE, "true");
- request.addRequestParameter(SortNodesOperation.RP_CASE_SENSITIVE, "true");
- request.setResource(resource);
- sorter.run(context.request(), mock(PostResponse.class), null);
-
- List children = new ArrayList<>();
- resource.getChildren().forEach(r -> children.add(r.adaptTo(Node.class)));
-
- assertSortOrder(Arrays.asList("jcr:content", "rep:policy", "page2" /*A: Page-2*/, "Page3" /*C: Page-3*/, "page1" /*a: Page-1*/), children);
- }
-
@Test
public void testInvalidTargetResource() {
String targetPath = "/unknown";
@@ -192,6 +194,20 @@ public void testNotSortableTarget() {
errCaptor.getValue().getMessage());
}
+ @Test
+ public void testUnknownSorter() {
+ PostResponse response = mock(PostResponse.class);
+ MockSlingHttpServletRequest request = context.request();
+ request.addRequestParameter(RP_SORTER_NAME, "sorterNA");
+ request.setResource(context.resourceResolver().getResource("/sortable"));
+ ArgumentCaptor errCaptor = ArgumentCaptor.forClass(Throwable.class);
+ sorter.run(request, response, null);
+
+ verify(response).setError(errCaptor.capture());
+ assertEquals("NodeSorter was not found: sorterNA. Available sorters are: [:nodeNameSorter, :nodeTitleSorter]",
+ errCaptor.getValue().getMessage());
+ }
+
private static void assertSortOrder(List expected, List list){
List actual = list.stream().map(n -> {
try {
diff --git a/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/sort-nodes/sort-nodes.html b/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/sort-nodes/sort-nodes.html
index dc70c5ddd6..5061e7a653 100755
--- a/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/sort-nodes/sort-nodes.html
+++ b/ui.apps/src/main/content/jcr_root/apps/acs-commons/components/utilities/sort-nodes/sort-nodes.html
@@ -48,27 +48,49 @@