[jbpm-commits] JBoss JBPM SVN: r6169 - in jbpm4/trunk/modules: api/src/main/java/org/jbpm/api/history and 3 other directories.
do-not-reply at jboss.org
do-not-reply at jboss.org
Wed Feb 10 13:50:44 EST 2010
Author: alex.guizar at jboss.com
Date: 2010-02-10 13:50:44 -0500 (Wed, 10 Feb 2010)
New Revision: 6169
Modified:
jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ExecutionService.java
jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java
jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstance.java
jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/EndProcessInstance.java
jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java
jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java
Log:
JBPM-2499: enhance HistoryProcessInstanceQuery with ended(), endedBefore(Date) and endedAfter(Date) conditions
clarify that HistoryProcessInstance stuff includes running and finished process instances
Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ExecutionService.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ExecutionService.java 2010-02-10 11:11:55 UTC (rev 6168)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ExecutionService.java 2010-02-10 18:50:44 UTC (rev 6169)
@@ -98,7 +98,10 @@
ProcessInstance signalExecutionById(String executionId, Map<String, ?> parameters);
- /** search for process instances with criteria */
+ /** search for process instances with criteria.
+ * be aware that this query only sees ongoing process instances.
+ * refer to {@link HistoryService#createHistoryTaskQuery()} for
+ * queries that include finished process instances. */
ProcessInstanceQuery createProcessInstanceQuery();
/** creates or overwrites a variable value on the referenced execution */
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-02-10 11:11:55 UTC (rev 6168)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java 2010-02-10 18:50:44 UTC (rev 6169)
@@ -23,8 +23,12 @@
import java.util.List;
+import org.jbpm.api.history.HistoryProcessInstanceQuery;
-/** query for {@link ProcessInstance process instances}.
+
+/** query for ongoing {@linkplain ProcessInstance process instances}
+ * exclusively. refer to {@link HistoryProcessInstanceQuery} for
+ * queries that include finished process instances.
*
* @author Tom Baeyens
*/
Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstance.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstance.java 2010-02-10 11:11:55 UTC (rev 6168)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstance.java 2010-02-10 18:50:44 UTC (rev 6169)
@@ -27,12 +27,12 @@
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessInstance;
-/** one occurence of an execution of a process definition.
+/** one particular instance of a process definition.
*
* Every {@link ProcessInstance} will have one HistoryProcessInstance
* associated. The difference is that the ProcessInstance will be
- * deleted when it is ended. And the history information will remain
- * in the DB. That keeps the runtime DB healthy and performant.
+ * deleted when it is ended, whereas the history information will remain
+ * in the DB. That keeps the runtime DB healthy and performing well.
*
* @author Tom Baeyens
*/
Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java 2010-02-10 11:11:55 UTC (rev 6168)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java 2010-02-10 18:50:44 UTC (rev 6169)
@@ -21,10 +21,12 @@
*/
package org.jbpm.api.history;
+import java.util.Date;
import java.util.List;
-/** query for occurences of {@link HistoryProcessInstance process instances}.
+/** query for ongoing and finished
+ * {@linkplain HistoryProcessInstance process instances}.
*
* @author Tom Baeyens
*/
@@ -64,6 +66,23 @@
/** select a specific page in the result set */
HistoryProcessInstanceQuery page(int firstResult, int maxResults);
+ /** select only process instances that have ended.
+ * cancels bounds previously set by {@link #endedBefore(Date)} or
+ * {@link #endedAfter(Date)} */
+ HistoryProcessInstanceQuery ended();
+
+ /** select only process instances that have ended before the given date.
+ * combine with {@link #endedAfter(Date)} to specify a finite interval
+ * @throws IllegalArgumentException if the given threshold is later
+ * than the end-after date (if set), thus yielding an empty interval */
+ HistoryProcessInstanceQuery endedBefore(Date threshold);
+
+ /** select only process instances that have ended on or after the given date.
+ * combine with {@link #endedBefore(Date)} to specify a finite interval
+ * @throws IllegalArgumentException if the given threshold is earlier
+ * than the end-before date (if set), thus yielding an empty interval */
+ HistoryProcessInstanceQuery endedAfter(Date threshold);
+
/** execute the query and obtain the list of {@link HistoryProcessInstance}s */
List<HistoryProcessInstance> list();
Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/EndProcessInstance.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/EndProcessInstance.java 2010-02-10 11:11:55 UTC (rev 6168)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/EndProcessInstance.java 2010-02-10 18:50:44 UTC (rev 6169)
@@ -22,7 +22,6 @@
package org.jbpm.pvm.internal.cmd;
import org.jbpm.api.cmd.Environment;
-import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.session.DbSession;
Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java 2010-02-10 11:11:55 UTC (rev 6168)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java 2010-02-10 18:50:44 UTC (rev 6169)
@@ -21,6 +21,7 @@
*/
package org.jbpm.pvm.internal.query;
+import java.util.Date;
import java.util.List;
import org.hibernate.Query;
@@ -30,6 +31,7 @@
/**
* @author Tom Baeyens
+ * @author Alejandro Guizar
*/
public class HistoryProcessInstanceQueryImpl extends AbstractQuery implements HistoryProcessInstanceQuery {
@@ -40,6 +42,10 @@
protected String processInstanceId;
protected String processInstanceKey;
+ protected boolean ended;
+ protected Date endedBefore;
+ protected Date endedAfter;
+
public String hql() {
StringBuilder hql = new StringBuilder();
@@ -70,12 +76,28 @@
appendWhereClause(" hpi.key = '" + processInstanceKey + "'", hql);
}
+ if (ended) {
+ appendWhereClause(" hpi.endTime is not null", hql);
+ }
+ if (endedBefore != null) {
+ appendWhereClause(" hpi.endTime < :before", hql);
+ }
+ if (endedAfter != null) {
+ appendWhereClause(" hpi.endTime >= :after", hql);
+ }
+
appendOrderByClause(hql);
return hql.toString();
}
protected void applyParameters(Query query) {
+ if (endedBefore != null) {
+ query.setTimestamp("before", endedBefore);
+ }
+ if (endedAfter != null) {
+ query.setTimestamp("after", endedAfter);
+ }
}
public List<HistoryProcessInstance> list() {
@@ -120,4 +142,28 @@
this.state = state;
return this;
}
+
+ public HistoryProcessInstanceQuery ended() {
+ ended = true;
+ endedBefore = endedAfter = null;
+ return this;
+ }
+
+ public HistoryProcessInstanceQuery endedBefore(Date threshold) {
+ if (endedAfter != null && endedAfter.after(threshold)) {
+ throw new IllegalArgumentException("threshold is later than endedAfter date");
+ }
+ endedBefore = threshold;
+ ended = false;
+ return this;
+ }
+
+ public HistoryProcessInstanceQuery endedAfter(Date threshold) {
+ if (endedBefore != null && endedBefore.before(threshold)) {
+ throw new IllegalArgumentException("threshold is earlier than endedBefore date");
+ }
+ endedAfter = threshold;
+ ended = false;
+ return this;
+ }
}
Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java 2010-02-10 11:11:55 UTC (rev 6168)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java 2010-02-10 18:50:44 UTC (rev 6169)
@@ -25,17 +25,21 @@
package org.jbpm.test.query;
import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
import java.util.List;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.history.HistoryProcessInstanceQuery;
+import org.jbpm.pvm.internal.util.Clock;
import org.jbpm.test.JbpmTestCase;
import org.jbpm.test.assertion.QueryAssertions;
/**
* @author Joram Barrez
+ * @author Alejandro Guizar
*/
public class HistoryProcessInstanceQueryTest extends JbpmTestCase {
@@ -86,11 +90,121 @@
testOrderByNaturalOrdening(HistoryProcessInstanceQuery.PROPERTY_DURATION, 4);
}
+ public void testQueryEnded() {
+ List<String> procInstIds = createTestHistoryProcessInstances(4);
+ String endedProcInstId = procInstIds.get(0);
+ executionService.endProcessInstance(endedProcInstId, "ended");
+
+ List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
+ .ended()
+ .list();
+ assertEquals(1, procInsts.size());
+
+ HistoryProcessInstance endedProcInst = procInsts.get(0);
+ assertEquals(endedProcInstId, endedProcInst.getProcessInstanceId());
+ }
+ public void testQueryEndedBefore() {
+ List<String> procInstIds = createTestHistoryProcessInstances(4);
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.add(Calendar.DATE, -1);
+ Date yesterday = calendar.getTime();
+ Clock.setExplicitTime(yesterday);
+
+ String endedYesterdayProcInstId = procInstIds.get(0);
+ executionService.endProcessInstance(endedYesterdayProcInstId, "ended");
+
+ calendar.add(Calendar.DATE, -1);
+ Date twoDaysAgo = calendar.getTime();
+ Clock.setExplicitTime(twoDaysAgo);
+
+ String endedTwoDaysAgoProcInstId = procInstIds.get(1);
+ executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
+
+ List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
+ .endedBefore(yesterday)
+ .list();
+ assertEquals(1, procInsts.size());
+
+ HistoryProcessInstance endedTwoDaysAgoProcInst = procInsts.get(0);
+ assertEquals(endedTwoDaysAgoProcInstId, endedTwoDaysAgoProcInst.getProcessInstanceId());
+
+ Clock.setExplicitTime(null);
+ }
+
+ public void testQueryEndedAfter() {
+ List<String> procInstIds = createTestHistoryProcessInstances(4);
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.add(Calendar.DATE, -1);
+ Date yesterday = calendar.getTime();
+ Clock.setExplicitTime(yesterday);
+
+ String endedYesterdayProcInstId = procInstIds.get(0);
+ executionService.endProcessInstance(endedYesterdayProcInstId, "ended");
+
+ calendar.add(Calendar.DATE, -1);
+ Date twoDaysAgo = calendar.getTime();
+ Clock.setExplicitTime(twoDaysAgo);
+
+ String endedTwoDaysAgoProcInstId = procInstIds.get(1);
+ executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
+
+ List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
+ .endedAfter(yesterday)
+ .list();
+ assertEquals(1, procInsts.size());
+
+ HistoryProcessInstance endedYesterdayProcInst = procInsts.get(0);
+ assertEquals(endedYesterdayProcInstId, endedYesterdayProcInst.getProcessInstanceId());
+
+ Clock.setExplicitTime(null);
+ }
+
+ public void testQueryEndedAfterAndBefore() {
+ List<String> procInstIds = createTestHistoryProcessInstances(4);
+
+ String endedTodayProcInstId = procInstIds.get(0);
+ executionService.endProcessInstance(endedTodayProcInstId, "ended");
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.add(Calendar.DATE, -1);
+ Date yesterday = calendar.getTime();
+ Clock.setExplicitTime(yesterday);
+
+ String endedYesterdayProcInstId = procInstIds.get(1);
+ executionService.endProcessInstance(endedYesterdayProcInstId, "ended");
+
+ calendar.add(Calendar.DATE, -1);
+ Date twoDaysAgo = calendar.getTime();
+ Clock.setExplicitTime(twoDaysAgo);
+
+ String endedTwoDaysAgoProcInstId = procInstIds.get(2);
+ executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
+
+ calendar.add(Calendar.DATE, 3);
+ Date tomorrow = calendar.getTime();
+
+ List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
+ .endedAfter(yesterday)
+ .endedBefore(tomorrow)
+ .list();
+ assertEquals(2, procInsts.size());
+
+ for (HistoryProcessInstance procInst : procInsts) {
+ String procInstId = procInst.getProcessInstanceId();
+ assert procInstId.equals(endedYesterdayProcInstId) ||
+ procInstId.equals(endedTodayProcInstId) : procInstId;
+ }
+
+ Clock.setExplicitTime(null);
+ }
+
/* -------------------------------------------------------------
* HELPER METHODS
* ------------------------------------------------------------- */
-
+
// Don't delete because it isn't used. Could be handy in the future!
private void testOrderBy(String property, List<Object> expectedValues) {
createTestHistoryProcessInstances(4);
@@ -128,8 +242,11 @@
deployJpdlXmlString(
"<process name='theProcess'>" +
" <start>" +
+ " <transition to='wait' />" +
+ " </start>" +
+ " <state name='wait'>" +
" <transition to='end' />" +
- " </start>" +
+ " </state>" +
" <end name='end' />" +
"</process>");
More information about the jbpm-commits
mailing list