KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workflow > WfFactory


1 /*
2  * $Id: WfFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.workflow;
26
27 import java.sql.Timestamp JavaDoc;
28 import java.util.Date JavaDoc;
29
30 import org.ofbiz.base.util.cache.UtilCache;
31 import org.ofbiz.entity.GenericDelegator;
32 import org.ofbiz.entity.GenericValue;
33 import org.ofbiz.service.DispatchContext;
34 import org.ofbiz.workflow.client.WorkflowClient;
35 import org.ofbiz.workflow.impl.WfActivityImpl;
36 import org.ofbiz.workflow.impl.WfAssignmentImpl;
37 import org.ofbiz.workflow.impl.WfEventAuditImpl;
38 import org.ofbiz.workflow.impl.WfProcessImpl;
39 import org.ofbiz.workflow.impl.WfProcessMgrImpl;
40 import org.ofbiz.workflow.impl.WfRequesterImpl;
41 import org.ofbiz.workflow.impl.WfResourceImpl;
42
43 /**
44  * WfFactory - Workflow Factory Class
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 2.0
49  */

50 public class WfFactory {
51         
52     public static final String JavaDoc module = WfFactory.class.getName();
53     
54     protected static UtilCache wfProcessMgrCache = new UtilCache("workflow.processmgr");
55     protected static UtilCache wfClientCache = new UtilCache("workflow.client");
56   
57     /**
58      * Creates a new {@link WfActivity} instance.
59      * @param value GenericValue object defining this activity.
60      * @param process The WorkEffort key of the parent process
61      * @return An instance of the WfActivify Interface
62      * @throws WfException
63      */

64     public static WfActivity getWfActivity(GenericValue value, String JavaDoc process) throws WfException {
65         if (value == null) throw new WfException("Activity definition value object cannot be null");
66         if (process == null) throw new WfException("Parent process WorkEffort key cannot be null");
67         return new WfActivityImpl(value, process);
68     }
69
70     public static WfActivity getWfActivity(GenericDelegator delegator, String JavaDoc workEffortId) throws WfException {
71         if (delegator == null) throw new WfException("The delegator object cannot be null");
72         if (workEffortId == null) throw new WfException("The WorkEffort key cannot be null");
73         return new WfActivityImpl(delegator, workEffortId);
74     }
75
76     /**
77      * Creates a new {@link WfAssignment} instance.
78      * @return An instance of the WfAssignment Interface
79      * @throws WfException
80      */

81     public static WfAssignment getWfAssignment(WfActivity activity, WfResource resource, Timestamp JavaDoc fromDate, boolean create) throws WfException {
82         if (activity == null) throw new WfException("WfActivity cannot be null");
83         if (resource == null) throw new WfException("WfResource cannot be null");
84         if (fromDate == null) fromDate = new Timestamp JavaDoc(new Date JavaDoc().getTime());
85         return new WfAssignmentImpl(activity, resource, fromDate, create);
86     }
87
88     public static WfAssignment getWfAssignment(GenericDelegator delegator, String JavaDoc work, String JavaDoc party, String JavaDoc role, Timestamp JavaDoc from) throws WfException {
89         WfActivity act = getWfActivity(delegator, work);
90         WfResource res = getWfResource(delegator, null, null, party, role);
91         return getWfAssignment(act, res, from, false);
92     }
93
94     /**
95      * Creates a new {@link WfProcess} instance.
96      * @param value The GenericValue object for the process definition.
97      * @param mgr The WfProcessMgr which is managing this process.
98      * @return An instance of the WfProcess Interface.
99      * @throws WfException
100      */

101     public static WfProcess getWfProcess(GenericValue value, WfProcessMgr mgr) throws WfException {
102         if (value == null) throw new WfException("Process definition value object cannot be null");
103         if (mgr == null) throw new WfException("WfProcessMgr cannot be null");
104         return new WfProcessImpl(value, mgr);
105     }
106
107     public static WfProcess getWfProcess(GenericDelegator delegator, String JavaDoc workEffortId) throws WfException {
108         if (delegator == null) throw new WfException("The delegator object cannot be null");
109         if (workEffortId == null) throw new WfException("The WorkEffort key cannot be null");
110         WfProcess process = null;
111         try {
112             process = new WfProcessImpl(delegator, workEffortId);
113         } catch (WfException e) {
114             try {
115                 WfActivity act = WfFactory.getWfActivity(delegator, workEffortId);
116                 if (act != null) {
117                     process = act.container();
118                 } else {
119                     throw e;
120                 }
121             } catch (WfException e2) {
122                 throw e;
123             }
124             if (process == null) {
125                 throw e;
126             }
127         }
128         if (process == null) {
129             throw new WfException("No process object found");
130         }
131         return process;
132     }
133
134     /**
135      * Creates a new {@link WfProcessMgr} instance.
136      * @param delegator The GenericDelegator to use for this manager.
137      * @param pkg The Workflow Package ID.
138      * @param pkver The Workflow Package Version.
139      * @param pid The Workflow Process ID.
140      * @param pver The Workflow Process Version.
141      * @return An instance of the WfProcessMgr Interface.
142      * @throws WfException
143      */

144     public static WfProcessMgr getWfProcessMgr(GenericDelegator delegator, String JavaDoc pkg, String JavaDoc pkver, String JavaDoc pid, String JavaDoc pver) throws WfException {
145         if (delegator == null) throw new WfException("Delegator cannot be null");
146         if (pkg == null) throw new WfException("Workflow package id cannot be null.");
147         if (pid == null) throw new WfException("Workflow process id cannot be null");
148         
149         String JavaDoc key = delegator.getDelegatorName() + ":" + pkg + ":" + pkver + ":" + pid + ":" + pver;
150         if (!wfProcessMgrCache.containsKey(key)) {
151             synchronized (WfFactory.class) {
152                 if (!wfProcessMgrCache.containsKey(key)) {
153                     wfProcessMgrCache.put(key, new WfProcessMgrImpl(delegator, pkg, pkver, pid, pver));
154                 }
155             }
156         }
157         return (WfProcessMgr) wfProcessMgrCache.get(key);
158     }
159
160     /**
161      * Creates a new {@link WfRequester} instance.
162      * @return An instance of the WfRequester Interface.
163      * @throws WfException
164      */

165     public static WfRequester getWfRequester() throws WfException {
166         return new WfRequesterImpl();
167     }
168
169     /**
170      * Creates a new {@link WfResource} instance.
171      * @param value The GenericValue object of the WorkflowParticipant
172      * @throws WfException
173      * @return An instance of the WfResource Interface.
174      */

175     public static WfResource getWfResource(GenericValue value) throws WfException {
176         if (value == null) throw new WfException("Value object for WfResource definition cannot be null");
177         return new WfResourceImpl(value);
178     }
179
180     /**
181      * Creates a new {@link WfResource} instance.
182      * @param delegator The GenericDelegator for this instance
183      * @param key The key for the resource
184      * @param name The name of the resource
185      * @param party The partyId of the resource
186      * @param role The roleTypeId of the resource
187      * @return An instance of the WfResource Interface.
188      * @throws WfException
189      */

190     public static WfResource getWfResource(GenericDelegator delegator, String JavaDoc key, String JavaDoc name, String JavaDoc party, String JavaDoc role) throws WfException {
191         if (delegator == null) throw new WfException("Delegator cannot be null");
192         if (party == null) party = "_NA_";
193         if (role == null) role = "_NA_";
194         return new WfResourceImpl(delegator, key, name, party, role);
195     }
196
197     /**
198      * Creates a new {@link WfEventAudit} instance.
199      * @return An instance of the WfEventAudit Interface.
200      * @throws WfException
201      */

202     public static WfEventAudit getWfEventAudit(WfExecutionObject object, String JavaDoc type) throws WfException {
203         return new WfEventAuditImpl(object, type);
204     }
205
206     public static WorkflowClient getClient(DispatchContext dctx) {
207         if (!wfClientCache.containsKey(dctx)) {
208             synchronized (WfFactory.class) {
209                 if (!wfClientCache.containsKey(dctx))
210                 wfClientCache.put(dctx, new WorkflowClient(dctx));
211             }
212         }
213         return (WorkflowClient) wfClientCache.get(dctx);
214     }
215
216 }
217
Popular Tags