<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">
    <description>Batch Jobs</description>
																     	     
	<named-native-query name="springBatchJobsByStatus" result-set-mapping="springBatchJobHistoryMapping">
		<query><![CDATA[
			select i.job_name job_name, e.start_time start_time, e.end_time end_time, 
			e.status status, e.exit_code exit_code, e.exit_message exit_message,
			p.string_val as runby,
			u.last_name last_name, u.first_name first_name, u.middle_name middle_name,
			job_stats =replace(ltrim(rtrim(convert(varchar(8000),(
				select exit_message + ' ' as [text()]
				from inf.batch_step_execution se 
				where se.job_execution_id = e.job_execution_id
				order by start_time asc
				for xml path('')
			)))),'&#x0D;','')
			from inf.batch_job_instance i 
			join inf.batch_job_execution e on i.job_instance_id = e.job_instance_id
			left join inf.batch_job_params p on i.job_instance_id = p.job_instance_id 
			and key_name = 'runBy'
			left join ht.app_user u on p.string_val = u.user_name
			where e.status = :status 
			order by start_time desc]]></query>
	</named-native-query>
		     	     
	<named-native-query name="springBatchJobHistory" result-set-mapping="springBatchJobHistoryMapping">
		<query><![CDATA[
			select top 20 i.job_name job_name, e.start_time start_time, e.end_time end_time, 
			e.status status, e.exit_code exit_code, e.exit_message exit_message,
			p.string_val as runby,
			u.last_name last_name, u.first_name first_name, u.middle_name middle_name,
			job_stats =replace(ltrim(rtrim(convert(varchar(8000),(
				select exit_message + ' ' as [text()]
				from inf.batch_step_execution se 
				where se.job_execution_id = e.job_execution_id
				order by start_time asc
				for xml path('')
			)))),'&#x0D;','')
			from inf.batch_job_instance i 
			join inf.batch_job_execution e on i.job_instance_id = e.job_instance_id		    
			left join inf.batch_job_params p on i.job_instance_id = p.job_instance_id 
			and key_name = 'runBy'
			left join ht.app_user u on p.string_val = u.user_name
			where i.job_name = :jobName 
			order by start_time desc]]></query>
	</named-native-query>
	     		     
    <sql-result-set-mapping name="rowcount" >
    	<column-result name="total_number"/>
	</sql-result-set-mapping>
		     		     
    <sql-result-set-mapping name="springBatchJobHistoryMapping">
    		<column-result name="job_name"/>
			<column-result name="start_time"/>
    		<column-result name="end_time"/>
    		<column-result name="status"/>
    		<column-result name="exit_code"/>
    		<column-result name="exit_message"/>
    		<column-result name="runby"/>
    		<column-result name="last_name"/>
    		<column-result name="first_name"/>
    		<column-result name="middle_name"/>
    		<column-result name="job_stats"/>
	</sql-result-set-mapping>		
	
    <mapped-superclass class="gov.va.med.fw.model.batchprocess.AbstractJob" access="FIELD">
    <attributes>                  
			<basic name="jobName"> <column name="JOB_NAME" updatable="false"/></basic>
            <basic name="jobGroup"><column name="JOB_GROUP_NAME"/></basic> 
            <basic name="jobSchedule"><column name="CRON_SCHEDULE"/></basic>
            <basic name="jobScheduleDescription"><column name="CRON_SCHEDULE_DESCRIPTION"/></basic> 
            <basic name="jobScheduleText"><column name="JOB_SCHEDULED_TXT"/></basic>  
            <basic name="emailDistributionList"> <column name="EMAIL_DISTRIBUTION_LIST"/></basic>
     </attributes>    
    </mapped-superclass>
    										
    <entity class="gov.va.med.fw.model.batchprocess.JobConfig" access="FIELD" name="JobConfig" >
	    <table schema="HT" name="JOB_CONFIG"/>
	    
	    <named-query name="jobConfigQuery_GetAll">
	    	<query>
	    		<![CDATA[select jc from gov.va.med.fw.model.batchprocess.JobConfig jc]]>
	    	</query>
	    </named-query>
	    	    
	    <named-query name="jobConfigQuery_GetByJobName">
	    	<query>
	    		<![CDATA[select jc from gov.va.med.fw.model.batchprocess.JobConfig jc 
	    			where jc.jobName = :jobName]]>
	    	</query>
	    </named-query>
	    
	    <named-query name="jobConfigQuery_GetByJobNameAndJobGroup">
	    	<query>
	    		<![CDATA[select jc from gov.va.med.fw.model.batchprocess.JobConfig jc 
	    			where jc.jobName = :jobName and jc.jobGroup = :jobGroup]]>
	    	</query>
	    </named-query>	    
	 	    	
	    <attribute-override name="id">
	    	<column name="JOB_CONFIG_ID"/>
	    </attribute-override>
	    <attributes>
	        
			<one-to-many name="internalPermisions" target-entity="gov.va.med.fw.model.batchprocess.BatchJobPermission" mapped-by="jobConfig" fetch="EAGER">
            	<cascade><cascade-all/></cascade>
            </one-to-many>                                  	        
        </attributes>                             
    </entity>
    
    <entity class="gov.va.med.fw.model.batchprocess.BatchJobPermission" 
    		access="FIELD" 
    		name="BatchJobPermission" >
	    <table schema="HT" name="JOB_PERMISSIONS"/>
	    <attribute-override name="id">
	    	<column name="JOB_PERMISSION_ID"/>
	    </attribute-override>		    
	    <attributes>    		
           <many-to-one name="jobConfig" target-entity="gov.va.med.fw.model.batchprocess.JobConfig">
				<join-column name="JOB_CONFIG_ID"/>
			</many-to-one>
			<many-to-one name="permission" 
				target-entity="gov.va.med.fw.security.Permission">
				<join-column name="PERMISSION_ID"/>
			</many-to-one>                                
        </attributes>                             
    </entity>               
</entity-mappings>

