1 25 package org.ofbiz.shark.requester; 26 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.io.Serializable ; 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 62 public abstract class AbstractRequester implements WfRequester, Serializable { 63 64 public static final String module = LoggingRequester.class.getName(); 65 protected transient GenericDelegator delegator = null; 66 protected transient GenericValue userLogin = null; 67 protected String delegatorName = null; 68 protected String userLoginId = null; 69 protected List performerIds = new ArrayList (); 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 getWRD(WfEventAudit event, Map initialContext) throws BaseException { 126 Map wrdMap = new HashMap (); 127 128 if (initialContext != null) { 130 wrdMap.putAll(initialContext); 131 } 132 133 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 try { 145 Map wrd = new HashMap (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 getPerformers() { 176 GenericValue userLogin = null; 177 List 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 (performerIds.size()); 199 Iterator i = performerIds.iterator(); 200 try { 201 while (i.hasNext()) { 202 String processId = (String ) i.next(); 203 exAdmin.getProcess(processId); 204 } 205 } catch (Exception 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 { 223 224 public WfProcessIteratorImpl(SharkTransaction trans, List performers) throws BaseException { 225 super(trans, null, performers); 226 } 227 228 public WfProcessIteratorImpl(List performers) throws BaseException { 229 super(null, null, performers); 230 } 231 } 232 } 233 | Popular Tags |