<?xml version="1.0" encoding="UTF-8"?>
<bpws:process name="orderingService"
  targetNamespace="http://orchestra.ow2.org/demos/orderingService"
  xmlns:bpelj="http://schemas.xmlsoap.org/ws/2003/03/business-process/java"
  xmlns:bpws="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
  xmlns:tns="http://orchestra.ow2.org/demos/orderingService"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <bpws:import importType="http://schemas.xmlsoap.org/wsdl/"
    location="orderingService.wsdl"
    namespace="http://orchestra.ow2.org/demos/orderingService" />

  <bpws:partnerLinks>
    <bpws:partnerLink myRole="order" name="order"
      partnerLinkType="tns:orderPLT" />
    <bpws:partnerLink myRole="validate" name="validate"
      partnerLinkType="tns:validatePLT" />
    <bpws:partnerLink myRole="init" name="init"
      partnerLinkType="tns:initPLT" />
  </bpws:partnerLinks>

  <bpws:variables>
    <bpws:variable messageType="tns:initMsg" name="initMessage" />
    <bpws:variable messageType="tns:orderMsg" name="orderMessage" />
    <bpws:variable messageType="tns:validateMsg" name="validateMessage" />
    <bpws:variable messageType="tns:purchaseResponse"
      name="purchaseOutput" />
    <bpws:variable name="order" type="xsd:string" />
    <bpws:variable name="deadline" type="xsd:string" />
    <bpws:variable name="purchaseValidated" type="xsd:int" />
    <bpws:variable name="sessionStillActive" type="xsd:int" />
    <bpws:variable name="sessionId" type="xsd:int" />
  </bpws:variables>

  <bpws:correlationSets>
    <bpws:correlationSet name="CorrelationSet" properties="tns:idProp" />
  </bpws:correlationSets>


  <bpws:sequence name="Sequence">
    <bpws:receive createInstance="yes" name="InitReceive"
      operation="init" partnerLink="init" portType="tns:initPT"
      variable="initMessage">
      <bpws:correlations>
        <bpws:correlation initiate="yes" set="CorrelationSet" />
      </bpws:correlations>
    </bpws:receive>

    <bpws:assign name="InitVariables" validate="no">
      <bpws:copy>
        <bpws:from><![CDATA[concat($initMessage.initInfo/tns:clientName, " ordered ")]]></bpws:from>
        <bpws:to variable="order" />
      </bpws:copy>
      <bpws:copy>
        <bpws:from><![CDATA[$initMessage.initInfo/tns:sessionExpiration/text()]]></bpws:from>
        <bpws:to variable="deadline" />
      </bpws:copy>
      <bpws:copy>
        <bpws:from><![CDATA[$initMessage.initInfo/tns:id]]></bpws:from>
        <bpws:to variable="sessionId" />
      </bpws:copy>
      <bpws:copy>
        <bpws:from><bpws:literal>0</bpws:literal></bpws:from>
        <bpws:to variable="purchaseValidated" />
      </bpws:copy>
      <bpws:copy>
        <bpws:from><bpws:literal>1</bpws:literal></bpws:from>
        <bpws:to variable="sessionStillActive" />
      </bpws:copy>
      <bpws:copy>
        <bpws:from>
          <bpws:literal>
            <tns:response
              xmlns:tns="http://orchestra.ow2.org/demos/orderingService">
              <tns:id />
              <tns:result />
            </tns:response>
          </bpws:literal>
        </bpws:from>
        <bpws:to part="result" variable="purchaseOutput" />
      </bpws:copy>
    </bpws:assign>

    <bpws:while>
      <bpws:condition><![CDATA[$purchaseValidated=0]]></bpws:condition>
      <bpws:pick>
        <bpws:onMessage operation="order" partnerLink="order"
          portType="tns:orderPT" variable="orderMessage">
          <bpws:correlations>
            <bpws:correlation initiate="no" set="CorrelationSet" />
          </bpws:correlations>
          <bpws:assign name="AddOrderToVariable" validate="no">
            <bpws:copy>
              <bpws:from><![CDATA[concat($order, $orderMessage.orderInfo/tns:productName, ", ")]]></bpws:from>
              <bpws:to variable="order" />
            </bpws:copy>
          </bpws:assign>
        </bpws:onMessage>
        <bpws:onMessage operation="validate" partnerLink="validate"
          portType="tns:validatePT" variable="validateMessage">
          <bpws:correlations>
            <bpws:correlation initiate="no" set="CorrelationSet" />
          </bpws:correlations>
          <bpws:assign name="AssignValidationCondition" validate="no">
            <bpws:copy>
              <bpws:from>
                <bpws:literal>1</bpws:literal>
              </bpws:from>
              <bpws:to variable="purchaseValidated" />
            </bpws:copy>
          </bpws:assign>
        </bpws:onMessage>
        <bpws:onAlarm>
          <bpws:until><![CDATA[$deadline]]></bpws:until>
          <bpws:assign name="AssignTimeoutReply" validate="no">
            <bpws:copy>
              <bpws:from>
                <bpws:literal>1</bpws:literal>
              </bpws:from>
              <bpws:to variable="purchaseValidated" />
            </bpws:copy>
            <bpws:copy>
              <bpws:from><![CDATA[concat("Timeout 1 occurred, transaction interrupted.Order was: ", $order)]]></bpws:from>
              <bpws:to variable="order" />
            </bpws:copy>
          </bpws:assign>
        </bpws:onAlarm>
        <bpws:onAlarm>
          <bpws:for><![CDATA['PT2M']]></bpws:for>
          <bpws:assign name="AssignTimeoutReply2" validate="no">
            <bpws:copy>
              <bpws:from>
                <bpws:literal>1</bpws:literal>
              </bpws:from>
              <bpws:to variable="purchaseValidated" />
            </bpws:copy>
            <bpws:copy>
              <bpws:from><![CDATA[concat("Timeout 2 occurred, transaction interrupted.Order was: ", $order)]]></bpws:from>
              <bpws:to variable="order" />
            </bpws:copy>
          </bpws:assign>
        </bpws:onAlarm>
      </bpws:pick>
    </bpws:while>

    <bpws:assign name="AssignReply" validate="no">
      <bpws:copy>
        <bpws:from variable="sessionId" />
        <bpws:to part="result" variable="purchaseOutput">
          <bpws:query
            queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:id]]></bpws:query>
        </bpws:to>
      </bpws:copy>
      <bpws:copy>
        <bpws:from variable="order" />
        <bpws:to part="result" variable="purchaseOutput">
          <bpws:query
            queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:result]]></bpws:query>
        </bpws:to>
      </bpws:copy>
    </bpws:assign>

    <bpws:reply name="PurchaseReply" operation="init" partnerLink="init"
      portType="tns:initPT" variable="purchaseOutput">
      <bpws:correlations>
        <bpws:correlation initiate="no" set="CorrelationSet" />
      </bpws:correlations>
    </bpws:reply>
  </bpws:sequence>
</bpws:process>
