LaunchImpl.java 6.66 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchException
 *  com.adobe.cq.launches.api.LaunchSource
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.Template
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.wcm.launches.impl;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchSource;
import com.adobe.cq.wcm.launches.impl.LaunchSourceImpl;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;

public class LaunchImpl
implements Launch {
    private final Resource resource;
    private final Resource contentRes;
    private final ResourceResolver resolver;
    private final ValueMap props;
    private final ValueMap contentProps;
    private List<LaunchSource> launchSources = null;

    LaunchImpl(Resource r) throws LaunchException {
        if (r == null) {
            throw new LaunchException("Cannot instantiate launch. Resource cannot be null.");
        }
        if (r.isResourceType("wcm/launches/components/launch")) {
            this.contentRes = r;
            this.resource = r.getParent();
        } else {
            this.contentRes = r.getChild("jcr:content");
            if (this.contentRes == null) {
                throw new LaunchException("Cannot instantiate launch. Resource has no jcr:content child node.");
            }
            if (!this.contentRes.isResourceType("wcm/launches/components/launch")) {
                throw new LaunchException("Cannot instantiate launch. Invalid resource type for [" + this.contentRes.getPath() + "].");
            }
            this.resource = r;
        }
        this.resolver = this.resource.getResourceResolver();
        this.props = (ValueMap)this.resource.adaptTo(ValueMap.class);
        this.contentProps = (ValueMap)this.contentRes.adaptTo(ValueMap.class);
        if (this.getRootResource() == null) {
            throw new LaunchException("Cannot instantiate launch. Root resource cannot be null.");
        }
    }

    public Resource getResource() {
        return this.resource;
    }

    public Resource getRootResource() {
        Resource sourceRootRes = this.getSourceRootResource();
        if (sourceRootRes != null) {
            return this.resource.getChild(sourceRootRes.getPath().substring(1));
        }
        return null;
    }

    public Resource getSourceRootResource() {
        String sourceRootResPath = (String)this.contentProps.get("sourceRootResource", String.class);
        return sourceRootResPath != null ? this.resolver.getResource(sourceRootResPath) : null;
    }

    public String getTitle() {
        return (String)this.contentProps.get("jcr:title", String.class);
    }

    public Calendar getLiveDate() {
        return (Calendar)this.contentProps.get("liveDate", Calendar.class);
    }

    public boolean isProductionReady() {
        return (Boolean)this.contentProps.get("isProductionReady", (Object)false);
    }

    public boolean isLiveCopy() {
        return (Boolean)this.contentProps.get("isLiveCopy", (Object)false);
    }

    public boolean isDeep() {
        return (Boolean)this.contentProps.get("isDeep", (Object)false);
    }

    public Calendar getCreated() {
        return (Calendar)this.props.get("jcr:created", Calendar.class);
    }

    public String getCreatedBy() {
        return (String)this.props.get("jcr:createdBy", String.class);
    }

    public Calendar getModified() {
        return (Calendar)this.contentProps.get("cq:lastModified", (Object)this.getCreated());
    }

    public String getModifiedBy() {
        return (String)this.contentProps.get("cq:lastModifiedBy", (Object)this.getCreatedBy());
    }

    public Calendar getLastPromoted() {
        return (Calendar)this.contentProps.get("lastPromoted", Calendar.class);
    }

    public String getLastPromotedBy() {
        return (String)this.contentProps.get("lastPromotedBy", String.class);
    }

    public boolean containsResource(Resource productionResource) {
        Resource r;
        Page p;
        if (productionResource == null) {
            return false;
        }
        try {
            r = LaunchUtils.getLaunchResource(this, productionResource);
        }
        catch (LaunchException e) {
            return false;
        }
        if (r != null && (p = (Page)r.adaptTo(Page.class)) != null) {
            Template t = p.getTemplate();
            return t == null || !"/libs/launches/templates/outofscope".equals(t.getPath());
        }
        return false;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public int compareTo(Launch launch) {
        int c;
        if (launch == null) {
            return 1;
        }
        if (this.getLiveDate() != null) {
            if (launch.getLiveDate() == null) return 1;
            c = this.getLiveDate().compareTo(launch.getLiveDate());
            if (c != 0) {
                return c;
            }
        } else if (launch.getLiveDate() != null) {
            return -1;
        }
        if ((c = this.getCreated().compareTo(launch.getCreated())) == 0) return this.getResource().getPath().compareTo(launch.getResource().getPath());
        return c;
    }

    public List<LaunchSource> getLaunchSources() {
        if (this.launchSources == null) {
            Iterable childList;
            this.launchSources = new ArrayList<LaunchSource>();
            Resource parentResource = this.contentRes.getChild("sources");
            if (parentResource != null && (childList = parentResource.getChildren()) != null) {
                for (Resource child : childList) {
                    ValueMap valueMap = (ValueMap)child.adaptTo(ValueMap.class);
                    String strPath = (String)valueMap.get("sourceRootResource", String.class);
                    Resource childResource = this.resolver.getResource(strPath);
                    boolean childIsDeep = (Boolean)valueMap.get("isDeep", (Object)false);
                    this.launchSources.add(new LaunchSourceImpl(childResource, childIsDeep));
                }
            }
        }
        if (this.launchSources.size() == 0) {
            this.launchSources.add(new LaunchSourceImpl(this.getSourceRootResource(), this.isDeep()));
        }
        return this.launchSources;
    }
}