001: /*
002: * $Id: WfFactory.java,v 1.1 2003/08/17 09:29:33 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.workflow;
026:
027: import java.sql.Timestamp;
028: import java.util.Date;
029:
030: import org.ofbiz.base.util.UtilCache;
031: import org.ofbiz.entity.GenericDelegator;
032: import org.ofbiz.entity.GenericValue;
033: import org.ofbiz.service.DispatchContext;
034: import org.ofbiz.workflow.client.WorkflowClient;
035: import org.ofbiz.workflow.impl.WfActivityImpl;
036: import org.ofbiz.workflow.impl.WfAssignmentImpl;
037: import org.ofbiz.workflow.impl.WfEventAuditImpl;
038: import org.ofbiz.workflow.impl.WfProcessImpl;
039: import org.ofbiz.workflow.impl.WfProcessMgrImpl;
040: import org.ofbiz.workflow.impl.WfRequesterImpl;
041: import org.ofbiz.workflow.impl.WfResourceImpl;
042:
043: /**
044: * WfFactory - Workflow Factory Class
045: *
046: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
047: * @version $Revision: 1.1 $
048: * @since 2.0
049: */
050: public class WfFactory {
051:
052: public static final String module = WfFactory.class.getName();
053:
054: protected static UtilCache wfProcessMgrCache = new UtilCache(
055: "workflow.processmgr");
056: protected static UtilCache wfClientCache = new UtilCache(
057: "workflow.client");
058:
059: /**
060: * Creates a new {@link WfActivity} instance.
061: * @param value GenericValue object defining this activity.
062: * @param process The WorkEffort key of the parent process
063: * @return An instance of the WfActivify Interface
064: * @throws WfException
065: */
066: public static WfActivity getWfActivity(GenericValue value,
067: String process) throws WfException {
068: if (value == null)
069: throw new WfException(
070: "Activity definition value object cannot be null");
071: if (process == null)
072: throw new WfException(
073: "Parent process WorkEffort key cannot be null");
074: return new WfActivityImpl(value, process);
075: }
076:
077: public static WfActivity getWfActivity(GenericDelegator delegator,
078: String workEffortId) throws WfException {
079: if (delegator == null)
080: throw new WfException("The delegator object cannot be null");
081: if (workEffortId == null)
082: throw new WfException("The WorkEffort key cannot be null");
083: return new WfActivityImpl(delegator, workEffortId);
084: }
085:
086: /**
087: * Creates a new {@link WfAssignment} instance.
088: * @return An instance of the WfAssignment Interface
089: * @throws WfException
090: */
091: public static WfAssignment getWfAssignment(WfActivity activity,
092: WfResource resource, Timestamp fromDate, boolean create)
093: throws WfException {
094: if (activity == null)
095: throw new WfException("WfActivity cannot be null");
096: if (resource == null)
097: throw new WfException("WfResource cannot be null");
098: if (fromDate == null)
099: fromDate = new Timestamp(new Date().getTime());
100: return new WfAssignmentImpl(activity, resource, fromDate,
101: create);
102: }
103:
104: public static WfAssignment getWfAssignment(
105: GenericDelegator delegator, String work, String party,
106: String role, Timestamp from) throws WfException {
107: WfActivity act = getWfActivity(delegator, work);
108: WfResource res = getWfResource(delegator, null, null, party,
109: role);
110: return getWfAssignment(act, res, from, false);
111: }
112:
113: /**
114: * Creates a new {@link WfProcess} instance.
115: * @param value The GenericValue object for the process definition.
116: * @param mgr The WfProcessMgr which is managing this process.
117: * @return An instance of the WfProcess Interface.
118: * @throws WfException
119: */
120: public static WfProcess getWfProcess(GenericValue value,
121: WfProcessMgr mgr) throws WfException {
122: if (value == null)
123: throw new WfException(
124: "Process definition value object cannot be null");
125: if (mgr == null)
126: throw new WfException("WfProcessMgr cannot be null");
127: return new WfProcessImpl(value, mgr);
128: }
129:
130: public static WfProcess getWfProcess(GenericDelegator delegator,
131: String workEffortId) throws WfException {
132: if (delegator == null)
133: throw new WfException("The delegator object cannot be null");
134: if (workEffortId == null)
135: throw new WfException("The WorkEffort key cannot be null");
136: WfProcess process = null;
137: try {
138: process = new WfProcessImpl(delegator, workEffortId);
139: } catch (WfException e) {
140: try {
141: WfActivity act = WfFactory.getWfActivity(delegator,
142: workEffortId);
143: if (act != null) {
144: process = act.container();
145: } else {
146: throw e;
147: }
148: } catch (WfException e2) {
149: throw e;
150: }
151: if (process == null) {
152: throw e;
153: }
154: }
155: if (process == null) {
156: throw new WfException("No process object found");
157: }
158: return process;
159: }
160:
161: /**
162: * Creates a new {@link WfProcessMgr} instance.
163: * @param delegator The GenericDelegator to use for this manager.
164: * @param pkg The Workflow Package ID.
165: * @param pkver The Workflow Package Version.
166: * @param pid The Workflow Process ID.
167: * @param pver The Workflow Process Version.
168: * @return An instance of the WfProcessMgr Interface.
169: * @throws WfException
170: */
171: public static WfProcessMgr getWfProcessMgr(
172: GenericDelegator delegator, String pkg, String pkver,
173: String pid, String pver) throws WfException {
174: if (delegator == null)
175: throw new WfException("Delegator cannot be null");
176: if (pkg == null)
177: throw new WfException("Workflow package id cannot be null.");
178: if (pid == null)
179: throw new WfException("Workflow process id cannot be null");
180:
181: String key = delegator.getDelegatorName() + ":" + pkg + ":"
182: + pkver + ":" + pid + ":" + pver;
183: if (!wfProcessMgrCache.containsKey(key)) {
184: synchronized (WfFactory.class) {
185: if (!wfProcessMgrCache.containsKey(key)) {
186: wfProcessMgrCache.put(key, new WfProcessMgrImpl(
187: delegator, pkg, pkver, pid, pver));
188: }
189: }
190: }
191: return (WfProcessMgr) wfProcessMgrCache.get(key);
192: }
193:
194: /**
195: * Creates a new {@link WfRequester} instance.
196: * @return An instance of the WfRequester Interface.
197: * @throws WfException
198: */
199: public static WfRequester getWfRequester() throws WfException {
200: return new WfRequesterImpl();
201: }
202:
203: /**
204: * Creates a new {@link WfResource} instance.
205: * @param value The GenericValue object of the WorkflowParticipant
206: * @throws WfException
207: * @return An instance of the WfResource Interface.
208: */
209: public static WfResource getWfResource(GenericValue value)
210: throws WfException {
211: if (value == null)
212: throw new WfException(
213: "Value object for WfResource definition cannot be null");
214: return new WfResourceImpl(value);
215: }
216:
217: /**
218: * Creates a new {@link WfResource} instance.
219: * @param delegator The GenericDelegator for this instance
220: * @param key The key for the resource
221: * @param name The name of the resource
222: * @param party The partyId of the resource
223: * @param role The roleTypeId of the resource
224: * @return An instance of the WfResource Interface.
225: * @throws WfException
226: */
227: public static WfResource getWfResource(GenericDelegator delegator,
228: String key, String name, String party, String role)
229: throws WfException {
230: if (delegator == null)
231: throw new WfException("Delegator cannot be null");
232: if (party == null)
233: party = "_NA_";
234: if (role == null)
235: role = "_NA_";
236: return new WfResourceImpl(delegator, key, name, party, role);
237: }
238:
239: /**
240: * Creates a new {@link WfEventAudit} instance.
241: * @return An instance of the WfEventAudit Interface.
242: * @throws WfException
243: */
244: public static WfEventAudit getWfEventAudit(
245: WfExecutionObject object, String type) throws WfException {
246: return new WfEventAuditImpl(object, type);
247: }
248:
249: public static WorkflowClient getClient(DispatchContext dctx) {
250: if (!wfClientCache.containsKey(dctx)) {
251: synchronized (WfFactory.class) {
252: if (!wfClientCache.containsKey(dctx))
253: wfClientCache.put(dctx, new WorkflowClient(dctx));
254: }
255: }
256: return (WorkflowClient) wfClientCache.get(dctx);
257: }
258:
259: }
|