001: package org.jbpm.command;
002:
003: import java.util.Iterator;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007: import org.jbpm.JbpmContext;
008: import org.jbpm.graph.def.Node;
009: import org.jbpm.graph.def.ProcessDefinition;
010: import org.jbpm.graph.def.Transition;
011: import org.jbpm.graph.exe.ProcessInstance;
012: import org.jbpm.graph.exe.Token;
013: import org.jbpm.taskmgmt.exe.TaskInstance;
014:
015: /**
016: * abstract base class for "get" commands which also implements
017: * default pre-fetching.
018: *
019: * Note: prefetching logs is not possible here, so you have to load
020: * Loags explicit with GetProcessInstanceLogCommand
021: *
022: * @author Bernd Ruecker (bernd.ruecker@camunda.com)
023: */
024: public abstract class AbstractGetObjectBaseCommand implements Command {
025:
026: static final Log log = LogFactory
027: .getLog(AbstractGetObjectBaseCommand.class);
028:
029: private static final long serialVersionUID = 1L;
030:
031: /**
032: * if true, all process variables in the context (process instance / task)
033: * are prefetched too
034: */
035: private boolean includeAllVariables = false;
036:
037: /**
038: * specify the names of the variables to prefetch
039: */
040: private String[] variablesToInclude = new String[0];
041:
042: private transient JbpmContext jbpmContext;
043:
044: public AbstractGetObjectBaseCommand() {
045: }
046:
047: public AbstractGetObjectBaseCommand(boolean includeAllVariables,
048: boolean includeLogs) {
049: this .includeAllVariables = includeAllVariables;
050: }
051:
052: public AbstractGetObjectBaseCommand(String[] variablesToInclude) {
053: this .variablesToInclude = variablesToInclude;
054: }
055:
056: public void retrieveTaskInstanceDetails(TaskInstance ti) {
057: try {
058: ti.getToken().getProcessInstance().getProcessDefinition()
059: .getName();
060:
061: // in TaskInstances created with jbpm 3.1, this association was
062: // not present!
063: ti.setProcessInstance(ti.getToken().getProcessInstance());
064: ti.getToken().getNode().getName();
065: ti.getTask().getName();
066:
067: ti.getAvailableTransitions();
068:
069: retrieveVariables(ti);
070: } catch (Exception ex) {
071: log.warn(
072: "exception while retrieving task instance data for task instance "
073: + ti.getId(), ex);
074: }
075: }
076:
077: public ProcessInstance retrieveProcessInstance(ProcessInstance pi) {
078: try {
079: pi.getProcessDefinition().getName();
080: retrieveToken(pi.getRootToken());
081:
082: // load at least the super process id and token id
083: if (pi.getSuperProcessToken() != null) {
084: pi.getSuperProcessToken().getId();
085: pi.getSuperProcessToken().getProcessInstance().getId();
086: }
087:
088: retrieveVariables(pi);
089: } catch (Exception ex) {
090: log.warn(
091: "exception while retrieving process instance data for process instance "
092: + pi.getId(), ex);
093: }
094: return pi;
095: }
096:
097: public ProcessDefinition retrieveProcessDefinition(
098: ProcessDefinition pd) {
099: try {
100: pd.getName();
101: // often needed to start a process:
102: Iterator iter = pd.getStartState().getLeavingTransitions()
103: .iterator();
104: while (iter.hasNext()) {
105: Transition t = (Transition) iter.next();
106: t.getName();
107: }
108: } catch (Exception ex) {
109: log.warn(
110: "exception while retrieving process instance data for process definiton "
111: + pd.getName(), ex);
112: }
113: return pd;
114: }
115:
116: protected void retrieveToken(Token t) {
117: retrieveNode(t.getNode());
118: t.getAvailableTransitions();
119:
120: // if (includeLogs)
121: // t.getProcessInstance().getLoggingInstance().
122:
123: Iterator iter = t.getChildren().values().iterator();
124: while (iter.hasNext()) {
125: retrieveToken((Token) iter.next());
126: }
127: }
128:
129: protected void retrieveNode(Node n) {
130: n.getName();
131: n.getLeavingTransitions();
132: if (n.getSuperState() != null)
133: retrieveNode(n.getSuperState());
134: }
135:
136: public void retrieveVariables(ProcessInstance pi) {
137: if (includeAllVariables) {
138: pi.getContextInstance().getVariables();
139: } else {
140: for (int i = 0; i < variablesToInclude.length; i++) {
141: pi.getContextInstance().getVariable(
142: variablesToInclude[i]);
143: }
144: }
145: }
146:
147: public void retrieveVariables(TaskInstance ti) {
148: if (includeAllVariables) {
149: ti.getVariables();
150: } else {
151: for (int i = 0; i < variablesToInclude.length; i++) {
152: ti.getVariable(variablesToInclude[i]);
153: }
154: }
155: }
156:
157: public boolean isIncludeAllVariables() {
158: return includeAllVariables;
159: }
160:
161: public void setIncludeAllVariables(boolean includeAllVariables) {
162: this .includeAllVariables = includeAllVariables;
163: }
164:
165: public String[] getVariablesToInclude() {
166: return variablesToInclude;
167: }
168:
169: public void setVariablesToInclude(String[] variablesToInclude) {
170: this .variablesToInclude = variablesToInclude;
171: }
172:
173: public void setVariablesToInclude(String variableToInclude) {
174: this .variablesToInclude = new String[] { variableToInclude };
175: }
176:
177: protected JbpmContext getJbpmContext() {
178: return jbpmContext;
179: }
180:
181: protected void setJbpmContext(JbpmContext jbpmContext) {
182: this.jbpmContext = jbpmContext;
183: }
184: }
|