FrameworkContentExporterUtils.java 6.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.Filter
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageFilter
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.mobile.angular.data.util;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.day.cq.commons.Filter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageFilter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

public class FrameworkContentExporterUtils {
    public static final String NG_PAGE_RESOURCE_TYPE = "mobileapps/components/angular/ng-page";
    public static final String NG_FRAMEWORK_TYPE_ID = "angular";
    private static final String JCR_CONTENT = "jcr:content/";

    public static String getRelativePathToDescendantResource(Resource parentResource, Resource descendantResource) {
        return FrameworkContentExporterUtils.getRelativePathToDescendantPath(parentResource, descendantResource.getPath());
    }

    public static String getRelativePathToDescendantPath(Resource parentResource, String absoluteDescendantPath) {
        String parentPath = parentResource.getPath();
        if (absoluteDescendantPath.indexOf(parentPath) == 0) {
            String relativeResourcePath = parentResource.getName() + absoluteDescendantPath.substring(parentPath.length());
            return relativeResourcePath;
        }
        return null;
    }

    public static String getPathToAsset(Resource parentResource, String absoluteDescendantAssetPath, boolean appExport) {
        if (!appExport) {
            return absoluteDescendantAssetPath;
        }
        String relativeResourcePath = FrameworkContentExporterUtils.getRelativePathToDescendantPath(parentResource, absoluteDescendantAssetPath);
        relativeResourcePath = FrameworkContentExporterUtils.replaceJcrContent(relativeResourcePath);
        return relativeResourcePath;
    }

    public static Resource getTopLevelAppResource(Resource resource) {
        Resource parent = resource.getParent();
        if (parent == null || !"cq:Page".equals(parent.getResourceType())) {
            return resource;
        }
        MobileResource parentMobileRes = (MobileResource)parent.adaptTo(MobileResource.class);
        if (parentMobileRes.isA(MobileResourceType.CONTENT.getType(), MobileResourceType.INSTANCE.getType())) {
            return resource;
        }
        return FrameworkContentExporterUtils.getTopLevelAppResource(parent);
    }

    public static boolean isTopLevelAppResource(Resource resource) {
        if (resource == null) {
            return false;
        }
        return resource.equals((Object)FrameworkContentExporterUtils.getTopLevelAppResource(resource));
    }

    public static long getMillisecondsSinceUnixEpoch() {
        return System.currentTimeMillis();
    }

    public static List<Page> getAllAngularDescendantPages(Page page, PageFilter pageFilter) {
        ArrayList<Page> pageList = new ArrayList<Page>();
        FrameworkContentExporterUtils.getAllDescendantsOfResourceType(page, "mobileapps/components/angular/ng-page", pageFilter, pageList);
        return pageList;
    }

    public static String getJsFriendlyResourceName(String resourcePath) {
        if (resourcePath == null) {
            return null;
        }
        String uniqueName = resourcePath;
        String key = "jcr:content/";
        int indexOfKey = uniqueName.indexOf(key);
        if (indexOfKey > 0) {
            uniqueName = uniqueName.substring(indexOfKey + key.length());
        }
        return uniqueName.replaceAll("[^A-Za-z0-9]", "");
    }

    public static String getRelativePathToRootLevel(Resource resource) {
        String path = "";
        Resource ancestor = resource.getParent();
        while ((ancestor = ancestor.getParent()) != null) {
            path = path + "../";
        }
        return path;
    }

    public static List<Resource> getAllAngularPageComponents(Resource pageContentResource) {
        ArrayList<Resource> angularComponents = new ArrayList<Resource>();
        FrameworkContentExporterUtils.getAllAngularPageComponentsHelper(pageContentResource, angularComponents);
        return angularComponents;
    }

    public static Resource getAncestorTemplateResource(Resource resource, String resourceType) {
        if (resource == null || resourceType == null) {
            return null;
        }
        Resource contentResource = resource.getChild("jcr:content");
        if (contentResource != null && contentResource.isResourceType(resourceType)) {
            return resource;
        }
        return FrameworkContentExporterUtils.getAncestorTemplateResource(resource.getParent(), resourceType);
    }

    public static String getRelativeComponentPath(String path) {
        if (path == null || !path.contains("jcr:content/")) {
            return path;
        }
        return path.substring(path.indexOf("jcr:content/") + "jcr:content/".length());
    }

    private static void getAllAngularPageComponentsHelper(Resource resource, List<Resource> components) {
        Iterator resourceChildrenIter = resource.listChildren();
        while (resourceChildrenIter.hasNext()) {
            Resource childResource = (Resource)resourceChildrenIter.next();
            ValueMap childProperties = (ValueMap)childResource.adaptTo(ValueMap.class);
            if ("angular".equals(childProperties.get("frameworkType", String.class))) {
                components.add(childResource);
            }
            FrameworkContentExporterUtils.getAllAngularPageComponentsHelper(childResource, components);
        }
    }

    private static void getAllDescendantsOfResourceType(Page page, String resourceType, PageFilter pageFilter, List<Page> list) {
        Iterator children = page.listChildren((Filter)pageFilter, true);
        while (children.hasNext()) {
            Page child = (Page)children.next();
            Resource contentResource = child.getContentResource();
            if (contentResource == null || !contentResource.isResourceType(resourceType)) continue;
            list.add(child);
        }
    }

    private static String replaceJcrContent(String path) {
        if (path == null) {
            return null;
        }
        return path.replaceAll("/jcr:content/", "/jcr_content/");
    }
}