1 5 package com.opensymphony.workflow.spi.ejb; 6 7 import com.opensymphony.workflow.StoreException; 8 import com.opensymphony.workflow.query.WorkflowExpressionQuery; 9 import com.opensymphony.workflow.query.WorkflowQuery; 10 import com.opensymphony.workflow.spi.SimpleStep; 11 import com.opensymphony.workflow.spi.SimpleWorkflowEntry; 12 import com.opensymphony.workflow.spi.Step; 13 import com.opensymphony.workflow.spi.WorkflowEntry; 14 15 import java.sql.Timestamp ; 16 17 import java.util.*; 18 19 import javax.ejb.SessionBean ; 20 21 22 58 public abstract class WorkflowStoreSessionEJB implements SessionBean { 59 61 64 public void setEntryState(long entryId, int state) throws StoreException { 65 try { 66 WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().findByPrimaryKey(new Long (entryId)); 67 entry.setState(state); 68 } catch (Exception e) { 69 throw new StoreException("Could not find workflow instance #" + entryId, e); 70 } 71 } 72 73 76 public Step createCurrentStep(long entryId, int stepId, String owner, Date startDate, Date dueDate, String status, long[] previousIds) throws StoreException { 77 try { 78 WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().findByPrimaryKey(new Long (entryId)); 79 entry.setState(WorkflowEntry.ACTIVATED); 80 81 Timestamp realDueDate = null; 82 83 if (dueDate != null) { 84 realDueDate = new Timestamp (dueDate.getTime()); 85 } 86 87 CurrentStepLocal step = CurrentStepHomeFactory.getLocalHome().create(entryId, stepId, owner, new Timestamp (startDate.getTime()), realDueDate, status); 88 89 long id = step.getId().longValue(); 90 91 for (int i = 0; i < previousIds.length; i++) { 92 long previousId = previousIds[i]; 93 CurrentStepPrevHomeFactory.getLocalHome().create(id, previousId); 94 } 95 96 return new SimpleStep(id, entryId, stepId, 0, owner, startDate, dueDate, null, status, previousIds, null); 97 } catch (Exception e) { 98 throw new StoreException("Could not create new current step for workflow instance #" + entryId + " step #" + stepId + ":" + e, e); 99 } 100 } 101 102 105 public WorkflowEntry createEntry(String workflowName) throws StoreException { 106 try { 107 WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().create(workflowName); 108 109 return new SimpleWorkflowEntry(entry.getId().longValue(), entry.getWorkflowName(), WorkflowEntry.CREATED); 110 } catch (Exception e) { 111 throw new StoreException("Could not create new workflow instance", e); 112 } 113 } 114 115 118 public List findCurrentSteps(long entryId) throws StoreException { 119 try { 120 Collection results = CurrentStepHomeFactory.getLocalHome().findByEntryId(entryId); 121 TreeSet set = new TreeSet(new StepComparator()); 122 123 for (Iterator iterator = results.iterator(); iterator.hasNext();) { 124 CurrentStepLocal stepLocal = (CurrentStepLocal) iterator.next(); 125 126 long id = stepLocal.getId().longValue(); 127 Collection prevSteps = CurrentStepPrevHomeFactory.getLocalHome().findByStepId(id); 128 long[] prevIds = new long[prevSteps.size()]; 129 int i = 0; 130 131 for (Iterator iterator2 = prevSteps.iterator(); 132 iterator2.hasNext();) { 133 CurrentStepPrevLocal stepPrev = (CurrentStepPrevLocal) iterator2.next(); 134 prevIds[i] = stepPrev.getPreviousId().longValue(); 135 i++; 136 } 137 138 SimpleStep step = new SimpleStep(id, stepLocal.getEntryId(), stepLocal.getStepId(), stepLocal.getActionId(), stepLocal.getOwner(), stepLocal.getStartDate(), stepLocal.getDueDate(), stepLocal.getFinishDate(), stepLocal.getStatus(), prevIds, stepLocal.getCaller()); 139 set.add(step); 140 } 141 142 return new ArrayList(set); 143 } catch (Exception e) { 144 throw new StoreException("Error in findCurrentSteps", e); 145 } 146 } 147 148 151 public WorkflowEntry findEntry(long entryId) throws StoreException { 152 try { 153 WorkflowEntryLocal entry = WorkflowEntryHomeFactory.getLocalHome().findByPrimaryKey(new Long (entryId)); 154 155 return new SimpleWorkflowEntry(entry.getId().longValue(), entry.getWorkflowName(), entry.getState()); 156 } catch (Exception e) { 157 throw new StoreException("Could not find workflow instance #" + entryId, e); 158 } 159 } 160 161 164 public List findHistorySteps(long entryId) throws StoreException { 165 try { 166 Collection results = HistoryStepHomeFactory.getLocalHome().findByEntryId(entryId); 167 TreeSet set = new TreeSet(new StepComparator()); 168 169 for (Iterator iterator = results.iterator(); iterator.hasNext();) { 170 HistoryStepLocal stepRemote = (HistoryStepLocal) iterator.next(); 171 172 long id = stepRemote.getId().longValue(); 173 Collection prevSteps = HistoryStepPrevHomeFactory.getLocalHome().findByStepId(id); 174 long[] prevIds = new long[prevSteps.size()]; 175 int i = 0; 176 177 for (Iterator iterator2 = prevSteps.iterator(); 178 iterator2.hasNext();) { 179 HistoryStepPrevLocal stepPrev = (HistoryStepPrevLocal) iterator2.next(); 180 prevIds[i] = stepPrev.getPreviousId().longValue(); 181 i++; 182 } 183 184 SimpleStep step = new SimpleStep(stepRemote.getId().longValue(), stepRemote.getEntryId(), stepRemote.getStepId(), stepRemote.getActionId(), stepRemote.getOwner(), stepRemote.getStartDate(), stepRemote.getDueDate(), stepRemote.getFinishDate(), stepRemote.getStatus(), prevIds, stepRemote.getCaller()); 185 set.add(step); 186 } 187 188 return new ArrayList(set); 189 } catch (Exception e) { 190 throw new StoreException("Could not find history steps for workflow instance #" + entryId, e); 191 } 192 } 193 194 197 public Step markFinished(Step step, int actionId, Date finishDate, String status, String caller) throws StoreException { 198 try { 199 CurrentStepLocal currentStep = CurrentStepHomeFactory.getLocalHome().findByPrimaryKey(new Long (step.getId())); 200 currentStep.setActionId(actionId); 201 currentStep.setFinishDate(new Timestamp (finishDate.getTime())); 202 currentStep.setStatus(status); 203 currentStep.setCaller(caller); 204 205 SimpleStep theStep = (SimpleStep) step; 206 theStep.setActionId(actionId); 207 theStep.setFinishDate(finishDate); 208 theStep.setStatus(status); 209 theStep.setCaller(caller); 210 211 return theStep; 212 } catch (Exception e) { 213 throw new StoreException("Could not mark step finished for #" + step.getEntryId(), e); 214 } 215 } 216 217 220 public void moveToHistory(Step step) throws StoreException { 221 Long id = new Long (step.getId()); 222 223 try { 224 CurrentStepLocal currentStep = CurrentStepHomeFactory.getLocalHome().findByPrimaryKey(id); 225 long[] previousIds = step.getPreviousStepIds(); 226 Timestamp realDueDate = null; 227 228 if (step.getDueDate() != null) { 229 realDueDate = new Timestamp (step.getDueDate().getTime()); 230 } 231 232 Timestamp finishedDate = null; 233 234 if (step.getFinishDate() != null) { 235 finishedDate = new Timestamp (step.getFinishDate().getTime()); 236 } 237 238 HistoryStepLocalHome historyHome = HistoryStepHomeFactory.getLocalHome(); 239 historyHome.create(id.longValue(), step.getEntryId(), step.getStepId(), step.getActionId(), step.getOwner(), new Timestamp (step.getStartDate().getTime()), realDueDate, finishedDate, step.getStatus(), step.getCaller()); 240 241 for (int i = 0; i < previousIds.length; i++) { 242 long previousId = previousIds[i]; 243 HistoryStepPrevHomeFactory.getLocalHome().create(id.longValue(), previousId); 244 } 245 246 Collection oldPrevSteps = CurrentStepPrevHomeFactory.getLocalHome().findByStepId(id.longValue()); 247 248 for (Iterator iterator = oldPrevSteps.iterator(); 249 iterator.hasNext();) { 250 CurrentStepPrevLocal oldPrevStep = (CurrentStepPrevLocal) iterator.next(); 251 oldPrevStep.remove(); 252 } 253 254 currentStep.remove(); 255 } catch (Exception e) { 256 throw new StoreException("Could not move step to history for workflow instance #" + id, e); 257 } 258 } 259 260 263 public List query(WorkflowQuery query) throws StoreException { 264 throw new StoreException("EJB store does not support queries"); 266 } 267 268 271 public List query(WorkflowExpressionQuery query) throws StoreException { 272 throw new StoreException("EJB store does not support queries"); 274 } 275 } 276 | Popular Tags |