<?xml version="1.0" encoding="UTF-8"?>
<bpws:process exitOnStandardFault="yes" name="PurchaseOrder"
	targetNamespace="http://acme.com/ws-bp/purchase" xmlns:bpws="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
	xmlns:lns="http://manufacturing.org/wsdl/purchase">
	<bpws:documentation>WS-BPEL process for handling a purchase order.
	</bpws:documentation>

	<bpws:import importType="http://schemas.xmlsoap.org/wsdl/"
		location="purchase.wsdl" namespace="http://manufacturing.org/wsdl/purchase" />

	<bpws:partnerLinks>
		<bpws:partnerLink myRole="purchaseService" name="purchasing"
			partnerLinkType="lns:purchasingLT" />
		<bpws:partnerLink myRole="invoiceRequester" name="invoicing"
			partnerLinkType="lns:invoicingLT" partnerRole="invoiceService" />
		<bpws:partnerLink myRole="shippingRequester" name="shipping"
			partnerLinkType="lns:shippingLT" partnerRole="shippingService" />
		<bpws:partnerLink name="scheduling" partnerLinkType="lns:schedulingLT"
			partnerRole="schedulingService" />
	</bpws:partnerLinks>

	<bpws:variables>
		<bpws:variable messageType="lns:POMessage" name="PO" />
		<bpws:variable messageType="lns:InvMessage" name="Invoice" />
		<bpws:variable messageType="lns:shippingRequestMessage" name="shippingRequest" />
		<bpws:variable messageType="lns:shippingInfoMessage" name="shippingInfo" />
		<bpws:variable messageType="lns:scheduleMessage" name="shippingSchedule" />
	</bpws:variables>

	<bpws:faultHandlers>
		<bpws:catch faultMessageType="lns:orderFaultType" faultName="lns:cannotCompleteOrder"
			faultVariable="POFault">
			<bpws:reply faultName="cannotCompleteOrder" name="NotifyPurchaseFault"
				operation="sendPurchaseOrder" partnerLink="purchasing" portType="lns:purchaseOrderPT"
				variable="POFault" />
		</bpws:catch>
	</bpws:faultHandlers>

	<bpws:sequence name="Main">
		<bpws:receive createInstance="yes" name="ReceivePurchaseOrder"
			operation="sendPurchaseOrder" partnerLink="purchasing" portType="lns:purchaseOrderPT"
			variable="PO" />

		<bpws:flow>
			<bpws:documentation>A parallel flow to handle shipping, invoicing and
				scheduling</bpws:documentation>

			<bpws:links>
				<bpws:link name="ship-to-invoice" />
				<bpws:link name="ship-to-scheduling" />
			</bpws:links>

			<bpws:sequence name="Invoicing">
				<bpws:invoke inputVariable="PO" name="InitiatePriceCalculation"
					operation="initiatePriceCalculation" partnerLink="invoicing" portType="lns:computePricePT" />

				<bpws:invoke inputVariable="shippingInfo" name="SendShippingPrice"
					operation="sendShippingPrice" partnerLink="invoicing" portType="lns:computePricePT">
					<bpws:targets>
						<bpws:target linkName="ship-to-invoice" />
					</bpws:targets>
				</bpws:invoke>

				<bpws:receive name="ReceiveInvoice" operation="sendInvoice"
					partnerLink="invoicing" portType="lns:invoiceCallbackPT" variable="Invoice" />
			</bpws:sequence>

			<bpws:sequence name="Shipping">
				<bpws:assign name="PrepareShipping" validate="no">
					<bpws:copy>
						<bpws:from part="customerInfo" variable="PO" />
						<bpws:to part="customerInfo" variable="shippingRequest" />
					</bpws:copy>
				</bpws:assign>

				<bpws:invoke inputVariable="shippingRequest" name="RequestShipping"
					operation="requestShipping" outputVariable="shippingInfo" partnerLink="shipping"
					portType="lns:shippingPT">
					<bpws:sources>
						<bpws:source linkName="ship-to-invoice" />
					</bpws:sources>
				</bpws:invoke>

				<bpws:receive name="ReceiveSchedule" operation="sendSchedule"
					partnerLink="shipping" portType="lns:shippingCallbackPT" variable="shippingSchedule">
					<bpws:sources>
						<bpws:source linkName="ship-to-scheduling" />
					</bpws:sources>
				</bpws:receive>
			</bpws:sequence>
			
			<bpws:sequence name="Scheduling">
				<bpws:invoke inputVariable="PO" name="RequestScheduling"
					operation="requestProductionScheduling" partnerLink="scheduling" portType="lns:schedulingPT" />

				<bpws:invoke inputVariable="shippingSchedule" name="SendShippingSchedule"
					operation="sendShippingSchedule" partnerLink="scheduling" portType="lns:schedulingPT">
					<bpws:targets>
						<bpws:target linkName="ship-to-scheduling" />
					</bpws:targets>
				</bpws:invoke>
			</bpws:sequence>
		</bpws:flow>

		<bpws:reply name="SendPurchaseOrder" operation="sendPurchaseOrder"
			partnerLink="purchasing" portType="lns:purchaseOrderPT" variable="Invoice" />
	</bpws:sequence>
</bpws:process>
