BlueprintManagerImpl.java 3.36 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.Blueprint
 *  com.day.cq.wcm.msm.api.BlueprintManager
 *  com.day.cq.wcm.msm.api.RolloutConfigManager
 *  com.day.text.Text
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.SlingException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.Blueprint;
import com.day.cq.wcm.msm.api.BlueprintManager;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.cq.wcm.msm.impl.BlueprintImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerFactoryImpl;
import com.day.text.Text;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.sling.api.SlingException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BlueprintManagerImpl
implements BlueprintManager {
    private final Logger log = LoggerFactory.getLogger(BlueprintManagerImpl.class);
    private final RolloutConfigManager rolloutConfigManager;
    private final BlueprintManagerFactoryImpl factory;
    private final ResourceResolver resourceResolver;

    BlueprintManagerImpl(ResourceResolver resolver, RolloutConfigManager rolloutConfigManager, BlueprintManagerFactoryImpl factory) {
        this.resourceResolver = resolver;
        this.rolloutConfigManager = rolloutConfigManager;
        this.factory = factory;
    }

    public Collection<Blueprint> getBlueprints() {
        HashSet<BlueprintImpl> res = new HashSet<BlueprintImpl>();
        try {
            for (String path : this.factory.getBlueprintPaths()) {
                Page page = this.getPage(path);
                if (page == null) continue;
                res.add(new BlueprintImpl(page, this.rolloutConfigManager));
            }
        }
        catch (RepositoryException e) {
            throw new SlingException(e.getMessage(), (Throwable)e);
        }
        return Collections.unmodifiableSet(res);
    }

    public BlueprintImpl getBlueprint(String absPath) {
        try {
            Page page;
            if (this.factory.getBlueprintPaths().contains(absPath) && (page = this.getPage(absPath)) != null) {
                return new BlueprintImpl(page, this.rolloutConfigManager);
            }
        }
        catch (RepositoryException e) {
            this.log.error("Failed to retrieve Blueprint {}", (Throwable)e);
        }
        return null;
    }

    public BlueprintImpl getContainingBlueprint(String path) {
        for (Blueprint blueprint : this.getBlueprints()) {
            if (blueprint.getSitePath() == null || !Text.isDescendantOrEqual((String)blueprint.getSitePath(), (String)path)) continue;
            return (BlueprintImpl)blueprint;
        }
        return null;
    }

    private Page getPage(String absPath) {
        Resource resource = this.resourceResolver.getResource(absPath);
        if (resource == null) {
            return null;
        }
        return (Page)resource.adaptTo(Page.class);
    }
}