CreateManuscriptWorkflowProcess.java 8.12 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.granite.workflow.WorkflowException
 *  com.adobe.granite.workflow.WorkflowSession
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.exec.Workflow
 *  com.adobe.granite.workflow.exec.WorkflowData
 *  com.adobe.granite.workflow.exec.WorkflowProcess
 *  com.adobe.granite.workflow.metadata.MetaDataMap
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.AssetManager
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  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.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 */
package com.adobe.cq.projects.impl.workflow;

import com.adobe.cq.projects.api.Project;
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.Workflow;
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.commons.jcr.JcrUtil;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;

@Component
@Service(value={WorkflowProcess.class})
@Properties(value={@Property(name="service.description", value={"Create Manuscript Workflow Process"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"Create Manuscript Process"})})
public class CreateManuscriptWorkflowProcess
implements WorkflowProcess {
    private static final String ARTICLE_MIME_TYPE = "text/plain";
    private static final String ARTICLE_FILE_EXTENSION = ".txt";
    private static final String DEFAULT_PATH = "/libs/media/articles/admin/content/default.txt";
    private static final String DEFAULT_BODY = "Article goes here";
    private static final String ASSIGNMENT = "assignment";
    private static final String TARGET_WORD_COUNT = "targetWordCount";
    private static final String BRIEF = "brief";

    public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        Asset createdManuscriptAsset;
        ResourceResolver resourceResolver = (ResourceResolver)workflowSession.adaptTo(ResourceResolver.class);
        String projectPath = (String)item.getWorkflowData().getMetaDataMap().get("project.path", String.class);
        if (projectPath == null) {
            throw new WorkflowException("Failed to load project path identified by metadata: project.path");
        }
        Resource projectResource = resourceResolver.getResource(projectPath);
        if (projectResource == null || ResourceUtil.isNonExistingResource((Resource)projectResource)) {
            throw new WorkflowException("Failed to load project identified by project path " + projectPath);
        }
        Project project = (Project)projectResource.adaptTo(Project.class);
        Resource assetFolder = project.getAssetFolder();
        AssetManager assetManager = (AssetManager)resourceResolver.adaptTo(AssetManager.class);
        String title = (String)item.getWorkflowData().getMetaDataMap().get("manuscripttitle", String.class);
        if (StringUtils.isBlank((CharSequence)title)) {
            title = (String)item.getWorkflowData().getMetaDataMap().get("workflowTitle", String.class);
        }
        String brief = (String)item.getWorkflowData().getMetaDataMap().get("manuscriptbrief", String.class);
        String targetWordCount = (String)item.getWorkflowData().getMetaDataMap().get("manuscripttargetwordcount", String.class);
        Node target = (Node)assetFolder.adaptTo(Node.class);
        try {
            String name = JcrUtil.createValidChildName((Node)target, (String)title);
            if (target.hasNode(name + ".txt")) {
                String leafNodeName;
                int i = 0;
                do {
                    leafNodeName = name + String.valueOf(i) + ".txt";
                    ++i;
                } while (target.hasNode(leafNodeName));
                name = leafNodeName;
            } else {
                name = name + ".txt";
            }
            Session session = target.getSession();
            String fullManuscriptPath = target.getPath() + "/" + name;
            if (session.itemExists("/libs/media/articles/admin/content/default.txt/jcr:content")) {
                Node defaultNode = session.getNode("/libs/media/articles/admin/content/default.txt/jcr:content");
                createdManuscriptAsset = assetManager.createAsset(fullManuscriptPath, defaultNode.getProperty("jcr:data").getBinary().getStream(), "text/plain", true);
            } else {
                createdManuscriptAsset = assetManager.createAsset(fullManuscriptPath, (InputStream)new ByteArrayInputStream("Article goes here".getBytes()), "text/plain", true);
            }
            Node assetNode = (Node)createdManuscriptAsset.adaptTo(Node.class);
            Node jcrContent = assetNode.getNode("jcr:content");
            Node assignment = jcrContent.addNode("assignment", "nt:unstructured");
            assignment.setProperty("brief", brief);
            try {
                if (targetWordCount == null) {
                    targetWordCount = "";
                }
                assignment.setProperty("targetWordCount", Long.parseLong(targetWordCount));
            }
            catch (NumberFormatException ex) {
                assignment.setProperty("targetWordCount", (String)null);
            }
            Node metadata = jcrContent.getNode("metadata");
            metadata.setProperty("dc:title", title);
            jcrContent.setProperty("jcr:lastModified", (Calendar)new GregorianCalendar());
            assetNode.getSession().save();
        }
        catch (RepositoryException ex) {
            throw new WorkflowException("Could not create Article", (Throwable)ex);
        }
        if (createdManuscriptAsset != null) {
            String newPayloadPath = createdManuscriptAsset.getPath();
            WorkflowData data = this.cloneWorkflowDataWithNewPayload(workflowSession, newPayloadPath, item.getWorkflowData());
            workflowSession.updateWorkflowData(item.getWorkflow(), data);
            try {
                Workflow instance = item.getWorkflow();
                Method method = instance.getClass().getMethod("setWorkflowData", WorkflowData.class);
                if (method != null) {
                    method.invoke((Object)instance, new Object[]{data});
                }
            }
            catch (Exception e) {
                throw new WorkflowException("failed to update in-memory payload", (Throwable)e);
            }
        }
    }

    private WorkflowData cloneWorkflowDataWithNewPayload(WorkflowSession wfSession, String contentPath, WorkflowData wfToClone) {
        WorkflowData result = wfSession.newWorkflowData("JCR_PATH", (Object)contentPath);
        MetaDataMap sourceMap = wfToClone.getMetaDataMap();
        MetaDataMap targetMap = result.getMetaDataMap();
        for (String key : sourceMap.keySet()) {
            targetMap.put((Object)key, sourceMap.get((Object)key));
        }
        return result;
    }
}