1 5 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 ; 28 import java.io.Serializable ; 29 30 import java.util.Date ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 35 44 public class PrevaylerWorkflowStore implements WorkflowStore, Serializable { 45 47 private transient Prevayler _prevayler = null; 48 private transient String _prevalenceBase; 49 private WorkflowStore _store = null; 50 51 53 public PrevaylerWorkflowStore() throws IOException , ClassNotFoundException { 54 this("WorkflowPrevaylenceBase"); 55 } 56 57 public PrevaylerWorkflowStore(String prevalenceBase) throws IOException , ClassNotFoundException { 58 super(); 59 _prevalenceBase = prevalenceBase; 60 _store = new WorkflowSystem(); 61 } 62 63 65 68 public void setEntryState(long entryId, int state) throws StoreException { 69 Object [] o = {new Long (entryId), new Integer (state)}; 70 71 try { 72 _prevayler.execute(new TransactionImpl(o) { 73 public void execute(WorkflowSystem store) { 74 Object [] o = (Object []) _object; 75 long entryId = ((Long ) o[0]).longValue(); 76 int state = ((Integer ) o[1]).intValue(); 77 78 try { 79 store.setEntryState(entryId, state); 80 } catch (StoreException e) { 81 throw new RuntimeException (e); 82 } 83 } 84 }); 85 } catch (Exception e) { 86 if (e.getCause() instanceof StoreException) { 87 throw ((StoreException) e.getCause()); 88 } else { 89 throw new StoreException(e); 90 } 91 } 92 } 93 94 97 public PropertySet getPropertySet(long entryId) throws StoreException { 98 try { 99 return (PropertySet) _prevayler.execute(new TransactionWithQueryImpl(new Long (entryId)) { 100 public Object execute(WorkflowSystem store) throws StoreException { 101 return store.getPropertySet(((Long ) _object).longValue()); 102 } 103 }); 104 } catch (Exception e) { 105 throw new StoreException(e); 106 } 107 } 108 109 112 public Step createCurrentStep(long entryId, int stepId, String owner, Date startDate, Date dueDate, String status, long[] previousIds) throws StoreException { 113 Object [] oArray = { 114 new Long (entryId), new Integer (stepId), owner, startDate, dueDate, 115 status, previousIds 116 }; 117 118 try { 119 return (Step) _prevayler.execute(new TransactionWithQueryImpl(oArray) { 120 public Object execute(WorkflowSystem store) throws StoreException { 121 Object [] o = (Object []) _object; 122 long entryId = ((Long ) o[0]).longValue(); 123 int stepId = ((Integer ) o[1]).intValue(); 124 String owner = (String ) o[2]; 125 Date startDate = (Date ) o[3]; 126 Date dueDate = (Date ) o[4]; 127 String status = (String ) 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 e) { 134 throw new StoreException(e); 135 } 136 } 137 138 141 public WorkflowEntry createEntry(String workflowName) throws StoreException { 142 try { 143 return (WorkflowEntry) _prevayler.execute(new TransactionWithQueryImpl(workflowName) { 144 public Object execute(WorkflowSystem store) throws StoreException { 145 return store.createEntry(((String ) _object)); 146 } 147 }); 148 } catch (Exception e) { 149 throw new StoreException(e); 150 } 151 } 152 153 156 public List findCurrentSteps(long entryId) throws StoreException { 157 try { 158 return (List ) _prevayler.execute(new TransactionWithQueryImpl(new Long (entryId)) { 159 public Object execute(WorkflowSystem store) throws StoreException { 160 return store.findCurrentSteps(((Long ) _object).longValue()); 161 } 162 }); 163 } catch (Exception e) { 164 throw new StoreException(e); 165 } 166 } 167 168 171 public WorkflowEntry findEntry(long entryId) throws StoreException { 172 try { 173 return (WorkflowEntry) _prevayler.execute(new TransactionWithQueryImpl(new Long (entryId)) { 174 public Object execute(WorkflowSystem store) throws StoreException { 175 return store.findEntry(((Long ) _object).longValue()); 176 } 177 }); 178 } catch (Exception e) { 179 throw new StoreException(e); 180 } 181 } 182 183 186 public List findHistorySteps(long entryId) throws StoreException { 187 try { 188 return (List ) _prevayler.execute(new TransactionWithQueryImpl(new Long (entryId)) { 189 public Object execute(WorkflowSystem store) throws StoreException { 190 return store.findHistorySteps(((Long ) _object).longValue()); 191 } 192 }); 193 } catch (Exception e) { 194 throw new StoreException(e); 195 } 196 } 197 198 201 public void init(Map props) throws StoreException { 202 _store.init(props); 203 204 boolean isTransient = false; 205 String pBaseKey = "base"; 206 207 if (props.containsKey(pBaseKey)) { 208 _prevalenceBase = (String ) props.get(pBaseKey); 209 } 210 211 String transientKey = "transient"; 212 213 if (props.containsKey(transientKey)) { 214 String value = (String ) props.get(transientKey); 215 216 if (value.equalsIgnoreCase("true")) { 217 isTransient = true; 218 } 219 } 220 221 try { 222 initializePrevaylenceSystem(_prevalenceBase, isTransient); 223 } catch (Exception e) { 224 throw new StoreException(e); 225 } 226 } 227 228 231 public Step markFinished(Step step, int actionId, Date finishDate, String status, String caller) throws StoreException { 232 Object [] oArray = { 233 step, new Integer (actionId), finishDate, status, caller 234 }; 235 236 try { 237 return (Step) _prevayler.execute(new TransactionWithQueryImpl(oArray) { 238 public Object execute(WorkflowSystem store) throws StoreException { 239 Object [] o = (Object []) _object; 240 Step step = (Step) o[0]; 241 int actionId = ((Integer ) o[1]).intValue(); 242 Date finishDate = (Date ) o[2]; 243 String status = (String ) o[3]; 244 String caller = (String ) o[4]; 245 246 return store.markFinished(step, actionId, finishDate, status, caller); 247 } 248 }); 249 } catch (Exception e) { 250 throw new StoreException(e); 251 } 252 } 253 254 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 e) { 265 throw new StoreException(e); 266 } 267 } 268 269 272 public List query(WorkflowQuery query) throws StoreException { 273 try { 274 return (List ) _prevayler.execute(new QueryImpl(query) { 275 public Object execute(WorkflowSystem store) throws StoreException { 276 return store.query(((WorkflowQuery) _object)); 277 } 278 }); 279 } catch (Exception e) { 280 throw new StoreException(e); 281 } 282 } 283 284 287 public List query(WorkflowExpressionQuery query) throws StoreException { 288 try { 289 return (List ) _prevayler.execute(new QueryImpl(query) { 290 public Object execute(WorkflowSystem store) throws StoreException { 291 return store.query(((WorkflowExpressionQuery) _object)); 292 } 293 }); 294 } catch (Exception e) { 295 throw new StoreException(e); 296 } 297 } 298 299 302 private void initializePrevaylenceSystem(String prevalenceBase, boolean isTransient) throws IOException , ClassNotFoundException { 303 if (isTransient) { 304 _prevayler = PrevaylerFactory.createTransientPrevayler(((Serializable ) _store)); 305 } else { 306 _prevayler = PrevaylerFactory.createPrevayler(((Serializable ) _store), prevalenceBase); 307 } 308 } 309 310 312 private class ObjectActioner implements Serializable { 313 protected Object _object = null; 314 315 public ObjectActioner() { 316 } 317 318 public ObjectActioner(Object object) { 319 _object = object; 320 } 321 } 322 323 private abstract class QueryImpl extends ObjectActioner implements Query { 324 public QueryImpl(Object object) { 325 super(object); 326 } 327 328 public abstract Object execute(WorkflowSystem store) throws StoreException; 329 330 public Object query(Object prevSystem, Date ignored) throws Exception { 331 return execute(((WorkflowSystem) prevSystem)); 332 } 333 } 334 335 private abstract class TransactionImpl extends ObjectActioner implements Transaction { 336 public TransactionImpl(Object object) { 337 super(object); 338 } 339 340 public abstract void execute(WorkflowSystem store) throws StoreException; 341 342 public void executeOn(Object prevSystem, Date ignored) { 343 try { 344 execute(((WorkflowSystem) prevSystem)); 345 } catch (StoreException e) { 346 throw new RuntimeException (e); 347 } 348 } 349 } 350 351 private abstract class TransactionWithQueryImpl extends ObjectActioner implements TransactionWithQuery { 352 public TransactionWithQueryImpl(Object object) { 353 super(object); 354 } 355 356 public abstract Object execute(WorkflowSystem store) throws StoreException; 357 358 public Object executeAndQuery(Object prevSystem, Date ignored) throws Exception { 359 return execute(((WorkflowSystem) prevSystem)); 360 } 361 } 362 } 363 | Popular Tags |