001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.shark.requester;
019:
020: import java.util.List;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.HashMap;
025: import java.io.Serializable;
026:
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.entity.GenericDelegator;
029: import org.ofbiz.entity.GenericEntityException;
030: import org.ofbiz.base.util.UtilMisc;
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.shark.container.SharkContainer;
033:
034: import org.enhydra.shark.api.client.wfmodel.WfProcessIterator;
035: import org.enhydra.shark.api.client.wfmodel.WfProcess;
036: import org.enhydra.shark.api.client.wfmodel.WfEventAudit;
037: import org.enhydra.shark.api.client.wfmodel.InvalidPerformer;
038: import org.enhydra.shark.api.client.wfmodel.WfRequester;
039: import org.enhydra.shark.api.client.wfmodel.SourceNotAvailable;
040: import org.enhydra.shark.api.client.wfbase.BaseException;
041: import org.enhydra.shark.api.client.wfservice.AdminInterface;
042: import org.enhydra.shark.api.client.wfservice.ExecutionAdministration;
043: import org.enhydra.shark.api.client.wfservice.ConnectFailed;
044: import org.enhydra.shark.api.client.wfservice.NotConnected;
045: import org.enhydra.shark.api.SharkTransaction;
046: import org.enhydra.shark.WfProcessIteratorWrapper;
047:
048: /**
049: * Shark Workflow Abstract Requester
050: */
051: public abstract class AbstractRequester implements WfRequester,
052: Serializable {
053:
054: public static final String module = LoggingRequester.class
055: .getName();
056: protected transient GenericDelegator delegator = null;
057: protected transient GenericValue userLogin = null;
058: protected String delegatorName = null;
059: protected String userLoginId = null;
060: protected List performerIds = new ArrayList();
061:
062: public AbstractRequester(GenericValue userLogin) {
063: this .delegator = userLogin.getDelegator();
064: this .userLogin = userLogin;
065:
066: this .delegatorName = delegator.getDelegatorName();
067: this .userLoginId = userLogin.getString("userLoginId");
068: }
069:
070: public void addPerformer(WfProcess process) throws BaseException {
071: performerIds.add(process.key());
072: }
073:
074: public int how_many_performer() throws BaseException {
075: return performerIds.size();
076: }
077:
078: public int how_many_performer(SharkTransaction trans)
079: throws BaseException {
080: return performerIds.size();
081: }
082:
083: public WfProcessIterator get_iterator_performer()
084: throws BaseException {
085: return new WfProcessIteratorImpl(this .getPerformers());
086: }
087:
088: public WfProcessIterator get_iterator_performer(
089: SharkTransaction trans) throws BaseException {
090: return new WfProcessIteratorImpl(trans, this .getPerformers());
091: }
092:
093: public WfProcess[] get_sequence_performer(int i)
094: throws BaseException {
095: if (i > how_many_performer()) {
096: i = how_many_performer();
097: }
098: return (WfProcess[]) this .getPerformers().subList(0, i)
099: .toArray();
100: }
101:
102: public WfProcess[] get_sequence_performer(SharkTransaction trans,
103: int i) throws BaseException {
104: if (i > how_many_performer()) {
105: i = how_many_performer();
106: }
107: return (WfProcess[]) this .getPerformers().subList(0, i)
108: .toArray();
109: }
110:
111: public boolean is_member_of_performer(WfProcess process)
112: throws BaseException {
113: return performerIds.contains(process.key());
114: }
115:
116: public boolean is_member_of_performer(SharkTransaction trans,
117: WfProcess process) throws BaseException {
118: return performerIds.contains(process.key());
119: }
120:
121: public abstract void receive_event(WfEventAudit event)
122: throws BaseException, InvalidPerformer;
123:
124: public abstract void receive_event(SharkTransaction trans,
125: WfEventAudit event) throws BaseException, InvalidPerformer;
126:
127: protected Map getWRD(WfEventAudit event, Map initialContext)
128: throws BaseException {
129: Map wrdMap = new HashMap();
130:
131: // set the initial context (overrided by any new data)
132: if (initialContext != null) {
133: wrdMap.putAll(initialContext);
134: }
135:
136: // these are static values available to the service
137: wrdMap.put("eventType", event.event_type());
138: wrdMap.put(org.ofbiz.shark.SharkConstants.activityId, event
139: .activity_key());
140: wrdMap.put(org.ofbiz.shark.SharkConstants.activityName, event
141: .activity_name());
142: wrdMap.put(org.ofbiz.shark.SharkConstants.processId, event
143: .process_key());
144: wrdMap.put(org.ofbiz.shark.SharkConstants.processName, event
145: .process_name());
146: wrdMap.put(org.ofbiz.shark.SharkConstants.processMgrName, event
147: .process_mgr_name());
148: wrdMap.put("processMgrVersion", event.process_mgr_version());
149: wrdMap.put("eventTime", event.time_stamp().getTimestamp());
150:
151: // all WRD is also available to the service, but what this contains is not known
152: try {
153: Map wrd = new HashMap(event.source().process_context());
154: if (wrd != null) {
155: wrdMap.put("_WRDMap", wrd);
156: wrdMap.putAll(wrd);
157: }
158: } catch (SourceNotAvailable e) {
159: Debug
160: .logError(
161: e,
162: "No WRD available since event.source() cannot be obtained",
163: module);
164: }
165:
166: return wrdMap;
167: }
168:
169: protected synchronized GenericDelegator getDelegator() {
170: if (this .delegator == null && this .delegatorName != null) {
171: this .delegator = GenericDelegator
172: .getGenericDelegator(this .delegatorName);
173: }
174: return this .delegator;
175: }
176:
177: protected synchronized GenericValue getUserLogin()
178: throws GenericEntityException {
179: if (userLogin == null && this .userLoginId != null) {
180: GenericDelegator delegator = this .getDelegator();
181: if (delegator != null) {
182: this .userLogin = delegator.findByPrimaryKey(
183: "UserLogin", UtilMisc.toMap("userLoginId",
184: this .userLoginId));
185: }
186: }
187: return this .userLogin;
188: }
189:
190: protected List getPerformers() {
191: GenericValue userLogin = null;
192: List performers = null;
193: try {
194: userLogin = this .getUserLogin();
195: } catch (GenericEntityException e) {
196: Debug.logError(e, module);
197: }
198: if (userLogin != null) {
199: AdminInterface admin = SharkContainer.getAdminInterface();
200: ExecutionAdministration exAdmin = admin
201: .getExecutionAdministration();
202: boolean connected = true;
203: try {
204: exAdmin.connect(userLogin.getString("userLoginId"),
205: userLogin.getString("currentPassword"), null,
206: null);
207: } catch (BaseException e) {
208: Debug.logError(e, module);
209: connected = false;
210: } catch (ConnectFailed e) {
211: Debug.logError(e, module);
212: connected = false;
213: }
214:
215: if (connected) {
216: performers = new ArrayList(performerIds.size());
217: Iterator i = performerIds.iterator();
218: try {
219: while (i.hasNext()) {
220: String processId = (String) i.next();
221: exAdmin.getProcess(processId);
222: }
223: } catch (Exception e) {
224: Debug.logError(e, module);
225: performers = null;
226: } finally {
227: try {
228: exAdmin.disconnect();
229: } catch (BaseException e) {
230: Debug.logError(e, module);
231: } catch (NotConnected e) {
232: Debug.logError(e, module);
233: }
234: }
235: }
236: }
237: return performers;
238: }
239:
240: protected class WfProcessIteratorImpl extends
241: WfProcessIteratorWrapper implements Serializable {
242:
243: /**
244: *
245: */
246: private static final long serialVersionUID = 1L;
247:
248: public WfProcessIteratorImpl(SharkTransaction trans,
249: List performers) throws BaseException {
250: super (trans, null, performers);
251: }
252:
253: public WfProcessIteratorImpl(List performers)
254: throws BaseException {
255: super(null, null, performers);
256: }
257: }
258: }
|