InvokeDDXProcess.java 10.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.fd.assembler.client.AssemblerOptionSpec
 *  com.adobe.fd.assembler.client.AssemblerResult
 *  com.adobe.fd.assembler.service.AssemblerService
 *  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.metadata.MetaDataMap
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Session
 *  javax.jcr.nodetype.NodeType
 *  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.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.workflow.assembler;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.assembler.client.AssemblerOptionSpec;
import com.adobe.fd.assembler.client.AssemblerResult;
import com.adobe.fd.assembler.service.AssemblerService;
import com.adobe.fd.workflow.internal.common.AEMFDWorkflowProcess;
import com.adobe.fd.workflow.utils.DocumentUtils;
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.metadata.MetaDataMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
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.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
@Properties(value={@Property(name="service.description", value={"InvokeDDXProcess"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"InvokeDDXProcess"})})
public class InvokeDDXProcess
extends AEMFDWorkflowProcess {
    private static Logger LOGGER = LoggerFactory.getLogger(InvokeDDXProcess.class);
    private static final String DDX_PARAM = "ddx";
    private static final String POPULATE_MAP_FROM_PAYLOAD_PARAM = "populateFromPayLoad";
    private static final String INPUT_DOCS_PARAM = "inputDocs";
    private static final String INPUT_KEY_PARAM = "inputkey";
    private static final String SOURCE_PATH_PARAM = "sourcePath";
    private static final String LOG_LEVEL_PARAM = "logLevel";
    private static final String VALIDATE_ONLY_PARAM = "validateOnly";
    private static final String FAIL_ON_ERROR_PARAM = "failOnError";
    private static final String BATES_NUMBER_PARAM = "batesNumber";
    private static final String DEFAULT_STYLE_PARAM = "defaultStyle";
    private static final String SAVE_TO_PAYLOAD_PARAM = "saveToPayload";
    private static final String OUTPUT_DOCS_PARAM = "outputDocs";
    private static final String OUTPUT_KEY_PARAM = "outputkey";
    private static final String OUTPUT_PATH_PARAM = "outputPath";
    private static final String JOB_LOG_PARAM = "jobLog";
    @Reference
    AssemblerService assemblerService;

    @Override
    protected void internal_execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        try {
            Map<String, Object> inputMap = this.createInputMap(workItem, workflowSession, metaDataMap);
            AssemblerOptionSpec options = this.createAssemblerOptions(metaDataMap);
            Document ddxDocument = DocumentUtils.getInputDocument(workItem, "ddx");
            if (ddxDocument == null) {
                throw new WorkflowException("DDX document cannot be null.");
            }
            AssemblerResult assemblerResult = this.assemblerService.invoke(ddxDocument, inputMap, options);
            if (!options.isValidateOnly()) {
                this.saveOutputDocuments(assemblerResult, workItem, workflowSession, metaDataMap);
            }
            if (metaDataMap.get("jobLog", String.class) != null) {
                DocumentUtils.saveOutputDocument(workItem, workflowSession, "jobLog", assemblerResult.getJobLog());
            }
        }
        catch (Exception e) {
            throw new WorkflowException((Throwable)e);
        }
    }

    private Map<String, Object> createInputMap(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws Exception {
        HashMap<String, Object> inputMap = new HashMap<String, Object>();
        if (metaDataMap.get("populateFromPayLoad", Boolean.class) != null) {
            boolean populateFromPayload = (Boolean)metaDataMap.get("populateFromPayLoad", Boolean.class);
            LOGGER.debug("populateFromPayLoad= " + populateFromPayload);
            if (populateFromPayload) {
                this.populateInputMapFromPayload(inputMap, workItem, workflowSession);
            }
        }
        if (metaDataMap.get("inputDocs", String[].class) != null) {
            String[] inputEntries = (String[])metaDataMap.get("inputDocs", String[].class);
            LOGGER.debug("inputDocs length = " + inputEntries.length);
            for (String inputEntry : inputEntries) {
                JSONObject jsonObject = new JSONObject(inputEntry);
                Document iDoc = DocumentUtils.getInputDocument(workItem.getWorkflowData().getPayload().toString(), jsonObject.getString("sourcePath"));
                if (iDoc == null) continue;
                inputMap.put(jsonObject.getString("inputkey"), (Object)iDoc);
            }
        }
        return inputMap;
    }

    private AssemblerOptionSpec createAssemblerOptions(MetaDataMap metaDataMap) {
        AssemblerOptionSpec options = new AssemblerOptionSpec();
        if (metaDataMap.get("logLevel", String.class) != null) {
            String logLevel = (String)metaDataMap.get("logLevel", String.class);
            LOGGER.debug("logLevel= " + logLevel);
            options.setLogLevel(logLevel);
        }
        if (metaDataMap.get("validateOnly", Boolean.class) != null) {
            boolean validateOnly = (Boolean)metaDataMap.get("validateOnly", Boolean.class);
            LOGGER.debug("validateOnly= " + validateOnly);
            options.setValidateOnly(validateOnly);
        }
        if (metaDataMap.get("failOnError", Boolean.class) != null) {
            boolean failOnError = (Boolean)metaDataMap.get("failOnError", Boolean.class);
            LOGGER.debug("failOnError= " + failOnError);
            options.setFailOnError(failOnError);
        }
        if (metaDataMap.get("batesNumber", Integer.class) != null) {
            int batesNumber = (Integer)metaDataMap.get("batesNumber", Integer.class);
            LOGGER.debug("batesNumber= " + batesNumber);
            options.setFirstBatesNumber(batesNumber);
        }
        if (metaDataMap.get("defaultStyle", String.class) != null) {
            String defaultStyle = (String)metaDataMap.get("defaultStyle", String.class);
            LOGGER.debug("defaultStyle= " + defaultStyle);
            options.setDefaultStyle(defaultStyle);
        }
        return options;
    }

    private void saveOutputDocuments(AssemblerResult assemblerResult, WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws Exception {
        String payloadPath = workItem.getWorkflowData().getPayload().toString();
        Map outputDocumentsMap = assemblerResult.getDocuments();
        if (metaDataMap.get("saveToPayload", Boolean.class) != null) {
            boolean saveToPayload = (Boolean)metaDataMap.get("saveToPayload", Boolean.class);
            LOGGER.debug("saveToPayload= " + saveToPayload);
            if (saveToPayload) {
                for (Map.Entry resultEntry : outputDocumentsMap.entrySet()) {
                    if (((Session)workflowSession.adaptTo(Session.class)).nodeExists(payloadPath + "/" + (String)resultEntry.getKey())) {
                        DocumentUtils.updateDocument(workflowSession, (Document)resultEntry.getValue(), payloadPath + "/" + (String)resultEntry.getKey());
                        continue;
                    }
                    DocumentUtils.createOrUpdateDocument(workflowSession, (Document)resultEntry.getValue(), payloadPath, (String)resultEntry.getKey());
                }
            }
        }
        if (metaDataMap.get("outputDocs", String[].class) != null) {
            String[] outputEntries = (String[])metaDataMap.get("outputDocs", String[].class);
            LOGGER.debug("outputDocs length = " + outputEntries.length);
            for (String outputEntry : outputEntries) {
                JSONObject jsonObject = new JSONObject(outputEntry);
                DocumentUtils.createOrUpdateDocument(workflowSession, (Document)outputDocumentsMap.get(jsonObject.getString("outputkey")), workItem.getWorkflowData().getPayload().toString(), jsonObject.getString("outputPath"));
            }
        }
    }

    private void populateInputMapFromPayload(Map<String, Object> inputMap, WorkItem workItem, WorkflowSession workflowSession) throws Exception {
        String payload = workItem.getWorkflowData().getPayload() + "";
        Node payLoadNode = ((Session)workflowSession.adaptTo(Session.class)).getNode(payload);
        this.populateMapFromNodeChildren(payLoadNode, inputMap);
    }

    private void populateMapFromNodeChildren(Node parentNode, Map<String, Object> inputMap) throws Exception {
        NodeIterator childItr = parentNode.getNodes();
        while (childItr.hasNext()) {
            Node childNode = childItr.nextNode();
            if (childNode.getPrimaryNodeType().toString().equals("dam:Asset")) {
                inputMap.put(childNode.getName(), (Object)new Document(childNode.getPath()));
                continue;
            }
            if (!childNode.getPrimaryNodeType().equals("{http://www.jcp.org/jcr/nt/1.0}folder") && !childNode.getPrimaryNodeType().toString().equals("sling:OrderedFolder") && !childNode.getPrimaryNodeType().toString().equals("sling:Folder")) continue;
            this.populateMapFromNodeChildren(childNode, inputMap);
        }
    }

    protected void bindAssemblerService(AssemblerService assemblerService) {
        this.assemblerService = assemblerService;
    }

    protected void unbindAssemblerService(AssemblerService assemblerService) {
        if (this.assemblerService == assemblerService) {
            this.assemblerService = null;
        }
    }
}