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


	<context:property-placeholder location="classpath:cassandra.properties"/>

	<int:poller default="true" fixed-delay="50"/>

	<cassandra:mapping entity-base-packages="org.springframework.integration.cassandra.test.domain">
		<cassandra:entity
				class="org.springframework.integration.cassandra.test.domain.Book">
			<cassandra:table name="book"/>
		</cassandra:entity>
	</cassandra:mapping>

	<cassandra:session id="cassandraSession" keyspace-name="${cassandra.keyspace}"
					   contact-points="${cassandra.contactpoints}" port="${cassandra.port}"
					   local-datacenter="datacenter1"
					   cassandra-converter-ref="cassandraConverter"
					   schema-action="RECREATE">
		<cassandra:keyspace name="${cassandra.keyspace}" action="CREATE_DROP" durable-writes="true"/>
	</cassandra:session>

	<cassandra:converter id="cassandraConverter"/>

	<bean id="reactiveCassandraTemplate" class="org.springframework.data.cassandra.core.ReactiveCassandraTemplate">
		<constructor-arg name="converter" ref="cassandraConverter"/>
		<constructor-arg name="session">
			<bean class="org.springframework.data.cassandra.core.cql.session.DefaultBridgedReactiveSession">
				<constructor-arg ref="cassandraSession"/>
			</bean>
		</constructor-arg>
	</bean>

	<int-cassandra:outbound-channel-adapter id="cassandraMessageHandler1"
											cassandra-template="reactiveCassandraTemplate"
											async="false"/>

	<int-cassandra:outbound-channel-adapter id="cassandraMessageHandler2"
											cassandra-template="reactiveCassandraTemplate"
											async="false"/>

	<int-cassandra:outbound-channel-adapter id="cassandraMessageHandler3"
											cassandra-template="reactiveCassandraTemplate"
											ingest-query="insert into book (isbn, title, author, pages, saleDate, isInStock) values (?, ?, ?, ?, ?, ?)"
											async="false"/>

	<int-cassandra:outbound-channel-adapter id="cassandraMessageHandler4"
											cassandra-template="reactiveCassandraTemplate"
											statement-expression="T(QueryBuilder).truncate('book').build()"
											async="false"/>

	<int:channel id="inputChannel"/>

	<bean id="resultChannel" class="org.springframework.integration.channel.FluxMessageChannel"/>

	<int-cassandra:outbound-gateway id="cassandraMessageHandler5"
									request-channel="inputChannel"
									cassandra-template="reactiveCassandraTemplate"
									mode="STATEMENT"
									query="SELECT * FROM book limit :size"
									reply-channel="resultChannel">
		<int-cassandra:parameter-expression name="author" expression="payload"/>
		<int-cassandra:parameter-expression name="size" expression="headers.limit"/>
	</int-cassandra:outbound-gateway>

</beans>
