<?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 entity-name="clinicalDocumentEvents" table="CLINICAL_DOCUMENT_V" lazy="false" node="clinicalDocumentEvents">
        <id name="id" type="java.lang.Long" >
            <column name="CLINICAL_DOCUMENT_ID" />
 			<generator class="sequence">
				<param name="sequence">clinical_document_seq_id</param>
			</generator>
        </id>

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

        <component name="recordIdentifier" node="recordIdentifier">
            <property name="identity" column="RECORDIDENTIFIER_IDENTITY" type="java.lang.String" node="identity" />
            <property name="namespaceId" column="RECORDIDENTIFIER_NAMESPACEID" type="java.lang.String" node="namespaceId" />
            <property name="universalId" column="RECORDIDENTIFIER_UNIVERSALID" type="java.lang.String" node="universalId"/>
            <property name="universalIdType" column="RECORDIDENTIFIER_UNIVERSTYP" type="java.lang.String" node="universalIdType" />
        </component>
  
        <component name="referenceDate" node="referenceDate" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="REFERENCE_DT" />
            <column name="REFERENCE_DT_NBR"/>
          </property>
        </component>
       
       <component name="parentIdentifier" node="parentIdentifier">
            <property name="identity" column="PARENTIDENTIFIER_IDENTITY" type="java.lang.String" node="identity"/>
            <property name="namespaceId" column="PARENTIDENTIFIER_NAMESPACEID" type="java.lang.String" node="namespaceId"/>
            <property name="universalId" column="PARENTIDENTIFIER_UNIVERSALID" type="java.lang.String" node="universalId"/>
            <property name="universalIdType" column="PARENTIDENTIFIER_UNIVERSTYP" type="java.lang.String" node="universalIdType"/>
        </component>
        
        <component name="standardizedDocumentTitleCode" node="standardizedDocumentTitle">
            <property name="code" column="STANDARD_DOC_TITLE_CODE" type="java.lang.String" node="code"/>
            <property name="displayText" column="STANDARD_DOC_TITLE_DSPLYTXT" type="java.lang.String" node="displayText"/>
            <property name="codingSystem" column="STANDARD_DOC_TITLE_CODSYS" type="java.lang.String" node="codingSystem"/>
        </component>

        <property name="documentTitle" column="DOCUMENT_TITLE" type="java.lang.String" node="documentTitle"/>

        <property name="documentSubject" column="DOCUMENT_SUBJECT" type="java.lang.String" node="documentSubject"/>

        <property name="documentContent" column="DOCUMENT_CONTENT" type="java.lang.String" node="documentContent"/>
    
        <component name="statusCode" node="statusCode">
            <property name="code" column="STATUS_CODE" type="java.lang.String" node="code"/>
            <property name="displayText" column="STATUS_DSPLYTXT" type="java.lang.String" node="displayText" />
            <property name="codingSystem" column="STATUS_CODSYS" type="java.lang.String" node="codingSystem"/>
        </component>

        <component name="enteredDate" node="enteredDate" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="ENTERED_DT" />
            <column name="ENTERED_DT_NBR"/>
          </property>
        </component>
        
        <component name="amendmentDate" node="amendmentDate" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="AMENDMENT_DT" />
            <column name="AMENDMENT_DT_NBR"/>
          </property>
        </component>

        <component name="dictationDate" node="dictationDate" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="DICTATION_DT" />
            <column name="DICTATION_DT_NBR"/>
          </property>
        </component>
        
        <component name="patientMovementDate" node="patientMovementDate" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="PATIENT_MVMNT_DT" />
            <column name="PATIENT_MVMNT_DT_NBR"/>
          </property>
        </component>
                
        <component name="archiveDate" node="archiveDate" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="ARCHIVE_DT" />
            <column name="ARCHIVE_DT_NBR"/>
          </property>
        </component>
                
        <component name="procedureDate" node="procedureDate" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="PROCEDURE_DT" />
            <column name="PROCEDURE_DT_NBR"/>
          </property>
        </component>
                
       <component name="procedureSummaryCode" node="procedureSummaryCode">
            <property name="code" column="PROCEDURE_SUMMARY_CODE" type="java.lang.String" node="code"/>
            <property name="displayText" column="PROCEDURE_SUMMARY_DSPLYTXT" type="java.lang.String" node="displayText"/>
            <property name="codingSystem" column="PROCEDURE_SUMMARY_CODSYS" type="java.lang.String" node="codingSystem"/>
        </component>

        <component name="treatingSpecialtyCode" node="treatingSpecialtyCode">
            <property name="code" column="TREATING_SPCLTY_CODE" type="java.lang.String" node="code"/>
            <property name="displayText" column="TREATING_SPCLTY_DSPLYTXT" type="java.lang.String" node="displayText"/>
            <property name="codingSystem" column="TREATING_SPCLTY_CODSYS" type="java.lang.String" node="codingSystem"/>
        </component>

        <component name="serviceCode" node="serviceCode">
            <property name="code" column="SERVICE_CODE" type="java.lang.String" node="code"/>
            <property name="displayText" column="SERVICE_DSPLYTXT" type="java.lang.String" node="displayText"/>
            <property name="codingSystem" column="SERVICE_CODSYS" type="java.lang.String" node="codingSystem"/>
        </component>

        <component name="cosignatureNeededCode" node="cosignatureNeeded">
            <property name="code" column="COSIGN_NEEDED_CODE" type="java.lang.String" node="code"/>
            <property name="displayText" column="COSIGN_NEEDED_DSPLYTXT" type="java.lang.String" node="displayText"/>
            <property name="codingSystem" column="COSIGN_NEEDED_CODSYS" type="java.lang.String" node="codingSystem"/>
        </component>

        <component name="treatmentEpisode" node="treatmentEpisode">
        
          <component name="episodeBeginDate" node="episodeBeginDate" >
            <property name="episodeBeginDate" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
                <column name="TRMNT_EPISODE_BEGIN_DT" />
                <column name="TRMNT_EPISODE_BEGIN_DT_NBR"/>
            </property>
          </component>
           
          <component name="episodeEndDate" node="episodeEndDate" >
            <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
                <column name="TRMNT_EPISODE_END_DT" />
                <column name="TRMNT_EPISODE_END_DT_NBR"/>
            </property>
          </component>

        </component>

        <component name="retraction" node="retraction">
            <many-to-one name="retractor" entity-name="TiuSinglePractitionerLite" node="retractor">
                <column name="RETRACTOR_TPP_ID" />
            </many-to-one>
            
            <component name="reasonCode" node="reasonCode">
                <property name="code" column="RETRACTION_REASON_CODE" type="java.lang.String" node="code"/>
                <property name="displayText" column="RETRACTION_REASON_DSPLYTXT" type="java.lang.String" node="displayText"/>
                <property name="codingSystem" column="RETRACTION_REASON_CODSYS" type="java.lang.String" node="codingSystem"/>
            </component>
    
            <component name="retractionDate" node="retractionDate" >
              <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
                <column name="RETRACTION_DT" />
                <column name="RETRACTION_DT_NBR"/>
              </property>
            </component>
                    
            <component name="retractedOriginalIdentifier" node="retractedOriginal">
                <property name="identity" column="RTCRORGIDENTIFIER_IDENTITY" type="java.lang.String" node="identity"/>
                <property name="namespaceId" column="RTCRORGIDENTIFIER_NAMESPACEID" type="java.lang.String" node="namespaceId"/>
                <property name="universalId" column="RTCRORGIDENTIFIER_UNIVERSALID" type="java.lang.String" node="universalId"/>
                <property name="universalIdType" column="RTCRORGIDENTIFIER_UNIVERSTYP" type="java.lang.String" node="universalIdType"/>
            </component>
        </component>

        <property name="recordVersion" column="RECORD_VERSION" type="integer" node="recordVersion"/>

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

        <component name="recordUpdateTime" node="recordUpdateTime" >
          <property name="literal" type="gov.va.med.cds.persistence.hibernate.common.Dom4jPointInTimeUserType" node="literal">
            <column name="RECORD_UPDATE_TM" />
            <column name="RECORD_UPDATE_TM_NBR"/>
          </property>
        </component>
        
        <many-to-one name="patientVisit" entity-name="patientVisit" node="patientVisit">
            <column name="TIU_PATIENT_VISIT_ID" />
        </many-to-one>

        <list name="documentText" table="TIU_TEXT_V" node="." fetch="subselect" batch-size="100">
            <key>
                <column name="CLINICAL_DOCUMENT_ID" />
            </key>
            <list-index base="1" column="LINE_NBR" ></list-index>
            <element column="DOCUMENT_TXT" type="java.lang.String" node="documentText"/>
        </list>

        <many-to-one name="dataEnterer" entity-name="TiuSinglePractitionerLite" node="dataEnterer">
            <column name="DATAENTERER_TPP_ID" />
        </many-to-one>
        
        <many-to-one name="author" entity-name="TiuSinglePractitionerLite" node="author">
            <column name="AUTHOR_TPP_ID" />
        </many-to-one>

        <many-to-one name="chartMarkedSigned" entity-name="TiuSinglePractitionerLite" node="chartMarkedSigned">
            <column name="CHARTMARKEDSIGNED_TPP_ID" />
        </many-to-one>

        <many-to-one name="chartMarkedCosigned" entity-name="TiuSinglePractitionerLite" node="chartMarkedCosigned">
            <column name="CHARTMARKEDCOSIGNED_TPP_ID" />
        </many-to-one>

        <many-to-one name="expectedSigner" entity-name="TiuSinglePractitionerLite" node="expectedSigner" >
            <column name="EXPECTEDSIGNER_TPP_ID" />
        </many-to-one>

        <many-to-one name="expectedCosigner" entity-name="TiuSinglePractitionerLite" node="expectedCosigner">
            <column name="EXPECTEDCOSIGNER_TPP_ID" />
        </many-to-one>

        <many-to-one name="signer" entity-name="TiuSingleManagedPractitionerParticipationLite" node="signer">
            <column name="SIGNER_TPP_ID" />
        </many-to-one>

        <many-to-one name="cosigner" entity-name="TiuSingleManagedPractitionerParticipationLite" node="cosigner">
            <column name="COSIGNER_TPP_ID" />
        </many-to-one>

        <many-to-one name="adminCloser" entity-name="TiuSingleManagedPractitionerParticipationLite" node="adminCloser">
            <column name="ADMINCLOSER_TPP_ID" />
        </many-to-one>

        <many-to-one name="amender" entity-name="TiuSingleManagedPractitionerParticipationLite" node="amender">
            <column name="AMENDER_TPP_ID" />
        </many-to-one>

		<!--  one-to-many sets -->
		<set name="otherExpectedCosigners" where="discriminator='OEC'" fetch="subselect" batch-size="100">
			<key column="CLINICAL_DOCUMENT_ID" not-null="true" update="false" />
			<one-to-many entity-name="otherExpectedCosigners" />
		</set>

		<set name="otherCosigners" where="discriminator='OCS'" fetch="subselect" batch-size="100">
			<key column="CLINICAL_DOCUMENT_ID" not-null="true" update="false" />
			<one-to-many entity-name="otherCosigners" />
		</set>

		<set name="images" cascade="all" fetch="subselect" batch-size="100">
			<key column="CLINICAL_DOCUMENT_ID" not-null="true" update="false" />
			<one-to-many entity-name="images" />
		</set>

		<set name="documentUpdateEvents" cascade="all" fetch="subselect" batch-size="100">
			<key column="CLINICAL_DOCUMENT_ID" not-null="true" update="false" />
			<one-to-many entity-name="documentUpdateEvents" />
		</set>
    </class>

 
    <!-- Association otherExpectedCosigners Queries -->
    <query name="ClinicalDocumentEvent.otherExpectedCosigners">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.otherExpectedCosigners as asn
         where cd.id = asn.clinicalDocumentId
         and asn.discriminator = 'OEC'
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.otherExpectedCosigners.date">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.otherExpectedCosigners as asn
         where cd.id = asn.clinicalDocumentId
         and asn.discriminator = 'OEC'
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and ( cd.referenceDate.literal.numeric between :startDate and :endDate or cd.referenceDate.literal.numeric is null )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.otherExpectedCosigners.identifiers">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.otherExpectedCosigners as asn
         where cd.id = asn.clinicalDocumentId
         and asn.discriminator = 'OEC'
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and cd.recordIdentifier.identity || '/' || cd.recordIdentifier.namespaceId || '/' ||
         cd.recordIdentifier.universalId || '/' || cd.recordIdentifier.universalIdType in ( :recordIdentifiers )
      ]]>
    </query>
   
    
    <!-- Association otherCosigners Queries -->
    <query name="ClinicalDocumentEvent.otherCosigners">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.otherCosigners as asn
         where cd.id = asn.clinicalDocumentId
         and asn.discriminator = 'OCS'
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.otherCosigners.date">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.otherCosigners as asn
         where cd.id = asn.clinicalDocumentId
         and asn.discriminator = 'OCS'
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and ( cd.referenceDate.literal.numeric between :startDate and :endDate or cd.referenceDate.literal.numeric is null )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.otherCosigners.identifiers">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.otherCosigners as asn
         where cd.id = asn.clinicalDocumentId
         and asn.discriminator = 'OCS'
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and cd.recordIdentifier.identity || '/' || cd.recordIdentifier.namespaceId || '/' ||
         cd.recordIdentifier.universalId || '/' || cd.recordIdentifier.universalIdType in ( :recordIdentifiers )
      ]]>
    </query>
    
    
    <!-- Association images Queries -->
    <query name="ClinicalDocumentEvent.images">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.images as asn
         where cd.id = asn.clinicalDocumentId
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.images.date">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.images as asn
         where cd.id = asn.clinicalDocumentId
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and ( cd.referenceDate.literal.numeric between :startDate and :endDate or cd.referenceDate.literal.numeric is null )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.images.identifiers">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.images as asn
         where cd.id = asn.clinicalDocumentId
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and cd.recordIdentifier.identity || '/' || cd.recordIdentifier.namespaceId || '/' ||
         cd.recordIdentifier.universalId || '/' || cd.recordIdentifier.universalIdType in ( :recordIdentifiers )
      ]]>
    </query>
    
    
    <!-- Association documentUpdateEvents Queries -->
    <query name="ClinicalDocumentEvent.documentUpdateEvents">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.documentUpdateEvents as asn
         where cd.id = asn.clinicalDocumentId
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.documentUpdateEvents.date">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.documentUpdateEvents as asn
         where cd.id = asn.clinicalDocumentId
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and ( cd.referenceDate.literal.numeric between :startDate and :endDate or cd.referenceDate.literal.numeric is null )
      ]]>
    </query>
    <query name="ClinicalDocumentEvent.documentUpdateEvents.identifiers">
        <![CDATA[
         select asn
         from clinicalDocumentEvents cd
         join cd.documentUpdateEvents as asn
         where cd.id = asn.clinicalDocumentId
         and cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
         and cd.recordIdentifier.identity || '/' || cd.recordIdentifier.namespaceId || '/' ||
         cd.recordIdentifier.universalId || '/' || cd.recordIdentifier.universalIdType in ( :recordIdentifiers )
      ]]>
    </query>


<!--
 -->
    <query name="ClinicalDocumentEvent.patient">
		<![CDATA[
		from clinicalDocumentEvents cd
		where cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
      ]]>
	</query>

    <query name="ClinicalDocumentEvent.patient.date">
		<![CDATA[
		from clinicalDocumentEvents cd
		where cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
		and ( cd.referenceDate.literal.numeric between :startDate and :endDate or cd.referenceDate.literal.numeric is null )
      ]]>
	</query>

    <query name="ClinicalDocumentEvent.patient.identifiers">
		<![CDATA[
		from clinicalDocumentEvents cd
		where cd.patient.identifier.assigningFacility || '/' || cd.patient.identifier.identity in ( :pids )
		and cd.recordIdentifier.identity || '/' || cd.recordIdentifier.namespaceId || '/' ||
		cd.recordIdentifier.universalId || '/' || cd.recordIdentifier.universalIdType in ( :recordIdentifiers )
      ]]>
	</query>
  
</hibernate-mapping>
