Featured

Thursday 30 April 2015

Custom workflow in cq5



       Here we are implementing a workflow to set a node property to the payload which we choose for.

       Firstly we need to register our workflow service, and then we can use this service while creating the workflow by using the workflow console (http://localhost:4502/libs/cq/workflow/content/console.html) .


package com.demo.train;

import org.osgi.framework.Constants;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;

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 javax.jcr.Node;
import javax.jcr.RepositoryException;

@Component
@Service
@Properties({
    @Property(name = Constants.SERVICE_DESCRIPTION, value = "Workflow process implementation"),
    @Property(name = Constants.SERVICE_VENDOR, value = "Adobe"),
    @Property(name = "process.label", value = "Property Setter Workflow")
})
public class PropSetter implements WorkflowProcess
{
    private static final String TYPE_JCR_PATH = "JCR_PATH";
   
    @Override
    public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws                                                                                                                                              WorkflowException
    {      
        WorkflowData workflowData = item.getWorkflowData();
   
        // If the workflow payload is a path in the JCR
        if (workflowData.getPayloadType().equals(TYPE_JCR_PATH))
        {
            String path = workflowData.getPayload().toString() + "/jcr:content";      
            try
            {
                Node node = (Node) session.getSession().getItem(path);      
                if (node != null)
                {
                    node.setProperty("myproperty", readArgument(args));
                    session.getSession().save();
                }
            }
            catch (RepositoryException e)
            {
                throw new WorkflowException(e.getMessage(), e);
            }
        }
    }
    private static boolean readArgument(MetaDataMap args)
    {
        String argument = args.get("PROCESS_ARGS", "false");
        return argument.equalsIgnoreCase("true");
    }
}

Now our workflow service ready, and we can use this service in "ProcessStep" of workflow and can build a new workflow.

This workflow can be applied for any payload.

Thanks.


Hope it helps :)
Thanks,
SonyCharan

No comments: