<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
[
]
>

<hibernate-mapping default-cascade="all">
    <class entity-name="censusMasterIngest" name="gov.va.med.cds.scheduler.persistence.CensusMasterIngest" table="CENSUS_MASTER_INGEST" lazy="false"  >

        <id name="censusMasterId" type="java.lang.Long">
            <column name="CENSUS_MASTER_ID" />
            <generator class="assigned" />
        </id>
        
		<property name="reportStartDt" column="REPORT_START_DT" type="date" />
		<property name="reportEndDt" column="REPORT_END_DT" type="date" />
		<property name="vendorName" column="VENDOR_NAME" type="java.lang.String" />
		<property name="vendorFacility" column="VENDOR_FACILITY" type="java.lang.String" />
		<property name="segmentsExpectedCount" column="SEGMENTS_EXPECTED_CNT" type="java.lang.Long" />
		<property name="segmentsReceivedCount" column="SEGMENTS_RECEIVED_CNT" type="java.lang.Long" />
		<property name="segmentDateTime" column="SEGMENT_DATETIME" type="java.lang.String" />
		<property name="ingestStatus" column="INGEST_STATUS" type="java.lang.String" />
		<property name="ingestStatusDt" column="INGEST_STATUS_DT" type="date" />
		<property name="transformStatus" column="TRANSFORM_STATUS" type="java.lang.String" />
		<property name="transformStatusDt" column="TRANSFORM_STATUS_DT" type="date" />
		<property name="messageInsertionTime" column="MESSAGE_INSERTION_TIME" type="timestamp" />
		<property name="senderAddress" column="SENDER_ADDRESS" type="java.lang.String" />
		<property name="ingestExpireTime" column="INGEST_EXPIRE_TIME" type="timestamp" />
		<property name="ingestExpiredStatusTime" column="INGEST_EXPIRED_STATUS_TIME" type="timestamp" />
  		<property name="ackStatus" column="ACK_STATUS" type="java.lang.String" />
  		<property name="ackStatusDt" column="ACK_STATUS_DT" type="timestamp" />
		<property name="requestId" column="REQUEST_ID" type="java.lang.String" />
		<property name="sendingApplication" column="SENDING_APPLICATION" type="java.lang.String" />
		<property name="stationNumber" column="STATION_NO" type="java.lang.String" />
		<property name="sendingFacility" column="SENDING_FACILITY" type="java.lang.String" />
		<property name="receivingApplication" column="RECEIVING_APPLICATION" type="java.lang.String" />
		<property name="receivingFacility" column="RECEIVING_FACILITY" type="java.lang.String" />
    </class>
    
	<query name="censusMasterIngest.list.expiredReports">
		<![CDATA[ 
			from censusMasterIngest c 
			where (c.ingestStatus = 'PENDING'
			or (c.ingestStatus = 'EXPIRED' 
				and (NVL(c.ackStatus,'X') IN ('NEW','READY') 
					OR (NVL(c.ackStatus,'X') = 'INPROGRESS'
                 		AND NVL(c.ackStatusDt,SYSDATE) < SYSDATE -(1/24))
                	)))
			and c.sendingFacility IS NOT NULL
			and c.ingestExpireTime < sysdate ]]>
	</query>
	
	<query name="censusMasterIngest.update.ingestStatus">
		<![CDATA[ 
			update censusMasterIngest c 
			set c.ingestStatus = 'EXPIRED', c.ingestStatusDt = SYSDATE, c.ackStatus = 'INPROGRESS', c.ackStatusDt = SYSDATE
			where c.censusMasterId = :censusMasterId ]]>
	</query>
	
	<query name="censusMasterIngest.update.ackStatus">
		<![CDATA[ 
			update censusMasterIngest c 
			set c.ackStatus = :ackStatus, c.ackStatusDt = SYSDATE
			where c.censusMasterId = :censusMasterId ]]>
	</query>
	
</hibernate-mapping>
