KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > spi > prevayler > PrevaylerWorkflowStore


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

5 /*
6  * Created on Feb 11, 2004
7  *
8  *
9  */

10 package com.opensymphony.workflow.spi.prevayler;
11
12 import com.opensymphony.module.propertyset.PropertySet;
13
14 import com.opensymphony.workflow.StoreException;
15 import com.opensymphony.workflow.query.WorkflowExpressionQuery;
16 import com.opensymphony.workflow.query.WorkflowQuery;
17 import com.opensymphony.workflow.spi.Step;
18 import com.opensymphony.workflow.spi.WorkflowEntry;
19 import com.opensymphony.workflow.spi.WorkflowStore;
20
21 import org.prevayler.Prevayler;
22 import org.prevayler.PrevaylerFactory;
23 import org.prevayler.Query;
24 import org.prevayler.Transaction;
25 import org.prevayler.TransactionWithQuery;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 import java.util.Date JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34
35 /**
36  *
37  * This is a WorkflowStore implementation which uses Prevaylence as its datastore.
38  * See http://www.prevayler.org/wiki.jsp
39  *
40  * It creates a transient or non-transient store depending upon the properties set.
41  *
42  * @author Christopher Farnham
43  **/

44 public class PrevaylerWorkflowStore implements WorkflowStore, Serializable JavaDoc {
45     //~ Instance fields ////////////////////////////////////////////////////////
46

47     private transient Prevayler _prevayler = null;
48     private transient String JavaDoc _prevalenceBase;
49     private WorkflowStore _store = null;
50
51     //~ Constructors ///////////////////////////////////////////////////////////
52

53     public PrevaylerWorkflowStore() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
54         this("WorkflowPrevaylenceBase");
55     }
56
57     public PrevaylerWorkflowStore(String JavaDoc prevalenceBase) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
58         super();
59         _prevalenceBase = prevalenceBase;
60         _store = new WorkflowSystem();
61     }
62
63     //~ Methods ////////////////////////////////////////////////////////////////
64

65     /* (non-Javadoc)
66      * @see com.opensymphony.workflow.spi.WorkflowStore#setEntryState(long, int)
67      */

68     public void setEntryState(long entryId, int state) throws StoreException {
69         Object JavaDoc[] o = {new Long JavaDoc(entryId), new Integer JavaDoc(state)};
70
71         try {
72             _prevayler.execute(new TransactionImpl(o) {
73                     public void execute(WorkflowSystem store) {
74                         Object JavaDoc[] o = (Object JavaDoc[]) _object;
75                         long entryId = ((Long JavaDoc) o[0]).longValue();
76                         int state = ((Integer JavaDoc) o[1]).intValue();
77
78                         try {
79                             store.setEntryState(entryId, state);
80                         } catch (StoreException e) {
81                             throw new RuntimeException JavaDoc(e);
82                         }
83                     }
84                 });
85         } catch (Exception JavaDoc e) {
86             if (e.getCause() instanceof StoreException) {
87                 throw ((StoreException) e.getCause());
88             } else {
89                 throw new StoreException(e);
90             }
91         }
92     }
93
94     /* (non-Javadoc)
95      * @see com.opensymphony.workflow.spi.WorkflowStore#getPropertySet(long)
96      */

97     public PropertySet getPropertySet(long entryId) throws StoreException {
98         try {
99             return (PropertySet) _prevayler.execute(new TransactionWithQueryImpl(new Long JavaDoc(entryId)) {
100                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
101                         return store.getPropertySet(((Long JavaDoc) _object).longValue());
102                     }
103                 });
104         } catch (Exception JavaDoc e) {
105             throw new StoreException(e);
106         }
107     }
108
109     /* (non-Javadoc)
110      * @see com.opensymphony.workflow.spi.WorkflowStore#createCurrentStep(long, int, java.lang.String, java.util.Date, java.util.Date, java.lang.String, long[])
111      */

112     public Step createCurrentStep(long entryId, int stepId, String JavaDoc owner, Date JavaDoc startDate, Date JavaDoc dueDate, String JavaDoc status, long[] previousIds) throws StoreException {
113         Object JavaDoc[] oArray = {
114             new Long JavaDoc(entryId), new Integer JavaDoc(stepId), owner, startDate, dueDate,
115             status, previousIds
116         };
117
118         try {
119             return (Step) _prevayler.execute(new TransactionWithQueryImpl(oArray) {
120                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
121                         Object JavaDoc[] o = (Object JavaDoc[]) _object;
122                         long entryId = ((Long JavaDoc) o[0]).longValue();
123                         int stepId = ((Integer JavaDoc) o[1]).intValue();
124                         String JavaDoc owner = (String JavaDoc) o[2];
125                         Date JavaDoc startDate = (Date JavaDoc) o[3];
126                         Date JavaDoc dueDate = (Date JavaDoc) o[4];
127                         String JavaDoc status = (String JavaDoc) o[5];
128                         long[] previousIds = (long[]) o[6];
129
130                         return store.createCurrentStep(entryId, stepId, owner, startDate, dueDate, status, previousIds);
131                     }
132                 });
133         } catch (Exception JavaDoc e) {
134             throw new StoreException(e);
135         }
136     }
137
138     /* (non-Javadoc)
139      * @see com.opensymphony.workflow.spi.WorkflowStore#createEntry(java.lang.String)
140      */

141     public WorkflowEntry createEntry(String JavaDoc workflowName) throws StoreException {
142         try {
143             return (WorkflowEntry) _prevayler.execute(new TransactionWithQueryImpl(workflowName) {
144                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
145                         return store.createEntry(((String JavaDoc) _object));
146                     }
147                 });
148         } catch (Exception JavaDoc e) {
149             throw new StoreException(e);
150         }
151     }
152
153     /* (non-Javadoc)
154      * @see com.opensymphony.workflow.spi.WorkflowStore#findCurrentSteps(long)
155      */

156     public List JavaDoc findCurrentSteps(long entryId) throws StoreException {
157         try {
158             return (List JavaDoc) _prevayler.execute(new TransactionWithQueryImpl(new Long JavaDoc(entryId)) {
159                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
160                         return store.findCurrentSteps(((Long JavaDoc) _object).longValue());
161                     }
162                 });
163         } catch (Exception JavaDoc e) {
164             throw new StoreException(e);
165         }
166     }
167
168     /* (non-Javadoc)
169      * @see com.opensymphony.workflow.spi.WorkflowStore#findEntry(long)
170      */

171     public WorkflowEntry findEntry(long entryId) throws StoreException {
172         try {
173             return (WorkflowEntry) _prevayler.execute(new TransactionWithQueryImpl(new Long JavaDoc(entryId)) {
174                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
175                         return store.findEntry(((Long JavaDoc) _object).longValue());
176                     }
177                 });
178         } catch (Exception JavaDoc e) {
179             throw new StoreException(e);
180         }
181     }
182
183     /* (non-Javadoc)
184      * @see com.opensymphony.workflow.spi.WorkflowStore#findHistorySteps(long)
185      */

186     public List JavaDoc findHistorySteps(long entryId) throws StoreException {
187         try {
188             return (List JavaDoc) _prevayler.execute(new TransactionWithQueryImpl(new Long JavaDoc(entryId)) {
189                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
190                         return store.findHistorySteps(((Long JavaDoc) _object).longValue());
191                     }
192                 });
193         } catch (Exception JavaDoc e) {
194             throw new StoreException(e);
195         }
196     }
197
198     /* (non-Javadoc)
199      * @see com.opensymphony.workflow.spi.WorkflowStore#init(java.util.Map)
200      */

201     public void init(Map JavaDoc props) throws StoreException {
202         _store.init(props);
203
204         boolean isTransient = false;
205         String JavaDoc pBaseKey = "base";
206
207         if (props.containsKey(pBaseKey)) {
208             _prevalenceBase = (String JavaDoc) props.get(pBaseKey);
209         }
210
211         String JavaDoc transientKey = "transient";
212
213         if (props.containsKey(transientKey)) {
214             String JavaDoc value = (String JavaDoc) props.get(transientKey);
215
216             if (value.equalsIgnoreCase("true")) {
217                 isTransient = true;
218             }
219         }
220
221         try {
222             initializePrevaylenceSystem(_prevalenceBase, isTransient);
223         } catch (Exception JavaDoc e) {
224             throw new StoreException(e);
225         }
226     }
227
228     /* (non-Javadoc)
229      * @see com.opensymphony.workflow.spi.WorkflowStore#markFinished(com.opensymphony.workflow.spi.Step, int, java.util.Date, java.lang.String, java.lang.String)
230      */

231     public Step markFinished(Step step, int actionId, Date JavaDoc finishDate, String JavaDoc status, String JavaDoc caller) throws StoreException {
232         Object JavaDoc[] oArray = {
233             step, new Integer JavaDoc(actionId), finishDate, status, caller
234         };
235
236         try {
237             return (Step) _prevayler.execute(new TransactionWithQueryImpl(oArray) {
238                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
239                         Object JavaDoc[] o = (Object JavaDoc[]) _object;
240                         Step step = (Step) o[0];
241                         int actionId = ((Integer JavaDoc) o[1]).intValue();
242                         Date JavaDoc finishDate = (Date JavaDoc) o[2];
243                         String JavaDoc status = (String JavaDoc) o[3];
244                         String JavaDoc caller = (String JavaDoc) o[4];
245
246                         return store.markFinished(step, actionId, finishDate, status, caller);
247                     }
248                 });
249         } catch (Exception JavaDoc e) {
250             throw new StoreException(e);
251         }
252     }
253
254     /* (non-Javadoc)
255      * @see com.opensymphony.workflow.spi.WorkflowStore#moveToHistory(com.opensymphony.workflow.spi.Step)
256      */

257     public void moveToHistory(Step step) throws StoreException {
258         try {
259             _prevayler.execute(new TransactionImpl(step) {
260                     public void execute(WorkflowSystem store) throws StoreException {
261                         store.moveToHistory(((Step) _object));
262                     }
263                 });
264         } catch (Exception JavaDoc e) {
265             throw new StoreException(e);
266         }
267     }
268
269     /* (non-Javadoc)
270      * @see com.opensymphony.workflow.spi.WorkflowStore#query(com.opensymphony.workflow.query.WorkflowQuery)
271      */

272     public List JavaDoc query(WorkflowQuery query) throws StoreException {
273         try {
274             return (List JavaDoc) _prevayler.execute(new QueryImpl(query) {
275                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
276                         return store.query(((WorkflowQuery) _object));
277                     }
278                 });
279         } catch (Exception JavaDoc e) {
280             throw new StoreException(e);
281         }
282     }
283
284     /* (non-Javadoc)
285      * @see com.opensymphony.workflow.spi.WorkflowStore#query(com.opensymphony.workflow.query.WorkflowExpressionQuery)
286      */

287     public List JavaDoc query(WorkflowExpressionQuery query) throws StoreException {
288         try {
289             return (List JavaDoc) _prevayler.execute(new QueryImpl(query) {
290                     public Object JavaDoc execute(WorkflowSystem store) throws StoreException {
291                         return store.query(((WorkflowExpressionQuery) _object));
292                     }
293                 });
294         } catch (Exception JavaDoc e) {
295             throw new StoreException(e);
296         }
297     }
298
299     /**
300      * @param prevalenceBase
301      */

302     private void initializePrevaylenceSystem(String JavaDoc prevalenceBase, boolean isTransient) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
303         if (isTransient) {
304             _prevayler = PrevaylerFactory.createTransientPrevayler(((Serializable JavaDoc) _store));
305         } else {
306             _prevayler = PrevaylerFactory.createPrevayler(((Serializable JavaDoc) _store), prevalenceBase);
307         }
308     }
309
310     //~ Inner Classes //////////////////////////////////////////////////////////
311

312     private class ObjectActioner implements Serializable JavaDoc {
313         protected Object JavaDoc _object = null;
314
315         public ObjectActioner() {
316         }
317
318         public ObjectActioner(Object JavaDoc object) {
319             _object = object;
320         }
321     }
322
323     private abstract class QueryImpl extends ObjectActioner implements Query {
324         public QueryImpl(Object JavaDoc object) {
325             super(object);
326         }
327
328         public abstract Object JavaDoc execute(WorkflowSystem store) throws StoreException;
329
330         public Object JavaDoc query(Object JavaDoc prevSystem, Date JavaDoc ignored) throws Exception JavaDoc {
331             return execute(((WorkflowSystem) prevSystem));
332         }
333     }
334
335     private abstract class TransactionImpl extends ObjectActioner implements Transaction {
336         public TransactionImpl(Object JavaDoc object) {
337             super(object);
338         }
339
340         public abstract void execute(WorkflowSystem store) throws StoreException;
341
342         public void executeOn(Object JavaDoc prevSystem, Date JavaDoc ignored) {
343             try {
344                 execute(((WorkflowSystem) prevSystem));
345             } catch (StoreException e) {
346                 throw new RuntimeException JavaDoc(e);
347             }
348         }
349     }
350
351     private abstract class TransactionWithQueryImpl extends ObjectActioner implements TransactionWithQuery {
352         public TransactionWithQueryImpl(Object JavaDoc object) {
353             super(object);
354         }
355
356         public abstract Object JavaDoc execute(WorkflowSystem store) throws StoreException;
357
358         public Object JavaDoc executeAndQuery(Object JavaDoc prevSystem, Date JavaDoc ignored) throws Exception JavaDoc {
359             return execute(((WorkflowSystem) prevSystem));
360         }
361     }
362 }
363
Popular Tags