[#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"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

<!-- 

	factoryId: common-profile-2
	factoryType: entaxy.runtime.profile
	outputType: init 

  -->
  
[#import "templates:object-commons/metadata-storage.ftl" as ms]

	<bean id="[=properties.systemName].metadataEnricher" class="ru.entaxy.platform.core.support.runtime.metadata.ExchangeEnricher" activation="eager">
	</bean>	


[#if properties.profileProperties??]	
[@ms.metadata_storage id=properties.systemName+".metadataStorage" data=properties.profileProperties prefix="NTX_profile_" consumer=null /]
[/#if]              

	<bean id="[=properties.systemName].metadataStorageWrapper" class="ru.entaxy.platform.core.support.runtime.metadata.MetadataStorageWrapper">
		<property name="origin" ref="[=properties.systemName].metadataStorage" />
		<property name="prefix" value="NTX_TARGET_profile_" />
		<property name="consumer" ref="[=properties.systemName].metadataEnricher" />
	</bean>

	<service interface="ru.entaxy.platform.core.support.runtime.metadata.MetadataStorage" 
			ref="[=properties.systemName].metadataStorage">
		<service-properties>
            <entry key="profile.name" value="[=properties.systemName]"/>
       	</service-properties>
	</service>  

	<bean id="profile-[=properties.systemName]" class="ru.entaxy.platform.profiles.runtime.impl.SystemProfileImpl" 
			activation="eager" init-method="activate">
		<property name="id" value="[=properties.systemName]" />
		<property name="type" value="[=properties.profileType]" />
		<property name="bundleContext" ref="blueprintBundleContext" />
	</bean>      

    <service interface="ru.entaxy.platform.profiles.runtime.SystemProfile"
    		ref="profile-[=properties.systemName]">
		<service-properties>
            <entry key="profile.name" value="[=properties.systemName]"/>
       	</service-properties>
    </service>              
                                                      
	<camelContext id="profile-[=properties.systemName]-context" xmlns="http://camel.apache.org/schema/blueprint">
	
		<route id="profile-[=properties.systemName]-backward-compatible">
			<from uri="direct-vm:profile-[=properties.systemName]-exit-dispatcher" />
			<to uri="direct-vm:profile-[=properties.systemName]-in-flow" />
		</route>
		
		<route id="profile-[=properties.systemName]-in-flow">
			<from uri="direct-vm:profile-[=properties.systemName]-in-flow" />
			<log message="Message ${headers} send to profile '[=properties.systemName]'" loggingLevel="TRACE"/>
			<to uri="direct:profile-[=properties.systemName]-in-flow-pre-route" />
			<to uri="direct:profile-[=properties.systemName]-in-flow-exit-route" />
		</route>
		
		<route id="profile-[=properties.systemName]-in-flow-exit-route">
			<from uri="direct:profile-[=properties.systemName]-in-flow-exit-route" />
			<setHeader name="NTX_Origin">
				<constant>[=systemName]</constant>
			</setHeader>

			<choice>
				<when>
					<simple>${headers.NTX_RESPOND_exec} == 'true'</simple>
					<choice>
						<when>
							<simple>${headers.NTX_RESPOND_continue} == 'true'</simple>
							<choice>
								<when>
									<simple>${headers.NTX_RESPOND_continueAsync} == 'true'</simple>
									<removeHeaders pattern="NTX_RESPOND_.+" />
									<!-- multicast shareUnitOfWork="true" -->
										<to uri="seda:profile-[=properties.systemName]-in-flow-exit-route-output-async" />
									[#if properties.inFlowProcessResponse]
										<to uri="direct:profile-[=properties.systemName]-in-flow-response-route" />
									[/#if]	
									<!-- /multicast -->
								</when>
								<otherwise>
									<removeHeaders pattern="NTX_RESPOND_.+" />
									<multicast shareUnitOfWork="true">
									[#if properties.inFlowProcessResponse]
										<to uri="direct:profile-[=properties.systemName]-in-flow-response-route" />
									[/#if]	
										<to uri="direct:profile-[=properties.systemName]-in-flow-exit-route-output" />
									</multicast>	
								</otherwise>	
							</choice>							
						</when>
						[#if properties.inFlowProcessResponse]
						<otherwise>
							<removeHeaders pattern="NTX_RESPOND_.+" />
							<to uri="direct:profile-[=properties.systemName]-in-flow-response-route" />
						</otherwise>	
						[/#if]	
					</choice>
				</when>
				<otherwise>
					<to uri="direct:profile-[=properties.systemName]-in-flow-exit-route-output" />
				[#if properties.inFlowProcessResponse]					
					<to uri="direct:profile-[=properties.systemName]-in-flow-response-route" />
				[/#if]
				</otherwise>
			</choice>

		</route>
		
		<route id="profile-[=properties.systemName]-in-flow-exit-route-output-async">
			<from uri="seda:profile-[=properties.systemName]-in-flow-exit-route-output-async" />
			<to uri="direct:profile-[=properties.systemName]-in-flow-exit-route-output" />
		</route>
		
		<route id="profile-[=properties.systemName]-in-flow-exit-route-output">
			<from uri="direct:profile-[=properties.systemName]-in-flow-exit-route-output" />
			<choice>
				<when>
					<simple>${headers.NTX_NextPoint} != null</simple>
					<setProperty name="NTX_Tmp">
						<simple>${headers.NTX_NextPoint}</simple>
					</setProperty>
					<removeHeader headerName="NTX_NextPoint"/>
					<toD uri="${exchangeProperty.NTX_Tmp}"/>
				</when>
				<otherwise>
					<to uri="direct-vm:profile-[=properties.systemName]-default-route"/>
				</otherwise>
			</choice>			
		</route>
		
		<route id="out-flow-entry">
            <from uri="direct-vm:profile-[=properties.systemName]-enter-point"/>
            <removeHeader headerName="ENTAXY_Destination"/>
            <log message="Message ${headers} send to profile output" loggingLevel="TRACE"/>
            <choice>
                <when>
                    <simple>${headers.NTX_Origin} == null</simple>
                    <toD uri="permission:checkSystemAccessException?objectId=${headers.X-SystemId}&amp;subjectId=[=systemName]"/>
                </when>
            </choice>
            <to uri="direct-vm:profile-[=properties.systemName]-out-flow" />		
		</route>

		<route id="profile-[=properties.systemName]-out-flow">
			<from uri="direct-vm:profile-[=properties.systemName]-out-flow" />		
			<to uri="bean:[=properties.systemName].metadataEnricher" />			
			<to uri="direct:profile-[=properties.systemName]-out-flow-pre-route" />
			<to uri="direct:profile-[=properties.systemName]-connector-selector" />
			<to uri="direct:profile-[=properties.systemName]-out-flow-exit-route" />
		[#if properties.outFlowProcessResponse]
			<to uri="direct:profile-[=properties.systemName]-out-flow-response-route" />
		[/#if]			
		</route>
		
		<route id="profile-[=properties.systemName]-out-flow-exit-route">
			<from uri="direct:profile-[=properties.systemName]-out-flow-exit-route" />
			<choice>
				<when>
					<simple>${headers.NTX_target_connector} != null</simple>
					<toD uri="direct-vm:${headers.NTX_target_connector}-entry-point" />
				</when>
				<otherwise>
					<!-- try to route via legacy scheme -->
					<log message="Message ${headers} is routing via legacy scheme" loggingLevel="WARN"/>
					<to uri="bean:profileOutput?method=sendTo"/>
				</otherwise>
			</choice>
		</route>								
																										
	</camelContext>
  
</blueprint>
