<?xml version="1.0" encoding="UTF-8"?>

<process-definition xmlns="urn:jbpm.org:jpdl-3.1" name="wfl:lifecycleapproval">

    <swimlane name="initiator" />

    <start-state name="start">
        <task name="wf:submitReviewTask" swimlane="initiator" />

        <event type="node-leave">
            <!-- Call script once the workflow package exists i.e. on node-leave -->
            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
                <!-- Apply the Workflow Lifecycle Aspect (wfl:status) if not set already. Note: The default wfl:status property is draft -->
                <script>
                    for (var i = 0; i &lt; bpm_package.children.length; i++)
                    {
                       if (!bpm_package.children[i].hasAspect("wfl:status"))
                       {
                          bpm_package.children[i].addAspect("wfl:status");
                       }
                    }
                </script>
            </action>
        </event>

        <transition name="" to="review" />
    </start-state>

    <swimlane name="reviewer">
       <assignment class="org.alfresco.repo.workflow.jbpm.AlfrescoAssignment">
          <actor>#{bpm_assignee}</actor>
       </assignment>
    </swimlane>

    <task-node name="review">
        <event type="node-enter">
            <!-- Update the status to In Review when we enter this task -->
            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
                <script>
                    for (var i = 0; i &lt; bpm_package.children.length; i++)
                    {
                       bpm_package.children[i].properties["wfl:status"] = "In Review";
                       bpm_package.children[i].save();
                    }
                </script>
            </action>
        </event>

        <task name="wf:reviewTask" swimlane="reviewer">
            <event type="task-create">
                <script>
                    if (bpm_workflowDueDate != void) taskInstance.dueDate = bpm_workflowDueDate;
                    if (bpm_workflowPriority != void) taskInstance.priority = bpm_workflowPriority;
                </script>
            </event>
        </task>
        
        <transition name="reject" to="rejected" />
        <transition name="approve" to="approved" />
    </task-node>

    <task-node name="rejected">
        <event type="node-enter">
            <!-- Update the status to Draft when we enter this task -->
            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
                <script>
                    for (var i = 0; i &lt; bpm_package.children.length; i++)
                    {
                       bpm_package.children[i].properties["wfl:status"] = "Draft";
                       bpm_package.children[i].save();
                    }
                </script>
            </action>
        </event>

        <task name="wf:rejectedTask" swimlane="initiator" />
        <transition name="" to="end" />
    </task-node>

    <task-node name="approved">
        <event type="node-enter">
            <!-- Update the status to Approved when we enter this task -->
            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
                <script>
                    for (var i = 0; i &lt; bpm_package.children.length; i++)
                    {
                       bpm_package.children[i].properties["wfl:status"] = "Approved";
                       bpm_package.children[i].save();
                    }
                </script>
            </action>
        </event>

        <task name="wf:approvedTask" swimlane="initiator" />
        <transition name="" to="end" />
    </task-node>

    <end-state name="end" />

    <event type="process-end">
        <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
                if (cancelled)
                {
                    for (var i = 0; i &lt; bpm_package.children.length; i++)
                    {
                       if (bpm_package.children[0].hasAspect("wfl:status"))
                       {
                          bpm_package.children[i].properties["wfl:status"] = "Draft";
                          bpm_package.children[i].save();
                       }
                    }
                    if (logger.isLoggingEnabled()) logger.log("Workflow cancelled, status reset to Draft");
                } 
                else                
                {
                    if (logger.isLoggingEnabled()) logger.log("Workflow completed");
                } 
            </script>
        </action>
    </event>

</process-definition>