CanvasReferenceProvider.java 5.85 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.designer.Designer
 *  com.day.cq.wcm.api.reference.Reference
 *  com.day.cq.wcm.api.reference.ReferenceProvider
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.designimporter;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.reference.Reference;
import com.day.cq.wcm.api.reference.ReferenceProvider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class CanvasReferenceProvider
implements ReferenceProvider {
    private static final Logger logger = LoggerFactory.getLogger(CanvasReferenceProvider.class);
    private static final String IMPORTER_RESOURCE_TYPE = "wcm/designimporter/components/importer";
    private static final String CQ_CLIENT_LIBRARY_FOLDER = "cq:ClientLibraryFolder";
    private static final String NN_CANVAS = "canvas";
    private static final String PN_REIMPORT = "reimport";

    public List<Reference> findReferences(Resource resource) {
        ArrayList<Reference> references = new ArrayList<Reference>();
        LinkedList<Resource> foundComponents = new LinkedList<Resource>();
        this.findImporterComponent(resource, foundComponents);
        for (Resource importerComponent : foundComponents) {
            references.addAll(this.getReferences(importerComponent));
        }
        Collections.reverse(references);
        return references;
    }

    private List<Reference> getReferences(Resource importerComponent) {
        boolean hasCanvas = importerComponent.getChild("canvas") != null;
        ValueMap properties = (ValueMap)importerComponent.adaptTo(ValueMap.class);
        boolean isReimport = Boolean.TRUE.equals(properties.get((Object)"reimport"));
        LinkedList<Reference> references = new LinkedList<Reference>();
        if (hasCanvas && !isReimport) {
            try {
                references.addAll(this.getDesignReferences(importerComponent));
                references.addAll(this.getCanvasReferences(importerComponent));
            }
            catch (RepositoryException e) {
                logger.error("Error obtaining canvas references for the resource " + importerComponent.getPath(), (Throwable)e);
            }
        }
        return references;
    }

    private List<Reference> getDesignReferences(Resource importerComponent) throws RepositoryException {
        LinkedList<Reference> references = new LinkedList<Reference>();
        ResourceResolver resourceResolver = importerComponent.getResourceResolver();
        PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
        Page page = pageManager.getContainingPage(importerComponent);
        Designer designer = (Designer)resourceResolver.adaptTo(Designer.class);
        String designPath = designer.getDesignPath(page);
        String canvasDesignPath = designPath + "/" + "canvas" + importerComponent.getPath();
        Resource canvasDesign = resourceResolver.resolve(canvasDesignPath);
        this.addReferencesRecursive(canvasDesign, references);
        return references;
    }

    private List<Reference> getCanvasReferences(Resource importerComponent) throws RepositoryException {
        LinkedList<Reference> references = new LinkedList<Reference>();
        Resource canvas = importerComponent.getChild("canvas");
        String componentPath = "/apps/" + canvas.getResourceType();
        Resource component = canvas.getResourceResolver().resolve(componentPath);
        this.addReferencesRecursive(component, references);
        return references;
    }

    private void addReferencesRecursive(Resource root, List<Reference> references) throws RepositoryException {
        if (!this.isContentNode(root)) {
            if (this.isClientLibFolder(root)) {
                references.add(new Reference("artifact", root.getName(), root, -1));
            } else {
                references.add(0, new Reference("artifact", root.getName(), root, -1));
            }
        }
        Iterator iter = root.listChildren();
        while (iter.hasNext()) {
            this.addReferencesRecursive((Resource)iter.next(), references);
        }
    }

    private boolean isContentNode(Resource r) throws RepositoryException {
        return "jcr:content".equals(r.getName());
    }

    private boolean isClientLibFolder(Resource r) throws RepositoryException {
        ValueMap properties = (ValueMap)r.adaptTo(ValueMap.class);
        String nodeType = (String)properties.get("jcr:primaryType", String.class);
        return "cq:ClientLibraryFolder".equals(nodeType);
    }

    private void findImporterComponent(Resource root, List<Resource> components) {
        if ("wcm/designimporter/components/importer".equals(root.getResourceType())) {
            components.add(root);
        } else {
            Iterator childIterator = root.listChildren();
            while (childIterator.hasNext()) {
                Resource child = (Resource)childIterator.next();
                this.findImporterComponent(child, components);
            }
        }
    }
}