<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:kie="http://drools.org/schema/kie-spring"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
        http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">

  <cxf:rsServer id="cxfConsumer"
                address="http://localhost:8080/rest"
                serviceClass="org.kie.camel.container.api.service.KieCamelTestService"/>

  <!-- add custom class to the configuration so it can be marshalled -->
  <bean id="kieConfiguration" class="org.kie.camel.KieConfiguration">
    <property name="kieServicesConfigurationCustomizer">
      <bean class="org.kie.camel.container.module.CamelKieServiceConfigurationCustomizer" />
    </property>
  </bean>
    
  <bean id="kie" class="org.kie.camel.KieComponent" >
    <property name="configuration" ref="kieConfiguration" /> 
  </bean>

  <bean id="batchExecutionHelperProvider" class="org.drools.core.runtime.help.impl.BatchExecutionHelperProviderImpl" />
  <bean id="xstreamBean" factory-bean="batchExecutionHelperProvider" factory-method="newXStreamMarshaller" />
  <bean id="executionServerMarshaller" class="org.kie.camel.container.module.Marshaller" />
  <bean id="listProcessInstancesProcessor" class="org.kie.camel.container.module.ListProcessInstancesProcessor"/>
  <bean id="executionServerCommandProcessor" class="org.kie.camel.container.module.ExecutionServerCommandProcessor"/>

  <bean id="kieSpringPostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor"/>
  <kie:kmodule id="test-kmodule">
    <kie:kbase name="rules-kbase" packages="org.kie.camel.container.rules,org.kie.camel.container.api.model" >
      <kie:ksession name="stateless-session" type="stateless"/>
    </kie:kbase>
    <kie:kbase name="process-kbase" packages="org.kie.camel.container.processes,org.kie.camel.container.api.model,org.kie.camel.container.rules" >
      <kie:ksession name="stateful-session" type="stateful"/>
    </kie:kbase>
  </kie:kmodule>

  <camelContext id="camel-context" xmlns="http://camel.apache.org/schema/spring">
    <route id="cxfConsumerToProcessorRoute">
      <from id="cxfrsConsumer" uri="cxfrs:bean:cxfConsumer" />
      <choice>
        <when>
          <simple>${header.operationName} == 'verifyAge'</simple>
          <to uri="direct:ageVerification" />
        </when>
        <when>
          <simple>${header.operationName} == 'runCommand'</simple>
          <to uri="direct:runCommand"/>
        </when>
        <when>
          <simple>${header.operationName} == 'runOnExecServer'</simple>
          <to uri="direct:runOnExecServer"/>
        </when>
      </choice>
    </route>

    <route id="ageVerificationRoute">
      <from uri="direct:ageVerification" />
      <transform><simple>${body[0]}</simple></transform>
      <to uri="kie-local:stateless-session?action=insertBody"/>
    </route>

    <route id="runCommand">
      <from uri="direct:runCommand" />
      <convertBodyTo type="java.lang.String"/>
      <to uri="bean:xstreamBean?method=fromXML(String)"/>
      <choice>
        <when>
          <simple>${body} is 'org.drools.core.command.runtime.process.GetProcessInstancesCommand'</simple>
          <to uri="direct:runListProcessesCommand"/>
        </when>
        <otherwise>
          <to uri="direct:runOtherCommand"/>
        </otherwise>
      </choice>
    </route>

    <route id="runListProcessesCommand">
      <from uri="direct:runListProcessesCommand" />
      <to uri="kie-local:stateful-session?action=execute"/>
      <to uri="bean:listProcessInstancesProcessor"/>
      <to uri="bean:xstreamBean?method=toXML(Object)"/>
    </route>

    <route id="runOtherCommand">
      <from uri="direct:runOtherCommand" />
      <to uri="kie-local:stateful-session?action=execute"/>
      <to uri="bean:xstreamBean?method=toXML(Object)"/>
    </route>

    <route id="execServerRoute">
      <from uri="direct:runOnExecServer"/>
      <convertBodyTo type="java.lang.String"/>
      <to uri="bean:executionServerMarshaller?method=unmarshall(String)"/>
      <to uri="bean:executionServerCommandProcessor"/>
      <to uri="kie:http://yoda:test@localhost:8080/kie-server-services/services/rest/server"/>
      <to uri="bean:executionServerMarshaller?method=marshall(Object)"/>
    </route>
  </camelContext>
</beans>
