<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
	xmlns:si="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			https://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			https://www.springframework.org/schema/integration/spring-integration.xsd
			http://www.springframework.org/schema/integration/xml
			https://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd
			http://www.springframework.org/schema/util
			https://www.springframework.org/schema/util/spring-util.xsd">

	<si:channel id="ordersChannel" />
	<si:channel id="stockCheckerChannel" />
	<si:channel id="orderRoutingChannel" />
	<si:channel id="warehouseDispatchChannel" />
	<si:channel id="outOfStockChannel" />
	<si:channel id="resupplyOrderChannel" />

	<!-- map of namespace prefix to URI -->
	<util:map id="orderNamespaceMap">
		<entry key="orderNs" value="http://www.example.org/orders" />
		<entry key="productNs" value="https://www.example.org/prodcuts" />
	</util:map>

	<!-- split the inbound order into a number of orderItem documents that can be processed separately -->
	<si-xml:xpath-splitter id="orderItemSplitter" input-channel="ordersChannel" output-channel="stockCheckerChannel" create-documents="true">
		<si-xml:xpath-expression expression="/orderNs:order/orderNs:orderItem" namespace-map="orderNamespaceMap" />
	</si-xml:xpath-splitter>

	<!-- if each order -->
	<si:service-activator input-channel="stockCheckerChannel" output-channel="orderRoutingChannel">
		<bean class="org.springframework.integration.samples.xml.StockChecker">
			<constructor-arg ref="selectIsbnXpath" />
		</bean>
	</si:service-activator>

	<si-xml:xpath-expression id="selectIsbnXpath" namespace-map="orderNamespaceMap" expression="/orderNs:orderItem/orderNs:isbn/text()" />

	<!-- if in stock route to the warehouse else route to the out of stock channel -->
	<si-xml:xpath-router id="instockRouter" input-channel="orderRoutingChannel" resolution-required="true">
		<si-xml:xpath-expression expression="/orderNs:orderItem/@in-stock" namespace-map="orderNamespaceMap"  />
		<si-xml:mapping value="true" channel="warehouseDispatchChannel"/>
		<si-xml:mapping value="false" channel="outOfStockChannel"/>
	</si-xml:xpath-router>
	
	<!--  dispatch order if in stock -->
	<si:outbound-channel-adapter method="dispatch" channel="warehouseDispatchChannel">
		<bean class="org.springframework.integration.samples.xml.WarehouseDispatch" />
	</si:outbound-channel-adapter>

	<!--  convert the order item to a format that can be understood by BigBooks the wholesaler -->
	<si-xml:xslt-transformer input-channel="outOfStockChannel" output-channel="resupplyOrderChannel" 
			xsl-resource="classpath:/META-INF/spring/integration/bigBooksSupplierTransformer.xsl"/>

	<!-- send the resupply order -->
	<si:outbound-channel-adapter method="orderResupply" channel="resupplyOrderChannel">
		<bean class="org.springframework.integration.samples.xml.ExternalResupply" />
	</si:outbound-channel-adapter>

</beans>
