displaydevices.jsp 8.13 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.device.Device,
                com.adobe.cq.screens.device.DeviceConfig,
                com.adobe.cq.screens.device.DeviceManager,
                com.adobe.cq.screens.device.DeviceStatus,
                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.ResourceResolver,
                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 devices.
     *
     * @datasource
     * @name Devices
     * @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));

    final String path = 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 sortName = slingRequest.getParameter("sortName");
    final String sortDir = slingRequest.getParameter("sortDir");

    final DeviceManager deviceManager = resourceResolver.adaptTo(DeviceManager.class);

    DataSource ds;
    if (deviceManager == null || path == null) {
        ds = EmptyDataSource.instance();
    } else {
        final Resource displayResource = path != null ? resourceResolver.getResource(path) : null;
        final Page displayPage = displayResource != null ? displayResource.adaptTo(Page.class) : null;
        final Iterator<Page> sortedChildren =
            displayPage != null
                ? ((sortName != null && sortDir != null)
                    ? getSortedIterator(displayPage.listChildren(), sortName, sortDir)
                    : displayPage.listChildren())
                : null;

        @SuppressWarnings("unchecked")
        final Iterator<Page> configs = new PagingIterator<Page>(new FilterIterator(sortedChildren, new Predicate() {
            public boolean evaluate(Object o) {
                Page page = (Page) o;
                DeviceConfig cfg = page.adaptTo(DeviceConfig.class);
                return cfg != null && path.equals(cfg.getDisplayPath());
            }
        }), offset, limit);

        final String itemRT = dsCfg.get("itemResourceType", String.class);

        @SuppressWarnings("unchecked")
        DataSource datasource = new AbstractDataSource() {

            @SuppressWarnings("unchecked")
            public Iterator<Resource> iterator() {
                return new TransformIterator(configs, new Transformer() {
                    public Object transform(Object o) {
                        Page page = (Page) o;
                        Resource r = page.getContentResource();

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

        ds = datasource;
    }

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

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

    private Comparator<Page> getComparator(final String sortName, final boolean reverse) {
        Comparator comparator = new Comparator<Page>() {
            public int compare(Page p1, Page p2) {
                Resource r1 = p1.adaptTo(Resource.class);
                Resource r2 = p2.adaptTo(Resource.class);
                DeviceConfig dc1 = r1.adaptTo(DeviceConfig.class);
                DeviceConfig dc2 = r2.adaptTo(DeviceConfig.class);
                String id1 = dc1.getAssignedDeviceId();
                if (id1 == null) {
                    id1 = "";
                }
                String id2 = dc2.getAssignedDeviceId();
                if (id2 == null) {
                    id2 = "";
                }

                DeviceManager deviceMgr = r1.getResourceResolver().adaptTo(DeviceManager.class);
                Device d1 = id1 != null && !id1.isEmpty() ? deviceMgr.getDevice(id1) : null;
                Device d2 = id2 != null && !id2.isEmpty() ? deviceMgr.getDevice(id2) : null;
                DeviceStatus ds1 = d1 != null ? d1.adaptTo(Resource.class).adaptTo(DeviceStatus.class) : null;
                DeviceStatus ds2 = d2 != null ? d2.adaptTo(Resource.class).adaptTo(DeviceStatus.class) : null;

                switch (sortName) {
                    case "title":
                        return compareProperty(p1.getContentResource(), p2.getContentResource(), "jcr:title");
                    case "deviceId":
                        return id1.compareToIgnoreCase(id2);
                    case "model":
                        return compareProperty(d1, d2, "model");
                    case "resolution":
                        return compareProperty(d1, d2, "resolution");
                    case "platform":
                        return compareProperty(d1, d2, "platform");
                    case "version":
                        String v1 = ds1 != null ? ds1.getFirmware().getVersion() : "";
                        String v2 = ds2 != null ? ds2.getFirmware().getVersion() : "";
                        return v1.compareToIgnoreCase(v2);
                    case "cordova":
                        return compareProperty(d1, d2, "cordova");                        
                    default:
                        return 0;
                }
            }
        };

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

    private int compareProperty(Resource r1, Resource r2, String prop) {
        ValueMap vm1 = r1.getValueMap();
        ValueMap vm2 = r2.getValueMap();
        return vm1.get(prop, "").compareToIgnoreCase(vm2.get(prop, ""));
    }

    private int compareProperty(Device d1, Device d2, String prop) {
        ValueMap vm1 = d1 != null ? d1.adaptTo(ValueMap.class) : null;
        ValueMap vm2 = d2 != null ? d2.adaptTo(ValueMap.class) : null;
        String v1 = vm1 != null ? vm1.get(prop, "") : "";
        String v2 = vm2 != null ? vm2.get(prop, "") : "";
        return v1.compareToIgnoreCase(v2);
    }
%>