[jbpm-commits] JBoss JBPM SVN: r6752 - in jbpm4/trunk/modules: pvm/src/main/java/org/jbpm/pvm/internal/hibernate and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Sat Oct 9 08:11:47 EDT 2010


Author: rebody
Date: 2010-10-09 08:11:46 -0400 (Sat, 09 Oct 2010)
New Revision: 6752

Modified:
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/hibernate/QuerySessionImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java
Log:
JBPM-2688 support query processDefinitionKey in ProcessInstanceQuery

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java	2010-10-09 10:16:53 UTC (rev 6751)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java	2010-10-09 12:11:46 UTC (rev 6752)
@@ -29,38 +29,44 @@
 /** query for ongoing {@linkplain ProcessInstance process instances}
  * exclusively. refer to {@link HistoryProcessInstanceQuery} for
  * queries that include finished process instances.
- * 
+ *
  * @author Tom Baeyens
  */
 public interface ProcessInstanceQuery {
-  
+
   /** key property to be used as property in {@link #orderAsc(String)} and {@link #orderDesc(String)} */
   String PROPERTY_KEY = "key";
-  
+
   /** select only process instances for the given process definition */
   ProcessInstanceQuery processDefinitionId(String processDefinitionId);
 
+  /** select only process instances for the given key of process definition. */
+  ProcessInstanceQuery processDefinitionKey(String processDefinitionKey);
+
   /** select only a specific process instances using the process instance id */
   ProcessInstanceQuery processInstanceId(String processInstanceId);
-  
+
   /** select only specific process instances using a business key */
   ProcessInstanceQuery processInstanceKey(String processInstanceKey);
-  
+
   /** select only process instances that were started be given user */
   ProcessInstanceQuery initiator(String userId);
-  
+
   /** select only process instances for the given process definition, allows to specify query operand */
   ProcessInstanceQuery processDefinitionId(QueryOperator operator, String... processDefinitionId);
 
+  /** select only process instances for the given process definition, allows to specify query operand */
+  ProcessInstanceQuery processDefinitionKey(QueryOperator operator, String... processDefinitionKey);
+
   /** select only a specific process instances using the process instance id, allows to specify query operand */
   ProcessInstanceQuery processInstanceId(QueryOperator operator, String... processInstanceId);
-  
+
   /** select only specific process instances using a business key, allows to specify query operand */
   ProcessInstanceQuery processInstanceKey(QueryOperator operator, String... processInstanceKey);
 
   /** select only process instances that were started be given user, allows to specify query operand */
   ProcessInstanceQuery initiator(QueryOperator operator, String... userId);
-  
+
   /** select only suspended process definitions */
   ProcessInstanceQuery suspended();
 
@@ -81,8 +87,8 @@
 
   /** execute the query and obtain the unique {@link ProcessInstance} */
   ProcessInstance uniqueResult();
-  
-  /** execute a count(*) query and returns number of results */ 
+
+  /** execute a count(*) query and returns number of results */
   long count();
 
 }

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/hibernate/QuerySessionImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/hibernate/QuerySessionImpl.java	2010-10-09 10:16:53 UTC (rev 6751)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/hibernate/QuerySessionImpl.java	2010-10-09 12:11:46 UTC (rev 6752)
@@ -71,11 +71,12 @@
 
     public Object query(QueryProperty queryProperty) {
         if (DeploymentImpl.class.getName().equals(queryProperty.getKey())
-                || ExecutionImpl.class.getName().equals(queryProperty.getKey())
                 || JobImpl.class.getName().equals(queryProperty.getKey())
                 || MessageImpl.class.getName().equals(queryProperty.getKey())
                 || TimerImpl.class.getName().equals(queryProperty.getKey())) {
             return queryDefault(queryProperty);
+        } else if (ExecutionImpl.class.getName().equals(queryProperty.getKey())) {
+            return queryProcessInstance(queryProperty);
         } else if (TaskImpl.class.getName().equals(queryProperty.getKey())) {
             return queryTask(queryProperty);
         } else if (ProcessDefinitionImpl.class.getName().equals(queryProperty.getKey())) {
@@ -117,6 +118,60 @@
         return executeQuery(buff.toString(), params, queryProperty);
     }
 
+    // ~ ======================================================================
+
+    protected Object queryProcessInstance(QueryProperty queryProperty) {
+        StringBuilder buff = new StringBuilder();
+        String key = queryProperty.getKey();
+
+        // select, from
+        if (queryProperty.getResultType() == QueryProperty.RESULT_TYPE_COUNT) {
+            buff.append("select count(distinct e) from ")
+                .append(key)
+                .append(" e");
+        } else {
+            buff.append("select distinct e from ")
+                .append(key)
+                .append(" e");
+        }
+
+        // where
+        Map params = new HashMap();
+        boolean isAlreadyAddWhere = false;
+        if (queryProperty.hasCondition("processDefinitionKey")) {
+            QueryCondition condition = queryProperty.getCondition("processDefinitionKey");
+            Object processDefinitionKey = condition.getValue();
+            buff.append(", ")
+                .append(DeploymentProperty.class.getName())
+                .append(" as idProperty, ")
+                .append(DeploymentProperty.class.getName())
+                .append(" as keyProperty ")
+                .append(" where idProperty.objectName = keyProperty.objectName ")
+                .append(" and idProperty.deployment = keyProperty.deployment ")
+                .append(" and idProperty.stringValue = e.processDefinitionId ")
+                .append(" and keyProperty.stringValue ")
+                .append(condition.getOperator());
+            if (condition.getOperator() == QueryOperator.IN || condition.getOperator() == QueryOperator.NOT_IN) {
+                buff.append(" (:processDefinitionKey) ");
+            } else {
+                buff.append(" :processDefinitionKey ");
+            }
+
+            params.put("processDefinitionKey", processDefinitionKey);
+
+            isAlreadyAddWhere = true;
+            queryProperty.removeCondition("processDefinitionKey");
+        }
+        this.appendWhereClause(buff, queryProperty, params, isAlreadyAddWhere);
+
+        // order by
+        this.appendOrderClause(buff, queryProperty);
+
+        return this.executeQuery(buff.toString(), params, queryProperty);
+    }
+
+    // ~ ======================================================================
+
     protected Object queryTask(QueryProperty queryProperty) {
         if (queryProperty.hasCondition("candidate")) {
             return queryTaskWithParticipant(queryProperty);
@@ -228,6 +283,8 @@
         return this.executeQuery(buff.toString(), params, queryProperty);
     }
 
+    // ~ ======================================================================
+
     protected Object queryProcessDefinition(QueryProperty queryProperty) {
         StringBuilder buff = new StringBuilder();
 
@@ -290,6 +347,8 @@
         }
     }
 
+    // ~ ======================================================================
+
     protected Object queryHistoryActivityInstance(QueryProperty queryProperty) {
         StringBuilder buff = new StringBuilder();
         String key = queryProperty.getKey();
@@ -363,6 +422,8 @@
         return this.executeQuery(buff.toString(), params, queryProperty);
     }
 
+    // ~ ======================================================================
+
     protected Object queryHistoryDetail(QueryProperty queryProperty) {
         StringBuilder buff = new StringBuilder();
         String key = queryProperty.getKey();
@@ -412,6 +473,8 @@
         return this.executeQuery(buff.toString(), params, queryProperty);
     }
 
+    // ~ ======================================================================
+
     protected Object queryHistoryProcessInstance(QueryProperty queryProperty) {
         StringBuilder buff = new StringBuilder();
         String key = queryProperty.getKey();
@@ -461,6 +524,8 @@
         return this.executeQuery(buff.toString(), params, queryProperty);
     }
 
+    // ~ ======================================================================
+
     protected Object queryHistoryTask(QueryProperty queryProperty) {
         StringBuilder buff = new StringBuilder();
         String key = queryProperty.getKey();

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java	2010-10-09 10:16:53 UTC (rev 6751)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java	2010-10-09 12:11:46 UTC (rev 6752)
@@ -57,6 +57,7 @@
   private static final long serialVersionUID = 1L;
 
   protected String[] processDefinitionId;
+  protected String[] processDefinitionKey;
   protected String[] processInstanceId;
   protected String[] processInstanceKey;
   protected Boolean suspended;
@@ -73,12 +74,12 @@
   }
 
   public ProcessInstanceQuery orderAsc(String property) {
-    orders.put(property, "asc");
+    orders.put("e." + property, "asc");
     return this;
   }
 
   public ProcessInstanceQuery orderDesc(String property) {
-    orders.put(property, "desc");
+    orders.put("e." + property, "desc");
     return this;
   }
 
@@ -97,6 +98,11 @@
     return this;
   }
 
+  public ProcessInstanceQuery processDefinitionKey(String processDefinitionKey) {
+    this.processDefinitionKey = new String[] { processDefinitionKey };
+    return this;
+  }
+
   public ProcessInstanceQuery suspended() {
     this.suspended = true;
     return this;
@@ -128,6 +134,12 @@
     return this;
   }
 
+  public ProcessInstanceQuery processDefinitionKey(QueryOperator operator, String... processDefinitionKey) {
+    this.processDefinitionKey = processDefinitionKey;
+    parameterOperators.put("processDefinitionKey", operator);
+    return this;
+  }
+
   public ProcessInstanceQuery processInstanceId(QueryOperator operator, String... processInstanceId) {
     this.processInstanceId = processInstanceId;
     parameterOperators.put("processInstanceId", operator);
@@ -172,30 +184,34 @@
     }
 
     // conditions
-    queryProperty.addCondition("parent", null, QueryOperator.IS_NULL);
+    queryProperty.addCondition("e.parent", null, QueryOperator.IS_NULL);
 
     if (suspended != null) {
         if (suspended.equals(Boolean.TRUE)) {
-          queryProperty.addCondition("state", ExecutionImpl.STATE_SUSPENDED, QueryOperator.EQUALS);
+          queryProperty.addCondition("e.state", ExecutionImpl.STATE_SUSPENDED, QueryOperator.EQUALS);
         } else {
-          queryProperty.addCondition("state", ExecutionImpl.STATE_SUSPENDED, QueryOperator.NOT_EQUALS);
+          queryProperty.addCondition("e.state", ExecutionImpl.STATE_SUSPENDED, QueryOperator.NOT_EQUALS);
         }
     }
 
     if (processInstanceId != null) {
-      queryProperty.addCondition("id", processInstanceId, findOperator("processInstanceId"));
+      queryProperty.addCondition("e.id", processInstanceId, findOperator("processInstanceId"));
     }
 
     if (processDefinitionId != null) {
-      queryProperty.addCondition("processDefinitionId", processDefinitionId, findOperator("processDefinitionId"));
+      queryProperty.addCondition("e.processDefinitionId", processDefinitionId, findOperator("processDefinitionId"));
     }
 
+    if (processDefinitionKey != null) {
+      queryProperty.addCondition("processDefinitionKey", processDefinitionKey, findOperator("processDefinitionKey"));
+    }
+
     if (processInstanceKey != null) {
-      queryProperty.addCondition("key", processInstanceKey, findOperator("processInstanceKey"));
+      queryProperty.addCondition("e.key", processInstanceKey, findOperator("processInstanceKey"));
     }
 
     if (initiator != null) {
-      queryProperty.addCondition("initiator", initiator, findOperator("initiator"));
+      queryProperty.addCondition("e.initiator", initiator, findOperator("initiator"));
     }
 
     // orders

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java	2010-10-09 10:16:53 UTC (rev 6751)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java	2010-10-09 12:11:46 UTC (rev 6752)
@@ -123,6 +123,26 @@
     assertEquals(6, processInstances.size());
   }
 
+  public void testQueryByProcessDefinitionKey() {
+    startTestProcesses(10, 6);
+
+    //ProcessDefinition definitionProcess1 =
+    //  repositoryService.createProcessDefinitionQuery().processDefinitionKey(TEST_PROCESS_1_KEY).uniqueResult();
+    //ProcessDefinition definitionProcess2 =
+    //  repositoryService.createProcessDefinitionQuery().processDefinitionKey(TEST_PROCESS_2_KEY).uniqueResult();
+
+
+    List<ProcessInstance> processInstances = executionService.createProcessInstanceQuery()
+                                                             .processDefinitionKey(TEST_PROCESS_1_KEY)
+                                                             .list();
+    assertEquals(10, processInstances.size());
+
+    processInstances = executionService.createProcessInstanceQuery()
+                                       .processDefinitionKey(TEST_PROCESS_2_KEY)
+                                       .list();
+    assertEquals(6, processInstances.size());
+  }
+
   public void testQueryBySuspended() {
     startTestProcesses(6, 0); // Don't start any instance of test process 2
 



More information about the jbpm-commits mailing list