[#ftl attributes={"generated.type":"blueprint"}]
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:soap-type="http://www.entaxy.ru/ExchangeTypes/1.0"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    [#import "templates:object-commons/common-utils.ftl" as utils]
    <reference id="entaxy-broker" interface="org.apache.camel.Component"
               filter="(connection.name=entaxy-broker)"/>

    <bean id="uuidGenerator" class="java.util.UUID" scope="prototype" factory-method="randomUUID"/>

    <bean id="headerMergeAggregator" class="ru.entaxy.esb.system.common.aggregation.HeaderMergeAggregatorImpl"/>

    <bean id="headersConverter" class="ru.entaxy.esb.system.common.util.HeadersConverter">
        <property name="namespace" value="http://www.entaxy.ru/ExchangeTypes/1.0"/>
        <property name="customHeaders" value="NTX_CustomHeaders"/>
        <property name="customHeaderPrefix" value="NTX_UNIFORM_EXCHANGE_CustomHeader"/>
    </bean>

    <camelContext id="[=objectId]" xmlns="http://camel.apache.org/schema/blueprint">

        <route id="[=objectId].output-route">
            <from uri="direct:entry-cascade-finish"/>
            <when>
                <simple>${headers.ENTAXY_Priority} == null || ${headers.ENTAXY_Priority} == ""</simple>
                <setHeader name="ENTAXY_Priority">
                    <simple resultType="Integer">4</simple>
                </setHeader>
            </when>
            <setHeader name="NTX_OutQueue">
                <simple>entaxy.uniform.service.exchange.active.[=properties.profile]</simple>
            </setHeader>
            <removeHeaders pattern="X-.+"/>
            <log message="${headers.NTX_loggingKey} Message sent to broker to queue:${headers.NTX_OutQueue}"
                 loggingLevel="INFO"/>
            <log message="${headers.NTX_loggingKey} Body is: ${body}" loggingLevel="TRACE"/>
            <toD uri="entaxy-broker:queue:${headers.NTX_OutQueue}?priority=${headers.ENTAXY_Priority}"/>
        </route>

        <route id="[=objectId].soap-out-active-connector" streamCache="true">
            <from uri="entaxy-broker:queue:entaxy.uniform.service.exchange.active.[=properties.profile]"/>
            <log message="${headers.NTX_loggingKey} Message ${headers} send to system" loggingLevel="TRACE"/>
            <setHeader name="NTX_SystemId">
                <constant>[=properties.profile]</constant>
            </setHeader>
            <setHeader name="ENTAXY_SystemEndpoint">
                <constant>[=utils.convertConfigValue(properties.endpointUrl)]</constant>
            </setHeader>

            <log message="${headers.NTX_loggingKey} ACTIVE-CONSUMER: Message for system ${headers.NTX_SystemId} to ${headers.ENTAXY_SystemEndpoint}"
                 loggingLevel="DEBUG"/>
            <when>
                <simple>${headers.ENTAXY_SystemEndpoint} == null || ${headers.NTX_SystemId} == null</simple>
                <log message="${headers.NTX_loggingKey} ENTAXY_SystemEndpoint for ${headers.NTX_SystemId} is not set"
                     loggingLevel="ERROR"/>
                <stop/>
            </when>
            <bean ref="headersConverter" method="json2xml"/>

            <setHeader name="NTX_OutQueue">
                <simple>entaxy.uniform.service.exchange.active.[=properties.profile]</simple>
            </setHeader>
            <setHeader name="ENTAXY_AcknowledgeMsgID">
                <simple>${bean:uuidGenerator.toString}</simple>
            </setHeader>

            <to uri="direct-vm:uniform-service-common-save-acknowledge?block=true&amp;timeout=70000"/>

            <setBody>
                <simple><![CDATA[
                    <body>${body}</body>
                    ]]>
                </simple>
            </setBody>
            <to uri="xslt:ru/entaxy/esb/platform/runtime/modules/uniform-service/xslt/PutPacketRequest.xsl"/>
            <setHeader name="operationNamespace">
                <constant>http://www.entaxy.ru/connector/universal/system/1.0</constant>
            </setHeader>
            <setHeader name="operationName">
                <constant>PutPackets</constant>
            </setHeader>
            <setHeader name="CamelDestinationOverrideUrl">
                <simple>${headers.ENTAXY_SystemEndpoint}</simple>
            </setHeader>

            <to uri="direct-vm:uniform-service-send-package-active"/>
            <choice>
                <when>
                    <xpath>not(//soap-type:status/text() = '200')</xpath>
                    <log message="${headers.NTX_loggingKey} PutPackets fail" loggingLevel="ERROR"/>
                </when>
                <otherwise>
                    <setHeader name="NTX_AckMessage">
                        <simple>true</simple>
                    </setHeader>
                    <to uri="entaxy-broker:queue:entaxy.uniform.service.exchange.aggregate?exchangePattern=InOnly&amp;asyncConsumer=true"/>
                </otherwise>
            </choice>
        </route>
    </camelContext>
</blueprint>
