001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.spi.ofbiz;
006:
007: import com.opensymphony.module.propertyset.PropertySet;
008: import com.opensymphony.module.propertyset.PropertySetManager;
009:
010: import com.opensymphony.workflow.QueryNotSupportedException;
011: import com.opensymphony.workflow.StoreException;
012: import com.opensymphony.workflow.query.WorkflowExpressionQuery;
013: import com.opensymphony.workflow.query.WorkflowQuery;
014: import com.opensymphony.workflow.spi.*;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018:
019: import org.ofbiz.core.entity.*;
020: import org.ofbiz.core.util.UtilMisc;
021:
022: import java.sql.Timestamp;
023:
024: import java.util.*;
025:
026: /**
027: * OpenForBusiness Entity Engine implemenation.
028: * <p>
029: *
030: * Has one <b>optional</b> property that can be provided:
031: * <ul>
032: * <li>delegator - the delegator name, defaults to "default"</li>
033: * </ul>
034: *
035: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
036: */
037: public class OfbizWorkflowStore implements WorkflowStore {
038: //~ Static fields/initializers /////////////////////////////////////////////
039:
040: private static final Log log = LogFactory
041: .getLog(OfbizWorkflowStore.class);
042:
043: //~ Instance fields ////////////////////////////////////////////////////////
044:
045: private GenericDelegator gd;
046: private String delegatorName;
047:
048: //~ Methods ////////////////////////////////////////////////////////////////
049:
050: public void setEntryState(long entryId, int state)
051: throws StoreException {
052: try {
053: GenericValue gv = gd.findByPrimaryKey("OSWorkflowEntry",
054: UtilMisc.toMap("id", new Long(entryId)));
055: gv.set("state", new Integer(state));
056: gd.store(gv);
057: } catch (GenericEntityException e) {
058: throw new StoreException(
059: "Could not update workflow instance #" + entryId
060: + " to status " + state, e);
061: }
062: }
063:
064: public PropertySet getPropertySet(long entryId) {
065: HashMap args = new HashMap(2);
066: args.put("entityId", new Long(entryId));
067: args.put("entityName", "WorkflowEntry");
068:
069: return PropertySetManager.getInstance("ofbiz", args);
070: }
071:
072: public Step createCurrentStep(long entryId, int stepId,
073: String owner, Date startDate, Date dueDate, String status,
074: long[] previousIds) throws StoreException {
075: try {
076: Long id = gd.getNextSeqId("OSCurrentStep");
077: HashMap valueMap = new HashMap();
078: valueMap.put("id", id);
079: valueMap.put("entryId", new Long(entryId));
080: valueMap.put("actionId", new Integer(0));
081: valueMap.put("stepId", new Integer(stepId));
082: valueMap.put("owner", owner);
083: valueMap.put("startDate",
084: new Timestamp(startDate.getTime()));
085:
086: Timestamp realDueDate = null;
087:
088: if (dueDate != null) {
089: realDueDate = new Timestamp(dueDate.getTime());
090: }
091:
092: valueMap.put("dueDate", realDueDate);
093: valueMap.put("finishDate", null);
094: valueMap.put("status", status);
095:
096: GenericValue gv = gd.create("OSCurrentStep", valueMap);
097: ArrayList storeList = new ArrayList();
098: storeList.add(gv);
099:
100: if (previousIds != null) {
101: if (!((previousIds.length == 1) && (previousIds[0] == 0))) {
102: for (int i = 0; i < previousIds.length; i++) {
103: long previousId = previousIds[i];
104: GenericValue prevGv = gd.create(
105: "OSCurrentStepPrev", UtilMisc.toMap(
106: "id", id, "previousId",
107: new Long(previousId)));
108: storeList.add(prevGv);
109: }
110: }
111: }
112:
113: gd.storeAll(storeList);
114:
115: return new SimpleStep(id.longValue(), entryId, stepId, 0,
116: owner, startDate, dueDate, null, status,
117: previousIds, null);
118: } catch (GenericEntityException e) {
119: throw new StoreException(
120: "Could not create new current step for #" + entryId,
121: e);
122: }
123: }
124:
125: public WorkflowEntry createEntry(String workflowName)
126: throws StoreException {
127: try {
128: Long id = gd.getNextSeqId("OSWorkflowEntry");
129: GenericValue gv = gd.create("OSWorkflowEntry", UtilMisc
130: .toMap("id", id, "name", workflowName, "state",
131: new Integer(WorkflowEntry.CREATED)));
132: gd.storeAll(UtilMisc.toList(gv));
133:
134: return new SimpleWorkflowEntry(id.longValue(),
135: workflowName, WorkflowEntry.CREATED);
136: } catch (GenericEntityException e) {
137: throw new StoreException(
138: "Could not create workflow instance", e);
139: }
140: }
141:
142: public List findCurrentSteps(long entryId) throws StoreException {
143: try {
144: Collection c = gd.findByAnd("OSCurrentStep", UtilMisc
145: .toMap("entryId", new Long(entryId)));
146: ArrayList list = new ArrayList();
147:
148: for (Iterator iterator = c.iterator(); iterator.hasNext();) {
149: GenericValue gv = (GenericValue) iterator.next();
150: long id = gv.getLong("id").longValue();
151: int stepId = gv.getInteger("stepId").intValue();
152: int actionId = gv.getInteger("actionId").intValue();
153: String owner = gv.getString("owner");
154: Timestamp startDate = gv.getTimestamp("startDate");
155: Timestamp dueDate = gv.getTimestamp("dueDate");
156: Timestamp finishDate = gv.getTimestamp("finishDate");
157: String status = gv.getString("status");
158: String caller = gv.getString("caller");
159:
160: Collection prevGvs = gd.findByAnd("OSCurrentStepPrev",
161: UtilMisc.toMap("id", new Long(id)));
162: long[] prevIds = new long[prevGvs.size()];
163: int i = 0;
164:
165: for (Iterator iterator2 = prevGvs.iterator(); iterator2
166: .hasNext();) {
167: GenericValue prevGv = (GenericValue) iterator2
168: .next();
169: prevIds[i] = prevGv.getLong("previousId")
170: .longValue();
171: i++;
172: }
173:
174: SimpleStep step = new SimpleStep(id, entryId, stepId,
175: actionId, owner, startDate, dueDate,
176: finishDate, status, prevIds, caller);
177: list.add(step);
178: }
179:
180: return list;
181: } catch (GenericEntityException e) {
182: throw new StoreException(
183: "Could not find current steps for #" + entryId, e);
184: }
185: }
186:
187: public WorkflowEntry findEntry(long entryId) throws StoreException {
188: try {
189: GenericValue gv = gd.findByPrimaryKey("OSWorkflowEntry",
190: UtilMisc.toMap("id", new Long(entryId)));
191: String workflowName = gv.getString("name");
192:
193: return new SimpleWorkflowEntry(entryId, workflowName, gv
194: .getInteger("state").intValue());
195: } catch (GenericEntityException e) {
196: throw new StoreException(
197: "Could not find workflow instance #" + entryId, e);
198: }
199: }
200:
201: public List findHistorySteps(long entryId) throws StoreException {
202: try {
203: Collection c = gd.findByAnd("OSHistoryStep", UtilMisc
204: .toMap("entryId", new Long(entryId)), UtilMisc
205: .toList("id DESC"));
206: ArrayList list = new ArrayList();
207:
208: for (Iterator iterator = c.iterator(); iterator.hasNext();) {
209: GenericValue gv = (GenericValue) iterator.next();
210: long id = gv.getLong("id").longValue();
211: int stepId = gv.getInteger("stepId").intValue();
212: int actionId = gv.getInteger("actionId").intValue();
213: String owner = gv.getString("owner");
214: Timestamp startDate = gv.getTimestamp("startDate");
215: Timestamp dueDate = gv.getTimestamp("dueDate");
216: Timestamp finishDate = gv.getTimestamp("finishDate");
217: String status = gv.getString("status");
218: String caller = gv.getString("caller");
219:
220: Collection prevGvs = gd.findByAnd("OSHistoryStepPrev",
221: UtilMisc.toMap("id", new Long(id)));
222: long[] prevIds = new long[prevGvs.size()];
223: int i = 0;
224:
225: for (Iterator iterator2 = prevGvs.iterator(); iterator2
226: .hasNext();) {
227: GenericValue prevGv = (GenericValue) iterator2
228: .next();
229: prevIds[i] = prevGv.getLong("previousId")
230: .longValue();
231: i++;
232: }
233:
234: SimpleStep step = new SimpleStep(id, entryId, stepId,
235: actionId, owner, startDate, dueDate,
236: finishDate, status, prevIds, caller);
237: list.add(step);
238: }
239:
240: return list;
241: } catch (GenericEntityException e) {
242: throw new StoreException(
243: "Could not find history steps for #" + entryId, e);
244: }
245: }
246:
247: public void init(Map props) throws StoreException {
248: delegatorName = (String) props.get("delegator");
249:
250: if (delegatorName == null) {
251: delegatorName = "default";
252: }
253:
254: try {
255: gd = GenericDelegator.getGenericDelegator(delegatorName);
256: } catch (Exception t) {
257: throw new StoreException("Error getting GenericDelegator",
258: t);
259: }
260: }
261:
262: public Step markFinished(Step step, int actionId, Date finishDate,
263: String status, String caller) throws StoreException {
264: try {
265: GenericValue gv = gd.findByPrimaryKey("OSCurrentStep",
266: UtilMisc.toMap("id", new Long(step.getId())));
267: gv.set("actionId", new Integer(actionId));
268: gv.set("finishDate", new Timestamp(finishDate.getTime()));
269: gv.set("status", status);
270: gv.set("caller", caller);
271: gd.store(gv);
272:
273: SimpleStep theStep = (SimpleStep) step;
274: theStep.setStatus(status);
275: theStep.setFinishDate(finishDate);
276: theStep.setActionId(actionId);
277: theStep.setCaller(caller);
278:
279: return theStep;
280: } catch (GenericEntityException e) {
281: throw new StoreException("Error marking step #"
282: + step.getId() + " finished", e);
283: }
284: }
285:
286: public void moveToHistory(Step step) throws StoreException {
287: try {
288: Long id = new Long(step.getId());
289: gd.removeByAnd("OSCurrentStep", UtilMisc.toMap("id", id));
290:
291: HashMap valueMap = new HashMap();
292: valueMap.put("id", id);
293: valueMap.put("entryId", new Long(step.getEntryId()));
294: valueMap.put("actionId", new Integer(step.getActionId()));
295: valueMap.put("stepId", new Integer(step.getStepId()));
296: valueMap.put("owner", step.getOwner());
297: valueMap.put("startDate", new Timestamp(step.getStartDate()
298: .getTime()));
299:
300: Timestamp realDueDate = null;
301:
302: if (step.getDueDate() != null) {
303: realDueDate = new Timestamp(step.getDueDate().getTime());
304: }
305:
306: valueMap.put("dueDate", realDueDate);
307:
308: if (step.getFinishDate() != null) {
309: valueMap.put("finishDate", new Timestamp(step
310: .getFinishDate().getTime()));
311: }
312:
313: valueMap.put("status", step.getStatus());
314: valueMap.put("caller", step.getCaller());
315:
316: GenericValue gv = gd.create("OSHistoryStep", valueMap);
317: ArrayList storeList = new ArrayList();
318: storeList.add(gv);
319:
320: long[] previousIds = step.getPreviousStepIds();
321:
322: if (previousIds != null) {
323: for (int i = 0; i < previousIds.length; i++) {
324: long previousId = previousIds[i];
325: GenericValue prevGv = gd.create(
326: "OSHistoryStepPrev", UtilMisc.toMap("id",
327: id, "previousId", new Long(
328: previousId)));
329: storeList.add(prevGv);
330: }
331: }
332:
333: gd.storeAll(storeList);
334: } catch (GenericEntityException e) {
335: throw new StoreException(
336: "Could not move to history step for #"
337: + step.getEntryId(), e);
338: }
339: }
340:
341: public List query(WorkflowExpressionQuery query)
342: throws StoreException {
343: throw new QueryNotSupportedException(
344: "Ofbiz Store does not support queries");
345: }
346:
347: public List query(WorkflowQuery query) throws StoreException {
348: throw new QueryNotSupportedException(
349: "Ofbiz Store does not support queries");
350: }
351: }
|