<?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-lazy="false" default-cascade="all">

	<class entity-name="labTestPromises" table="VISTA_HDSI.LAB_X_CHEM_HEM_TOX_63_04_EXT" lazy="false" node="labTestPromises">

		<id name="id" type="java.lang.String">
			<column name="ROWID" />
			<generator class="assigned" />
		</id>

		<property name="labData" column="LAB_DATA" type="big_decimal" node="labData" />

		<component name="recordIdentifier" node="recordIdentifier">
			<property name="identity" type="java.lang.String" insert="false" update="false" node="identity">
				<formula>
					(CASE
					WHEN (UID IS NOT NULL) THEN UID
					ELSE ACCESSION
					END)
				</formula>
			</property>
			<property name="namespaceId" formula="'LR'" type="java.lang.String" node="namespaceId" />
			<property name="universalId" formula="VISTA_HDSI.HDSREPSQLUtil1_KSPDomainName()" type="java.lang.String" node="universalId" />
			<property name="universalIdType" formula="'DNS'" type="java.lang.String" node="universalIdType" />
		</component>

		<property name="reportCompleteDate" formula="STRING(DATE_REPORT_COMPLETED,'^',(VISTA_HDSI.HDSREPSQLUtil1_StationNumber()))" type="gov.va.med.cds.persistence.hibernate.common.VistaPointInTimeUserType"
			node="reportCompleteDate//literal" />

		<property name="labSubscript" formula="'CH'" type="java.lang.String" node="labSubscript" />

		<component name="labTestRequest" node="labTestRequest">
			<component name="author" node="author">
				<component name="identifier" node="identifier">
					<property name="identity" column="REQUESTING_PERSON" type="big_decimal" node="identity" />
					<property name="assigningFacility" type="java.lang.String" node="assigningFacility">
						<formula>
							(CASE
							WHEN (REQUESTING_PERSON IS NOT NULL) THEN VISTA_HDSI.HDSREPSQLUtil1_StationNumber()
							ELSE NULL
							END)
						</formula>
					</property>
					<property name="assigningAuthority" type="java.lang.String" node="assigningAuthority">
						<formula>
							(CASE
							WHEN (REQUESTING_PERSON IS NOT NULL) THEN 'USVHA'
							ELSE NULL
							END)
						</formula>
					</property>
				</component>
				<property name="idSourceTable" type="java.lang.String" node="idSourceTable">
					<formula>
						(CASE
						WHEN (REQUESTING_PERSON IS NOT NULL) THEN 'VISTA200'
						ELSE NULL
						END)
					</formula>
				</property>

			</component>

			<property name="name" type="gov.va.med.cds.persistence.hibernate.common.VistaPersonNameUserType" node="author//name">
				<formula>
					(CASE
					WHEN (REQUESTING_PERSON IS NOT NULL) THEN VISTA_HDSI.HDSREPSQLUtil1_MiscNameNE(REQUESTING_PERSON)
					ELSE NULL
					END)
				</formula>
			</property>

			<component name="orderingFacilityIdentifier" node="orderingFacilityIdentifier">
				<property name="identity" type="java.lang.String" node="identity">
					<formula>
						(CASE
						WHEN (ORDERING_SITE IS NULL) THEN RequestingOrganizationCodeExt
						ELSE ORDERING_SITE
						END)
					</formula>
				</property>
				<property name="name" type="java.lang.String" node="name">
					<formula>
						(CASE
						WHEN (OrderingFacilityNameExt IS NULL) THEN RequestingOrganizationTextExt
						ELSE OrderingFacilityNameExt
						END)
					</formula>
				</property>
				<property name="assigningAuthority" type="java.lang.String" node="assigningAuthority">
					<formula>
						(CASE
						WHEN (ORDERING_SITE IS NOT NULL OR RequestingOrganizationCodeExt IS NOT NULL) THEN 'USVHA'
						ELSE NULL
						END)
					</formula>
				</property>
				<property name="nameTypeCode" type="java.lang.String" node="nameTypeCode">
					<formula>
						(CASE
						WHEN (ORDERING_SITE IS NOT NULL OR RequestingOrganizationCodeExt IS NOT NULL) THEN 'L'
						ELSE NULL
						END)
					</formula>
				</property>
				<property name="identityTypeCode" type="java.lang.String" node="identityTypeCode">
					<formula>
						(CASE
						WHEN (ORDERING_SITE IS NOT NULL OR RequestingOrganizationCodeExt IS NOT NULL) THEN 'FI'
						ELSE NULL
						END)
					</formula>
				</property>
				<property name="nameRepresentation" type="java.lang.String" node="nameRepresentation">
					<formula>
						(CASE
						WHEN (ORDERING_SITE IS NOT NULL) THEN NameRepresentationExt
						ELSE RequestingOrganizationTextExt
						END)
					</formula>
				</property>
				<property name="stationNumber" type="java.lang.String" node="stationNumber">
					<formula>
						(CASE
						WHEN (ORDERING_SITE IS NOT NULL) THEN StationNumberExt
						ELSE RequestingOrganizationCodeExt
						END)
					</formula>
				</property>
			</component>
		</component>

		<component name="specimen" node="specimen">
			<component name="specimenSource" node="specimenSource">
				<property name="code" column="SPECIMENSOURCECDEXT" type="java.lang.String" node="code" />
				<property name="displayText" column="SpecimenLediHL7Ext"  type="java.lang.String" node="displayText"/>
				<property name="codingSystem" type="java.lang.String" node="codingSystem">
					<formula>
						(CASE
						WHEN (SPECIMENSOURCECDEXT IS NOT NULL OR SPECIMENTYPEX IS NOT NULL) THEN VISTA_HDSI.HDSREPSQLUtil1_StationNumber()||'_61'
						ELSE NULL
						END)
					</formula>
				</property>
				<property name="alternateCode" type="java.lang.String" node="alternateCode">
					<formula>
						(CASE
						WHEN (SpecimenSourceCodeExt IS NOT NULL) THEN STRING('T-',SpecimenSourceCodeExt)
						ELSE NULL
						END)
					</formula>
				</property>
				<property name="alternateDisplayText" column="SPECIMENTYPEX" insert="false" update="false" type="java.lang.String" node="alternateDisplayText" />
				<property name="alternateCodingSystem" formula="'SNM'" type="java.lang.String" node="alternateCodingSystem">
					<formula>
						(CASE
						WHEN (SpecimenSourceCodeExt IS NOT NULL OR SPECIMENTYPEX IS NOT NULL) THEN 'SNM'
						ELSE NULL
						END)
					</formula>
				</property>
				<property name="alternateCodingSystemVersion" formula="'1974'" type="java.lang.String" node="alternateCodingSystemVersion" />
				<property name="originalText" column="SPECIMENTYPEX" type="java.lang.String" node="originalText" insert="false" update="false" />
			</component>

			<property name="specimenTakenDate" type="gov.va.med.cds.persistence.hibernate.common.VistaPointInTimeUserTypeQuery" insert="false"
				update="false" node="specimenTakenDate//literal">
				<column name="DATETIME_SPECIMEN_TAKEN" />
				<formula>VISTA_HDSI.HDSREPSQLUtil1_StationNumber()</formula>
			</property>

		</component>

		<property name="labTests" type="gov.va.med.cds.persistence.hibernate.common.VistaMHVLabTestsUserType" node="labTests">
			<column name="ConcatenatedResultsExt" />
			<column name="ConcatenatedResultsManualExt" />
			<formula>VISTA_HDSI.HDSREPSQLUtil1_StationNumber()</formula>
			<column name="DATETIME_SPECIMEN_TAKEN" />
			<column name="VERIFY_PERSON" />
			<column name="AccessioningInstitutionID" />
			<column name="AccessioningInstitution" />
			<column name="SPECIMEN_TYPE" />
		</property>

		<property name="concatenatedResultsExt" column="ConcatenatedResultsExt" type="java.lang.String" insert="false" update="false" />

		<component name="recordSource" node="recordSource">
			<property name="namespaceId" formula="VISTA_HDSI.HDSREPSQLUtil1_StationNumber()" type="java.lang.String" node="namespaceId" />
			<property name="universalId" formula="VISTA_HDSI.HDSREPSQLUtil1_KSPDomainName()" type="java.lang.String" node="universalId" />
			<property name="universalIdType" formula="'DNS'" type="java.lang.String" node="universalIdType" />
		</component>

		<set name="comments" table="VISTA_HDSI.LAB_X_CHEM_HEMX_COMMENT_63_041" order-by="ChildSub2" fetch="subselect" batch-size="100" node="labCommentEvents">
			<key column="SUB_CHEM_HEM_TOX_RIA_SER_etc_" />
			<element type="java.lang.String" node="comments">
				<formula>
					(CASE
					WHEN (COMMENT IS NOT NULL) THEN COMMENT
					ELSE ''
					END)
				</formula>
			</element>
		</set>

	</class>

	<class entity-name="patient" table="VISTA_HDSI.PATIENT_2" lazy="false" node="patient">
		<id name="id" type="big_decimal">
			<column name="ROWID" />
			<generator class="assigned" />
		</id>

		<property name="laboratoryReference" column="LABORATORY_REFERENCE" type="big_decimal" node="laboratoryReference" />

		<component name="identifier" node="identifier">
			<property name="identity" formula="RowId" type="big_decimal" insert="false" update="false" node="identity" />
			<property name="assigningFacility" formula="VISTA_HDSI.HDSREPSQLUtil1_StationNumber()" type="java.lang.String" insert="false"
				update="false" node="assigningFacility" />
			<property name="assigningAuthority" formula="'USVHA'" type="java.lang.String" node="assigningAuthority" />
		</component>
		<property name="patientName" formula="VISTA_HDSI.HDSREPSQLUtil1_PatientNE(RowId)" type="gov.va.med.cds.persistence.hibernate.common.VistaPersonNameUserType"
			node="name" />
	</class>


	<!-- Patient Query -->
	<query name="MHVLabRead40011.LabTestPromise.labTestPromises">
		 <![CDATA[ 
		 	select lab 
		 	from  labTestPromises lab 
		 	where lab.labData IN (select laboratoryReference from patient where id in (:pids) )
		 	and lab.concatenatedResultsExt is not null
		 ]]>

	</query>

	<!-- Patient Query with Dates -->
	<query name="MHVLabRead40011.LabTestPromise.labTestPromises.date">
		<![CDATA[ 
		 	select lab 
		 	from  labTestPromises lab 
		 	where lab.labData IN (select laboratoryReference from patient where id in (:pids) )
		 	and lab.concatenatedResultsExt is not null
		 	and ((lab.specimen.specimenTakenDate.timestamp between :startDate and :endDate) or lab.specimen.specimenTakenDate.timestamp is null) 
		]]>
	</query>

	<query name="MHVLabRead40011.LabTestPromise.labTestPromisesPatient"> 
		 <![CDATA[ 
		 	from patient patient 
		 	where patient.id in (:pids)
	 	 ]]>
	</query>

	<query name="MHVLabRead40011.LabTestPromise.labTestPromisesPatient.date"> 
	 	<![CDATA[ 
	 		from patient patient 
	 		where patient.id in (:pids) 
	 		and patient.id not between :startDate and :endDate
	 	]]>
	</query>

</hibernate-mapping>
