<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~~~~~~licensing~~~~~~
  events-impl
  ==========
  Copyright (C) 2020 - 2021 EmDev LLC
  ==========
  Licensed 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.
  ~~~~~~/licensing~~~~~~
  -->

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"
           xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <jpa:enable/>
    <tx:enable-annotations/>

    <reference id="sessionFactory"
               interface="org.hibernate.SessionFactory"
               timeout="30000"/>

    <reference id="permissionService"
               interface="ru.entaxy.esb.system.core.permission.jpa.PermissionService"
               timeout="30000"
               availability="mandatory"/>

    <service ref="eventTopicService"
             interface="ru.entaxy.esb.system.core.events.jpa.EventTopicService"/>

    <bean id="eventTopicService"
          class="ru.entaxy.esb.system.core.events.jpa.impl.EventTopicServiceImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
        <property name="permissionService" ref="permissionService"/>
        <property name="systemSubscriptionService" ref="systemSubscriptionService"/>
    </bean>

    <service ref="systemSubscriptionService"
             interface="ru.entaxy.esb.system.core.events.jpa.SystemSubscriptionService"/>

    <bean id="systemSubscriptionService"
          class="ru.entaxy.esb.system.core.events.jpa.impl.SystemSubscriptionServiceImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
        <property name="permissionService" ref="permissionService"/>
    </bean>

    <camelContext id="test-hiber-context" xmlns="http://camel.apache.org/schema/blueprint">

        <route id="list" autoStartup="false">
            <from uri="timer:foo?period=15000&amp;repeatCount=1"/>

            <to uri="direct:eventTopicService"/>

        </route>

        <route id="eventTopicService" autoStartup="true">
            <from uri="direct:eventTopicService"/>

            <log message="TEST---------------------EventTopicService------------------"/>

            <bean ref="eventTopicService" method="list"/>
            <log message="All topics ${body}"/>

            <when>
                <simple>${body} != null &amp;&amp; ${body.size()} > 0</simple>
                <split>
                    <simple>${body}</simple>

                    <log message="-----------------Preferred involved topic ${body}-----------"/>

                    <setHeader name="TOPIC">
                        <simple>${body}</simple>
                    </setHeader>
                    <setBody>
                        <simple>null</simple>
                    </setBody>
                    <bean ref="eventTopicService" method="get(${header.TOPIC.getId()})"/>
                    <log message="Topic by id ${body}"/>

                    <bean ref="eventTopicService" method="getByName(${header.TOPIC.getName()})"/>
                    <log message="Topic by name ${body}"/>

                    <log message="----------------------------------------------}-----------"/>
                </split>

                <setHeader name="TOPIC">
                    <simple>${body.get(0)}</simple>
                </setHeader>

                <log message="Set id 0 ${header.TOPIC.setId(0)}"/>
                <log message="Set topicName ${header.TOPIC.setName('testTopic')}"/>
                <log message="ADD NEW topic - testTopic"/>
                <bean ref="eventTopicService" method="add(${header.TOPIC})"/>

                <!-- 				<setBody> -->
                <!-- 					<simple resultType="java.lang.String">testTopic2</simple> -->
                <!-- 				</setBody> -->

                <!-- 				<bean ref="eventTopicService" method="saveOrUpdate(java.lang.String, ${header.TOPIC.getCreateDate()}, ${header.TOPIC.getCreatedBy())" /> -->

                <bean ref="eventTopicService" method="getByName('testTopic')"/>
                <log message="NEW Topic by topicName ${body}"/>
                <bean ref="eventTopicService" method="remove(${body.getId()})"/>
                <log message="DELETE Topic by Id ${body.getId()}"/>

                <!-- 				<bean ref="eventTopicService" method="getByName('testTopic2')" /> -->
                <!-- 				<log message="NEW Topic by topicName ${body}" /> -->
                <!-- 				<bean ref="eventTopicService" method="remove(${body.getId()})" /> -->
                <!-- 				<log message="DELETE Topic by Id ${body.getId()}" /> -->

                <doTry>
                    <bean ref="eventTopicService" method="getByName('testTopic')"/>
                    <log message="NEW Topic by topicName AFTER DELETE ${body}"/>
                    <doCatch>
                        <exception>javax.persistence.NoResultException</exception>
                        <log message="EventTopic not found" loggingLevel="WARN"/>
                    </doCatch>
                </doTry>
            </when>
        </route>

    </camelContext>

</blueprint>
