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

<!-- This describes a process for submitting changed content in a user -->
<!-- sandbox, via an approver, to the staging sandbox. -->

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

    <swimlane name="initiator"/>

    <!--              -->
    <!-- Start Review -->
    <!--              -->

    <start-state name="start">
        <task name="wcmwf:submitReviewTask" swimlane="initiator"/>
        <transition name="" to="serialorparallel"/>
    </start-state>


    <!--                                      -->
    <!-- Split into Serial or Parallel Review -->
    <!--                                      -->

    <decision name="serialorparallel">
       <event type="node-enter">
            <script>
               <variable name="wcmwf_reviewerCnt" access="write"/>
               <variable name="wcmwf_approveCnt" access="write"/>
               <variable name="wcmwf_reviewType" access="write"/>
               <expression>
                  wcmwf_reviewerCnt = bpm_assignees.size();
                  wcmwf_approveCnt = 0;
                  wcmwf_reviewType = wcmwf_submitReviewType;
               </expression>
            </script>
       </event>

       <transition name="serial" to="submitserialreview" />
       <transition name="parallel" to="submitparallelreview">
          <condition>#{wcmwf_reviewType == "Parallel"}</condition>
       </transition>
    </decision>   


    <!--               -->
    <!-- Serial Review -->
    <!--               -->

    <decision name="submitserialreview">
       <transition name="endreview" to="endreview" />
       <transition name="review" to="serialreview">
         <condition>#{wcmwf_approveCnt &lt; wcmwf_reviewerCnt}</condition>
       </transition>
    </decision>   

    <task-node name="serialreview">
        <task name="wcmwf:reviewTask">
           <assignment class="org.alfresco.repo.workflow.jbpm.AlfrescoAssignment">
              <actor>#{bpm_assignees.get(wcmwf_approveCnt)}</actor>
           </assignment>
        </task>

        <transition name="reject" to="endreview" />
        <transition name="approve" to="submitserialreview">
           <script>
              <variable name="wcmwf_approveCnt" access="read, write"/>
              <expression>
                 wcmwf_approveCnt = wcmwf_approveCnt + 1;
              </expression>
           </script>
        </transition>
    </task-node>


    <!--                 -->
    <!-- Parallel Review -->
    <!--                 -->

    <node name="submitparallelreview">
        <action class="org.alfresco.repo.workflow.jbpm.ForEachFork">
            <foreach>#{bpm_assignees}</foreach>
            <var>reviewer</var>
        </action>
        <transition name="review" to="parallelreview" />
    </node>

    <task-node name="parallelreview">
        <task name="wcmwf:reviewTask">
           <assignment class="org.alfresco.repo.workflow.jbpm.AlfrescoAssignment">
              <actor>#{reviewer}</actor>
           </assignment>
        </task>
        <transition name="reject" to="joinparallelreview" />
        <transition name="approve" to="joinparallelreview">
            <script>
                <variable name="wcmwf_approveCnt" access="read,write" />
                <expression>
                    wcmwf_approveCnt = wcmwf_approveCnt +1;
                 </expression>
            </script>
        </transition>
    </task-node>

    <join name="joinparallelreview">
        <transition to="endreview" />
    </join>


    <!--                -->
    <!-- End the Review -->
    <!--                -->

    <decision name="endreview">
       <transition name="rejected" to="rejected">
           <action class="org.alfresco.repo.avm.wf.AVMClearSubmittedHandler"/>
       </transition>
       <transition name="approved" to="approved">
           <condition>#{wcmwf_approveCnt == wcmwf_reviewerCnt}</condition>
           <action class="org.alfresco.repo.avm.wf.AVMSubmitPackageHandler"/>
       </transition>
    </decision>   

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

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


    <!--                 -->
    <!-- End the Process -->
    <!--                 -->

    <end-state name="end"/>
    
    <event type="process-end">
        <action class="org.alfresco.repo.avm.wf.AVMClearSubmittedHandler"/>
        <action class="org.alfresco.repo.avm.wf.AVMRemoveWFStoreHandler"/>
    </event>

</process-definition>