<?xml version="1.0" encoding="UTF-8"?>
<bpws:process exitOnStandardFault="no" name="loanService"
    suppressJoinFailure="yes"
    targetNamespace="http://orchestra.ow2.org/loanApproval/loanService"
    xmlns:bpws="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
    xmlns:ns="http://orchestra.ow2.org/loanApproval/common"
    xmlns:ns0="http://orchestra.ow2.org/loanApproval/riskAssessment"
    xmlns:ns1="http://orchestra.ow2.org/loanApproval/approval" xmlns:tns="http://orchestra.ow2.org/loanApproval/loanService">
   
    <bpws:import importType="http://schemas.xmlsoap.org/wsdl/"
        location="loanService.wsdl" namespace="http://orchestra.ow2.org/loanApproval/loanService"/>
    <bpws:import importType="http://schemas.xmlsoap.org/wsdl/"
        location="riskAssessment/riskAssessment.wsdl" namespace="http://orchestra.ow2.org/loanApproval/riskAssessment"/>
    <bpws:import importType="http://schemas.xmlsoap.org/wsdl/"
        location="approval/approval.wsdl" namespace="http://orchestra.ow2.org/loanApproval/approval"/>
    <bpws:import importType="http://schemas.xmlsoap.org/wsdl/"
        location="common.wsdl" namespace="http://orchestra.ow2.org/loanApproval/common"/>
    
    <bpws:partnerLinks>
        <bpws:partnerLink myRole="loanService" name="customer" partnerLinkType="tns:loanServiceLT"/>
        <bpws:partnerLink name="approver"
            partnerLinkType="tns:approvalLT" partnerRole="approver"/>
        <bpws:partnerLink name="assessor"
            partnerLinkType="tns:riskAssessmentLT" partnerRole="assessor"/>
    </bpws:partnerLinks>
    <bpws:variables>
        <bpws:variable messageType="ns:creditInformationMessage" name="request"/>
        <bpws:variable messageType="ns0:riskAssessmentMessage" name="risk"/>
        <bpws:variable messageType="ns:approvalMessage" name="approval"/>
    </bpws:variables>
    <bpws:flow name="Flow">
        <bpws:links>
            <bpws:link name="receive-to-assess"/>
            <bpws:link name="receive-to-approval"/>
            <bpws:link name="approval-to-reply"/>
            <bpws:link name="assess-to-setMessage"/>
            <bpws:link name="setMessage-to-reply"/>
            <bpws:link name="assess-to-approval"/>
        </bpws:links>
        <bpws:receive createInstance="yes" name="Receive"
            operation="request" partnerLink="customer"
            portType="tns:loanServicePT" variable="request">
            <bpws:sources>
                <bpws:source linkName="receive-to-assess">
                    <bpws:transitionCondition><![CDATA[$request.amount < 10000]]></bpws:transitionCondition>
                </bpws:source>
                <bpws:source linkName="receive-to-approval">
                    <bpws:transitionCondition><![CDATA[$request.amount >= 10000]]></bpws:transitionCondition>
                </bpws:source>
            </bpws:sources>
        </bpws:receive>
        <bpws:invoke inputVariable="request" name="Assess"
            operation="check" outputVariable="risk"
            partnerLink="assessor" portType="ns0:riskAssessmentPT">
            <bpws:targets>
                <bpws:target linkName="receive-to-assess"/>
            </bpws:targets>
            <bpws:sources>
                <bpws:source linkName="assess-to-setMessage">
                    <bpws:transitionCondition><![CDATA[$risk.level='low']]></bpws:transitionCondition>
                </bpws:source>
                <bpws:source linkName="assess-to-approval">
                    <bpws:transitionCondition><![CDATA[$risk.level!='low']]></bpws:transitionCondition>
                </bpws:source>
            </bpws:sources>
        </bpws:invoke>
        <bpws:assign name="SetMessage" validate="no">
            <bpws:targets>
                <bpws:target linkName="assess-to-setMessage"/>
            </bpws:targets>
            <bpws:sources>
                <bpws:source linkName="setMessage-to-reply"/>
            </bpws:sources>
            <bpws:copy>
                <bpws:from>
                    <bpws:literal>yes</bpws:literal>
                </bpws:from>
                <bpws:to part="accept" variable="approval"/>
            </bpws:copy>
        </bpws:assign>
        <bpws:invoke inputVariable="request" name="Approval"
            operation="approve" outputVariable="approval"
            partnerLink="approver" portType="ns1:approvalPT">
            <bpws:targets>
                <bpws:target linkName="receive-to-approval"/>
                <bpws:target linkName="assess-to-approval"/>
            </bpws:targets>
            <bpws:sources>
                <bpws:source linkName="approval-to-reply"/>
            </bpws:sources>
        </bpws:invoke>
        <bpws:reply name="Reply" operation="request"
            partnerLink="customer" portType="tns:loanServicePT" variable="approval">
            <bpws:targets>
                <bpws:target linkName="approval-to-reply"/>
                <bpws:target linkName="setMessage-to-reply"/>
            </bpws:targets>
        </bpws:reply>
    </bpws:flow>
</bpws:process>
