<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~~~~~~licensing~~~~~~
  events-impl
  ==========
  Copyright (C) 2020 - 2023 EmDev LLC
  ==========
  You may not use this file except in accordance with the License Terms of the Copyright
  Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
  rights to the Software and any copies are the property of the Copyright Holder. Unless
  it is explicitly allowed the Copyright Holder, the User is prohibited from using the
  Software for commercial purposes to provide services to third parties.
  
  The Copyright Holder hereby declares that the Software is provided on an "AS IS".
  Under no circumstances does the Copyright Holder guarantee or promise that the
  Software provided by him will be suitable or not suitable for the specific purposes
  of the User, that the Software will meet all commercial and personal subjective
  expectations of the User, that the Software will work properly, without technical
  errors, quickly and uninterruptedly.
  
  Under no circumstances shall the Copyright Holder or its Affiliates is not liable
  to the User for any direct or indirect losses of the User, his expenses or actual
  damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
  or damage to data, property, etc.
  ~~~~~~/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>
