CreateLanguageCopyLaunchProcess.java 7.62 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchManager
 *  com.adobe.cq.launches.api.LaunchManager$CreateOptions
 *  com.adobe.cq.launches.api.LaunchManagerFactory
 *  com.day.cq.workflow.WorkflowException
 *  com.day.cq.workflow.WorkflowSession
 *  com.day.cq.workflow.exec.WorkItem
 *  com.day.cq.workflow.exec.WorkflowData
 *  com.day.cq.workflow.exec.WorkflowProcess
 *  com.day.cq.workflow.metadata.MetaDataMap
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.translation.impl.process;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.translation.impl.process.AbstractWCMTranslationProcess;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.util.Calendar;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={WorkflowProcess.class})
@Properties(value={@Property(name="service.description", value={"Create Launch for Language Copy Workflow Process"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"WCM: Create Launch for Language Copy Process"})})
public class CreateLanguageCopyLaunchProcess
extends AbstractWCMTranslationProcess {
    private static final Logger log = LoggerFactory.getLogger(CreateLanguageCopyLaunchProcess.class);
    private static final String META_TITLE = "workflowLaunchTitle";
    private static final String META_LIVE_DATE = "liveDate";
    private static final String PROP_IS_UPDATE_LANGUAGE_COPY = "isUpdateLanguageCopy";
    @Reference
    private ResourceResolverFactory resourceResolverFactory = null;
    @Reference
    LaunchManagerFactory launchesManagerFactory = null;

    public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        log.debug("creating launch for workitem {}", (Object)(item != null ? item.getId() : "<null>"));
        String processArgs = (String)args.get("PROCESS_ARGS", String.class);
        if (processArgs == null) {
            throw new WorkflowException("Provide launch rollout configs as process arguments");
        }
        String[] arguments = processArgs.trim().split(" ");
        if (arguments.length != 3) {
            throw new WorkflowException("Provide 3 rollout configs: source rollout, source rollout (shallow) and promote rollout");
        }
        String sourceRolloutConfig = arguments[0];
        String sourceRolloutConfigShallow = arguments[1];
        String promoteRolloutConfig = arguments[2];
        ResourceResolver resourceResolver = null;
        try {
            resourceResolver = this.resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "translation-job"));
            LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resourceResolver);
            WorkflowData data = item.getWorkflowData();
            MetaDataMap metaData = data.getMetaDataMap();
            String payloadData = this.getPayloadPath(data);
            Resource launchResource = resourceResolver.getResource(payloadData);
            if (launchResource == null || ResourceUtil.isNonExistingResource((Resource)launchResource)) {
                throw new WorkflowException("Failed to load content path from payload");
            }
            String masterLanguage = (String)metaData.get("language", String.class);
            if (StringUtils.isEmpty((CharSequence)masterLanguage)) {
                throw new WorkflowException("Failed to load master language from payload.");
            }
            String title = (String)metaData.get("workflowLaunchTitle", String.class);
            Calendar liveDate = (Calendar)metaData.get("liveDate", Calendar.class);
            boolean deep = "true".equals(metaData.get("deep", String.class));
            LaunchManager.CreateOptions options = new LaunchManager.CreateOptions();
            options.resource = launchResource;
            options.title = title;
            options.liveDate = liveDate;
            options.isDeep = deep;
            options.isLiveCopy = false;
            String[] arrstring = new String[1];
            arrstring[0] = deep ? sourceRolloutConfig : sourceRolloutConfigShallow;
            options.sourceRolloutConfigs = arrstring;
            options.promoteRolloutConfigs = new String[]{promoteRolloutConfig};
            Launch launch = launchManager.createLaunch(options);
            if (launch != null) {
                Resource contentResource = launch.getResource().getChild("jcr:content");
                if (contentResource != null) {
                    ModifiableValueMap properties = (ModifiableValueMap)contentResource.adaptTo(ModifiableValueMap.class);
                    properties.put((Object)"isUpdateLanguageCopy", (Object)true);
                    contentResource.getResourceResolver().commit();
                }
                String newPayloadPath = launch.getResource().getPath();
                this.updateWorkflowData(item, workflowSession, "launchRootResourcePath", newPayloadPath);
            }
        }
        catch (Exception e) {
            log.error("Error during execution of launch creation for language copy process.", (Throwable)e);
            throw new WorkflowException((Throwable)e);
        }
        finally {
            if (resourceResolver != null && resourceResolver.isLive()) {
                resourceResolver.close();
            }
        }
    }

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }

    protected void bindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        this.launchesManagerFactory = launchManagerFactory;
    }

    protected void unbindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        if (this.launchesManagerFactory == launchManagerFactory) {
            this.launchesManagerFactory = null;
        }
    }
}