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

How to return Json data from servlet in cq5


                By passing some data to the servlet(We can use Ajax call to pass the data -- Refer here for Ajax call ), we can generate a json as per our requirement and return to the front end page / Dialog.


package com.demo.train;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.rmi.ServerException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.json.simple.JSONObject;
import java.util.UUID;

@SlingServlet(paths="/bin/convertPropsAsJson", methods = "POST", metatype=true)
public class HandleClaim extends org.apache.sling.api.servlets.SlingAllMethodsServlet {
     private static final long serialVersionUID = 23426532342349515L;
   
     @Reference
     private SlingRepository repository;
   
     public void bindRepository(SlingRepository repository) {
            this.repository = repository;
            }
         
     @Override
     protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
     
      try
      {
         //Get the submitted form data that is sent from the
              //CQ web page
          String id = UUID.randomUUID().toString();
          String value1 = request.getParameter("value1");
          String value2 = request.getParameter("value2");
          String value3 = request.getParameter("value3");
     
          //Encode the submitted form data to JSON
          JSONObject obj=new JSONObject();
          obj.put("id",id);
          obj.put("value1",value1);
          obj.put("value2",value2);
          obj.put("value3",value3);
         
             //Get the JSON formatted data  
          String jsonData = obj.toJSONString();
         
             //Return the JSON formatted data
         response.getWriter().write(jsonData);
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }
}

Now can split and use our jsonData as per our requirement.

Thanks.

Hope it helps :)