<?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>DMP</description>
	
	<named-native-query name="findDmpFacilitators" result-set-mapping="users">
	     <query><![CDATA[
				select distinct au.APP_USER_ID AS [USER_ID], 
				       USER_NAME AS [USER_NAME], 
				       LAST_NAME AS [LAST_NAME], 
				       FIRST_NAME AS [FIRST_NAME]
				from ht.user_role ur, ht.app_user au, dmp.DMP dmp
				where ur.role_id = (select role_id from ht.role where role_name = 'DMP Administrator')
				and ur.app_user_id = au.app_user_id
				and dmp.record_created_by = au.USER_NAME
				order by LAST_NAME        
	     		]]>
	     </query>	
	</named-native-query>
	<named-native-query name="findAllUsers" result-set-mapping="users">
	     <query><![CDATA[
				select distinct au.APP_USER_ID AS [USER_ID], 
				USER_NAME AS [USER_NAME], 
				LAST_NAME AS [LAST_NAME], 
				FIRST_NAME AS [FIRST_NAME]
				from  ht.app_user au
				order by LAST_NAME	     
	     		]]>
	     </query>	
	</named-native-query>
					
	<sql-result-set-mapping name="users" >
		<column-result name="USER_ID"/>
		<column-result name="USER_NAME"/>
		<column-result name="LAST_NAME"/>
    	<column-result name="FIRST_NAME"/>			
	</sql-result-set-mapping>	
	<!--
		DMP related data objects
	-->
	<entity class="gov.va.med.ccht.model.dmp.DMPSearch" access="FIELD"
		name="DMPSearch">
		
		<table schema="dmp" name="DMP" /><named-query name="getAllDmps">
			<query>
	    		<![CDATA[
	    			select l from gov.va.med.ccht.model.dmp.DMPSearch l order by l.name
	    		]]>
	    	</query>
		</named-query>
		<named-query name="searchAllDmps">
			<query>
	    		<![CDATA[
	    			select d from gov.va.med.ccht.model.dmp.DMPSearch d 
	    			where d.name like :dmpName 
	    			or d.tag like :dmpTag
	    			order by d.name asc
	    		]]>
	    	</query>
		</named-query>
					
		<attribute-override name="id">
			<column name="DMP_ID" />
		</attribute-override>
		<attributes>
			<basic name="name" >
				<column name="name" />
			</basic>
			<basic name="description">
				<column name="description" />
			</basic>
			<basic name="objective">
				<column name="objective" />
			</basic>
			<basic name="dateCreated">
				<column name="date_created" />
			</basic>
			<basic name="tag">
				<column name="dmp_tag" />
			</basic>
			<basic name="dmpVersion">
				<column name="dmp_version" />
			</basic>			
			<basic name="status" >
				<column name="STATUS_TYPE_ID" insertable="false" updatable="false"/>
			</basic>
			<basic name="question" optional="true" >
				<column name="STATUS_TYPE_ID" nullable="true" insertable="false" updatable="false"/>
			</basic>
			<basic name="response" optional="true" >
				<column name="STATUS_TYPE_ID" nullable="true" insertable="false" updatable="false"/>
			</basic>
			<basic name="assignee" optional="true" >
				<column name="STATUS_TYPE_ID" nullable="true" insertable="false" updatable="false"/>
			</basic>
	   		<many-to-one name="dmpStatusType" 
				target-entity="gov.va.med.ccht.model.dmp.DMPStatusType">
				<join-column name="STATUS_TYPE_ID" />
			</many-to-one>
	 	<!-- 	 <one-to-many name="assignees" target-entity="gov.va.med.ccht.model.dmp.DMPToolUser"
				mapped-by="dmp" fetch="EAGER">
			</one-to-many>	
			-->
			<one-to-many name="questions" target-entity="gov.va.med.ccht.model.dmp.DMPQuestion"
				mapped-by="dmp" fetch="EAGER">
				<order-by>contentOrder</order-by>
			</one-to-many>
			<transient name="createdByName"/>	
			<transient name="modifiedByName"/>
		</attributes>
	</entity>
	<!-- 
	<entity class="gov.va.med.ccht.model.dmp.DMPToolUser" access="FIELD"
		name="DMPToolUser">
		<table schema="dmp" name="DMP_TOOL_USER" />
		<attribute-override name="id">
			<column name="DMP_TOOL_USER_ID" />
		</attribute-override>
		<attributes>
			<basic name="assigned">
				<column name="assigned_ind" />
			</basic>
			<many-to-one name="dmp" target-entity="gov.va.med.ccht.model.dmp.DMPSearch">
				<join-column name="DMP_ID" />
			</many-to-one>
			<many-to-one name="appUser" target-entity="gov.va.med.ccht.model.User">
				<join-column name="APP_USER_ID" />
			</many-to-one>
		</attributes>
	</entity>	
	 -->

	</entity-mappings>