AFtoDORStep.java 8.52 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.addon.dor.DoROptions
 *  com.adobe.aemds.guide.addon.dor.DoRResult
 *  com.adobe.aemds.guide.addon.dor.DoRService
 *  com.adobe.aemfd.docmanager.Document
 *  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.adobe.granite.workflow.model.WorkflowNode
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.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.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.workflow.dorGeneration;

import com.adobe.aemds.guide.addon.dor.DoROptions;
import com.adobe.aemds.guide.addon.dor.DoRResult;
import com.adobe.aemds.guide.addon.dor.DoRService;
import com.adobe.aemfd.docmanager.Document;
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.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowNode;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.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.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
@Properties(value={@Property(name="service.description", value={"AF To DOR Conversion Step"}), @Property(name="process.label", value={"AF To DOR Conversion Step"})})
public class AFtoDORStep
implements WorkflowProcess {
    protected final Logger log;
    @Reference
    private DoRService dorService;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    public AFtoDORStep() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    public void execute(WorkItem workItem, WorkflowSession session, MetaDataMap args) throws WorkflowException {
        Document dor = null;
        try {
            Node afNode;
            String afPath = this.getAFPath(workItem);
            String relativeDataXmlPath = this.getInputDataXMLPath(workItem);
            String dorOutputPath = this.getDORPAth(workItem);
            Locale inputLocale = new Locale(this.getLocale(workItem));
            String payLoadPath = this.getPayloadPath(workItem);
            String dataXmlPath = null;
            Session userSession = (Session)session.adaptTo(Session.class);
            String afContentPath = afPath + "/" + "jcr:content";
            if (!(!userSession.nodeExists(afContentPath) || (afNode = userSession.getNode(afContentPath)).hasProperty("guide") && afNode.getProperty("guide").getString().equals("1"))) {
                throw new Exception("Not a valid Adaptive Form");
            }
            if (StringUtils.isNotBlank((String)relativeDataXmlPath)) {
                if ((relativeDataXmlPath.startsWith("FILE_PAYLOAD") || this.isPayloadFile(userSession, payLoadPath)) && dorOutputPath.startsWith("RELATIVE_PLOAD")) {
                    throw new Exception("Can't save node under node of type file");
                }
                dataXmlPath = DocumentUtils.getAbsolutePath(payLoadPath, relativeDataXmlPath);
            }
            DoROptions dorOptions = new DoROptions();
            ResourceResolver resourceResolver = this.getResourceResolver((Session)session.adaptTo(Session.class));
            dorOptions.setFormResource(resourceResolver.getResource(afPath));
            dorOptions.setData(new String(DocumentUtils.readFileContentForFileNode(userSession.getNode(dataXmlPath))));
            dorOptions.setLocale(inputLocale);
            DoRResult dorResult = this.dorService.render(dorOptions);
            dor = new Document(dorResult.getContent());
            String dorPath = null;
            if (dorOutputPath.startsWith("OVERWRITE:")) {
                dorOutputPath = "";
                dorPath = payLoadPath;
            } else {
                dorPath = (dorOutputPath = dorOutputPath.split(":")[1]).startsWith("/") ? payLoadPath + dorOutputPath : payLoadPath + "/" + dorOutputPath;
                dorOutputPath = dorOutputPath.contains("/") ? dorOutputPath.substring(0, dorOutputPath.lastIndexOf("/")) : "";
            }
            if (userSession.nodeExists(payLoadPath)) {
                Node parentNode = userSession.getNode(payLoadPath);
                if (StringUtils.isNotBlank((String)dorOutputPath)) {
                    DocumentUtils.createFolderPathForFile(userSession, parentNode, dorOutputPath);
                }
                DocumentUtils.createOrUpdateDocument(userSession, dor, dorPath);
            }
        }
        catch (Exception e) {
            throw new WorkflowException((Throwable)e);
        }
        finally {
            try {
                if (dor != null) {
                    dor.close();
                }
            }
            catch (IOException e) {
                this.log.error("exception while closing the dor", (Throwable)e);
            }
        }
    }

    protected ResourceResolver getResourceResolver(Session session) throws WorkflowException {
        try {
            HashMap<String, Session> authenticationMap = new HashMap<String, Session>();
            authenticationMap.put("user.jcr.session", session);
            return this.resourceResolverFactory.getResourceResolver(authenticationMap);
        }
        catch (Exception e) {
            throw new WorkflowException("Exception in getting Resource Resolver", (Throwable)e);
        }
    }

    private boolean isPayloadFile(Session session, String payloadPath) throws RepositoryException {
        Node payloadNode;
        boolean isFile = false;
        if (session.nodeExists(payloadPath) && (payloadNode = session.getNode(payloadPath)).hasProperty("jcr:primaryType") && payloadNode.getProperty("jcr:primaryType").getString().equals("nt:file")) {
            isFile = true;
        }
        return isFile;
    }

    private String getPayloadPath(WorkItem workItem) {
        return (String)workItem.getWorkflowData().getPayload();
    }

    private String getAFPath(WorkItem workItem) {
        MetaDataMap args = workItem.getNode().getMetaDataMap();
        return (String)args.get("AF_PATH", String.class);
    }

    private String getInputDataXMLPath(WorkItem workItem) {
        MetaDataMap args = workItem.getNode().getMetaDataMap();
        return (String)args.get("INPUT_DATAXML", String.class);
    }

    private String getDORPAth(WorkItem workItem) {
        MetaDataMap args = workItem.getNode().getMetaDataMap();
        return (String)args.get("DOR_PATH", String.class);
    }

    private String getLocale(WorkItem workItem) {
        MetaDataMap args = workItem.getNode().getMetaDataMap();
        return (String)args.get("LOCALE", String.class);
    }

    protected void bindDorService(DoRService doRService) {
        this.dorService = doRService;
    }

    protected void unbindDorService(DoRService doRService) {
        if (this.dorService == doRService) {
            this.dorService = null;
        }
    }

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

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