KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

10 package com.opensymphony.workflow.spi.prevayler;
11
12 import com.opensymphony.module.propertyset.PropertySet;
13 import com.opensymphony.module.propertyset.PropertySetManager;
14
15 import com.opensymphony.workflow.StoreException;
16 import com.opensymphony.workflow.query.WorkflowExpressionQuery;
17 import com.opensymphony.workflow.query.WorkflowQuery;
18 import com.opensymphony.workflow.spi.SimpleStep;
19 import com.opensymphony.workflow.spi.SimpleWorkflowEntry;
20 import com.opensymphony.workflow.spi.Step;
21 import com.opensymphony.workflow.spi.WorkflowEntry;
22 import com.opensymphony.workflow.spi.WorkflowStore;
23
24 import java.io.Serializable JavaDoc;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  *
36  *
37  *
38  * @author Christopher Farnham chris.farnham@wrycan.com
39  **/

40 public class WorkflowSystem implements WorkflowStore, Serializable JavaDoc {
41     //~ Instance fields ////////////////////////////////////////////////////////
42

43     private HashMap JavaDoc _currentStepsCache = new HashMap JavaDoc();
44     private HashMap JavaDoc _entryCache = new HashMap JavaDoc();
45     private HashMap JavaDoc _historyStepsCache = new HashMap JavaDoc();
46     private HashMap JavaDoc _propertySetCache = new HashMap JavaDoc();
47     private transient QueryLogic _queryLogic = new QueryLogic(this);
48     private long _globalEntryId = 1;
49     private long _globalStepId = 1;
50
51     //~ Constructors ///////////////////////////////////////////////////////////
52

53     /**
54      *
55      */

56     public WorkflowSystem() {
57         super();
58     }
59
60     //~ Methods ////////////////////////////////////////////////////////////////
61

62     /* (non-Javadoc)
63      * @see com.opensymphony.workflow.spi.WorkflowStore#setEntryState(long, int)
64      */

65     public void setEntryState(long entryId, int state) throws StoreException {
66         SimpleWorkflowEntry entry = (SimpleWorkflowEntry) findEntry(entryId);
67         entry.setState(state);
68     }
69
70     /* (non-Javadoc)
71      * @see com.opensymphony.workflow.spi.WorkflowStore#getPropertySet(long)
72      */

73     public PropertySet getPropertySet(long entryId) throws StoreException {
74         PropertySet ps = (PropertySet) _propertySetCache.get(new Long JavaDoc(entryId));
75
76         if (ps == null) {
77             ps = PropertySetManager.getInstance("serializable", null);
78             _propertySetCache.put(new Long JavaDoc(entryId), ps);
79         }
80
81         return ps;
82     }
83
84     /* (non-Javadoc)
85      * @see com.opensymphony.workflow.spi.WorkflowStore#createCurrentStep(long, int, java.lang.String, java.util.Date, java.util.Date, java.lang.String, long[])
86      */

87     public Step createCurrentStep(long entryId, int stepId, String JavaDoc owner, Date JavaDoc startDate, Date JavaDoc dueDate, String JavaDoc status, long[] previousIds) throws StoreException {
88         long id = _globalStepId++;
89         SimpleStep step = new SimpleStep(id, entryId, stepId, 0, owner, startDate, dueDate, null, status, previousIds, null);
90
91         List JavaDoc currentSteps = (List JavaDoc) _currentStepsCache.get(new Long JavaDoc(entryId));
92
93         if (currentSteps == null) {
94             currentSteps = new ArrayList JavaDoc();
95             _currentStepsCache.put(new Long JavaDoc(entryId), currentSteps);
96         }
97
98         currentSteps.add(step);
99
100         return step;
101     }
102
103     /* (non-Javadoc)
104      * @see com.opensymphony.workflow.spi.WorkflowStore#createEntry(java.lang.String)
105      */

106     public WorkflowEntry createEntry(String JavaDoc workflowName) throws StoreException {
107         long id = _globalEntryId++;
108         SimpleWorkflowEntry entry = new SimpleWorkflowEntry(id, workflowName, WorkflowEntry.CREATED);
109         _entryCache.put(new Long JavaDoc(id), entry);
110
111         return entry;
112     }
113
114     /* (non-Javadoc)
115      * @see com.opensymphony.workflow.spi.WorkflowStore#findCurrentSteps(long)
116      */

117     public List JavaDoc findCurrentSteps(long entryId) throws StoreException {
118         List JavaDoc currentSteps = (List JavaDoc) _currentStepsCache.get(new Long JavaDoc(entryId));
119
120         if (currentSteps == null) {
121             currentSteps = new ArrayList JavaDoc();
122             _currentStepsCache.put(new Long JavaDoc(entryId), currentSteps);
123         }
124
125         return currentSteps;
126     }
127
128     /* (non-Javadoc)
129      * @see com.opensymphony.workflow.spi.WorkflowStore#findEntry(long)
130      */

131     public WorkflowEntry findEntry(long entryId) throws StoreException {
132         return (WorkflowEntry) _entryCache.get(new Long JavaDoc(entryId));
133     }
134
135     /* (non-Javadoc)
136      * @see com.opensymphony.workflow.spi.WorkflowStore#findHistorySteps(long)
137      */

138     public List JavaDoc findHistorySteps(long entryId) throws StoreException {
139         List JavaDoc historySteps = (List JavaDoc) _historyStepsCache.get(new Long JavaDoc(entryId));
140
141         if (historySteps == null) {
142             historySteps = new ArrayList JavaDoc();
143             _historyStepsCache.put(new Long JavaDoc(entryId), historySteps);
144         }
145
146         return historySteps;
147     }
148
149     /* (non-Javadoc)
150      * @see com.opensymphony.workflow.spi.WorkflowStore#init(java.util.Map)
151      */

152     public void init(Map JavaDoc props) throws StoreException {
153     }
154
155     /* (non-Javadoc)
156      * @see com.opensymphony.workflow.spi.WorkflowStore#markFinished(com.opensymphony.workflow.spi.Step, int, java.util.Date, java.lang.String, java.lang.String)
157      */

158     public Step markFinished(Step step, int actionId, Date JavaDoc finishDate, String JavaDoc status, String JavaDoc caller) throws StoreException {
159         List JavaDoc currentSteps = (List JavaDoc) _currentStepsCache.get(new Long JavaDoc(step.getEntryId()));
160
161         for (Iterator JavaDoc iterator = currentSteps.iterator(); iterator.hasNext();) {
162             SimpleStep theStep = (SimpleStep) iterator.next();
163
164             if (theStep.getId() == step.getId()) {
165                 theStep.setStatus(status);
166                 theStep.setActionId(actionId);
167                 theStep.setFinishDate(finishDate);
168                 theStep.setCaller(caller);
169
170                 return theStep;
171             }
172         }
173
174         return null;
175     }
176
177     /* (non-Javadoc)
178      * @see com.opensymphony.workflow.spi.WorkflowStore#moveToHistory(com.opensymphony.workflow.spi.Step)
179      */

180     public void moveToHistory(Step step) throws StoreException {
181         List JavaDoc currentSteps = (List JavaDoc) _currentStepsCache.get(new Long JavaDoc(step.getEntryId()));
182
183         List JavaDoc historySteps = (List JavaDoc) _historyStepsCache.get(new Long JavaDoc(step.getEntryId()));
184
185         if (historySteps == null) {
186             historySteps = new ArrayList JavaDoc();
187             _historyStepsCache.put(new Long JavaDoc(step.getEntryId()), historySteps);
188         }
189
190         SimpleStep simpleStep = (SimpleStep) step;
191
192         for (Iterator JavaDoc iterator = currentSteps.iterator(); iterator.hasNext();) {
193             Step currentStep = (Step) iterator.next();
194
195             if (simpleStep.getId() == currentStep.getId()) {
196                 iterator.remove();
197                 historySteps.add(simpleStep);
198
199                 break;
200             }
201         }
202     }
203
204     /* (non-Javadoc)
205      * @see com.opensymphony.workflow.spi.WorkflowStore#query(com.opensymphony.workflow.query.WorkflowQuery)
206      */

207     public List JavaDoc query(WorkflowQuery query) throws StoreException {
208         ArrayList JavaDoc results = new ArrayList JavaDoc();
209
210         for (Iterator JavaDoc iterator = _entryCache.entrySet().iterator();
211                 iterator.hasNext();) {
212             Map.Entry JavaDoc mapEntry = (Map.Entry JavaDoc) iterator.next();
213             Long JavaDoc entryId = (Long JavaDoc) mapEntry.getKey();
214
215             if (_queryLogic.query(entryId, query)) {
216                 results.add(entryId);
217             }
218         }
219
220         return results;
221     }
222
223     /* (non-Javadoc)
224      * @see com.opensymphony.workflow.spi.WorkflowStore#query(com.opensymphony.workflow.query.WorkflowExpressionQuery)
225      */

226     public List JavaDoc query(WorkflowExpressionQuery query) throws StoreException {
227         ArrayList JavaDoc results = new ArrayList JavaDoc();
228
229         for (Iterator JavaDoc iterator = _entryCache.entrySet().iterator();
230                 iterator.hasNext();) {
231             Map.Entry JavaDoc mapEntry = (Map.Entry JavaDoc) iterator.next();
232             Long JavaDoc entryId = (Long JavaDoc) mapEntry.getKey();
233
234             if (_queryLogic.query(entryId, query)) {
235                 results.add(entryId);
236             }
237         }
238
239         return results;
240     }
241 }
242
Popular Tags