1 5 package com.opensymphony.workflow.spi.ofbiz; 6 7 import com.opensymphony.module.propertyset.PropertySet; 8 import com.opensymphony.module.propertyset.PropertySetManager; 9 10 import com.opensymphony.workflow.QueryNotSupportedException; 11 import com.opensymphony.workflow.StoreException; 12 import com.opensymphony.workflow.query.WorkflowExpressionQuery; 13 import com.opensymphony.workflow.query.WorkflowQuery; 14 import com.opensymphony.workflow.spi.*; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import org.ofbiz.core.entity.*; 20 import org.ofbiz.core.util.UtilMisc; 21 22 import java.sql.Timestamp ; 23 24 import java.util.*; 25 26 27 38 public class OfbizWorkflowStore implements WorkflowStore { 39 41 private static final Log log = LogFactory.getLog(OfbizWorkflowStore.class); 42 43 45 private GenericDelegator gd; 46 private String delegatorName; 47 48 50 public void setEntryState(long entryId, int state) throws StoreException { 51 try { 52 GenericValue gv = gd.findByPrimaryKey("OSWorkflowEntry", UtilMisc.toMap("id", new Long (entryId))); 53 gv.set("state", new Integer (state)); 54 gd.store(gv); 55 } catch (GenericEntityException e) { 56 throw new StoreException("Could not update workflow instance #" + entryId + " to status " + state, e); 57 } 58 } 59 60 public PropertySet getPropertySet(long entryId) { 61 HashMap args = new HashMap(2); 62 args.put("entityId", new Long (entryId)); 63 args.put("entityName", "WorkflowEntry"); 64 65 return PropertySetManager.getInstance("ofbiz", args); 66 } 67 68 public Step createCurrentStep(long entryId, int stepId, String owner, Date startDate, Date dueDate, String status, long[] previousIds) throws StoreException { 69 try { 70 Long id = gd.getNextSeqId("OSCurrentStep"); 71 HashMap valueMap = new HashMap(); 72 valueMap.put("id", id); 73 valueMap.put("entryId", new Long (entryId)); 74 valueMap.put("actionId", new Integer (0)); 75 valueMap.put("stepId", new Integer (stepId)); 76 valueMap.put("owner", owner); 77 valueMap.put("startDate", new Timestamp (startDate.getTime())); 78 79 Timestamp realDueDate = null; 80 81 if (dueDate != null) { 82 realDueDate = new Timestamp (dueDate.getTime()); 83 } 84 85 valueMap.put("dueDate", realDueDate); 86 valueMap.put("finishDate", null); 87 valueMap.put("status", status); 88 89 GenericValue gv = gd.create("OSCurrentStep", valueMap); 90 ArrayList storeList = new ArrayList(); 91 storeList.add(gv); 92 93 if (previousIds != null) { 94 if (!((previousIds.length == 1) && (previousIds[0] == 0))) { 95 for (int i = 0; i < previousIds.length; i++) { 96 long previousId = previousIds[i]; 97 GenericValue prevGv = gd.create("OSCurrentStepPrev", UtilMisc.toMap("id", id, "previousId", new Long (previousId))); 98 storeList.add(prevGv); 99 } 100 } 101 } 102 103 gd.storeAll(storeList); 104 105 return new SimpleStep(id.longValue(), entryId, stepId, 0, owner, startDate, dueDate, null, status, previousIds, null); 106 } catch (GenericEntityException e) { 107 throw new StoreException("Could not create new current step for #" + entryId, e); 108 } 109 } 110 111 public WorkflowEntry createEntry(String workflowName) throws StoreException { 112 try { 113 Long id = gd.getNextSeqId("OSWorkflowEntry"); 114 GenericValue gv = gd.create("OSWorkflowEntry", UtilMisc.toMap("id", id, "name", workflowName, "state", new Integer (WorkflowEntry.CREATED))); 115 gd.storeAll(UtilMisc.toList(gv)); 116 117 return new SimpleWorkflowEntry(id.longValue(), workflowName, WorkflowEntry.CREATED); 118 } catch (GenericEntityException e) { 119 throw new StoreException("Could not create workflow instance", e); 120 } 121 } 122 123 public List findCurrentSteps(long entryId) throws StoreException { 124 try { 125 Collection c = gd.findByAnd("OSCurrentStep", UtilMisc.toMap("entryId", new Long (entryId))); 126 ArrayList list = new ArrayList(); 127 128 for (Iterator iterator = c.iterator(); iterator.hasNext();) { 129 GenericValue gv = (GenericValue) iterator.next(); 130 long id = gv.getLong("id").longValue(); 131 int stepId = gv.getInteger("stepId").intValue(); 132 int actionId = gv.getInteger("actionId").intValue(); 133 String owner = gv.getString("owner"); 134 Timestamp startDate = gv.getTimestamp("startDate"); 135 Timestamp dueDate = gv.getTimestamp("dueDate"); 136 Timestamp finishDate = gv.getTimestamp("finishDate"); 137 String status = gv.getString("status"); 138 String caller = gv.getString("caller"); 139 140 Collection prevGvs = gd.findByAnd("OSCurrentStepPrev", UtilMisc.toMap("id", new Long (id))); 141 long[] prevIds = new long[prevGvs.size()]; 142 int i = 0; 143 144 for (Iterator iterator2 = prevGvs.iterator(); 145 iterator2.hasNext();) { 146 GenericValue prevGv = (GenericValue) iterator2.next(); 147 prevIds[i] = prevGv.getLong("previousId").longValue(); 148 i++; 149 } 150 151 SimpleStep step = new SimpleStep(id, entryId, stepId, actionId, owner, startDate, dueDate, finishDate, status, prevIds, caller); 152 list.add(step); 153 } 154 155 return list; 156 } catch (GenericEntityException e) { 157 throw new StoreException("Could not find current steps for #" + entryId, e); 158 } 159 } 160 161 public WorkflowEntry findEntry(long entryId) throws StoreException { 162 try { 163 GenericValue gv = gd.findByPrimaryKey("OSWorkflowEntry", UtilMisc.toMap("id", new Long (entryId))); 164 String workflowName = gv.getString("name"); 165 166 return new SimpleWorkflowEntry(entryId, workflowName, gv.getInteger("state").intValue()); 167 } catch (GenericEntityException e) { 168 throw new StoreException("Could not find workflow instance #" + entryId, e); 169 } 170 } 171 172 public List findHistorySteps(long entryId) throws StoreException { 173 try { 174 Collection c = gd.findByAnd("OSHistoryStep", UtilMisc.toMap("entryId", new Long (entryId)), UtilMisc.toList("id DESC")); 175 ArrayList list = new ArrayList(); 176 177 for (Iterator iterator = c.iterator(); iterator.hasNext();) { 178 GenericValue gv = (GenericValue) iterator.next(); 179 long id = gv.getLong("id").longValue(); 180 int stepId = gv.getInteger("stepId").intValue(); 181 int actionId = gv.getInteger("actionId").intValue(); 182 String owner = gv.getString("owner"); 183 Timestamp startDate = gv.getTimestamp("startDate"); 184 Timestamp dueDate = gv.getTimestamp("dueDate"); 185 Timestamp finishDate = gv.getTimestamp("finishDate"); 186 String status = gv.getString("status"); 187 String caller = gv.getString("caller"); 188 189 Collection prevGvs = gd.findByAnd("OSHistoryStepPrev", UtilMisc.toMap("id", new Long (id))); 190 long[] prevIds = new long[prevGvs.size()]; 191 int i = 0; 192 193 for (Iterator iterator2 = prevGvs.iterator(); 194 iterator2.hasNext();) { 195 GenericValue prevGv = (GenericValue) iterator2.next(); 196 prevIds[i] = prevGv.getLong("previousId").longValue(); 197 i++; 198 } 199 200 SimpleStep step = new SimpleStep(id, entryId, stepId, actionId, owner, startDate, dueDate, finishDate, status, prevIds, caller); 201 list.add(step); 202 } 203 204 return list; 205 } catch (GenericEntityException e) { 206 throw new StoreException("Could not find history steps for #" + entryId, e); 207 } 208 } 209 210 public void init(Map props) throws StoreException { 211 delegatorName = (String ) props.get("delegator"); 212 213 if (delegatorName == null) { 214 delegatorName = "default"; 215 } 216 217 try { 218 gd = GenericDelegator.getGenericDelegator(delegatorName); 219 } catch (Exception t) { 220 throw new StoreException("Error getting GenericDelegator", t); 221 } 222 } 223 224 public Step markFinished(Step step, int actionId, Date finishDate, String status, String caller) throws StoreException { 225 try { 226 GenericValue gv = gd.findByPrimaryKey("OSCurrentStep", UtilMisc.toMap("id", new Long (step.getId()))); 227 gv.set("actionId", new Integer (actionId)); 228 gv.set("finishDate", new Timestamp (finishDate.getTime())); 229 gv.set("status", status); 230 gv.set("caller", caller); 231 gd.store(gv); 232 233 SimpleStep theStep = (SimpleStep) step; 234 theStep.setStatus(status); 235 theStep.setFinishDate(finishDate); 236 theStep.setActionId(actionId); 237 theStep.setCaller(caller); 238 239 return theStep; 240 } catch (GenericEntityException e) { 241 throw new StoreException("Error marking step #" + step.getId() + " finished", e); 242 } 243 } 244 245 public void moveToHistory(Step step) throws StoreException { 246 try { 247 Long id = new Long (step.getId()); 248 gd.removeByAnd("OSCurrentStep", UtilMisc.toMap("id", id)); 249 250 HashMap valueMap = new HashMap(); 251 valueMap.put("id", id); 252 valueMap.put("entryId", new Long (step.getEntryId())); 253 valueMap.put("actionId", new Integer (step.getActionId())); 254 valueMap.put("stepId", new Integer (step.getStepId())); 255 valueMap.put("owner", step.getOwner()); 256 valueMap.put("startDate", new Timestamp (step.getStartDate().getTime())); 257 258 Timestamp realDueDate = null; 259 260 if (step.getDueDate() != null) { 261 realDueDate = new Timestamp (step.getDueDate().getTime()); 262 } 263 264 valueMap.put("dueDate", realDueDate); 265 266 if (step.getFinishDate() != null) { 267 valueMap.put("finishDate", new Timestamp (step.getFinishDate().getTime())); 268 } 269 270 valueMap.put("status", step.getStatus()); 271 valueMap.put("caller", step.getCaller()); 272 273 GenericValue gv = gd.create("OSHistoryStep", valueMap); 274 ArrayList storeList = new ArrayList(); 275 storeList.add(gv); 276 277 long[] previousIds = step.getPreviousStepIds(); 278 279 if (previousIds != null) { 280 for (int i = 0; i < previousIds.length; i++) { 281 long previousId = previousIds[i]; 282 GenericValue prevGv = gd.create("OSHistoryStepPrev", UtilMisc.toMap("id", id, "previousId", new Long (previousId))); 283 storeList.add(prevGv); 284 } 285 } 286 287 gd.storeAll(storeList); 288 } catch (GenericEntityException e) { 289 throw new StoreException("Could not move to history step for #" + step.getEntryId(), e); 290 } 291 } 292 293 public List query(WorkflowExpressionQuery query) throws StoreException { 294 throw new QueryNotSupportedException("Ofbiz Store does not support queries"); 295 } 296 297 public List query(WorkflowQuery query) throws StoreException { 298 throw new QueryNotSupportedException("Ofbiz Store does not support queries"); 299 } 300 } 301 | Popular Tags |