Thursday 21 May 2015

Workflow in cq5

This component will display all the pages having workflow in it, under the selected page in the path field :




Code in jsp : (Name of path field = "path")


<%-- Show pages having workflow.

  It will display the child pages having active workflow

--%>
   <%@include file="/libs/wcm/global.jsp"%><%
%><%@ page import="java.util.Iterator,com.day.cq.wcm.api.PageFilter,com.day.cq.workflow.status.WorkflowStatus"%>

<%
String path = "";

        %>
    <h2>show pages having active workflow</h2>
    <%


path = properties.get("path", currentPage.getPath());


Page root=pageManager.getPage(path);

if (root != null) 
{
String title_root = root.getTitle() == null ? root.getName() : root.getTitle();
%><a href="<%=root%>.html"><b><%=title_root%></b></a><br><%


     Iterator<Page> children = root.listChildren(new PageFilter(),true);
     while (children.hasNext()) 
     {


         Page child = children.next();


         WorkflowStatus wfState  = child.adaptTo(WorkflowStatus.class);
boolean status=false;
status = wfState.isInRunningWorkflow(false);

        if(status == true)
        {

           String title = child.getTitle() == null ? child.getName() : child.getTitle();

     %><a href="<%=child.getPath()%>.html"><b><%=title%></b></a> , Path = <%=child.getPath()%><br><%
       }
     }
   
}
%>





Now replacing the above code, the following code will also show all the details of workflow in a tabular form : 



<%--

  Workflow pages with details

--%>
    <%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>
<%@ page import=
    "com.adobe.granite.workflow.WorkflowSession,
                         com.adobe.granite.workflow.exec.Workflow,
                         com.adobe.granite.workflow.status.WorkflowStatus,
                         com.adobe.granite.workflow.exec.WorkflowData,
                         com.day.cq.wcm.api.PageFilter,
         com.adobe.granite.workflow.exec.WorkItem,
         java.util.*,
        org.apache.sling.commons.json.JSONObject"
%>
<style>
table {
    border-collapse: collapse;
    border: 3px solid black;
}

td, th {
    border: 1px solid blue;
    margin: 1px;
    padding: 10px;
    background-color: black;
    color: white;
}

</style>

<%
   String path = "";

%> <h2>show pages having active workflow-----------------</h2> <%

path = properties.get("path", currentPage.getPath());

Page navRootPage = pageManager.getPage(path);

if (navRootPage != null) 
{
  Iterator<Page> children = navRootPage.listChildren(new PageFilter(),true);

int i = 0;
    String str= null;
    String title= "Unavailable";
  while (children.hasNext()) 
{

        Page child = children.next();

    WorkflowStatus wf=child.adaptTo(WorkflowStatus.class);
boolean status=false;
        status = wf.isInRunningWorkflow(true);


WorkflowSession   session =slingRequest.getResourceResolver().adaptTo(WorkflowSession.class);
        Workflow[] work=session.getAllWorkflows();
if(status)
{
            if(!work[i].getWorkItems().isEmpty())
            {
str = work[i].getWorkItems().get(0).getNode().toString();
JSONObject json = new JSONObject(str);
                title = json.get("title").toString();

            }
        %> 
<b style= "color: red;">------[<%=i+1 %>.]------</b><br>
<table>
        <tr>
            <td><u>PAGE TITLE</u>:</td><td> <b><%=child.getTitle() %></b></td>
            <td> <a href="<%=child.getPath()%>.html">Go to page</a></td>
            <td><u>PAGE PATH</u> :</td><td> <%=child.getPath() %></td>
        </tr>
        <tr>
            <td><u>WORKFLOW TITLE</u> :</td><td><%=work[i].getWorkflowModel().getTitle()%></td>
            <td><u>INITIATED BY</u>:</td><td colspan="2"> <%=work[i].getInitiator()%></td>
        </tr>
    <tr>
            <td><u>STEP</u>:</td><td colspan="4"><%=title %></td>
        </tr>
        <tr>
            <td><u>WORKFLOW STATE</u>:</td><td colspan="4"> <%=work[i].getState()%></td>
        </tr>
        <tr>
            <td><u>WORKFLOW DESCRIPTION</u> :</td><td colspan="4"> <%=work[i].getWorkflowModel().getDescription()%></td>
        </tr>
        <tr>
            <td><u>WORKFLOW ID</u> :</td><td colspan="4"> <%=work[i].getWorkflowModel().getId()%></td>
        </tr>
        <tr>
            <td><u>WORKFLOW VERSION</u> :</td><td colspan="4"> <%=work[i].getWorkflowModel().getVersion()%></td>
        </tr>
        <tr>
            <td><u>WORKFLOW ENDNODE</u> :</td><td colspan="4"> <%=work[i].getWorkflowModel().getEndNode()%></td>
        </tr>
        <tr>
            <td><u>WORKFLOW DATA PAYLOAD TYPE</u> :</td><td colspan="4"> <%=work[i].getWorkflowData().getPayloadType()%></td>
        </tr>
        <tr>
            <td><u>Time</u> </td><td> <%=work[i].getTimeStarted() %>  </td><td> to </td><td colspan="2"><%=work[i].getTimeEnded()%></td>
        </tr>
        <tr>

       <%     if(work[i].getWorkItems().isEmpty())
            {
                %><td><u>WORKITEM</u> :</td><td colspan="4"> N/A</td><%
            }
            else
            {
                %><td ><u>WORKITEM NODE</u> :</td><td colspan="4"> <%=work[i].getWorkItems().get(0).getNode()%></td><%

}

  %></tr>
      </table>
<br><br><br><%

i++;
}

  }   
}


%>


Output:






The following code will show all the page and some basic workflow details i a single table: 


<%--

  Workflow 

--%>
    <%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>
<%@ page import=
    "com.adobe.granite.workflow.WorkflowSession,
                 com.adobe.granite.workflow.exec.Workflow,
                 com.adobe.granite.workflow.status.WorkflowStatus,
                 com.adobe.granite.workflow.exec.WorkflowData,
                 java.util.Iterator,
                 com.day.cq.wcm.api.PageFilter,
com.adobe.granite.workflow.exec.WorkItem,
java.util.*,
org.apache.sling.commons.json.JSONObject"
%>
<style>
table {
    border-collapse: collapse;
    border: 3px solid black;
}

td, th {
    border: 1px solid blue;
    margin: 1px;
    padding: 10px;
    background-color: black;
    color: white;
}

h2{
    color: green;
    background-color: #ddd;
    height: 40px;
    opacity:.6;
    padding: 0 50px; 
}
    #heading{
        border: 4px solid grey;
        font-weight: bold;

    }
</style>


<%
   String path = "";

%> <h2 id= "fix">--- show pages having workflow---</h2> <%

path = properties.get("path", currentPage.getPath());

Page navRootPage = pageManager.getPage(path);

if (navRootPage != null) 
{
  Iterator<Page> children = navRootPage.listChildren(new PageFilter(),true);

int i = 0;
boolean status=false;
    String step= "Unavailable";
    String instanceId = "N/A";
%>

<table>
        <tr id="heading">

            <td>S.NO.</td> <td>PAGE TITLE</td> <td>WORKFLOW TITLE</td> <td>STEP</td> <td>WORKFLOW INSTANCE ID</td> <td>STATUS</td>

        </tr>

<%
  while (children.hasNext()) 
{

        Page child = children.next();

    WorkflowStatus wf=child.adaptTo(WorkflowStatus.class);
status=false;
        status = wf.isInRunningWorkflow(true);


WorkflowSession   session =slingRequest.getResourceResolver().adaptTo(WorkflowSession.class);
        Workflow[] work=session.getAllWorkflows();
if(status)
{
            if(!work[i].getWorkItems().isEmpty())
            {
step = work[i].getWorkItems().get(0).getNode().toString();
JSONObject json = new JSONObject(step);
                step = json.get("title").toString();

                instanceId = work[i].getId().toString();
            }
        %> 

        <tr>
              <td><%=i+1 %>&nbsp.</td>

              <td> <a href="<%=child.getPath()%>.html"><%=child.getTitle() %></a></td>

              <td><%=work[i].getWorkflowModel().getTitle()%></td>

 <td><%=step %></td>

              <td><%=instanceId %></td>

              <td><%=work[i].getState()%></td>
        </tr>

<%
i++;
}

  }  

}


%>

    </table>


Output:




No comments:

Post a Comment