KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > spi > ejb > WorkflowStoreSessionEJB


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.spi.ejb;
6
7 import com.opensymphony.workflow.StoreException;
8 import com.opensymphony.workflow.query.WorkflowExpressionQuery;
9 import com.opensymphony.workflow.query.WorkflowQuery;
10 import com.opensymphony.workflow.spi.SimpleStep;
11 import com.opensymphony.workflow.spi.SimpleWorkflowEntry;
12 import com.opensymphony.workflow.spi.Step;
13 import com.opensymphony.workflow.spi.WorkflowEntry;
14
15 import java.sql.Timestamp JavaDoc;
16
17 import java.util.*;
18
19 import javax.ejb.SessionBean JavaDoc;
20
21
22 /**
23  * @ejb.bean
24  * type="Stateless"
25  * name="WorkflowStore"
26  * view-type="remote"
27  * transaction-type="Container"
28  *
29  * @ejb.ejb-ref
30  * ejb-name="HistoryStepPrev"
31  * view-type="local"
32  *
33  * @ejb.ejb-ref
34  * ejb-name="CurrentStepPrev"
35  * view-type="local"
36  *
37  * @ejb.ejb-ref
38  * ejb-name="CurrentStep"
39  * view-type="local"
40  *
41  * @ejb.ejb-ref
42  * ejb-name="HistoryStep"
43  * view-type="local"
44  *
45  * @ejb.ejb-ref
46  * ejb-name="WorkflowEntry"
47  * view-type="local"
48  *
49  * @ejb.permission unchecked="true"
50  * @ejb.transaction type="Supports"
51  *
52  * @author <a HREF="mailto:hani@formicary.net">Hani Suleiman</a>
53  * @version $Revision: 1.3 $
54  *
55  * Date: Apr 7, 2003
56  * Time: 10:57:28 PM
57  */

58 public abstract class WorkflowStoreSessionEJB implements SessionBean JavaDoc {
59     //~ Methods ////////////////////////////////////////////////////////////////
60

61     /**
62      * @ejb.interface-method
63      */

64     public void setEntryState(long entryId, int state) throws StoreException {
65         try {
66             WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().findByPrimaryKey(new Long JavaDoc(entryId));
67             entry.setState(state);
68         } catch (Exception JavaDoc e) {
69             throw new StoreException("Could not find workflow instance #" + entryId, e);
70         }
71     }
72
73     /**
74      * @ejb.interface-method
75      */

76     public Step createCurrentStep(long entryId, int stepId, String JavaDoc owner, Date startDate, Date dueDate, String JavaDoc status, long[] previousIds) throws StoreException {
77         try {
78             WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().findByPrimaryKey(new Long JavaDoc(entryId));
79             entry.setState(WorkflowEntry.ACTIVATED);
80
81             Timestamp JavaDoc realDueDate = null;
82
83             if (dueDate != null) {
84                 realDueDate = new Timestamp JavaDoc(dueDate.getTime());
85             }
86
87             CurrentStepLocal step = CurrentStepHomeFactory.getLocalHome().create(entryId, stepId, owner, new Timestamp JavaDoc(startDate.getTime()), realDueDate, status);
88
89             long id = step.getId().longValue();
90
91             for (int i = 0; i < previousIds.length; i++) {
92                 long previousId = previousIds[i];
93                 CurrentStepPrevHomeFactory.getLocalHome().create(id, previousId);
94             }
95
96             return new SimpleStep(id, entryId, stepId, 0, owner, startDate, dueDate, null, status, previousIds, null);
97         } catch (Exception JavaDoc e) {
98             throw new StoreException("Could not create new current step for workflow instance #" + entryId + " step #" + stepId + ":" + e, e);
99         }
100     }
101
102     /**
103      * @ejb.interface-method
104      */

105     public WorkflowEntry createEntry(String JavaDoc workflowName) throws StoreException {
106         try {
107             WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().create(workflowName);
108
109             return new SimpleWorkflowEntry(entry.getId().longValue(), entry.getWorkflowName(), WorkflowEntry.CREATED);
110         } catch (Exception JavaDoc e) {
111             throw new StoreException("Could not create new workflow instance", e);
112         }
113     }
114
115     /**
116      * @ejb.interface-method
117      */

118     public List findCurrentSteps(long entryId) throws StoreException {
119         try {
120             Collection results = CurrentStepHomeFactory.getLocalHome().findByEntryId(entryId);
121             TreeSet set = new TreeSet(new StepComparator());
122
123             for (Iterator iterator = results.iterator(); iterator.hasNext();) {
124                 CurrentStepLocal stepLocal = (CurrentStepLocal) iterator.next();
125
126                 long id = stepLocal.getId().longValue();
127                 Collection prevSteps = CurrentStepPrevHomeFactory.getLocalHome().findByStepId(id);
128                 long[] prevIds = new long[prevSteps.size()];
129                 int i = 0;
130
131                 for (Iterator iterator2 = prevSteps.iterator();
132                         iterator2.hasNext();) {
133                     CurrentStepPrevLocal stepPrev = (CurrentStepPrevLocal) iterator2.next();
134                     prevIds[i] = stepPrev.getPreviousId().longValue();
135                     i++;
136                 }
137
138                 SimpleStep step = new SimpleStep(id, stepLocal.getEntryId(), stepLocal.getStepId(), stepLocal.getActionId(), stepLocal.getOwner(), stepLocal.getStartDate(), stepLocal.getDueDate(), stepLocal.getFinishDate(), stepLocal.getStatus(), prevIds, stepLocal.getCaller());
139                 set.add(step);
140             }
141
142             return new ArrayList(set);
143         } catch (Exception JavaDoc e) {
144             throw new StoreException("Error in findCurrentSteps", e);
145         }
146     }
147
148     /**
149      * @ejb.interface-method
150      */

151     public WorkflowEntry findEntry(long entryId) throws StoreException {
152         try {
153             WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().findByPrimaryKey(new Long JavaDoc(entryId));
154
155             return new SimpleWorkflowEntry(entry.getId().longValue(), entry.getWorkflowName(), entry.getState());
156         } catch (Exception JavaDoc e) {
157             throw new StoreException("Could not find workflow instance #" + entryId, e);
158         }
159     }
160
161     /**
162      * @ejb.interface-method
163      */

164     public List findHistorySteps(long entryId) throws StoreException {
165         try {
166             Collection results = HistoryStepHomeFactory.getLocalHome().findByEntryId(entryId);
167             TreeSet set = new TreeSet(new StepComparator());
168
169             for (Iterator iterator = results.iterator(); iterator.hasNext();) {
170                 HistoryStepLocal stepRemote = (HistoryStepLocal) iterator.next();
171
172                 long id = stepRemote.getId().longValue();
173                 Collection prevSteps = HistoryStepPrevHomeFactory.getLocalHome().findByStepId(id);
174                 long[] prevIds = new long[prevSteps.size()];
175                 int i = 0;
176
177                 for (Iterator iterator2 = prevSteps.iterator();
178                         iterator2.hasNext();) {
179                     HistoryStepPrevLocal stepPrev = (HistoryStepPrevLocal) iterator2.next();
180                     prevIds[i] = stepPrev.getPreviousId().longValue();
181                     i++;
182                 }
183
184                 SimpleStep step = new SimpleStep(stepRemote.getId().longValue(), stepRemote.getEntryId(), stepRemote.getStepId(), stepRemote.getActionId(), stepRemote.getOwner(), stepRemote.getStartDate(), stepRemote.getDueDate(), stepRemote.getFinishDate(), stepRemote.getStatus(), prevIds, stepRemote.getCaller());
185                 set.add(step);
186             }
187
188             return new ArrayList(set);
189         } catch (Exception JavaDoc e) {
190             throw new StoreException("Could not find history steps for workflow instance #" + entryId, e);
191         }
192     }
193
194     /**
195      * @ejb.interface-method
196      */

197     public Step markFinished(Step step, int actionId, Date finishDate, String JavaDoc status, String JavaDoc caller) throws StoreException {
198         try {
199             CurrentStepLocal currentStep = CurrentStepHomeFactory.getLocalHome().findByPrimaryKey(new Long JavaDoc(step.getId()));
200             currentStep.setActionId(actionId);
201             currentStep.setFinishDate(new Timestamp JavaDoc(finishDate.getTime()));
202             currentStep.setStatus(status);
203             currentStep.setCaller(caller);
204
205             SimpleStep theStep = (SimpleStep) step;
206             theStep.setActionId(actionId);
207             theStep.setFinishDate(finishDate);
208             theStep.setStatus(status);
209             theStep.setCaller(caller);
210
211             return theStep;
212         } catch (Exception JavaDoc e) {
213             throw new StoreException("Could not mark step finished for #" + step.getEntryId(), e);
214         }
215     }
216
217     /**
218      * @ejb.interface-method
219      */

220     public void moveToHistory(Step step) throws StoreException {
221         Long JavaDoc id = new Long JavaDoc(step.getId());
222
223         try {
224             CurrentStepLocal currentStep = CurrentStepHomeFactory.getLocalHome().findByPrimaryKey(id);
225             long[] previousIds = step.getPreviousStepIds();
226             Timestamp JavaDoc realDueDate = null;
227
228             if (step.getDueDate() != null) {
229                 realDueDate = new Timestamp JavaDoc(step.getDueDate().getTime());
230             }
231
232             Timestamp JavaDoc finishedDate = null;
233
234             if (step.getFinishDate() != null) {
235                 finishedDate = new Timestamp JavaDoc(step.getFinishDate().getTime());
236             }
237
238             HistoryStepLocalHome historyHome = HistoryStepHomeFactory.getLocalHome();
239             historyHome.create(id.longValue(), step.getEntryId(), step.getStepId(), step.getActionId(), step.getOwner(), new Timestamp JavaDoc(step.getStartDate().getTime()), realDueDate, finishedDate, step.getStatus(), step.getCaller());
240
241             for (int i = 0; i < previousIds.length; i++) {
242                 long previousId = previousIds[i];
243                 HistoryStepPrevHomeFactory.getLocalHome().create(id.longValue(), previousId);
244             }
245
246             Collection oldPrevSteps = CurrentStepPrevHomeFactory.getLocalHome().findByStepId(id.longValue());
247
248             for (Iterator iterator = oldPrevSteps.iterator();
249                     iterator.hasNext();) {
250                 CurrentStepPrevLocal oldPrevStep = (CurrentStepPrevLocal) iterator.next();
251                 oldPrevStep.remove();
252             }
253
254             currentStep.remove();
255         } catch (Exception JavaDoc e) {
256             throw new StoreException("Could not move step to history for workflow instance #" + id, e);
257         }
258     }
259
260     /**
261      * @ejb.interface-method
262      */

263     public List query(WorkflowQuery query) throws StoreException {
264         // not implemented
265
throw new StoreException("EJB store does not support queries");
266     }
267
268     /**
269      * @ejb.interface-method
270      */

271     public List query(WorkflowExpressionQuery query) throws StoreException {
272         // not implemented
273
throw new StoreException("EJB store does not support queries");
274     }
275 }
276
Popular Tags