CreateEmailWorkflowProcess.java 7.79 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.granite.taskmanagement.Task
 *  com.adobe.granite.taskmanagement.TaskManager
 *  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
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.PageManagerFactory
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.designimporter.CanvasBuildOptions
 *  com.day.cq.wcm.designimporter.DesignImportResult
 *  com.day.cq.wcm.designimporter.DesignPackageImporter
 *  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.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.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 com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.designimporter.CanvasBuildOptions;
import com.day.cq.wcm.designimporter.DesignImportResult;
import com.day.cq.wcm.designimporter.DesignPackageImporter;
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 Email Workflow Process"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"WCM: Create Email Process"})})
public class CreateEmailWorkflowProcess
extends AbstractWCMProjectProcess {
    private static final Logger log = LoggerFactory.getLogger(CreateEmailWorkflowProcess.class);
    private static final String NAME_TEMPLATE_PATH = "templatePath";
    private static final String NAME_PAGE_LABEL = "pageLabel";
    @Reference
    private PageManagerFactory pageManagerFactory;
    @Reference
    private DesignPackageImporter designPackageImporter;

    public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        log.debug("creating email 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 emailResource = resourceResolver.getResource(contentPath);
        if (emailResource == null) {
            throw new WorkflowException("Failed to load content resource from payload");
        }
        PageManager pageManager = this.pageManagerFactory.getPageManager(resourceResolver);
        Page emailPage = null;
        try {
            Project project;
            Resource projectResource;
            String pageTitle = this.getStringParam(metaData, "workflowTitle", true, null);
            String pageLabel = this.getStringParam(metaData, "pageLabel", false, null);
            String templatePath = this.getStringParam(metaData, "templatePath", true, null);
            String designPackagePath = this.getDesignPackagePath(metaData, resourceResolver);
            Calendar liveDate = this.getCalendarParam(metaData, "liveDate", false, null);
            if (liveDate != null) {
                metaData.put((Object)"absoluteTime", (Object)liveDate.getTimeInMillis());
            }
            emailPage = pageManager.create(emailResource.getPath(), pageLabel, templatePath, pageTitle);
            if (StringUtils.isNotBlank((CharSequence)designPackagePath)) {
                this.designPackageImporter.importDesignPackage(emailPage, designPackagePath, null);
            }
            String newPayloadPath = emailPage.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(pageTitle);
                task.setCurrentAssignee(item.getCurrentAssignee());
                task.setContentPath(emailResource.getPath());
                task.setName("Email Creation Complete");
                task.setDescription("The email creation has been completed. Please see payload for this task for the final result. An approval process has started.");
                taskManager.createTask(task);
            }
        }
        catch (Exception e1) {
            if (emailPage != null) {
                try {
                    pageManager.delete(emailPage, false);
                }
                catch (WCMException e2) {
                    log.error("Error while attempting to delete email page.", (Throwable)e2);
                }
            }
            log.error("Error during execution of email creation process.", (Throwable)e1);
            throw new WorkflowException(e1.getMessage());
        }
    }

    protected void bindPageManagerFactory(PageManagerFactory pageManagerFactory) {
        this.pageManagerFactory = pageManagerFactory;
    }

    protected void unbindPageManagerFactory(PageManagerFactory pageManagerFactory) {
        if (this.pageManagerFactory == pageManagerFactory) {
            this.pageManagerFactory = null;
        }
    }

    protected void bindDesignPackageImporter(DesignPackageImporter designPackageImporter) {
        this.designPackageImporter = designPackageImporter;
    }

    protected void unbindDesignPackageImporter(DesignPackageImporter designPackageImporter) {
        if (this.designPackageImporter == designPackageImporter) {
            this.designPackageImporter = null;
        }
    }
}