devices.jsp 7.31 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.DeviceConstants,
                  com.adobe.cq.screens.device.Device,
                  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,
                  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,
                  org.apache.sling.api.resource.SyntheticResource,
                  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 {String} itemResourceType
*/
ExpressionHelper ex = cmp.getExpressionHelper();
Config dsCfg = new Config(resource.getChild(Config.DATASOURCE));

final String assigned = ex.getString(dsCfg.get("assigned", 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 String sortName = slingRequest.getParameter("sortName");
final String sortDir = slingRequest.getParameter("sortDir");

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

final Iterator<Device> devices = assigned != null && !assigned.isEmpty() ?
    deviceManager.getDevices("true".equals(assigned) ? DeviceConstants.PREDICATE_ASSIGNED : DeviceConstants.PREDICATE_NOT_ASSIGNED) : deviceManager.getDevices();
final Iterator<Device> sortedDevices = (sortName != null && sortDir != null) ? getSortedIterator(devices, sortName, sortDir) : devices;

DataSource ds;
if (deviceManager == null) {
    ds = EmptyDataSource.instance();
} else {
    final ResourceResolver resResolver = resourceResolver;
    final String itemRT = dsCfg.get("itemResourceType", String.class);

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

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

                    return true;
                }
            }), offset, limit);

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

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

    ds = datasource;
}

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

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

    private Comparator<Device> getComparator(final String sortName, final boolean reverse) {
        Comparator comparator = new Comparator<Device>() {
            public int compare(Device d1, Device d2) {
                Resource r1 = d1.adaptTo(Resource.class).getChild("profile_screens");
                Resource r2 = d2.adaptTo(Resource.class).getChild("profile_screens");
                DeviceStatus ds1, ds2;
                switch (sortName) {
                    case "title":       return compareProperty(r1, r2, "id");
                    case "assigned":    return compareProperty(r1, r2, "configPath");
                    case "make":        return compareProperty(r1, r2, "brand");
                    case "model":       return compareProperty(r1, r2, "model");
                    case "platform":    return compareProperty(r1, r2, "platform");
                    case "version":     return compareProperty(r1, r2, "version");
                    case "connection":
                    case "ping":
                        ds1 = r1.adaptTo(DeviceStatus.class);
                        ds2 = r2.adaptTo(DeviceStatus.class);
                        return ds2.getLastPing().getTime() < ds1.getLastPing().getTime() ? -1 : 1;
                    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, ""));
    }

    class DeviceResource extends SyntheticResource {

        private Device device;

        public DeviceResource(ResourceResolver resolver, Device device) {
            super(resolver, "", "");
            this.device = device;
        }

        public String getName() {
            return this.device.getId();
        }

        public String getPath() {
            return this.device.getPath();
        }

        public String getResourceType() {
            return "screens/core/components/device";
        }
    }

%>