1 5 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 ; 25 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 34 40 public class WorkflowSystem implements WorkflowStore, Serializable { 41 43 private HashMap _currentStepsCache = new HashMap (); 44 private HashMap _entryCache = new HashMap (); 45 private HashMap _historyStepsCache = new HashMap (); 46 private HashMap _propertySetCache = new HashMap (); 47 private transient QueryLogic _queryLogic = new QueryLogic(this); 48 private long _globalEntryId = 1; 49 private long _globalStepId = 1; 50 51 53 56 public WorkflowSystem() { 57 super(); 58 } 59 60 62 65 public void setEntryState(long entryId, int state) throws StoreException { 66 SimpleWorkflowEntry entry = (SimpleWorkflowEntry) findEntry(entryId); 67 entry.setState(state); 68 } 69 70 73 public PropertySet getPropertySet(long entryId) throws StoreException { 74 PropertySet ps = (PropertySet) _propertySetCache.get(new Long (entryId)); 75 76 if (ps == null) { 77 ps = PropertySetManager.getInstance("serializable", null); 78 _propertySetCache.put(new Long (entryId), ps); 79 } 80 81 return ps; 82 } 83 84 87 public Step createCurrentStep(long entryId, int stepId, String owner, Date startDate, Date dueDate, String 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 currentSteps = (List ) _currentStepsCache.get(new Long (entryId)); 92 93 if (currentSteps == null) { 94 currentSteps = new ArrayList (); 95 _currentStepsCache.put(new Long (entryId), currentSteps); 96 } 97 98 currentSteps.add(step); 99 100 return step; 101 } 102 103 106 public WorkflowEntry createEntry(String workflowName) throws StoreException { 107 long id = _globalEntryId++; 108 SimpleWorkflowEntry entry = new SimpleWorkflowEntry(id, workflowName, WorkflowEntry.CREATED); 109 _entryCache.put(new Long (id), entry); 110 111 return entry; 112 } 113 114 117 public List findCurrentSteps(long entryId) throws StoreException { 118 List currentSteps = (List ) _currentStepsCache.get(new Long (entryId)); 119 120 if (currentSteps == null) { 121 currentSteps = new ArrayList (); 122 _currentStepsCache.put(new Long (entryId), currentSteps); 123 } 124 125 return currentSteps; 126 } 127 128 131 public WorkflowEntry findEntry(long entryId) throws StoreException { 132 return (WorkflowEntry) _entryCache.get(new Long (entryId)); 133 } 134 135 138 public List findHistorySteps(long entryId) throws StoreException { 139 List historySteps = (List ) _historyStepsCache.get(new Long (entryId)); 140 141 if (historySteps == null) { 142 historySteps = new ArrayList (); 143 _historyStepsCache.put(new Long (entryId), historySteps); 144 } 145 146 return historySteps; 147 } 148 149 152 public void init(Map props) throws StoreException { 153 } 154 155 158 public Step markFinished(Step step, int actionId, Date finishDate, String status, String caller) throws StoreException { 159 List currentSteps = (List ) _currentStepsCache.get(new Long (step.getEntryId())); 160 161 for (Iterator 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 180 public void moveToHistory(Step step) throws StoreException { 181 List currentSteps = (List ) _currentStepsCache.get(new Long (step.getEntryId())); 182 183 List historySteps = (List ) _historyStepsCache.get(new Long (step.getEntryId())); 184 185 if (historySteps == null) { 186 historySteps = new ArrayList (); 187 _historyStepsCache.put(new Long (step.getEntryId()), historySteps); 188 } 189 190 SimpleStep simpleStep = (SimpleStep) step; 191 192 for (Iterator 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 207 public List query(WorkflowQuery query) throws StoreException { 208 ArrayList results = new ArrayList (); 209 210 for (Iterator iterator = _entryCache.entrySet().iterator(); 211 iterator.hasNext();) { 212 Map.Entry mapEntry = (Map.Entry ) iterator.next(); 213 Long entryId = (Long ) mapEntry.getKey(); 214 215 if (_queryLogic.query(entryId, query)) { 216 results.add(entryId); 217 } 218 } 219 220 return results; 221 } 222 223 226 public List query(WorkflowExpressionQuery query) throws StoreException { 227 ArrayList results = new ArrayList (); 228 229 for (Iterator iterator = _entryCache.entrySet().iterator(); 230 iterator.hasNext();) { 231 Map.Entry mapEntry = (Map.Entry ) iterator.next(); 232 Long entryId = (Long ) mapEntry.getKey(); 233 234 if (_queryLogic.query(entryId, query)) { 235 results.add(entryId); 236 } 237 } 238 239 return results; 240 } 241 } 242 | Popular Tags |