WorkflowActionFactory.java 6.71 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  com.day.cq.wcm.msm.commons.BaseAction
 *  com.day.cq.wcm.msm.commons.BaseActionFactory
 *  com.day.cq.workflow.WorkflowException
 *  com.day.cq.workflow.WorkflowService
 *  com.day.cq.workflow.WorkflowSession
 *  com.day.cq.workflow.exec.Workflow
 *  com.day.cq.workflow.exec.WorkflowData
 *  com.day.cq.workflow.model.WorkflowModel
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Modified
 *  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.ValueMap
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.actions;

import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.commons.BaseAction;
import com.day.cq.wcm.msm.commons.BaseActionFactory;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.model.WorkflowModel;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
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.ValueMap;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
public class WorkflowActionFactory
extends BaseActionFactory<BaseAction> {
    public static final String WF_DATA_SRC_PATH = "srcPath";
    public static final String ACTION_PARAM_TARGET = "target";
    @Deprecated
    @Property(name="cq.wcm.msm.action.rank")
    private static final int RANK = 701;
    @Deprecated
    @Property(name="cq.wcm.msm.action.title")
    private static final String TITLE = "Start target workflow";
    @Deprecated
    @Property(name="cq.wcm.msm.action.parameter")
    private static final String PARAMETER = "msm:actionWorkflow";
    @Deprecated
    @Property(name="cq.wcm.msm.action.properties")
    private static final String[] PROPS = new String[]{"target"};
    private static final Logger log = LoggerFactory.getLogger(WorkflowActionFactory.class);
    @Property(name="liveActionName")
    private static final String[] LIVE_ACTION_NAME = new String[]{WorkflowActionFactory.class.getSimpleName(), "workflow"};
    @Reference
    private WorkflowService workflowService = null;

    public String createsAction() {
        return LIVE_ACTION_NAME[0];
    }

    protected WorkflowAction newActionInstance(ValueMap config) {
        if (config != null && config.containsKey((Object)"target")) {
            return new WorkflowAction(config, this, this.workflowService);
        }
        log.debug("Config not sufficient for Action, need Workflow Model ID");
        return null;
    }

    @Activate
    @Modified
    protected void activate(ComponentContext context) {
    }

    protected void bindWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

    protected void unbindWorkflowService(WorkflowService workflowService) {
        if (this.workflowService == workflowService) {
            this.workflowService = null;
        }
    }

    private static final class WorkflowAction
    extends BaseAction {
        private final WorkflowService workflowService;
        private final String workflowModelID;
        private WorkflowModel model;

        private WorkflowAction(ValueMap config, BaseActionFactory<BaseAction> factory, WorkflowService workflowService) {
            super(config, factory);
            this.workflowService = workflowService;
            this.workflowModelID = (String)config.get("target", String.class);
        }

        protected boolean handles(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws WCMException, RepositoryException {
            if (!relation.getStatus().isPage()) {
                log.debug("Relation is not a Page {}: do not handle", (Object)relation.getTargetPath());
                return false;
            }
            Node masterNode = (Node)source.adaptTo(Node.class);
            if (masterNode != null && this.workflowService != null) {
                try {
                    WorkflowSession wfSession = this.workflowService.getWorkflowSession(masterNode.getSession());
                    this.model = wfSession.getModel(this.workflowModelID);
                    if (this.model == null) {
                        log.debug("Configured Workflow-Model {} does not exist: do not handle", (Object)this.workflowModelID);
                        return false;
                    }
                    return true;
                }
                catch (WorkflowException e) {
                    throw new WCMException((Throwable)e);
                }
            }
            return false;
        }

        protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            assert (source.adaptTo(Node.class) != null && this.workflowService != null && this.model != null);
            Node masterNode = (Node)source.adaptTo(Node.class);
            try {
                WorkflowSession wfSession = this.workflowService.getWorkflowSession(masterNode.getSession());
                WorkflowData data = wfSession.newWorkflowData("JCR_PATH", (Object)Utils.getPagePath(relation.getTargetPath()));
                HashMap<String, String> metaData = new HashMap<String, String>();
                metaData.put("srcPath", Utils.getPagePath(source.getPath()));
                wfSession.startWorkflow(this.model, data, metaData);
            }
            catch (WorkflowException e) {
                throw new WCMException((Throwable)e);
            }
        }
    }

}