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

<process name="BuyBook" 
         targetNamespace="http://oracle.com/bpel/buybook/" 
         xmlns="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
         xmlns:xs="http://www.w3.org/2001/XMLSchema" 
         xmlns:buy="http://oracle.com/bpel/buybook/"
         xmlns:bkr="http://oracle.com/service/bookrating/" 
         xmlns:bst="http://oracle.com/service/bookstore/"  >
   
   <partnerLinks> 
      <partnerLink name="Client" 
                   partnerLinkType="buy:BuyBookLT"
                   myRole="BuyBookService"
                   partnerRole="BuyBookCustomer"/> 

      <partnerLink name="BookRating" 
                   partnerLinkType="bkr:BookRatingLT"
                   partnerRole="BookRatingService"/> 

      <partnerLink name="BookStore1" 
                   partnerLinkType="bst:BookStoreLT"
                   myRole="BookStoreCustomer"
                   partnerRole="BookStoreService"/> 
                   
      <partnerLink name="BookStore2" 
                   partnerLinkType="bst:BookStoreLT"
                   myRole="BookStoreCustomer"
                   partnerRole="BookStoreService"/> 

   </partnerLinks> 

   <variables> 
      <!-- input for this process -->       
      <variable name="BookPurchase" messageType="bst:BookPurchaseMessage"/> 
      <!-- input for the Book Rating web service --> 
      <variable name="BookRatingRequest" messageType="bkr:BookRatingRequestMessage"/> 
      <!-- output from the Book Rating web service --> 
      <variable name="BookRatingResponse" messageType="bkr:BookRatingResponseMessage"/> 
      <!-- input for Book Store web services --> 
      <variable name="BookRequest" messageType="bst:BookRequestMessage"/> 
      <!-- output from BS1 --> 
      <variable name="BookResponse1" messageType="bst:BookResponseMessage"/> 
      <!-- output from BS2 --> 
      <variable name="BookResponse2" messageType="bst:BookResponseMessage"/> 
      <!-- output final --> 
      <variable name="BookResponseFinal" messageType="bst:BookResponseMessage"/> 
   </variables> 

   <sequence> 

      <!-- Receive the initial request from client --> 
      <receive partnerLink="Client" 
               portType="buy:BuyBookPT" 
               operation="BuyBook" 
               variable="BookPurchase"
               createInstance="yes" /> 

      <!-- Prepare the input for the Book Rating --> 
      <assign> 
        <copy> 
          <from variable="BookPurchase" part="book"/> 
          <to variable="BookRatingRequest" part="book"/> 
        </copy> 
      </assign> 
      
      <!-- Synchronously invoke the Book Rating Web Service --> 
      <invoke partnerLink="BookRating" 
              portType="bkr:BookRatingPT" 
              operation="BookRating"
              inputVariable="BookRatingRequest" 
              outputVariable="BookRatingResponse" /> 

      <!-- Prepare the input for Book Stores --> 
      <assign> 
        <copy> 
          <from variable="BookPurchase" part="book"/> 
          <to variable="BookRequest" part="book"/> 
        </copy> 
        <copy> 
          <from variable="BookPurchase" part="quantity"/> 
          <to variable="BookRequest" part="quantity"/> 
        </copy> 
      </assign> 

      <!-- Make a concurrent invocation to BS1 and BS2 --> 
      <flow> 
      
        <sequence> 
        
          <invoke partnerLink="BookStore1" 
              portType="bst:BookPurchasePT" 
              operation="BookPriceQuery"
              inputVariable="BookRequest" /> 

          <receive partnerLink="BookStore1" 
              portType="bst:BookPurchaseCallbackPT" 
              operation="BookPurchaseCallback"
              variable="BookResponse1" /> 

        </sequence> 

        <sequence> 
        
          <invoke partnerLink="BookStore2" 
              portType="bst:BookPurchasePT" 
              operation="BookPriceQuery"
              inputVariable="BookRequest" /> 

          <receive partnerLink="BookStore2" 
              portType="bst:BookPurchaseCallbackPT" 
              operation="BookPurchaseCallback"
              variable="BookResponse2" /> 

        </sequence> 

      </flow> 

      <!-- Select the best offer --> 
      <switch> 
              
        <case condition="bpws:getVariableData('BookResponse1','confirmation','/bst:confirmation/bst:Price') 
                      <= bpws:getVariableData('BookResponse2','confirmation','/bst:confirmation/bst:Price')"> 
                    
           <!-- Select BS1 --> 
           <sequence> 
             <invoke partnerLink="BookStore1"
                portType="bst:BookPurchasePT" 
                operation="BookPurchase"
                inputVariable="BookPurchase" /> 
      
             <receive partnerLink="BookStore1" 
                portType="bst:BookPurchaseCallbackPT" 
                operation="BookPurchaseCallback"
                variable="BookResponseFinal" /> 
           </sequence> 
           
        </case> 
                    
        <otherwise> 
           <!-- Select BS2 --> 
           <sequence> 
             <invoke partnerLink="BookStore2"
                portType="bst:BookPurchasePT" 
                operation="BookPurchase"
                inputVariable="BookPurchase" /> 
      
             <receive partnerLink="BookStore2" 
                portType="bst:BookPurchaseCallbackPT" 
                operation="BookPurchaseCallback"
                variable="BookResponseFinal" /> 
           </sequence> 

        </otherwise> 
      </switch> 


      <!-- Make a callback to the client --> 
      <invoke partnerLink="Client" 
              portType="buy:ClientCallbackPT" 
              operation="ClientCallback"
              inputVariable="BookResponseFinal" /> 
      
   </sequence> 

</process>