Skip to content

Commit

Permalink
Merge b9ae40d into ddd20a2
Browse files Browse the repository at this point in the history
  • Loading branch information
YegorKozlov committed Mar 7, 2021
2 parents ddd20a2 + b9ae40d commit f587857
Show file tree
Hide file tree
Showing 15 changed files with 910 additions and 0 deletions.
49 changes: 49 additions & 0 deletions bundle/src/main/java/com/adobe/acs/commons/sorter/NodeSorter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* #%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 javax.jcr.Node;
import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;

public interface NodeSorter {

/**
* The name of a sorter, e.g. 'byTitle' or 'byMyCustomProperty'
* It will be used to find the actual sorter from the {@link SortNodesOperation#RP_SORTER_NAME}
* request parameter
*/
String getName();

/**
* Optional label to show in the UI drop-down to select a sorter.
*/
default String getLabel(){
return getName();
}

/**
* Create a comparator to sort nodes.
* Implementations can read additional parameters from request, e.g.
* whether search should be case-sensitive, ascending/descending, etc.
*/
Comparator<Node> createComparator(HttpServletRequest request);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* #%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.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;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
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;

/**
* The <code>SortNodesOperation</code> class implements the {@link #OPERATION_SORT} operation for the Sling POST servlet.
* <p>
* Use this operation to alphabetize JCR nodes. For example, the following command line sorts the children of the /content/sample page:
* <pre>
* curl -u admin:admin -F":operation=acs-commons:sortNodes" http://localhost:4502/content/sample
* </pre>
* </p>
*
* 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:
* <pre>
* curl -u admin:admin -F":operation=acs-commons:sortNodes" \
* -F":byTitle:true" -F":caseSensitive:true" \
* http://localhost:4502/content/someFolder
* </pre>
*/
@Component(property = {
PROP_OPERATION_NAME + "=" + SortNodesOperation.OPERATION_SORT
})
public class SortNodesOperation implements PostOperation {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

/**
* Name of the sort operation.
* The acs-commons: prefix is to avoid name clash with other PostOperations .
*/
public static final String OPERATION_SORT = "acs-commons:sortNodes";

/**
* Name of the request parameter indicating whether to sort nodes by jcr:title
*
* If this request parameter is missing then nodes will be sorted by node name.
*/
public static final String RP_SORTER_NAME = ":sorterName";

public static final String DEFAULT_SORTER_NAME = NodeNameSorter.SORTER_NAME;


private final Map<String, NodeSorter> nodeSorters = Collections.synchronizedMap(new LinkedHashMap<>());

@Reference(service = NodeSorter.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC)
protected void bindNodeSorter(NodeSorter sorter, Map<String, Object> 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<String, Object> 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 {
Node targetNode = slingRequest.getResource().adaptTo(Node.class);
if (targetNode == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND,"Missing target node to sort: " + slingRequest.getResource().getPath());
return;
}
response.setPath(targetNode.getPath());
response.setLocation(targetNode.getPath());
if(targetNode.getParent() != null) {
response.setParentLocation(targetNode.getParent().getPath());
}

final long t0 = System.currentTimeMillis();
Comparator<Node> comparator = getNodeSorter(slingRequest);
List<Node> children = getSortedNodes(targetNode, comparator);

Node prev = null;
for (int i = 0; i < children.size(); i++) {
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 | IllegalArgumentException e) {
response.setError(e);
}
}

/**
* Collect child nodes and sort them using the supplied Comparator
*
* @param node the node who's children need to be sorted
* @param comparator the comparator to sort nodes
* @return list of sorted nodes
* @throws RepositoryException if something went wrong
*/
List<Node> getSortedNodes(Node node, Comparator<Node> comparator) throws RepositoryException {

List<Node> children = new ArrayList<>();
NodeIterator it = node.getNodes();
while (it.hasNext()) {
Node child = it.nextNode();
children.add(child);
}

children.sort(comparator);
return children;
}

Comparator<Node> getNodeSorter(SlingHttpServletRequest slingRequest){
String sorterId = slingRequest.getParameter(RP_SORTER_NAME);
if(sorterId == null){
sorterId = DEFAULT_SORTER_NAME;
}
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 sorter.createComparator(slingRequest);
}
}
45 changes: 45 additions & 0 deletions bundle/src/main/java/com/adobe/acs/commons/sorter/SortersPojo.java
Original file line number Diff line number Diff line change
@@ -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(){
// no op
}

public Collection<NodeSorter> getAvailableSorters(){
NodeSorter[] sorters = getSlingScriptHelper().getServices(NodeSorter.class, null);
return sorters == null ? Collections.emptyList() : Arrays.asList(sorters);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* #%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<Node> {
public static final String RP_NOT_HIERARCHY_FIRST = ":nonHierarchyFirst";

public static HierarchyNodeComparator INSTANCE = new HierarchyNodeComparator();

private HierarchyNodeComparator(){
// ensure singleton
}

@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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Node> 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<Node> 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;
}
});
}

}
Loading

0 comments on commit f587857

Please sign in to comment.