channels.jsp 5.35 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.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.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.Comparator,
                  java.util.List,
                  java.util.ArrayList,
                  java.util.Collections,
                  java.util.Iterator,
                  java.util.regex.Pattern,
                  com.day.cq.wcm.api.NameConstants,
                  com.adobe.cq.screens.binding.ScreensConstants,
                  com.adobe.cq.screens.display.DisplayService,
                  com.adobe.cq.screens.schedule.ScheduleService,
                  com.adobe.cq.screens.assignment.AssignmentService"%>
<%@ page import="org.slf4j.Logger" %><%
    /**
     A datasource returning the devices and screens of a given display

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

     @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 String filter = dsCfg.get("exclude", "");
    final Pattern pattern = Pattern.compile(filter);

    final Resource parentResource = parentPath != null ? resourceResolver.getResource(parentPath) : null;


    Iterator<Resource> assignments = null;
    if (parentResource != null) {
        Resource parentContent = parentResource.getChild(NameConstants.NN_CONTENT);

        if (parentContent.isResourceType(ScreensConstants.RT_DISPLAY)) {
            final DisplayService displayService = resourceResolver.adaptTo(DisplayService.class);
            assignments = displayService.getAssignments(resourceResolver, parentResource);
        }
        else if (parentContent.isResourceType(ScreensConstants.RT_SCHEDULE)) {
            final ScheduleService scheduleService = resourceResolver.adaptTo(ScheduleService.class);
            assignments = scheduleService.getAssignments(parentResource);
        }
    }

    DataSource ds;
    if (!assignments.hasNext()) {
        ds = EmptyDataSource.instance();
    } else {
        final String itemRT = dsCfg.get("itemResourceType", String.class);

        // Get channel assignments and order them by descending priority.
        final List sortedChannelAssignments = new ArrayList();
        while (assignments.hasNext()) {
            sortedChannelAssignments.add(assignments.next());
        }
        Collections.sort(sortedChannelAssignments, AssignmentService.CHANNEL_ASSIGNMENT_PRIORITY_COMPARATOR);

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

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

                        return true;
                    }
                }), 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);
%>