CreateLaunchWorkflowProcess.java 7.22 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.LaunchManager
 *  com.adobe.cq.launches.api.LaunchManagerFactory
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.granite.taskmanagement.Task
 *  com.adobe.granite.taskmanagement.TaskManager
 *  com.adobe.granite.taskmanagement.TaskManagerException
 *  com.adobe.granite.taskmanagement.TaskManagerFactory
 *  com.adobe.granite.workflow.WorkflowException
 *  com.adobe.granite.workflow.WorkflowSession
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.exec.WorkflowData
 *  com.adobe.granite.workflow.exec.WorkflowProcess
 *  com.adobe.granite.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.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.projects.wcm.impl.workflow;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.wcm.impl.workflow.AbstractWCMProjectProcess;
import com.adobe.granite.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskManager;
import com.adobe.granite.taskmanagement.TaskManagerException;
import com.adobe.granite.taskmanagement.TaskManagerFactory;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import java.util.Calendar;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={WorkflowProcess.class})
@Properties(value={@Property(name="service.description", value={"Create Launch Workflow Process"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"WCM: Create Launch Process"})})
public class CreateLaunchWorkflowProcess
extends AbstractWCMProjectProcess {
    private static final Logger log = LoggerFactory.getLogger(CreateLaunchWorkflowProcess.class);
    private static final String META_SHALLOW = "shallow";
    private static final String META_LIVE_COPY = "isLiveCopy";
    @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>"));
        ResourceResolver resourceResolver = (ResourceResolver)workflowSession.adaptTo(ResourceResolver.class);
        MetaDataMap metaData = item.getWorkflowData().getMetaDataMap();
        String contentPath = this.getPayloadPath(item.getWorkflowData());
        if (StringUtils.isEmpty((CharSequence)contentPath)) {
            throw new WorkflowException("Failed to load content path from payload");
        }
        Resource launchResource = resourceResolver.getResource(contentPath);
        if (launchResource == null) {
            throw new WorkflowException("Failed to load content resource from payload");
        }
        String title = this.getStringParam(metaData, "workflowTitle", true, null);
        Calendar liveDate = this.getCalendarParam(metaData, "liveDate", false, null);
        if (liveDate == null) {
            liveDate = this.getCalendarParam(metaData, "taskDueDate", false, null);
        }
        boolean shallow = "true".equals(metaData.get("shallow", String.class));
        boolean isLiveCopy = "true".equals(metaData.get("isLiveCopy", String.class));
        LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resourceResolver);
        Launch launch = null;
        try {
            launch = launchManager.createLaunch(launchResource, title, liveDate, !shallow, isLiveCopy);
            if (launch != null) {
                Resource projectResource;
                Project project;
                String newPayloadPath = launch.getRootResource().getPath();
                this.updateWorkflowPayload(item, workflowSession, newPayloadPath);
                Resource workLinkResource = this.getProjectWorkLink(item.getWorkflowData(), resourceResolver);
                if (workLinkResource != null) {
                    ModifiableValueMap valueMap = (ModifiableValueMap)workLinkResource.adaptTo(ModifiableValueMap.class);
                    valueMap.put((Object)"suffix", (Object)newPayloadPath);
                }
                if ((projectResource = (Resource)(project = this.getProject(item.getWorkflowData(), resourceResolver)).adaptTo(Resource.class)) != null) {
                    TaskManager taskManager = this.getTaskManager(projectResource);
                    Task task = taskManager.getTaskManagerFactory().newTask(launch.getTitle());
                    task.setCurrentAssignee(item.getCurrentAssignee());
                    task.setContentPath(newPayloadPath);
                    task.setName("Launch Creation Complete");
                    task.setDescription("The launch creation has been completed. Please see payload for this task for the final result. An approval process has started.");
                    taskManager.createTask(task);
                }
            }
        }
        catch (LaunchException e) {
            log.error("Error during execution of launch creation process.", (Throwable)e);
            throw new WorkflowException(e.getMessage());
        }
        catch (Exception e) {
            if (launch != null) {
                launchManager.deleteLaunch(launch);
            }
            if (e instanceof TaskManagerException) {
                throw new WorkflowException("Failed to add task to " + launchResource.getPath(), (Throwable)e);
            }
            log.error("Error during execution of email creation process.", (Throwable)e);
            throw new WorkflowException(e.getMessage());
        }
    }

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

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