<beans default-lazy-init="false"
    xmlns="http://www.springframework.org/schema/beans" 	
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"        
    xsi:schemaLocation="
    	http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">


	<bean id="common.scheduledProcessInvoker" class="gov.va.med.fw.batchprocess.BatchProcessInvoker"/>
	<alias name="common.scheduledProcessInvoker" alias="common.batchProcessInvoker"/>
	<alias name="common.scheduledProcessInvoker" alias="BatchProcessInvoker"/>

	<bean id="common.abstractScheduledProcess" parent="common.abstractService" abstract="true">
		<property name="proxyInterfaces">
			<list>
				<value>gov.va.med.fw.scheduling.ScheduledProcess</value>
			</list>
		</property>
	</bean>
	
	<!-- Trigger Groups -->
    <bean id="triggerGroup.staticBatchJob" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="staticField" value="gov.va.med.ccht.service.common.BatchProcessTriggerGroups.STATIC_BATCH_JOBS" />
    </bean>

    <bean id="triggerGroup.dynamicBatchJob" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="staticField" value="gov.va.med.ccht.service.common.BatchProcessTriggerGroups.DYNAMIC_BATCH_JOBS" />
    </bean>

	<!-- Default settings for dynamic and static Triggers -->
	<bean id="common.abstract.dynamicTrigger" abstract="true"
		class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean" >
		<property name="group" ref="triggerGroup.dynamicBatchJob"/>
	</bean>

	<bean id="common.abstract.staticTrigger" abstract="true"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" >
		<property name="group" ref="triggerGroup.staticBatchJob"/>
		<property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_DO_NOTHING"/>		
	</bean>
		
	<!-- A preconfigured trigger to start a job right away -->
	<bean id="immediateTrigger" parent="common.abstract.dynamicTrigger" scope="prototype">
		<property name="name" ref="immediateTriggerName"/>
		<property name="repeatCount" value="0"/>
		<!-- this start delay avoids unnecessary misfire situations -->
		<property name="startDelay" value="5000"/>
	</bean>
	<bean id="immediateTriggerName" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
		<property name="staticField" value="gov.va.med.fw.scheduling.SchedulingService.IMMEDIATE_TRIGGER_NAME" />
	</bean>

	<!-- A preconfigured trigger to start a job in a future -->
	<bean id="futureTrigger" parent="common.abstract.dynamicTrigger" scope="prototype">
		<property name="name" ref="futureTriggerName"/>
		<property name="repeatCount" value="0"/>
	</bean>
	<bean id="futureTriggerName" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
		<property name="staticField" value="gov.va.med.fw.scheduling.SchedulingService.FUTURE_TRIGGER_NAME" />
	</bean>    
	
	<!-- Common Scheduler service -->
	<bean id="schedulingService" parent="common.abstractService">
		<property name="target">
			<bean class="gov.va.med.fw.scheduling.QuartzSchedulingService">
				<property name="scheduler" ref="quartzSchedulerFactory" />
				<property name="immediateTriggerKey">
					<idref local="immediateTrigger"/>
				</property>
				<property name="futureTriggerKey">
					<idref local="futureTrigger"/>
				</property>
			</bean>
		</property>
	</bean>	
	
	<bean id="quartzProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="properties">
						<props>
				<!-- check for misfires every 3 minutes -->
				<prop key="org.quartz.jobStore.misfireThreshold">180000</prop>
				<!-- required for clustering -->
				<prop key="org.quartz.jobStore.isClustered">${isClustered}</prop>
				<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
				<!-- required for other reasons -->
				<!--
					This value is used in a cluster environment for each clustered instance
					to check into a cluster manager.  A default value is 7.5 seconds which
					is not realistic.  CISS sets each cluster instance to check in at 5 minutes
				<prop key="org.quartz.jobStore.driverDelegateClass">
					org.quartz.impl.jdbcjobstore.oracle.weblogic.WebLogicOracleDelegate</prop>					
				-->
			    <prop key="org.quartz.jobStore.tablePrefix">INF.QRTZ_</prop>				
				<prop key="org.quartz.jobStore.useProperties">true</prop>
				<prop key="org.quartz.jobStore.clusterCheckinInterval">300000</prop>
				<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.MSSQLDelegate</prop> 
				<prop key="org.quartz.plugin.shutdownHook.class">org.quartz.plugins.management.ShutdownHookPlugin</prop>
				<prop key="org.quartz.plugin.shutdownHook.cleanShutdown">true</prop>
			</props>
        </property>
    </bean>
    	
	<bean
		id="quartzSchedulerFactory"
		class="gov.va.med.fw.scheduling.ClusterAwareSchedulerFactoryBean"
		lazy-init="false">

		<property name="applicationContextSchedulerContextKey" value="applicationContext"/>
		<property name="autoStartup" value="true"/>
		<property name="overwriteExistingJobs" value="true"/>
		<property name="dataSource" ref="dataSource"/>
		<property name="transactionManager" ref="transactionManager"/>
		<property name="schedulerFactoryClass" value="gov.va.med.fw.scheduling.CustomSchedulerFactory"/>

		<!-- only use this for items that cannot set via Spring -->
		<property name="quartzProperties" ref="quartzProperties"/>
		<property name="globalTriggerListeners">
			<list>
				<bean class="gov.va.med.fw.scheduling.ScheduledProcessListener">
					<property name="name" value="TriggerListenerLogger" />
				</bean>
			</list>
		</property>
		<property name="globalJobListeners">
			<list>
				<!-- this does not show the stack trace, which is very helpful - so we do not use
				<bean class="org.quartz.plugins.history.LoggingJobHistoryPlugin"/ -->
				<bean class="gov.va.med.fw.scheduling.ScheduledJobListener"/>
			</list>
		</property>
	</bean>

	<!--
		@description: This bean defines a list of triggers.
		A scheduling service is a common scheduling service that is configured with
		one singleton scheduler for the whole application.  This bean must be
		eager loaded to initialize triggers at start-up.

		Also, allows the application to have complete control over when and when not to overwrite
		existing triggers in the JobStore
	-->
	<bean class="gov.va.med.fw.scheduling.SchedulerConfigurator" lazy-init="false">
		<property name="transactionManager" ref="transactionManager"/>
		<property name="scheduler" ref="quartzSchedulerFactory"/>
		<property name="triggers" ref="scheduledJobs.triggers" />
		<property name="batchProcessService" ref="batchProcessService" />
		<property name="transactionAttribute">
			<bean class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
				<property name="staticField" value="org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED" />
			</bean>    
		</property>
		<property name="transactionTimeout" value="600"/>
	</bean>
	
	<!-- Statically Scheduled Common jobs (remove entries if not scheduled by Quartz internally) -->
	<util:list id="scheduledJobs.triggers" >
			<ref local="purgeCompletedReportsCronTrigger"/>	
			
			<ref local="QIRVendorResponseDueCronTrigger"/>		
	</util:list>

	<!-- 
	<ref local="weeklyVendorComplianceReportCronTrigger"/>
	<bean id="weeklyVendorComplianceReportCronTrigger" parent="common.abstract.staticTrigger">
		<property name="jobDetail" ref="WeeklyVendorComplianceReport" />
		<property name="cronExpression" value="0 0 7 ? * MON *"/>
	</bean>	

	<bean id="WeeklyVendorComplianceReport" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass" value="gov.va.med.fw.batchprocess.JobLauncherDetails" />
		<property name="group" value="ccht-batch" />
		<property name="description" value="Device Model and Serial Number Vendor Compliance Report E-mail"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="jobName"><idref bean="weeklyVendorComplianceReportJob"/></entry>
				<entry key="messageProducerServiceName"> <idref bean="common.jms.producer.batchProcessEvents"/></entry>
				<entry key="runBatchJobJMSPayloadName"> <idref bean="runBatchJobJMSPayload"/></entry>				
			</map>
		</property>
	</bean>
	 -->
	<bean id="purgeCompletedReportsCronTrigger" parent="common.abstract.staticTrigger">
		<property name="jobDetail" ref="PurgeCompletedReports" />
		<property name="cronExpression" value="0 0 3 ? * SAT,SUN,FRI,MON,TUE,WED,THU *"/>
	</bean>
						
	<bean id="PurgeCompletedReports" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass" value="gov.va.med.fw.batchprocess.JobLauncherDetails" />
		<property name="group" value="ccht-batch" />
		<property name="description" value="Deletes all reports in the application that have expired"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="jobName"><idref bean="purgeCompletedReportsJob"/></entry>
				<entry key="messageProducerServiceName"> <idref bean="common.jms.producer.batchProcessEvents"/></entry>
				<entry key="runBatchJobJMSPayloadName"> <idref bean="runBatchJobJMSPayload"/></entry>				
			</map>
		</property>
	</bean>
	
			
	<bean id="QIRVendorResponseDueCronTrigger" parent="common.abstract.staticTrigger">
		<property name="jobDetail" ref="QIRVendorResponseDue" />
		<property name="cronExpression" value="0 0 3 ? * SAT,SUN,FRI,MON,TUE,WED,THU *"/>
	</bean>
						
	<bean id="QIRVendorResponseDue" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass" value="gov.va.med.fw.batchprocess.JobLauncherDetails" />
		<property name="group" value="ccht-batch" />
		<property name="description" value="Generate Notification to Vendor when the due date is passed"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="jobName"><idref bean="QIRVendorResponseDueJob"/></entry>
				<entry key="messageProducerServiceName"> <idref bean="common.jms.producer.batchProcessEvents"/></entry>
				<entry key="runBatchJobJMSPayloadName"> <idref bean="runBatchJobJMSPayload"/></entry>				
			</map>
		</property>
	</bean>
				
	<!--	Dynamic Scheduled Report Job	-->
	<bean 
		id="scheduledJob.StandardReportGeneration" 
		class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass" value="gov.va.med.fw.batchprocess.ReportLauncherDetails" />
		<property name="group" value="ccht-batch" />
		<property name="description" value="Scheduled Report Generation"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="messageProducerServiceName"> <idref bean="common.jms.producer.reportingEvents"/></entry>
				<entry key="jmsPayloadName"> <idref bean="createReportJMSPayload"/></entry>				
			</map>
		</property>
	</bean>		
</beans>

