<?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 orm_2_0.xsd"
  version="2.0">

  <!-- process instance queries -->
  <named-query name="getProcessInstances">
    <query>
      select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      ORDER BY
        log.processId, log.start
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstancesByStatus">
    <query>
      select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.status in (:states)
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstancesByStatusAndInitiator">
    <query>
      select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.status in (:states) and log.identity = :initiator
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstancesByDeploymentId">
    <query>
      select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.externalId=:externalId
        and log.status in (:states)
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstancesByProcessDefinition">
    <query>
     select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.processId =:processDefId
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstanceById">
    <query>
     select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.processInstanceId =:processId
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>


  <named-query name="getProcessInstancesByProcessIdAndStatus">
    <query>
     select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.status in (:states) and log.processId like :processId
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstancesByProcessIdAndStatusAndInitiator">
    <query>
     select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.status in (:states) and log.processId like :processId and log.identity = :initiator
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

<named-query name="getProcessInstancesByProcessNameAndStatus">
    <query>
     select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.status in (:states) and log.processName like :processName
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstancesByProcessNameAndStatusAndInitiator">
    <query>
     select
      new org.jbpm.kie.services.impl.model.ProcessInstanceDesc(
      log.processInstanceId,
      log.processId,
      log.processName,
      log.processVersion,
      log.status,
      log.externalId,
      log.start,
      log.identity
      )
      from
        ProcessInstanceLog log
      where
        log.status in (:states) and log.processName like :processName and log.identity = :initiator
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>
  
  <!-- node instance queries -->

  <named-query name="getProcessInstanceFullHistory">
    <query>
      select
      new org.jbpm.kie.services.impl.model.NodeInstanceDesc(
      log.nodeInstanceId,
      log.nodeId,
      log.nodeName,
      log.nodeType,
      log.externalId,
      log.processInstanceId,
      log.date,
      log.connection,
      log.type,
      log.workItemId
      )
      from
        NodeInstanceLog log
      where
        log.processInstanceId=:processId
      ORDER BY 
        log.date DESC, log.id DESC
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>
  
    <named-query name="getProcessInstanceFullHistoryByType">
    <query>
      select
      new org.jbpm.kie.services.impl.model.NodeInstanceDesc(
      log.nodeInstanceId,
      log.nodeId,
      log.nodeName,
      log.nodeType,
      log.externalId,
      log.processInstanceId,
      log.date,
      log.connection,
      log.type,
      log.workItemId
      )
      from
        NodeInstanceLog log
      where
        log.processInstanceId=:processId 
        AND log.type=:type
      ORDER BY 
        log.date DESC, log.id DESC
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>


  <named-query name="getProcessInstanceActiveNodes">
    <query>
      select
      new org.jbpm.kie.services.impl.model.NodeInstanceDesc(
      log.nodeInstanceId,
      log.nodeId,
      log.nodeName,
      log.nodeType,
      log.externalId,
      log.processInstanceId,
      log.date,
      log.connection,
      log.type,
      log.workItemId
      )
      from
        NodeInstanceLog log
      where
        log.nodeInstanceId in ( select nil.nodeInstanceId from NodeInstanceLog nil where nil.processInstanceId=:processId  
                    GROUP BY nil.nodeInstanceId
                    HAVING sum(nil.type) = 0)
        and log.type = 0  
        and log.processInstanceId=:processId          
      order by
        log.id ASC
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getProcessInstanceCompletedNodes">
    <query>
      select
      new org.jbpm.kie.services.impl.model.NodeInstanceDesc(
      log.nodeInstanceId,
      log.nodeId,
      log.nodeName,
      log.nodeType,
      log.externalId,
      log.processInstanceId,
      log.date,
      log.connection,
      log.type,
      log.workItemId
      )
      from
        NodeInstanceLog log
      where
        log.nodeInstanceId in (select nodeInstanceId from NodeInstanceLog nid where nid.processInstanceId=:processId AND nid.type = 1 
        						GROUP BY nid.nodeInstanceId
                    			HAVING sum(nid.type) &gt;= 1) 
       	and log.type = 1 
       	and log.processInstanceId=:processId  
      ORDER BY 
        log.date ASC
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>
  
    <named-query name="getNodeInstanceForWorkItem">
    <query>
      select
      new org.jbpm.kie.services.impl.model.NodeInstanceDesc(
      log.nodeInstanceId,
      log.nodeId,
      log.nodeName,
      log.nodeType,
      log.externalId,
      log.processInstanceId,
      log.date,
      log.connection,
      log.type,
      log.workItemId
      )
      from
        NodeInstanceLog log
      where
        log.workItemId=:workItemId
       	and log.type = 0   
      ORDER BY 
        log.nodeInstanceId, log.id ASC
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>


  <!-- variable instance queries -->
  <named-query name="getVariablesCurrentState">
    <query>
      select
      new org.jbpm.kie.services.impl.model.VariableStateDesc(
      log.variableId,
      log.variableInstanceId,
      log.oldValue,
      log.value,
      log.externalId,
      log.processInstanceId,
      log.date
      )
      from
        VariableInstanceLog log
      where
        log.processInstanceId =:processInstanceId AND log.id in 
        (select max(vss.id) FROM VariableInstanceLog vss WHERE vss.processInstanceId =:processInstanceId group by vss.variableId ) 
        order by log.id
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

  <named-query name="getVariableHistory">
    <query>
     select
      new org.jbpm.kie.services.impl.model.VariableStateDesc(
      log.variableId,
      log.variableInstanceId,
      log.oldValue,
      log.value,
      log.externalId,
      log.processInstanceId,
      log.date
      )
      from
        VariableInstanceLog log
      where
        log.processInstanceId =:processInstanceId AND log.variableId =:variableId 
      order by 
        log.id DESC
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>
  
  <named-query name="getTaskInstanceById">
    <query>
        select 
	      new org.jbpm.kie.services.impl.model.UserTaskInstanceDesc(
	      a.taskId,
	      a.status,
	      a.activationTime,
	      a.name,
	      a.description,
	      a.priority,
	      a.actualOwner,
	      a.createdBy,
	      a.deploymentId,
	      a.processId,
	      a.processInstanceId,
	      a.createdOn,
	      a.dueDate
	      )
        from 
        	AuditTaskImpl a 
        where 
        	a.taskId = :taskId 
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>
  
  <named-query name="getTaskInstanceByWorkItemId">
    <query>
        select 
	      new org.jbpm.kie.services.impl.model.UserTaskInstanceDesc(
	      a.taskId,
	      a.status,
	      a.activationTime,
	      a.name,
	      a.description,
	      a.priority,
	      a.actualOwner,
	      a.createdBy,
	      a.deploymentId,
	      a.processId,
	      a.processInstanceId,
	      a.createdOn,
	      a.dueDate
	      )
        from 
        	AuditTaskImpl a,
        	TaskImpl t 
        where 
        	a.taskId = t.id and
        	t.taskData.workItemId = :workItemId 
        	
    </query>
    <!-- hint name="org.hibernate.timeout" value="200"/ -->
  </named-query>

</entity-mappings>