breadcrumbs.jsp 6.3 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="java.util.ArrayList,
                  java.util.Calendar,
                  java.util.HashMap,
                  java.util.Iterator,
                  java.util.List,
                  org.apache.commons.collections.Transformer,
                  org.apache.commons.collections.iterators.TransformIterator,
                  org.apache.commons.lang.StringUtils,
                  org.apache.jackrabbit.util.Text,
                  org.apache.sling.api.resource.Resource,
                  org.apache.sling.api.resource.ResourceMetadata,
                  org.apache.sling.api.resource.ValueMap,
                  org.apache.sling.api.wrappers.ValueMapDecorator,
                  com.adobe.cq.screens.binding.ScreensConstants,
                  com.adobe.cq.screens.device.Device,
                  com.adobe.granite.ui.components.AttrBuilder,
                  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.commons.Filter,
                  com.day.cq.wcm.api.Page"%><%

    final Config cfg = cmp.getConfig();
    final String path = StringUtils.trimToNull(cmp.getExpressionHelper().getString(cfg.get("path", String.class)));
    final String parentPage = StringUtils.trimToNull(cmp.getExpressionHelper().getString(cfg.get("parentPage", String.class)));
    final String pageId = cfg.get("pageId", String.class);

    Resource contentResource = resourceResolver.getResource(path);

    if (contentResource == null) {
        return;
    }

    final String rootPath = "/content/screens";

    final List<Resource> crumbs = new ArrayList<Resource>();

    Resource current = contentResource;

    while (current != null) {
        ValueMap crumbVM = new ValueMapDecorator(new HashMap<String, Object>());
        boolean isRoot = current.getPath().equals(rootPath);

        String title;
        boolean isFolder = false;
        String thumbnailURL = null;

        Page cqPage = current.adaptTo(Page.class);
        Device device = current.adaptTo(Device.class);
        if (cqPage != null) {
            title = cqPage.getTitle();
            thumbnailURL = getThumbnailUrl(cqPage, 48, 48);
        } else if (device != null) {
            ValueMap deviceProps = device.adaptTo(ValueMap.class);
            title = deviceProps.get("jcr:title", String.class);
            if (title == null || title.isEmpty()) {
                title = device.getId();
            }
            isFolder = false;
        }
        else if (isRoot) {
            title = i18n.get("Screens");
            isFolder = true;
        } else {
            ValueMap vm = current.getValueMap();
            title = vm.get("jcr:content/jcr:title", vm.get("jcr:title", current.getName()));
            isFolder = true;
        }

        if (cqPage != null && cqPage.getContentResource().isResourceType(ScreensConstants.RT_DEVICE_FOLDER)) {
            if (pageId != null && "screens-PendingDevices".equals(pageId)) {
                crumbVM.put("title", i18n.get("Pending Devices"));
                crumbVM.put("href", "/screens/devices.html" + current.getPath());
                crumbs.add(new ValueMapResource(resourceResolver, new ResourceMetadata(), "nt:unstructured", crumbVM));
                crumbVM = new ValueMapDecorator(new HashMap<String, Object>());
            }
            crumbVM.put("href", "/screens/devices.html" + current.getPath());
        }
        else if (parentPage != null && current != contentResource) {
            crumbVM.put("href", parentPage + current.getPath());
        }
        else {
            crumbVM.put("path", current.getPath());
        }
        crumbVM.put("title", title);
        crumbVM.put("thumbnail", thumbnailURL);
        crumbVM.put("isFolder", isFolder);

        crumbs.add(new ValueMapResource(resourceResolver, new ResourceMetadata(), "nt:unstructured", crumbVM));

        if (isRoot) {
            break;
        }

        if (device != null) {
            Resource tenantResource = resourceResolver.resolve("/content/screens/" + current.getParent().getParent().getName());
            Page tenantPage = tenantResource != null ? tenantResource.adaptTo(Page.class) : null;
            if (tenantPage != null) {
                Iterator<Page> deviceFolderIterator = tenantPage.listChildren(new Filter<Page>() {
                    public boolean includes(Page child) {
                        return child.getContentResource().isResourceType(ScreensConstants.RT_DEVICE_FOLDER);
                    }
                });
                if (deviceFolderIterator.hasNext()) {
                    current = deviceFolderIterator.next().adaptTo(Resource.class);
                } else {
                    current = current.getParent();
                }
            } else {
                current = current.getParent();
            }
        }
        else {
            current = current.getParent();
        }
    }

    request.setAttribute(DataSource.class.getName(), new SimpleDataSource(crumbs.iterator()));
%><%!

    private String getThumbnailUrl(Page page, int width, int height) {
        String ck = "";

        ValueMap metadata = page.getProperties("image/file/jcr:content");
        if (metadata != null) {
            Calendar cal = metadata.get("jcr:lastModified", Calendar.class);
            if (cal != null) {
                ck = "" + (cal.getTimeInMillis() / 1000);
            }
        }

        return Text.escapePath(page.getPath()) + ".thumb." + width + "." + height + ".png?ck=" + ck;
    }
%>