001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: /*
006: * Created on Feb 12, 2004
007: *
008: *
009: */
010: package com.opensymphony.workflow.spi.prevayler;
011:
012: import com.opensymphony.module.propertyset.PropertySet;
013: import com.opensymphony.module.propertyset.PropertySetManager;
014:
015: import com.opensymphony.workflow.StoreException;
016: import com.opensymphony.workflow.query.WorkflowExpressionQuery;
017: import com.opensymphony.workflow.query.WorkflowQuery;
018: import com.opensymphony.workflow.spi.SimpleStep;
019: import com.opensymphony.workflow.spi.SimpleWorkflowEntry;
020: import com.opensymphony.workflow.spi.Step;
021: import com.opensymphony.workflow.spi.WorkflowEntry;
022: import com.opensymphony.workflow.spi.WorkflowStore;
023:
024: import java.io.Serializable;
025:
026: import java.util.ArrayList;
027: import java.util.Date;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: /**
034: *
035: *
036: *
037: * @author Christopher Farnham chris.farnham@wrycan.com
038: **/
039: public class WorkflowSystem implements WorkflowStore, Serializable {
040: //~ Instance fields ////////////////////////////////////////////////////////
041:
042: private HashMap _currentStepsCache = new HashMap();
043: private HashMap _entryCache = new HashMap();
044: private HashMap _historyStepsCache = new HashMap();
045: private HashMap _propertySetCache = new HashMap();
046: private transient QueryLogic _queryLogic = new QueryLogic(this );
047: private long _globalEntryId = 1;
048: private long _globalStepId = 1;
049:
050: //~ Constructors ///////////////////////////////////////////////////////////
051:
052: /**
053: *
054: */
055: public WorkflowSystem() {
056: super ();
057: }
058:
059: //~ Methods ////////////////////////////////////////////////////////////////
060:
061: /* (non-Javadoc)
062: * @see com.opensymphony.workflow.spi.WorkflowStore#setEntryState(long, int)
063: */
064: public void setEntryState(long entryId, int state)
065: throws StoreException {
066: SimpleWorkflowEntry entry = (SimpleWorkflowEntry) findEntry(entryId);
067: entry.setState(state);
068: }
069:
070: /* (non-Javadoc)
071: * @see com.opensymphony.workflow.spi.WorkflowStore#getPropertySet(long)
072: */
073: public PropertySet getPropertySet(long entryId)
074: throws StoreException {
075: PropertySet ps = (PropertySet) _propertySetCache.get(new Long(
076: entryId));
077:
078: if (ps == null) {
079: ps = PropertySetManager.getInstance("serializable", null);
080: _propertySetCache.put(new Long(entryId), ps);
081: }
082:
083: return ps;
084: }
085:
086: /* (non-Javadoc)
087: * @see com.opensymphony.workflow.spi.WorkflowStore#createCurrentStep(long, int, java.lang.String, java.util.Date, java.util.Date, java.lang.String, long[])
088: */
089: public Step createCurrentStep(long entryId, int stepId,
090: String owner, Date startDate, Date dueDate, String status,
091: long[] previousIds) throws StoreException {
092: long id = _globalStepId++;
093: SimpleStep step = new SimpleStep(id, entryId, stepId, 0, owner,
094: startDate, dueDate, null, status, previousIds, null);
095:
096: List currentSteps = (List) _currentStepsCache.get(new Long(
097: entryId));
098:
099: if (currentSteps == null) {
100: currentSteps = new ArrayList();
101: _currentStepsCache.put(new Long(entryId), currentSteps);
102: }
103:
104: currentSteps.add(step);
105:
106: return step;
107: }
108:
109: /* (non-Javadoc)
110: * @see com.opensymphony.workflow.spi.WorkflowStore#createEntry(java.lang.String)
111: */
112: public WorkflowEntry createEntry(String workflowName)
113: throws StoreException {
114: long id = _globalEntryId++;
115: SimpleWorkflowEntry entry = new SimpleWorkflowEntry(id,
116: workflowName, WorkflowEntry.CREATED);
117: _entryCache.put(new Long(id), entry);
118:
119: return entry;
120: }
121:
122: /* (non-Javadoc)
123: * @see com.opensymphony.workflow.spi.WorkflowStore#findCurrentSteps(long)
124: */
125: public List findCurrentSteps(long entryId) throws StoreException {
126: List currentSteps = (List) _currentStepsCache.get(new Long(
127: entryId));
128:
129: if (currentSteps == null) {
130: currentSteps = new ArrayList();
131: _currentStepsCache.put(new Long(entryId), currentSteps);
132: }
133:
134: return currentSteps;
135: }
136:
137: /* (non-Javadoc)
138: * @see com.opensymphony.workflow.spi.WorkflowStore#findEntry(long)
139: */
140: public WorkflowEntry findEntry(long entryId) throws StoreException {
141: return (WorkflowEntry) _entryCache.get(new Long(entryId));
142: }
143:
144: /* (non-Javadoc)
145: * @see com.opensymphony.workflow.spi.WorkflowStore#findHistorySteps(long)
146: */
147: public List findHistorySteps(long entryId) throws StoreException {
148: List historySteps = (List) _historyStepsCache.get(new Long(
149: entryId));
150:
151: if (historySteps == null) {
152: historySteps = new ArrayList();
153: _historyStepsCache.put(new Long(entryId), historySteps);
154: }
155:
156: return historySteps;
157: }
158:
159: /* (non-Javadoc)
160: * @see com.opensymphony.workflow.spi.WorkflowStore#init(java.util.Map)
161: */
162: public void init(Map props) throws StoreException {
163: }
164:
165: /* (non-Javadoc)
166: * @see com.opensymphony.workflow.spi.WorkflowStore#markFinished(com.opensymphony.workflow.spi.Step, int, java.util.Date, java.lang.String, java.lang.String)
167: */
168: public Step markFinished(Step step, int actionId, Date finishDate,
169: String status, String caller) throws StoreException {
170: List currentSteps = (List) _currentStepsCache.get(new Long(step
171: .getEntryId()));
172:
173: for (Iterator iterator = currentSteps.iterator(); iterator
174: .hasNext();) {
175: SimpleStep theStep = (SimpleStep) iterator.next();
176:
177: if (theStep.getId() == step.getId()) {
178: theStep.setStatus(status);
179: theStep.setActionId(actionId);
180: theStep.setFinishDate(finishDate);
181: theStep.setCaller(caller);
182:
183: return theStep;
184: }
185: }
186:
187: return null;
188: }
189:
190: /* (non-Javadoc)
191: * @see com.opensymphony.workflow.spi.WorkflowStore#moveToHistory(com.opensymphony.workflow.spi.Step)
192: */
193: public void moveToHistory(Step step) throws StoreException {
194: List currentSteps = (List) _currentStepsCache.get(new Long(step
195: .getEntryId()));
196:
197: List historySteps = (List) _historyStepsCache.get(new Long(step
198: .getEntryId()));
199:
200: if (historySteps == null) {
201: historySteps = new ArrayList();
202: _historyStepsCache.put(new Long(step.getEntryId()),
203: historySteps);
204: }
205:
206: SimpleStep simpleStep = (SimpleStep) step;
207:
208: for (Iterator iterator = currentSteps.iterator(); iterator
209: .hasNext();) {
210: Step currentStep = (Step) iterator.next();
211:
212: if (simpleStep.getId() == currentStep.getId()) {
213: iterator.remove();
214: historySteps.add(simpleStep);
215:
216: break;
217: }
218: }
219: }
220:
221: /* (non-Javadoc)
222: * @see com.opensymphony.workflow.spi.WorkflowStore#query(com.opensymphony.workflow.query.WorkflowQuery)
223: */
224: public List query(WorkflowQuery query) throws StoreException {
225: ArrayList results = new ArrayList();
226:
227: for (Iterator iterator = _entryCache.entrySet().iterator(); iterator
228: .hasNext();) {
229: Map.Entry mapEntry = (Map.Entry) iterator.next();
230: Long entryId = (Long) mapEntry.getKey();
231:
232: if (_queryLogic.query(entryId, query)) {
233: results.add(entryId);
234: }
235: }
236:
237: return results;
238: }
239:
240: /* (non-Javadoc)
241: * @see com.opensymphony.workflow.spi.WorkflowStore#query(com.opensymphony.workflow.query.WorkflowExpressionQuery)
242: */
243: public List query(WorkflowExpressionQuery query)
244: throws StoreException {
245: ArrayList results = new ArrayList();
246:
247: for (Iterator iterator = _entryCache.entrySet().iterator(); iterator
248: .hasNext();) {
249: Map.Entry mapEntry = (Map.Entry) iterator.next();
250: Long entryId = (Long) mapEntry.getKey();
251:
252: if (_queryLogic.query(entryId, query)) {
253: results.add(entryId);
254: }
255: }
256:
257: return results;
258: }
259: }
|