<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~~~~~~licensing~~~~~~
  system-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"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           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/>

    <cm:property-placeholder persistent-id="ru.entaxy.esb" update-strategy="reload">
        <cm:default-properties>
        </cm:default-properties>
    </cm:property-placeholder>

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

    <service ref="SystemService"
             interface="ru.entaxy.esb.system.jpa.SystemService"/>

    <bean id="SystemService"
          class="ru.entaxy.esb.system.jpa.impl.SystemServiceImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </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:SystemServiceTest"/>


        </route>

        <route>
            <from uri="direct:SystemServiceTest"/>
            <log message="TEST---------------------SystemService------------------"/>
            <bean ref="SystemService" method="getSystemList"/>
            <log message="All registry ${body}"/>
            <split>
                <simple>${body}</simple>

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

                <bean ref="SystemService" method="getByUuid(${header.SYSTEM.getUuid()})"/>
                <log message="Registry by uuid ${body}"/>

                <!-- 				<doTry> -->
                <!-- 					<toD uri="system:${header.SYSTEM.getName()}"/> -->
                <!-- 					<doCatch> -->
                <!-- 						<exception>ProfileNotFound</exception> -->
                <!-- 						<log message="${exception.stacktrace}" loggingLevel="ERROR" /> -->
                <!-- 					</doCatch> -->
                <!-- 				</doTry> -->
            </split>

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

            <log message="Set id 0 ${header.SYSTEM.setId(0)}"/>
            <log message="Set methods null ${header.SYSTEM.setExportAlloweds(null)}"/>
            <log message="Set uuid 777 ${header.SYSTEM.setUuid('777')}"/>
            <log message="Set name TestSystem ${header.SYSTEM.setName('TestSystem')}"/>
            <log message="CREATE NEW Registry with uuid 777"/>
            <bean ref="SystemService" method="create(${header.SYSTEM})"/>

            <bean ref="SystemService" method="getByUuid('777')"/>
            <log message="NEW Registry by uuid ${body}"/>

            <bean ref="SystemService" method="remove(${body.getId()})"/>
            <doTry>
                <bean ref="SystemService" method="getByUuid('777')"/>
                <log message="NEW Registry by uuid AFTER DELETE ${body}"/>
                <doCatch>
                    <exception>javax.persistence.NoResultException</exception>
                    <log message="System not found" loggingLevel="WARN"/>
                </doCatch>
            </doTry>
        </route>

    </camelContext>

</blueprint>
