childresources.jsp 7.02 KB
<%--
  ADOBE CONFIDENTIAL

  Copyright 2015 Adobe Systems Incorporated
  All Rights Reserved.

  NOTICE:  All information contained herein is, and remains
  the property of Adobe Systems Incorporated and its suppliers,
  if any.  The intellectual and technical concepts contained
  herein are proprietary to Adobe Systems Incorporated and its
  suppliers and may be covered by U.S. and Foreign Patents,
  patents in process, and are protected by trade secret or copyright law.
  Dissemination of this information or reproduction of this material
  is strictly forbidden unless prior written permission is obtained
  from Adobe Systems Incorporated.
--%><%
%><%@include file="/libs/granite/ui/global.jsp"%><%
%><%@page session="false"
          import="com.adobe.cq.screens.binding.ScreensConstants,
                  com.adobe.granite.ui.components.Config,
                  com.adobe.granite.ui.components.ExpressionHelper,
                  com.adobe.granite.ui.components.PagingIterator,
                  com.adobe.granite.ui.components.ds.AbstractDataSource,
                  com.adobe.granite.ui.components.ds.DataSource,
                  com.adobe.granite.ui.components.ds.EmptyDataSource,
                  com.day.cq.wcm.api.Page,
                  org.apache.commons.collections.IteratorUtils,
                  org.apache.commons.collections.Predicate,
                  org.apache.commons.collections.Transformer,
                  org.apache.commons.collections.iterators.FilterIterator,
                  org.apache.commons.collections.iterators.TransformIterator,
                  org.apache.sling.api.resource.Resource,
                  org.apache.sling.api.resource.ResourceWrapper,
                  java.util.Collections,
                  java.util.Comparator,
                  java.util.Iterator,
                  java.util.List,
                  java.util.regex.Pattern"%><%
    /**
     A datasource returning child pages and folders.
     Folders can be hidden by configuration (showFolders = false)
     Which resources should not be shown can be configured with the "notAllowed" property

     @datasource
     @name ChildPages
     @location /libs/screens/dcc/components/datasources/childresources

     @property {StringEL} path The path of the parent page
     @property {String} itemResourceType
     */
    ExpressionHelper ex = cmp.getExpressionHelper();
    Config dsCfg = new Config(resource.getChild(Config.DATASOURCE));

    String parentPath = ex.getString(dsCfg.get("path", String.class));
    final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class);
    final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class);
    final boolean showFolders = ex.get(dsCfg.get("showFolders", "true"), Boolean.class);
    final String filter = dsCfg.get("exclude", "");
    final Pattern pattern = Pattern.compile(filter);

    final String sortName = slingRequest.getParameter("sortName");
    final String sortDir = slingRequest.getParameter("sortDir");

    DataSource ds;
    if (parentPath == null) {
        ds = EmptyDataSource.instance();
    } else {
        final Resource parent = resourceResolver.getResource(parentPath);
        final Iterator<Resource> sortedChildrenIterator =
            parent != null
                ? ((sortName != null && sortDir != null)
                    ? getSortedIterator(parent.listChildren(), sortName, sortDir)
                    : parent.listChildren())
                : null;
        final String itemRT = dsCfg.get("itemResourceType", String.class);

        @SuppressWarnings("unchecked")
        DataSource datasource = new AbstractDataSource() {
            public Iterator<Resource> iterator() {
                Iterator<Resource> it = new PagingIterator<Resource>(new FilterIterator(sortedChildrenIterator, new Predicate() {
                    public boolean evaluate(Object o) {
                        Resource r = ((Resource) o);

                        if (pattern.matcher(r.getPath()).matches()) {
                            return false;
                        }

                        // Filter out device configs and screens from the results, as they are accessible trought the display dashbaord
                        Resource c = r.getChild("jcr:content");
                        if (c != null &&
                            (c.isResourceType(ScreensConstants.RT_DEVICE_CONFIG)
                                || c.isResourceType(ScreensConstants.RT_SCREEN))) {
                            return false;
                        }

                        Page p = r.adaptTo(Page.class);
                        boolean isFolder = r.isResourceType("sling:Folder") ||
                                r.isResourceType("sling:OrderedFolder") ||
                                r.isResourceType("nt:folder");
                        return (showFolders && isFolder) || p != null;
                    }
                }), offset, limit);

                return new TransformIterator(it, new Transformer() {
                    public Object transform(Object o) {
                        Resource r = ((Resource) o);

                        return new ResourceWrapper(r) {
                            public String getResourceType() {
                                return itemRT;
                            }
                        };
                    }
                });
            }
        };

        ds = datasource;
    }

    request.setAttribute(DataSource.class.getName(), ds);
%>
<%!

    Iterator<Resource> getSortedIterator(Iterator<Resource> children, String sortName, String sortDir){
        List<Resource> childrenList = IteratorUtils.toList(children);
        Comparator comparator = getComparator(sortName, sortDir.toLowerCase().equals("desc"));
        Collections.sort(childrenList, comparator);
        return childrenList.iterator();
    }

    private Comparator<Resource> getComparator(final String sortName, final boolean reverse) {
        Comparator comparator = new Comparator<Resource>() {
            public int compare(Resource r1, Resource r2) {
                switch (sortName) {
                    case "title":
                        return compareProperty(r1, r2, "jcr:title");
                    case "modified":
                        return compareProperty(r1, r2, "cq:lastModified");
                    case "published":
                        return compareProperty(r1, r2, "cq:lastReplicated");
                    default:
                        return 0;
                }
            }
        };

        // reverses order if necessary
        return reverse ? Collections.reverseOrder(comparator) : comparator;
    }

    private int compareProperty(Resource r1, Resource r2, String prop) {
        Resource cr1 = r1.getChild("jcr:content");
        if (cr1 == null) {
            cr1 = r1;
        }
        Resource cr2 = r2.getChild("jcr:content");
        if (cr2 == null) {
            cr2 = r2;
        }
        ValueMap vm1 = cr1.getValueMap();
        ValueMap vm2 = cr2.getValueMap();
        return vm1.get(prop, "").compareToIgnoreCase(vm2.get(prop, ""));
    }
%>