001: package org.jbpm.command;
002:
003: import java.util.Collection;
004: import java.util.Date;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.hibernate.Query;
011: import org.jbpm.JbpmContext;
012: import org.jbpm.db.JbpmSchema;
013: import org.jbpm.graph.exe.ProcessInstance;
014: import org.jbpm.graph.exe.Token;
015: import org.jbpm.taskmgmt.exe.TaskInstance;
016:
017: /**
018: *
019: * @author Bernd Ruecker (bernd.ruecker@camunda.com)
020: *
021: */
022: public class CancelProcessInstanceCommand extends AbstractCancelCommand
023: implements Command {
024:
025: private static final long serialVersionUID = 7145293049356621597L;
026:
027: private long processId;
028:
029: /**
030: * recursivly cancel subprocesses?
031: */
032: boolean cancelSubProcesses = false;
033:
034: public CancelProcessInstanceCommand() {
035: }
036:
037: public CancelProcessInstanceCommand(long processId) {
038: this .processId = processId;
039: }
040:
041: public Object execute(JbpmContext jbpmContext) throws Exception {
042: this .jbpmContext = jbpmContext;
043: cancelProcess(processId);
044: this .jbpmContext = null;
045: return null;
046: }
047:
048: protected void cancelProcess(long processIdToCancel) {
049: ProcessInstance pi = jbpmContext.getGraphSession()
050: .loadProcessInstance(processIdToCancel);
051:
052: log.info("cancel process instance " + pi.getId());
053:
054: // Record a standardized variable that we can use to determine that this
055: // process has been 'cancelled' and not just ended.
056: pi.getContextInstance().createVariable("cancelled", new Date());
057:
058: try {
059: // End the process instance and any open tokens
060: // TODO: better implementation (also cancel sub processes etc.)
061: // see http://intranet.computation.de/bugs/view_bug.php?bug_id=295
062:
063: cancelToken(pi.getRootToken());
064: cancelTokens(pi.getRootToken().getChildren().values());
065:
066: pi.end();
067:
068: if (cancelSubProcesses)
069: cancelSubProcesses(processIdToCancel);
070:
071: log.info("finished process cancellation");
072: } catch (RuntimeException ex) {
073: log.error("problems while cancel process", ex);
074: throw ex;
075: }
076: }
077:
078: /**
079: * cancel all sub processes of the process with the given id calls
080: * "cancelProcess" recursivly until all processes are ended
081: *
082: * @param processId
083: */
084: protected void cancelSubProcesses(long processId) {
085: Query q = jbpmContext.getSession().getNamedQuery(
086: "GraphSession.findSubProcessInstances");
087: q.setLong("instanceId", processId);
088: Iterator iter = q.list().iterator();
089: while (iter.hasNext()) {
090: ProcessInstance pi = (ProcessInstance) iter.next();
091: log.info("cancel sub process instance #" + pi.getId());
092: cancelProcess(pi.getId());
093: }
094: }
095:
096: public boolean isCancelSubProcesses() {
097: return cancelSubProcesses;
098: }
099:
100: public void setCancelSubProcesses(boolean cancelSubProcesses) {
101: this .cancelSubProcesses = cancelSubProcesses;
102: }
103:
104: public long getProcessId() {
105: return processId;
106: }
107:
108: public void setProcessId(long processId) {
109: this.processId = processId;
110: }
111:
112: }
|