001: package org.jbpm.command;
002:
003: import java.util.Date;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.hibernate.Query;
010: import org.jbpm.JbpmContext;
011: import org.jbpm.graph.exe.ProcessInstance;
012: import org.jbpm.graph.exe.Token;
013:
014: /**
015: * This command can retrieve all process instances (e.g. for admin client).
016: *
017: * You have the possibility to filter the command, therefor use the available
018: * attributes
019: *
020: * @author Bernd Ruecker (bernd.ruecker@camunda.com)
021: */
022: public class GetProcessInstancesCommand extends
023: AbstractGetObjectBaseCommand {
024:
025: private static final long serialVersionUID = -5601050489405283851L;
026:
027: private static final Log log = LogFactory
028: .getLog(GetProcessInstancesCommand.class);
029:
030: /*
031: * is true, only the running process instances are retrieved (ended and
032: * canceled ones are skipped)
033: */
034: private boolean onlyRunning = true;
035:
036: /*
037: * if given, only processes with start date >= given date are shown
038: */
039: private Date fromStartDate;
040:
041: /*
042: * if given, only processes with start date <= given date are shown
043: */
044: private Date untilStartDate;
045:
046: /*
047: * if given, only processes with this name are retrieved
048: */
049: private String processName;
050:
051: /*
052: * if given, only processes with this name are retrieved
053: */
054: private String stateName;
055:
056: private transient boolean firstExpression = true;
057:
058: private String getConcatExpression() {
059: if (firstExpression) {
060: firstExpression = false;
061: return " where ";
062: }
063: return " and ";
064: }
065:
066: public Object execute(JbpmContext jbpmContext) throws Exception {
067: setJbpmContext(jbpmContext);
068: firstExpression = true;
069: StringBuffer queryText = new StringBuffer("select pi"
070: + " from org.jbpm.graph.exe.ProcessInstance as pi ");
071:
072: if (onlyRunning) {
073: queryText.append(getConcatExpression()).append(
074: " pi.end = null");
075: }
076:
077: if (fromStartDate != null) {
078: queryText.append(getConcatExpression()).append(
079: " pi.start >= :from ");
080: }
081: if (untilStartDate != null) {
082: queryText.append(getConcatExpression()).append(
083: " pi.start <= :until ");
084: }
085:
086: // name
087: if (processName != null && processName.length() > 0) {
088: queryText
089: .append(getConcatExpression())
090: .append(
091: " pi.processDefinition.name = :processDefinitionName ");
092: }
093:
094: // TODO: this code only fecthes root tokens, child-tokens has to be
095: // considered too!
096: if (stateName != null && stateName.length() > 0) {
097: queryText.append(getConcatExpression()).append(
098: " pi.rootToken.node.name = :nodeName ");
099: }
100:
101: queryText.append(" order by pi.start desc");
102:
103: Query query = jbpmContext.getSession().createQuery(
104: queryText.toString());
105:
106: if (fromStartDate != null) {
107: query.setDate("from", fromStartDate);
108: }
109: if (untilStartDate != null) {
110: query.setDate("until", untilStartDate);
111: }
112:
113: if (processName != null && processName.length() > 0) {
114: query.setString("processDefinitionName", processName);
115: }
116:
117: if (stateName != null && stateName.length() > 0) {
118: query.setString("nodeName", stateName);
119: }
120:
121: return retrieveProcessInstanceDetails(query.list());
122: }
123:
124: /**
125: * access everything on all processInstance objects, which is not in the
126: * default fetch group from hibernate, but needs to be accesible from the
127: * client
128: *
129: * overwrite this, if you need more details in your client
130: */
131: public List retrieveProcessInstanceDetails(List processInstanceList) {
132: Iterator it = processInstanceList.iterator();
133: while (it.hasNext()) {
134: retrieveProcessInstance((ProcessInstance) it.next());
135: }
136: return processInstanceList;
137: }
138:
139: public Date getFromStartDate() {
140: return fromStartDate;
141: }
142:
143: public void setFromStartDate(Date fromStartDate) {
144: this .fromStartDate = fromStartDate;
145: }
146:
147: public boolean isOnlyRunning() {
148: return onlyRunning;
149: }
150:
151: public void setOnlyRunning(boolean onlyRunning) {
152: this .onlyRunning = onlyRunning;
153: }
154:
155: public String getProcessName() {
156: return processName;
157: }
158:
159: public void setProcessName(String processName) {
160: this .processName = processName;
161: }
162:
163: public String getStateName() {
164: return stateName;
165: }
166:
167: public void setStateName(String stateName) {
168: this .stateName = stateName;
169: }
170:
171: public Date getUntilStartDate() {
172: return untilStartDate;
173: }
174:
175: public void setUntilStartDate(Date untilStartDate) {
176: this.untilStartDate = untilStartDate;
177: }
178:
179: }
|