<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:sec="http://cxf.apache.org/configuration/security"
	xmlns:wsa="http://cxf.apache.org/ws/addressing"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:http="http://cxf.apache.org/transports/http/configuration"
	xmlns:t="http://dnetlib.eu/springbeans/t"
	xmlns:template="http://dnetlib.eu/springbeans/template"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
		http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://dnetlib.eu/springbeans/template http://dnetlib.eu/springbeans/template.xsd
		http://cxf.apache.org/ws/addressing http://cxf.apache.org/schemas/ws-addr-conf.xsd
		http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd">

	<!-- beans -->
	<bean id="msroService" class="eu.dnetlib.msro.MSROServiceImpl"
		init-method="start" destroy-method="stop" 
		p:notificationHandler-ref="msroNotificationHandlerChain"
		p:processRegistry-ref="graphProcessRegistry" />

	<bean id="msroNotificationHandlerChain"
		class="eu.dnetlib.enabling.tools.blackboard.NotificationHandlerChainImpl">
		<property name="handlerExecutor">
			<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
				p:corePoolSize="32" p:keepAliveSeconds="3600" p:queueCapacity="0" />
		</property>
		<property name="handlers">
			<list>
				<ref bean="msroBlackboardNotificationHandler" />
			</list>
		</property>
	</bean>

	<bean id="wfDependencyLauncherNotificationHandler" class="eu.dnetlib.msro.notification.WfDependencyLauncherNotificationHandler"
		p:enabled="true" p:topicExpression="UPDATE/WorkflowDSResourceType/*/RESOURCE_PROFILE/BODY/STATUS/LAST_EXECUTION_DATE" />

	<alias name="cxfEndpointReferenceBuilder" alias="eprBuilder" />
	
	<bean id="msroWfEngine" class="eu.dnetlib.msro.workflows.sarasvati.loader.InjectableMemEngine">
		<property name="graphFactory">
			<bean class="eu.dnetlib.msro.workflows.sarasvati.loader.InjectableMemGraphFactory">
				<property name="nodeFactory">
					<bean class="eu.dnetlib.msro.workflows.sarasvati.loader.InjectableNodeFactory"
						p:beanNamePrefix="wfNode" />				
				</property>
			</bean>
		</property>
	</bean>
	
	<bean id="graphProcessRegistry" class="eu.dnetlib.msro.workflows.sarasvati.registry.GraphProcessRegistry" 
		p:maxSize="${msro.wf.registry.maxSize}" />

	<bean id="sarasvatiGraphLoader" 
		class="eu.dnetlib.msro.workflows.sarasvati.loader.GraphLoader" 
		p:engine-ref="msroWfEngine" /> 
		
	<bean id="sarasvatiWorkflowExecutor" class="eu.dnetlib.msro.workflows.sarasvati.loader.WorkflowExecutor"
	      p:engine-ref="msroWfEngine"
	      p:graphLoader-ref="sarasvatiGraphLoader"
	      p:graphProcessRegistry-ref="graphProcessRegistry"
	      p:profileToSarasvatiConverter-ref="msroProfileToSarasvatiConverter"
	      p:maxRunningWorkflows="${msro.wf.max.running}"
	      init-method="init"/>
	
	<bean id="workflowsConstants" class="eu.dnetlib.msro.workflows.util.WorkflowsConstants"
		init-method="init" 
		p:dataproviderProtocolsJson="${msro.wf.ui.dataprovider.protocols}" 
		p:dataproviderTypologiesJson="${msro.wf.ui.dataprovider.typologies}" />
		
	<bean id="msroProfileToSarasvatiConverter" class="eu.dnetlib.msro.workflows.sarasvati.loader.ProfileToSarasvatiConverter"
	 	p:xslt="classpath:/eu/dnetlib/msro/workflows/xslt/wf_profile2sarasvati.xslt" />
	
	<bean id="msroBlackboardNotificationHandler"
		class="eu.dnetlib.enabling.tools.blackboard.DeletingBlackboardNotificationHandler"
		p:blackboardHandler-ref="blackboardClientHandler" />
	
	<bean id="datasourceMetaWorkflowFactory" class="eu.dnetlib.msro.workflows.metawf.DatasourceMetaWorkflowFactory"/>
	
	<bean id="msroProcessCountingResultSetFactory" 
		class="eu.dnetlib.msro.workflows.resultset.ProcessCountingResultSetFactory"
		p:resultSetFactory-ref="openResultSetFactory" p:serviceResolver-ref="serviceResolver" />
	
	<bean t:id="msroJobSchedulerAccessor"
		class="org.springframework.scheduling.quartz.SchedulerAccessorBean"
		p:scheduler-ref="jobScheduler">
		<property name="triggers">
			<list>
				<bean class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"
					p:startDelay="6000" p:repeatCount="0">
					<property name="jobDetail">
						<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
							p:targetObject-ref="msroBlackboardSubscriber" p:targetMethod="subscribeAll" />
					</property>
				</bean>
				<bean class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"
					p:startDelay="${msro.wf.scheduler.startDelay}" p:repeatInterval="${msro.wf.scheduler.repeatInterval}">
					<property name="jobDetail">
						<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
							p:targetObject-ref="msroScheduledWorkflowLauncher" p:targetMethod="verifySheduledWorkflows" />
					</property>
				</bean>
				<bean class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"
					p:startDelay="${msro.wf.mail.thread.startDelay}" p:repeatInterval="${msro.wf.mail.thread.repeatInterval}">
					<property name="jobDetail">
						<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
							p:targetObject-ref="msroMailDispatcher" p:targetMethod="processMailQueue" />
					</property>
				</bean>
			</list>
		</property>
	</bean>		
	
	<bean id="msroBlackboardSubscriber" class="eu.dnetlib.msro.workflows.nodes.blackboard.BlackboardSubscriber"
		p:endpoint-ref="msroServiceEndpoint" />
	
	<bean id="msroScheduledWorkflowLauncher" class="eu.dnetlib.msro.cron.ScheduledWorkflowLauncher"
		p:graphProcessRegistry-ref="graphProcessRegistry" p:workflowExecutor-ref="sarasvatiWorkflowExecutor"
		p:windowSize="${msro.wf.scheduler.window.size}" />
	
	
	<bean id="msroWorkflowLogger" class="eu.dnetlib.common.logging.DnetLogger"
		p:name="${msro.wf.logger.name}" p:indexConf="${msro.wf.logger.index.conf}" />
	
	<bean id="msroMailDispatcher" class="eu.dnetlib.msro.notification.EmailDispatcher"
		p:from="${msro.wf.mail.from}" p:fromName="${msro.wf.mail.fromName}" p:cc="${msro.wf.mail.cc}"
		p:smtpHost="${msro.wf.mail.smtp.host}" p:smtpPort="${msro.wf.mail.smtp.port}" p:smtpUser="${msro.wf.mail.smtp.user}" p:smtpPassword="${msro.wf.mail.smtp.password}" 
		p:baseUrl="${msro.wf.mail.message.baseUrl}" p:infrastructure="${msro.wf.mail.message.infrastructure}"/>
	
	<!-- System nodes -->
	<bean id="wfNodeSuccess" class="eu.dnetlib.msro.workflows.nodes.SuccessFailureNode" p:success="true" scope="prototype" />
	<bean id="wfNodeFailure" class="eu.dnetlib.msro.workflows.nodes.SuccessFailureNode" p:success="false" scope="prototype" />
		
	<!-- REPO_HI and REPO_BYE -->
	<bean id="wfNodeRegisterMetaWf"	class="eu.dnetlib.msro.workflows.nodes.repohi.RegisterMetaWfJobNode" scope="prototype" />
	<bean id="wfNodeUpdateMetaWf" class="eu.dnetlib.msro.workflows.nodes.repohi.UpdateMetaWfJobNode" scope="prototype" />
	<bean id="wfNodeUpdateMetaWfStatus"	class="eu.dnetlib.msro.workflows.nodes.repohi.UpdateMetaWfStatusJobNode" scope="prototype" />
	<bean id="wfNodeDeleteMetaWf" class="eu.dnetlib.msro.workflows.nodes.repobye.DeleteMetaWfJobNode" scope="prototype" />
	<bean id="wfNodeDeleteMDStore" class="eu.dnetlib.msro.workflows.nodes.repobye.DeleteMDStoreJobNode" scope="prototype" />
	
	<!-- endpoints -->
	<jaxws:endpoint id="msroServiceEndpoint" implementor="#msroService"
		implementorClass="eu.dnetlib.msro.rmi.MSROService"
		address="/msro" />

	<template:instance name="serviceRegistrationManager"
		t:serviceRegistrationManagerClass="eu.dnetlib.enabling.tools.registration.ValidatingServiceRegistrationManagerImpl"
		t:name="msroServiceRegistrationManager" t:service="msroService"
		t:endpoint="msroServiceEndpoint" t:jobScheduler="jobScheduler" />

</beans>
