1 package org.apache.fulcrum.osworkflow;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import com.opensymphony.module.propertyset.PropertySet;
28 import com.opensymphony.workflow.InvalidInputException;
29 import com.opensymphony.workflow.Workflow;
30 import com.opensymphony.workflow.WorkflowException;
31 import com.opensymphony.workflow.loader.ActionDescriptor;
32 import com.opensymphony.workflow.loader.WorkflowDescriptor;
33 import com.opensymphony.workflow.query.WorkflowQuery;
34 /***
35 * WorkflowInstance represents a specific instance of a workflow. Therefore it has
36 * all the context information like what state it is in, what it's actions are etc.
37 *
38 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
39 * @version $Id: WorkflowInstance.java 535465 2007-05-05 06:58:06Z tv $
40 */
41 public class WorkflowInstance
42 {
43 /*** The workflow that this instance is a type of */
44 private Workflow workflow;
45
46 /*** The id of this workflow */
47 private long id;
48
49 /***
50 * @return long id of this workflow
51 */
52 public long getId()
53 {
54 return id;
55 }
56 /***
57 * Make sure that the user can't construct a workflow instance directly.
58 * Must use the service instead.
59 *
60 */
61 private WorkflowInstance()
62 {
63
64 }
65
66 /***
67 * Simple constructor to create a workflow instance with the workflow, and the id of an
68 * instance of the workflow.
69 * @param workflow The workflow this instance belongs to
70 * @param id The id of this workflow instance
71 */
72 public WorkflowInstance(Workflow workflow, long id)
73 {
74 this.workflow = workflow;
75 this.id = id;
76 }
77 /***
78 * @param String workflowName The name of the workflow to be checked
79 * @param int initialStep The initial step I want to check
80 * @return boolean whether we can initialize at this step or not
81 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
82 */
83 public boolean canInitialize(String workflowName, int initialStep)
84 throws WorkflowException
85 {
86 return workflow.canInitialize(workflowName, initialStep);
87 }
88 /***
89 * Whether you can modify the entry state of this workflow instance
90 *
91 * @param int step The step to check
92 * @return Whether we can modify the entry state
93 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
94 */
95 public boolean canModifyEntryState(int step) throws WorkflowException
96 {
97 return workflow.canModifyEntryState(getId(), step);
98 }
99 /***
100 * Modify the state of the specified workflow instance.
101 * @param entryState the new state to change the workflow instance to.
102 * @throws com.opensymphony.workflow.WorkflowException If a problem occurs
103 */
104 public void changeEntryState(int entryState) throws WorkflowException
105 {
106 workflow.changeEntryState(getId(), entryState);
107 }
108 /***
109 * Perform an action on the the workflow instance.
110 * @param actionId The action id to perform (action id's are listed in the workflow descriptor).
111 * @param inputs The inputs to the workflow instance.
112 * @throws InvalidInputException if a validator is specified and an input is invalid.
113 * @throws com.opensymphony.workflow.WorkflowException If a problem occurs
114 */
115 public void doAction(int actionId, Map inputs)
116 throws InvalidInputException, WorkflowException
117 {
118 workflow.doAction(getId(), actionId, inputs);
119 }
120 /***
121 * Returns whether another workflow equals this workflow. Doesn't compare
122 * the id's.
123 * @see java.lang.Object#equals(java.lang.Object)
124 */
125 public boolean equals(Object obj)
126 {
127 return workflow.equals(obj);
128 }
129 /***
130 * Executes a special trigger-function using the context of the workflow instance.
131 *
132 * @param triggerId The id of the special trigger-function
133 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
134 */
135 public void executeTriggerFunction(int triggerId) throws WorkflowException
136 {
137 workflow.executeTriggerFunction(getId(), triggerId);
138 }
139 /***
140 * Get the available actions for the workflow instance.
141 * @param inputs The inputs map to pass on to conditions
142 * @return An array of action id's that can be performed on the specified entry
143 * @throws IllegalArgumentException if the specified id does not exist, or if its workflow
144 * descriptor is no longer available or has become invalid.
145 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
146 */
147 public int[] getAvailableActions(Map inputs) throws WorkflowException
148 {
149 return workflow.getAvailableActions(getId(), inputs);
150 }
151 /***
152 * Returns a Collection of Step objects that are the current steps of the workflow instance.
153 *
154 * @return The steps that the workflow instance is currently in.
155 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
156 */
157 public List getCurrentSteps() throws WorkflowException
158 {
159 return workflow.getCurrentSteps(getId());
160 }
161 /***
162 * Return the state of the workflow instance.
163 * @return int The state id of the specified workflow
164 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
165 */
166 public int getEntryState() throws WorkflowException
167 {
168 return workflow.getEntryState(getId());
169 }
170 /***
171 * Returns a list of all steps that are completed for the workflow instance.
172 *
173 * @return a List of Steps
174 * @see com.opensymphony.workflow.spi.Step
175 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
176 */
177 public List getHistorySteps() throws WorkflowException
178 {
179 return workflow.getHistorySteps(getId());
180 }
181 /***
182 * Get the PropertySet for the workflow instance.
183 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
184 */
185 public PropertySet getPropertySet() throws WorkflowException
186 {
187 return workflow.getPropertySet(getId());
188 }
189 /***
190 * Get a collection (Strings) of currently defined permissions for the workflow instance.
191 * @return A List of permissions specified currently (a permission is a string name).
192 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
193 */
194 public List getSecurityPermissions() throws WorkflowException
195 {
196 return workflow.getSecurityPermissions(getId());
197 }
198 /***
199 * Get the workflow descriptor for the workflow instance.
200 *
201 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
202 */
203 public WorkflowDescriptor getWorkflowDescriptor() throws WorkflowException
204 {
205 return workflow.getWorkflowDescriptor(getWorkflowName());
206 }
207 /***
208 * Get the name of the specified workflow instance.
209 * @return The name
210 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
211 */
212 public String getWorkflowName() throws WorkflowException
213 {
214 return workflow.getWorkflowName(getId());
215 }
216 /***
217 * @see java.lang.Object#hashCode()
218 */
219 public int hashCode()
220 {
221 return workflow.hashCode();
222 }
223 /***
224 * Execute a workflow query and returns the list of workflows
225 * @param query The workflow query
226 * @return a List of workflows
227 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
228 */
229 public List query(WorkflowQuery query) throws WorkflowException
230 {
231 return workflow.query(query);
232 }
233 /***
234 * @see java.lang.Object#toString()
235 */
236 public String toString()
237 {
238 return workflow.toString();
239 }
240 /***
241 * Get the available actions for the workflow instance.
242 * @return An list of actions that can be performed on the specified entry
243 * @throws IllegalArgumentException if the specified id does not exist, or if its workflow
244 * descriptor is no longer available or has become invalid.
245 * @throws com.opensymphony.workflow.WorkflowException If there is a problem
246 */
247 public List getAllAvailableActions() throws WorkflowException
248 {
249 List actions = new ArrayList();
250 int actionIds[] =
251 workflow.getAvailableActions(getId(), Collections.EMPTY_MAP);
252 for (int i = 0; i < actionIds.length; i++)
253 {
254 ActionDescriptor action =
255 getWorkflowDescriptor().getAction(actionIds[i]);
256 actions.add(action);
257 }
258 return actions;
259 }
260 }