KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > requester > AbstractRequester


1 /*
2  * $Id: AbstractRequester.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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.shark.requester;
26
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.io.Serializable JavaDoc;
33
34 import org.ofbiz.entity.GenericValue;
35 import org.ofbiz.entity.GenericDelegator;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.Debug;
39 import org.ofbiz.shark.container.SharkContainer;
40
41 import org.enhydra.shark.api.client.wfmodel.WfProcessIterator;
42 import org.enhydra.shark.api.client.wfmodel.WfProcess;
43 import org.enhydra.shark.api.client.wfmodel.WfEventAudit;
44 import org.enhydra.shark.api.client.wfmodel.InvalidPerformer;
45 import org.enhydra.shark.api.client.wfmodel.WfRequester;
46 import org.enhydra.shark.api.client.wfmodel.SourceNotAvailable;
47 import org.enhydra.shark.api.client.wfbase.BaseException;
48 import org.enhydra.shark.api.client.wfservice.AdminInterface;
49 import org.enhydra.shark.api.client.wfservice.ExecutionAdministration;
50 import org.enhydra.shark.api.client.wfservice.ConnectFailed;
51 import org.enhydra.shark.api.client.wfservice.NotConnected;
52 import org.enhydra.shark.api.SharkTransaction;
53 import org.enhydra.shark.WfProcessIteratorWrapper;
54
55 /**
56  * Shark Workflow Abstract Requester
57  *
58  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
59  * @version $Rev: 5462 $
60  * @since 3.1
61  */

62 public abstract class AbstractRequester implements WfRequester, Serializable JavaDoc {
63
64     public static final String JavaDoc module = LoggingRequester.class.getName();
65     protected transient GenericDelegator delegator = null;
66     protected transient GenericValue userLogin = null;
67     protected String JavaDoc delegatorName = null;
68     protected String JavaDoc userLoginId = null;
69     protected List JavaDoc performerIds = new ArrayList JavaDoc();
70
71     public AbstractRequester(GenericValue userLogin) {
72         this.delegator = userLogin.getDelegator();
73         this.userLogin = userLogin;
74
75         this.delegatorName = delegator.getDelegatorName();
76         this.userLoginId = userLogin.getString("userLoginId");
77     }
78
79     public void addPerformer(WfProcess process) throws BaseException {
80         performerIds.add(process.key());
81     }
82
83     public int how_many_performer() throws BaseException {
84         return performerIds.size();
85     }
86
87     public int how_many_performer(SharkTransaction trans) throws BaseException {
88         return performerIds.size();
89     }
90
91     public WfProcessIterator get_iterator_performer() throws BaseException {
92         return new WfProcessIteratorImpl(this.getPerformers());
93     }
94
95     public WfProcessIterator get_iterator_performer(SharkTransaction trans) throws BaseException {
96         return new WfProcessIteratorImpl(trans, this.getPerformers());
97     }
98
99     public WfProcess[] get_sequence_performer(int i) throws BaseException {
100         if (i > how_many_performer()) {
101             i = how_many_performer();
102         }
103         return (WfProcess[]) this.getPerformers().subList(0, i).toArray();
104     }
105
106     public WfProcess[] get_sequence_performer(SharkTransaction trans, int i) throws BaseException {
107         if (i > how_many_performer()) {
108             i = how_many_performer();
109         }
110         return (WfProcess[]) this.getPerformers().subList(0, i).toArray();
111     }
112
113     public boolean is_member_of_performer(WfProcess process) throws BaseException {
114         return performerIds.contains(process.key());
115     }
116
117     public boolean is_member_of_performer(SharkTransaction trans, WfProcess process) throws BaseException {
118         return performerIds.contains(process.key());
119     }
120
121     public abstract void receive_event(WfEventAudit event) throws BaseException, InvalidPerformer;
122
123     public abstract void receive_event(SharkTransaction trans, WfEventAudit event) throws BaseException, InvalidPerformer;
124
125     protected Map JavaDoc getWRD(WfEventAudit event, Map JavaDoc initialContext) throws BaseException {
126         Map JavaDoc wrdMap = new HashMap JavaDoc();
127
128         // set the initial context (overrided by any new data)
129
if (initialContext != null) {
130             wrdMap.putAll(initialContext);
131         }
132
133         // these are static values available to the service
134
wrdMap.put("eventType", event.event_type());
135         wrdMap.put("activityId", event.activity_key());
136         wrdMap.put("activityName", event.activity_name());
137         wrdMap.put("processId", event.process_key());
138         wrdMap.put("processName", event.process_name());
139         wrdMap.put("processMgrName", event.process_mgr_name());
140         wrdMap.put("processMgrVersion", event.process_mgr_version());
141         wrdMap.put("eventTime", event.time_stamp().getTimestamp());
142
143         // all WRD is also available to the service, but what this contains is not known
144
try {
145             Map JavaDoc wrd = new HashMap JavaDoc(event.source().process_context());
146             if (wrd != null) {
147                 wrdMap.put("_WRDMap", wrd);
148                 wrdMap.putAll(wrd);
149             }
150         } catch (SourceNotAvailable e) {
151             Debug.logError(e, "No WRD available since event.source() cannot be obtained", module);
152         }
153
154         return wrdMap;
155     }
156
157     protected synchronized GenericDelegator getDelegator() {
158         if (this.delegator == null && this.delegatorName != null) {
159             this.delegator = GenericDelegator.getGenericDelegator(this.delegatorName);
160         }
161         return this.delegator;
162     }
163
164     protected synchronized GenericValue getUserLogin() throws GenericEntityException {
165         if (userLogin == null && this.userLoginId != null) {
166             GenericDelegator delegator = this.getDelegator();
167             if (delegator != null) {
168                 this.userLogin = delegator.findByPrimaryKey("UserLogin",
169                         UtilMisc.toMap("userLoginId", this.userLoginId));
170             }
171         }
172         return this.userLogin;
173     }
174
175     protected List JavaDoc getPerformers() {
176         GenericValue userLogin = null;
177         List JavaDoc performers = null;
178         try {
179             userLogin = this.getUserLogin();
180         } catch (GenericEntityException e) {
181             Debug.logError(e, module);
182         }
183         if (userLogin != null) {
184             AdminInterface admin = SharkContainer.getAdminInterface();
185             ExecutionAdministration exAdmin = admin.getExecutionAdministration();
186             boolean connected = true;
187             try {
188                 exAdmin.connect(userLogin.getString("userLoginId"), userLogin.getString("currentPassword"), null, null);
189             } catch (BaseException e) {
190                 Debug.logError(e, module);
191                 connected = false;
192             } catch (ConnectFailed e) {
193                 Debug.logError(e, module);
194                 connected = false;
195             }
196
197             if (connected) {
198                 performers = new ArrayList JavaDoc(performerIds.size());
199                 Iterator JavaDoc i = performerIds.iterator();
200                 try {
201                     while (i.hasNext()) {
202                         String JavaDoc processId = (String JavaDoc) i.next();
203                         exAdmin.getProcess(processId);
204                     }
205                 } catch (Exception JavaDoc e) {
206                     Debug.logError(e, module);
207                     performers = null;
208                 } finally {
209                     try {
210                         exAdmin.disconnect();
211                     } catch (BaseException e) {
212                         Debug.logError(e, module);
213                     } catch (NotConnected e) {
214                         Debug.logError(e, module);
215                     }
216                 }
217             }
218         }
219         return performers;
220     }
221
222     protected class WfProcessIteratorImpl extends WfProcessIteratorWrapper implements Serializable JavaDoc {
223
224         public WfProcessIteratorImpl(SharkTransaction trans, List JavaDoc performers) throws BaseException {
225             super(trans, null, performers);
226         }
227
228         public WfProcessIteratorImpl(List JavaDoc performers) throws BaseException {
229             super(null, null, performers);
230         }
231     }
232 }
233
Popular Tags