<?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"
[
<!ENTITY patientLite SYSTEM 		"classpath://gov/va/med/cds/persistence/hibernate/common/PatientLite.hbm">
]
>

<hibernate-mapping default-lazy="false" default-cascade="all">
    <class name="gov.va.med.cds.cdm.lab.LabTestPromise" table="LAB_TEST_PRM_V" lazy="false">
        <id type="java.lang.Long" >
            <column name="LAB_TEST_PRM_ID" />
 			<generator class="sequence">
				<param name="sequence">lab_test_prm_seq_id</param>
			</generator>
        </id>


		<!-- import from .../common/PatientLite.hbm -->
		&patientLite;

        <component name="recordIdentifier" class="gov.va.med.cds.cdm.basedatatypes.EntityIdentifier">
			<property name="identity" column="RECORDIDENTIFIER_IDENTITY" type="java.lang.String" />
			<property name="namespaceId" column="RECORDIDENTIFIER_NAMESPACEID" type="java.lang.String" />
			<property name="universalId" column="RECORDIDENTIFIER_UNIVERSALID" type="java.lang.String" />
			<property name="universalIdType" column="RECORDIDENTIFIER_UNIVERSTYP" type="java.lang.String" />
		</component>

        <component name="placerOrderIdentifier" class="gov.va.med.cds.cdm.basedatatypes.EntityIdentifier">
			<property name="identity" column="PLACERORDERID_IDENTITY" type="java.lang.String" />
			<property name="namespaceId" column="PLACERORDERID_NAMESPACEID" type="java.lang.String" />
			<property name="universalId" column="PLACERORDERID_UNIVERSALID" type="java.lang.String" />
			<property name="universalIdType" column="PLACERORDERID_UNIVERSTYP" type="java.lang.String" />
		</component>

		<property name="placerAccessionReference" column="PLACER_ACCESSION_REF" type="java.lang.String"/>

		<property name="fillerIenReference" column="FILLER_IEN_REF" type="java.lang.String"/>


		<property name="reportCompleteDate" type="gov.va.med.cds.persistence.hibernate.common.PointInTimeUserType">
			<column name="REPORT_COMPLETE_DT"/>
			<column name="REPORT_COMPLETE_DT_NBR"/>
		</property>

		<property name="labSubscript" column="LAB_SUBSCRIPT" type="java.lang.String"/>

		<property name="priority" column="LAB_PRIORITY" type="java.lang.String"/>
		<property name="status" column="LAB_STATUS" type="java.lang.String"/>

        <component name="specimenSourceCode" class="gov.va.med.cds.cdm.basedatatypes.CodedElementExtended">
			<property name="code" column="SPECIMEN_SRC_CODE" type="java.lang.String" />
			<property name="displayText" column="SPECIMEN_SRC_DSPLYTXT" type="java.lang.String" />
			<property name="codingSystem" column="SPECIMEN_SRC_CODSYS" type="java.lang.String" />
			<property name="codingSystemVersion" column="SPECIMEN_SRC_CODSYS_VER" type="java.lang.String" />
			<property name="alternateCode" column="SPECIMEN_SRC_ALT_CODE" type="java.lang.String" />
			<property name="alternateDisplayText" column="SPECIMEN_SRC_ALT_DSPLYTXT" type="java.lang.String" />
			<property name="alternateCodingSystem" column="SPECIMEN_SRC_ALT_CODSYS" type="java.lang.String" />
			<property name="alternateCodingSystemVersion" column="SPECIMEN_SRC_ALT_CODSYS_VER" type="java.lang.String" />
			<property name="originalText" column="SPECIMEN_SRC_ORIG_TXT" type="java.lang.String" />
		</component>

 		<property name="specimenTakenDate" type="gov.va.med.cds.persistence.hibernate.common.PointInTimeUserType">
			<column name="SPECIMEN_TAKEN_DT"/>
			<column name="SPECIMEN_TAKEN_DT_NBR"/>
		</property>

		<property name="specimenReceivedDate" type="gov.va.med.cds.persistence.hibernate.common.PointInTimeUserType">
			<column name="SPECIMEN_RECEIVED_DT"/>
			<column name="SPECIMEN_RECEIVED_DT_NBR"/>
		</property>

		<property name="specimenAction" column="SPECIMEN_ACTION" type="java.lang.String"/>

        <component name="recordSource" class="gov.va.med.cds.cdm.basedatatypes.FacilityIdentifier">
			<property name="namespaceId" column="RECORDSOURCE_NAMESPACEID" type="java.lang.String" />
			<property name="universalId" column="RECORDSOURCE_UNIVERSALID" type="java.lang.String" />
			<property name="universalIdType" column="RECORDSOURCE_UNIVERSTYP" type="java.lang.String" />
		</component>
		
        <property name="recordVersion" column="RECORD_VERSION" type="integer" />

		<property name="recordUpdateTime" type="gov.va.med.cds.persistence.hibernate.common.PointInTimeUserType">
			<column name="RECORD_UPDATE_TM"/>
			<column name="RECORD_UPDATE_TM_NBR"/>
		</property>

		<property name="requestId" column="REQUEST_ID" type="java.lang.String"/>

		<!--  associated class LabTestRequest -->
		<component name="labTestRequest" class="gov.va.med.cds.cdm.lab.LabTestRequest">

			<component name="author" class="gov.va.med.cds.cdm.common.PractitionerLite">

				<component name="identifier" class="gov.va.med.cds.common.person.correlation.PersonIdentifier">
					<property name="identity" column="AUTH_PERSON_IDENTITY" type="java.lang.String"/>
					<property name="assigningFacility" column="AUTH_PERSON_ASGNING_FACILITY" type="java.lang.String"/>
					<property name="assigningAuthority" column="AUTH_PERSON_ASGNING_AUTHORITY" type="java.lang.String"/>
				</component>

		        <property name="idSourceTable" type="java.lang.String">
		            <column name="AUTH_PRACTITIONER_ID_SRC_TBL"/>
		        </property>

				<component name="name" class="gov.va.med.cds.cdm.basedatatypes.PersonName">
					<property name="prefix" column="AUTH_NAME_PREFIX" type="java.lang.String"/>
					<property name="given" column="AUTH_NAME_GIVEN" type="java.lang.String"/>
					<property name="middle" column="AUTH_NAME_MIDDLE" type="java.lang.String"/>
					<property name="family" column="AUTH_NAME_FAMILY" type="java.lang.String"/>
					<property name="suffix" column="AUTH_NAME_SUFFIX" type="java.lang.String"/>
					<property name="title" column="AUTH_NAME_TITLE" type="java.lang.String"/>
				</component>

			</component>

			<component name="requestingLocationIdentifier" class="gov.va.med.cds.cdm.basedatatypes.LocationIdentifier">
				<property name="pointOfCare" column="REQUEST_LOC_POINT_OF_CARE" type="java.lang.String" />
				<property name="room" column="REQUEST_LOC_ROOM" type="java.lang.String" />
				<property name="bed" column="REQUEST_LOC_BED" type="java.lang.String" />
				<property name="facilityNamespaceId" column="REQUEST_LOC_FCLTY_NMSPCID" type="java.lang.String" />
				<property name="facilityUniversalId" column="REQUEST_LOC_FCLTY_UNVSID" type="java.lang.String" />
				<property name="facilityUniversalIdType" column="REQUEST_LOC_FCLTY_UNVSIDTYP" type="java.lang.String" />
				<property name="personLocationType" column="REQUEST_LOC_PERS_LOC_TYP" type="java.lang.String" />
				<property name="locationDescription" column="REQUEST_LOC_DESCRIPTION" type="java.lang.String" />
			</component>

	        <component name="requestingOrganizationCode" class="gov.va.med.cds.cdm.basedatatypes.CodedElementLite">
				<property name="code" column="REQUEST_ORG_CODE" type="java.lang.String" />
				<property name="displayText" column="REQUEST_ORG_DSPLYTXT" type="java.lang.String" />
				<property name="codingSystem" column="REQUEST_ORG_CODSYS" type="java.lang.String" />
			</component>

	        <component name="orderingFacilityIdentifier" class="gov.va.med.cds.cdm.basedatatypes.OrganizationIdentifierExtended">
				<property name="identity" column="ORD_FCLTY_IDENTITY" type="java.lang.String" />
				<property name="name" column="ORD_FCLTY_NAME" type="java.lang.String" />
				<property name="assigningAuthority" column="ORD_FCLTY_ASSIGNING_AUTHORITY" type="java.lang.String" />
				<property name="nameTypeCode" column="ORD_FCLTY_NAME_TYP_CODE" type="java.lang.String" />
				<property name="identityTypeCode" column="ORD_FCLTY_IDENTITY_TYP_CODE" type="java.lang.String" />
				<property name="nameRepresentation" column="ORD_FCLTY_NAME_REP" type="java.lang.String" />
				<property name="stationNumber" column="ORD_FCLTY_STATION_NUMBER" type="java.lang.String" />
			</component>

			<component name="orderingFacilityAddress" class="gov.va.med.cds.cdm.basedatatypes.UsMailingAddress">
				<property name="line1" column="ORD_FCLTY_ADDR_LINE1" type="java.lang.String"/>
				<property name="line2" column="ORD_FCLTY_ADDR_LINE2" type="java.lang.String"/>
				<property name="city" column="ORD_FCLTY_ADDR_CITY" type="java.lang.String"/>
				<property name="postalCode" column="ORD_FCLTY_ADDR_POSTAL_CODE" type="java.lang.String"/>
				<property name="country" column="ORD_FCLTY_ADDR_COUNTRY" type="java.lang.String"/>
				<property name="state" column="ORD_FCLTY_ADDR_STATE" type="java.lang.String"/>
			</component>

		</component>

		<!--  one-to-one relationship (use many to one when parent and child primary key are diferent-->
		<many-to-one name="labPatientVisit" entity-name="LabPatientVisit" column="LAB_PATIENT_VISIT_ID" unique="true"/>

		<!--  one-to-many sets -->

       
		<list name="labCommentEvents" fetch="subselect" batch-size="100">
			<key column="LAB_TEST_PRM_ID" not-null="true" update="false" />
			<list-index column="LINE_NBR"  />
			<one-to-many entity-name="LabTestComments" />
		</list>

		<list name="labTests" fetch="subselect" batch-size="100">
			<key column="LAB_TEST_PRM_ID" not-null="true" update="false" />
			<list-index column="LINE_NBR"  />
			<one-to-many class="gov.va.med.cds.cdm.lab.LabTest" />
		</list>

    </class>


    <class name="gov.va.med.cds.cdm.lab.LabCommentEvent" entity-name="LabTestComments" table="LAB_COMMENT_EVENT_V">
        <id type="java.lang.Long">
            <column name="LAB_COMMENT_EVENT_ID" />
            <generator class="sequence" >
				<param name="sequence">LAB_COMMENT_EVENT_SEQ_ID</param>
			</generator>
        </id>

        <component name="commentTypeCode" class="gov.va.med.cds.cdm.basedatatypes.CodedElementLite">
			<property name="code" column="COMMENT_TYP_CODE" type="java.lang.String" />
			<property name="displayText" column="COMMENT_TYP_DSPLYTXT" type="java.lang.String" />
			<property name="codingSystem" column="COMMENT_TYP_CODSYS" type="java.lang.String" />
		</component>

		<list name="comments" table="LAB_COMMENT_V" fetch="subselect" batch-size="100">
			<key column="LAB_COMMENT_EVENT_ID" not-null="true" update="false" />
			<list-index column="LINE_NBR"  />
			<element type="string" column="COMMENT_TXT"/>
        </list>

    </class>

    <!-- continued from above, attempting to map the one-to-one relationship from labTestPromise to labPatientVisit -->
    <class name="gov.va.med.cds.cdm.lab.LabPatientVisit" entity-name="LabPatientVisit" table="LAB_PATIENT_VISIT_V">
        <id type="java.lang.Long">
            <column name="LAB_PATIENT_VISIT_ID" />
            <generator class="sequence" >
				<param name="sequence">LAB_PATIENT_VISIT_SEQ_ID</param>
			</generator>
        </id>

		<property name="patientClass" column="PATIENT_CLASS" type="java.lang.String"/>
		<property name="hospitalService" column="HOSPITAL_SERVICE" type="java.lang.String"/>
		<property name="patientType" column="PATIENT_TYP" type="java.lang.String"/>
		<property name="chargePriceIndicator" column="CHARGE_PRICE_IND" type="java.lang.String"/>
		<property name="dischargeDisposition" column="DISCHARGE_DISPOSITION" type="java.lang.String"/>
		<property name="serviceFacility" column="SERVICE_FACILITY" type="java.lang.String"/>
		<property name="alternateVisitId" column="ALTERNATE_VISIT_ID" type="java.lang.String"/>

		<component name="assignedLocationIdentifier" class="gov.va.med.cds.cdm.basedatatypes.LocationIdentifier">
			<property name="pointOfCare" column="ASSIGN_LOC_POINT_OF_CARE" type="java.lang.String" />
			<property name="room" column="ASSIGN_LOC_ROOM" type="java.lang.String" />
			<property name="bed" column="ASSIGN_LOC_BED" type="java.lang.String" />
			<property name="facilityNamespaceId" column="ASSIGN_LOC_FCLTY_NMSPCID" type="java.lang.String" />
			<property name="facilityUniversalId" column="ASSIGN_LOC_FCLTY_UNVSID" type="java.lang.String" />
			<property name="facilityUniversalIdType" column="ASSIGN_LOC_FCLTY_UNVSIDTYP" type="java.lang.String" />
			<property name="personLocationType" column="ASSIGN_LOC_PERS_LOC_TYP" type="java.lang.String" />
			<property name="locationDescription" column="ASSIGN_LOC_DESCRIPTION" type="java.lang.String" />
		</component>

		<component name="priorLocationIdentifier" class="gov.va.med.cds.cdm.basedatatypes.LocationIdentifier">
			<property name="pointOfCare" column="PRIOR_LOC_POINT_OF_CARE" type="java.lang.String" />
			<property name="room" column="PRIOR_LOC_ROOM" type="java.lang.String" />
			<property name="bed" column="PRIOR_LOC_BED" type="java.lang.String" />
			<property name="facilityNamespaceId" column="PRIOR_LOC_FCLTY_NMSPCID" type="java.lang.String" />
			<property name="facilityUniversalId" column="PRIOR_LOC_FCLTY_UNVSID" type="java.lang.String" />
			<property name="facilityUniversalIdType" column="PRIOR_LOC_FCLTY_UNVSIDTYP" type="java.lang.String" />
			<property name="personLocationType" column="PRIOR_LOC_PERS_LOC_TYP" type="java.lang.String" />
			<property name="locationDescription" column="PRIOR_LOC_DESCRIPTION" type="java.lang.String" />
		</component>

		<component name="attendingDoctor" class="gov.va.med.cds.cdm.common.PractitionerLite">

			<component name="identifier" class="gov.va.med.cds.common.person.correlation.PersonIdentifier">
				<property name="identity" column="AUTH_PERSON_IDENTITY" type="java.lang.String"/>
				<property name="assigningFacility" column="AUTH_PERSON_ASGNING_FACILITY" type="java.lang.String"/>
				<property name="assigningAuthority" column="AUTH_PERSON_ASGNING_AUTHORITY" type="java.lang.String"/>
			</component>

			<property name="idSourceTable" type="java.lang.String">
				<column name="AUTH_PRACTITIONER_ID_SRC_TBL"/>
			</property>

			<component name="name" class="gov.va.med.cds.cdm.basedatatypes.PersonName">
				<property name="prefix" column="AUTH_NAME_PREFIX" type="java.lang.String"/>
				<property name="given" column="AUTH_NAME_GIVEN" type="java.lang.String"/>
				<property name="middle" column="AUTH_NAME_MIDDLE" type="java.lang.String"/>
				<property name="family" column="AUTH_NAME_FAMILY" type="java.lang.String"/>
				<property name="suffix" column="AUTH_NAME_SUFFIX" type="java.lang.String"/>
				<property name="title" column="AUTH_NAME_TITLE" type="java.lang.String"/>
			</component>

		</component>

		<property name="admissionDate" type="gov.va.med.cds.persistence.hibernate.common.PointInTimeUserType">
			<column name="ADMISSION_DT"/>
			<column name="ADMISSION_DT_NBR"/>
		</property>

    </class>

    <query name="LabTestPromise.patient">
		<![CDATA[
		from LabTestPromise ltp
		left join fetch ltp.labCommentEvents
		left join fetch ltp.labTests
		where ltp.patient.identifier.assigningFacility || '/' || ltp.patient.identifier.identity in ( :pids )
      ]]>
	</query>

    <query name="LabTestPromise.patient.date">
		<![CDATA[
		from LabTestPromise ltp
		left join fetch ltp.labCommentEvents
		left join fetch ltp.labTests
        where ltp.patient.identifier.assigningFacility || '/' || ltp.patient.identifier.identity in ( :pids )
		and ( ltp.specimenTakenDate.numeric between :startDate and :endDate or ltp.specimenTakenDate.numeric is null )
      ]]>
	</query>

    <query name="LabTestPromise.patient.identifiers">
		<![CDATA[
		from LabTestPromise ltp
		left join fetch ltp.labCommentEvents
		left join fetch ltp.labTests
		where ltp.patient.identifier.assigningFacility || '/' || ltp.patient.identifier.identity in ( :pids ) and
		ltp.recordIdentifier.identity || '/' || ltp.recordIdentifier.namespaceId || '/' ||
		ltp.recordIdentifier.universalId || '/' || ltp.recordIdentifier.universalIdType in ( :recordIdentifiers )
      ]]>
	</query>

</hibernate-mapping>
