<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~  Licensed to the Apache Software Foundation (ASF) under one
  ~  or more contributor license agreements.  See the NOTICE file
  ~  distributed with this work for additional information
  ~  regarding copyright ownership.  The ASF licenses this file
  ~  to you under the Apache License, Version 2.0 (the
  ~  "License"); you may not use this file except in compliance
  ~  with the License.  You may obtain a copy of the License at
  ~ 
  ~   http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing,
  ~  software distributed under the License is distributed on an
  ~   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~  KIND, either express or implied.  See the License for the
  ~  specific language governing permissions and limitations
  ~  under the License.
  -->
  
<!-- Invoking remote EJB Stateful Session Beans from Synapse. -->
<definitions xmlns="http://ws.apache.org/ns/synapse">

    <proxy name="BuyAllProxy" transports="https http" startOnLoad="true" trace="disable">
        <target>
        
            <!-- Iterate over all items in the request and call addItem() on the ShoppingCart EJB for each item. -->
            <inSequence>
                <property name="SESSION_ID" expression="get-property('MessageID')"/>
                <iterate xmlns:m0="http://services.samples" preservePayload="false"
                         expression="//m0:buyItems/m0:items/m0:item">
                    <target>
                        <sequence>
                        
                            <!-- You might want to change the jndiName, depending on your EJB container. -->
                            <ejb class="samples.ejb.ShoppingCart" beanstalk="demo" method="addItem" sessionId="{get-property('SESSION_ID')}" jndiName="ShoppingCartBean/remote">
                                <args>
                                    <arg value="{//m:item//m:id}" xmlns:m="http://services.samples"/>
                                    <arg value="{//m:item//m:quantity}" xmlns:m="http://services.samples"/>
                                </args>
                            </ejb>
                            <sequence key="collector"/>
                        </sequence>
                    </target>
                </iterate>
            </inSequence>
        </target>
    </proxy>

    <!-- Prepare the response once all addItem() calls are finished. -->
    <sequence name="collector">
        <aggregate>
            <onComplete>
                <ejb class="samples.ejb.ShoppingCart" beanstalk="demo" method="getItemCount" sessionId="{get-property('SESSION_ID')}" target="ITEM_COUNT"/>
                <ejb class="samples.ejb.ShoppingCart" beanstalk="demo" method="getTotal" sessionId="{get-property('SESSION_ID')}" target="TOTAL" remove="true"/>
                <payloadFactory>
                    <format>
                        <buyAllResponse xmlns="">
                            <itemCount>$1</itemCount>
                            <total>$2</total>
                        </buyAllResponse>
                    </format>
                    <args>
                        <arg expression="get-property('ITEM_COUNT')"/>
                        <arg expression="get-property('TOTAL')"/>
                    </args>
                </payloadFactory>
                <respond/>
            </onComplete>
        </aggregate>
    </sequence>

</definitions>
