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

<!-- Workflow to support direct submit to staging sandbox -->

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

    <swimlane name="initiator"/>

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

    <start-state name="start">
        <task name="wcmwf:submitDirectTask" swimlane="initiator"/>
        <transition name="" to="submitted"/>
    </start-state>
    
    <task-node name="submitted" end-tasks="true">
        <task name="wcmwf:submittedDirectTask" swimlane="initiator">
            <timer duedate="5 seconds" transition="onsubmit">
                <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
                    <script>
                        logger.log("WCM Submit Process: Triggering submit for " + bpm_workflowDescription);
                    </script>
                </action>
            </timer>

	        <event type="task-end">
	            <script>
	               <variable name="submitfailed" access="write"/>
	               <expression>submitfailed = false;</expression>
	            </script>
	        
	            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
	                <script>
	                    logger.log("WCM Submit Process: Start submit for " + bpm_workflowDescription + " (by " + person.properties.userName + ")");
	                </script>
	            </action>
	
	            <action class="org.alfresco.repo.avm.wf.AVMSubmitPackageHandler"/>
	            <action class="org.alfresco.repo.avm.wf.AVMDeployHandler"/>
	           
	            <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
	                <script>
                        logger.log("WCM Submit Process: End submit for " + bpm_workflowDescription + " (by " + person.properties.userName + ")");
	                </script>
	            </action>
	        </event>
        </task>
        <transition name="onsubmit" to="checkfailedsubmit">
            <exception-handler>
                <script>
                    <variable name="submitfailed" access="write"/>
                    <expression>
                        logger.log("WCM Submit Process: Submit failed for " + bpm_workflowDescription + " (by " + person.properties.userName + ")");
                        submitfailed = true;
                    </expression>
                </script>
            </exception-handler>
        </transition>
    </task-node>

    <decision name="checkfailedsubmit">
        <transition name="failure" to="submitfailed">
           <condition>#{submitfailed == true}</condition>
        </transition>
        <transition name="success" to="end">
           <condition>#{submitfailed == false}</condition>
        </transition>
    </decision>

    <task-node name="submitfailed">
        <task name="wcmwf:submitfailedTask" 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.AVMRemoveAllSrcWebappsHandler"/>
        <action class="org.alfresco.repo.avm.wf.AVMReleaseTestServerHandler"/>
        <action class="org.alfresco.repo.avm.wf.AVMRemoveWFStoreHandler"/>
    </event>

</process-definition>
