<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
        http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
        http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">

   <description>
        This example demonstrates invoking a JSR-223 script as the service component.
    </description>

    <http:body-to-parameter-map-transformer name="HttpRequestToMap" doc:name="Body to Parameter Map"/>
    <custom-transformer name="StringToNumber" class="org.mule.example.scripting.StringToNumber" doc:name="Java"/>
    <custom-transformer name="DollarsToCents" class="org.mule.example.scripting.SimpleMathTransformer" doc:name="Java">
        <spring:property name="operation" value="multiply"/>
        <spring:property name="factor" value="100"/>
    </custom-transformer>

    <flow name="http-inbound">
        <http:inbound-endpoint host="0.0.0.0" port="${httpPort}" path="change-machine"
            exchange-pattern="request-response" doc:name="Http Endpoint"/>
		<transformer ref="HttpRequestToMap" doc:name="Transform HttpRequest tp Map"/>
        <choice doc:name="Choice">
            <when expression="payload.currency == 'USD'">
                <processor-chain>
                    <set-payload value="#[payload['amount']]" doc:name="Extract Amount"/>
                    <transformer ref="StringToNumber" doc:name="String to Number"/>
                    <transformer ref="DollarsToCents" doc:name="Dollars to Cents"/>
                    <scripting:component doc:name="USD Currency Script">
                        <scripting:script file="greedy.groovy">
                            <property key="currency" value="USD"/>
                        </scripting:script>
                    </scripting:component>
                </processor-chain>
             </when>

             <when expression="payload.currency == 'GBP'">
                <processor-chain>
                    <set-payload value="#[payload['amount']]" doc:name="Extract Amount"/>
                    <transformer ref="StringToNumber" doc:name="String to Number"/>
                    <transformer ref="DollarsToCents" doc:name="Dollars to Cents"/>
                    <scripting:component doc:name="GBP Currency Script">
                        <scripting:script file="greedy.py">
                            <property key="currency" value="GBP"/>
                        </scripting:script>
                    </scripting:component>
                </processor-chain>
             </when>
        </choice>
    </flow>
</mule>
