<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1"
                 xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
    <named-query name="PendingRequests">
        <query>
            Select r from RequestInfo as r where (r.status ='QUEUED' or r.status = 'RETRYING') and r.time &lt; :now ORDER BY r.time DESC
        </query>
    </named-query>
    <named-query name="LoadPendingRequests">
        <query>
            Select new org.jbpm.executor.entities.RequestInfo(r.id, r.priority, r.time) from RequestInfo as r where (r.status ='QUEUED' or r.status = 'RETRYING') and (r.owner = :owner or r.owner is null) ORDER BY r.time, priority DESC
        </query>
    </named-query>
    <named-query name="PendingRequestById">
        <query>
            Select r from RequestInfo as r where (r.status ='QUEUED' or r.status ='RETRYING') and id = :id
        </query>
    </named-query>
    <named-query name="EligibleRequestById">
        <query>
            Select r from RequestInfo as r where (r.status ='QUEUED' or r.status ='RETRYING' or r.status ='ERROR') and id = :id
        </query>
    </named-query>
    <named-query name="RunningRequests">
        <query>
            Select r from RequestInfo as r where r.status ='RUNNING' ORDER BY r.id
        </query>
    </named-query>
    <named-query name="QueuedRequests">
        <query>
            Select r from RequestInfo as r where r.status ='QUEUED' ORDER BY r.id
        </query>
    </named-query>
    <named-query name="FutureQueuedRequests">
        <query>
            Select r from RequestInfo as r where r.status ='QUEUED' and r.time &gt; :now ORDER BY r.time ASC
        </query>
    </named-query>
    <named-query name="CompletedRequests">
        <query>
            Select r from RequestInfo as r where r.status ='DONE' ORDER BY r.id
        </query>
    </named-query>
      
    <named-query name="InErrorRequests">
        <query>
            Select r from RequestInfo as r where r.status ='ERROR' ORDER BY r.id
        </query>
    </named-query>
      
    <named-query name="CancelledRequests">
        <query>
            Select r from RequestInfo as r where r.status ='CANCELLED' ORDER BY r.id
        </query>
    </named-query>
      
    <named-query name="GetAllRequests">
        <query>
            Select r from RequestInfo as r  ORDER BY r.id
        </query>
    </named-query>
      
    <named-query name="GetAllErrors">
        <query>
            Select e from ErrorInfo as e  ORDER BY e.id
        </query>
    </named-query>
    
    <named-query name="GetErrorsByRequestId">
        <query>
            Select e from ErrorInfo as e where e.requestInfo.id = :id 
        </query>
    </named-query>
    
    <named-query name="GetRequestsByStatus">
        <query>
            Select r from RequestInfo as r where r.status in (:statuses) ORDER BY r.id DESC
        </query>
    </named-query>

    <named-query name="GetRequestsByBusinessKey">
        <query>
            Select r from RequestInfo as r where r.key = :businessKey ORDER BY r.id
        </query>
    </named-query>
    
    <named-query name="GetRequestsByBusinessKeyAndStatus">
        <query>
            Select r from RequestInfo as r where r.key = :businessKey and r.status in (:statuses) ORDER BY r.id
        </query>
    </named-query>
    
    <named-query name="GetRequestsByCommand">
        <query>
            Select r from RequestInfo as r where r.commandName = :command ORDER BY r.id
        </query>
    </named-query>
    
    <named-query name="GetRequestsByCommandAndStatus">
        <query>
            Select r from RequestInfo as r where r.commandName = :command and r.status in (:statuses) ORDER BY r.id
        </query>
    </named-query>
    
    <named-query name="GetRequestsByDeploymentAndStatus">
        <query>
            Select r from RequestInfo as r where r.deploymentId = :deploymentId and r.status in (:statuses) ORDER BY r.id
        </query>
    </named-query>
    
    <named-query name="GetRequestsByProcessInstanceAndStatus">
        <query>
            Select r from RequestInfo as r where r.processInstanceId = :processInstanceId and r.status in (:statuses) ORDER BY r.id
        </query>
    </named-query>        
    
    <named-query name="PendingRequestsForProcessing">
        <query>
            Select r from RequestInfo as r where (r.status ='QUEUED' or r.status = 'RETRYING') 
            and r.time &lt; :now and (r.owner = :owner or r.owner is null) and (r.deploymentId in (:deploymentId) or r.deploymentId is null) order by r.priority desc
        </query>
    </named-query>
    
        <named-query name="PendingRequestByIdForProcessing">
        <query>
            Select r from RequestInfo as r where (r.status ='QUEUED' or r.status = 'RETRYING') 
            and r.id = :requestId and (r.deploymentId in (:deploymentId) or r.deploymentId is null)
        </query>
    </named-query>
    
</entity-mappings>
