referencedchannels.jsp 4 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.cq.screens.display.DisplayService,
                  com.adobe.granite.ui.components.Config,
                  com.adobe.granite.ui.components.ds.DataSource,
                  com.adobe.granite.ui.components.ds.SimpleDataSource,
                  com.adobe.granite.ui.components.ds.ValueMapResource,
                  com.day.cq.wcm.api.Page,
                  org.apache.commons.collections.Transformer,
                  org.apache.commons.collections.iterators.TransformIterator,
                  org.apache.sling.api.resource.Resource,
                  org.apache.sling.api.resource.ResourceMetadata,
                  org.apache.sling.api.resource.ResourceResolver,
                  org.apache.sling.api.resource.ValueMap,
                  org.apache.sling.api.wrappers.ValueMapDecorator,
                  java.lang.IllegalArgumentException,
                  java.util.ArrayList,
                  java.util.HashMap,
                  java.util.Iterator" %>
<%@ page import="java.util.List" %><%

    // Find the referenced channels for the specified display
    Config dsCfg = new Config(resource.getChild(Config.DATASOURCE));
    final DisplayService displayService = resourceResolver.adaptTo(DisplayService.class);


    // Handle bulk editing
    String displayPath = null;
    try {
        String[] displayPaths = cmp.getExpressionHelper().get(dsCfg.get("path", String.class), String[].class);
        displayPath = displayPaths.length > 0 ? displayPaths[0] : null;
    }
    catch (IllegalArgumentException ex) {
        displayPath = cmp.getExpressionHelper().getString(dsCfg.get("path", String.class));
    }
    finally {
        if (displayPath == null) {
            return;
        }
    }

    // Transform the channels list to an options datasource for the select
    final Resource displayResource = resourceResolver.getResource(displayPath);
    Iterator<Resource> displayAssignments = displayService.getAssignments(resourceResolver, displayResource);

    if (displayAssignments == null) {
        return;
    }

    List<Resource> data = new ArrayList<Resource>();
    ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
    vm.put("value", "(auto)");
    vm.put("text", "(auto)");
    data.add(new ValueMapResource(resourceResolver, new ResourceMetadata(), "nt:unstructured", vm));
    while (displayAssignments.hasNext()) {
        Resource displayAssignment = displayAssignments.next();
        ValueMap displayAssignmentProps = displayAssignment.adaptTo(ValueMap.class);

        // Only accept local channel assignments for now and ignore schedule assignments
        // because this would break the "current channel" logic.
        if (!displayAssignment.isResourceType(ScreensConstants.RT_SCHEDULE_ASSIGNMENT_ABSOLUTE)) {
            vm = new ValueMapDecorator(new HashMap<String, Object>());
            vm.put("value", displayAssignmentProps.get(ScreensConstants.PN_ROLE));
            vm.put("text", displayAssignmentProps.get(ScreensConstants.PN_ROLE));
            data.add(new ValueMapResource(resourceResolver, new ResourceMetadata(), "nt:unstructured", vm));
        }
    }

    SimpleDataSource ds = new SimpleDataSource(data.iterator());

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