1 package org.enhydra.shark; 2 3 import java.util.Collection ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import org.enhydra.shark.api.SharkTransaction; 9 import org.enhydra.shark.api.TransactionException; 10 import org.enhydra.shark.api.client.wfbase.BaseException; 11 import org.enhydra.shark.api.client.wfmodel.InvalidRequester; 12 import org.enhydra.shark.api.client.wfmodel.NotEnabled; 13 import org.enhydra.shark.api.client.wfmodel.RequesterRequired; 14 import org.enhydra.shark.api.client.wfmodel.TransitionNotAllowed; 15 import org.enhydra.shark.api.client.wfmodel.process_mgr_stateType; 16 import org.enhydra.shark.api.common.SharkConstants; 17 import org.enhydra.shark.api.internal.instancepersistence.PersistenceException; 18 import org.enhydra.shark.api.internal.instancepersistence.ProcessMgrPersistenceInterface; 19 import org.enhydra.shark.api.internal.working.WfProcessInternal; 20 import org.enhydra.shark.api.internal.working.WfProcessMgrInternal; 21 import org.enhydra.shark.api.internal.working.WfRequesterInternal; 22 import org.enhydra.shark.xpdl.XMLCollectionElement; 23 import org.enhydra.shark.xpdl.XPDLConstants; 24 import org.enhydra.shark.xpdl.elements.FormalParameter; 25 import org.enhydra.shark.xpdl.elements.FormalParameters; 26 import org.enhydra.shark.xpdl.elements.WorkflowProcess; 27 28 33 public class WfProcessMgrImpl implements WfProcessMgrInternal { 34 35 private WorkflowProcess wp; 36 37 private String packageId; 38 private String processDefinitionId; 39 40 private String name; 41 42 private process_mgr_stateType state=process_mgr_stateType.enabled; 43 44 Map contextSignature; 45 Map resultSignature; 46 Map inputSignature; 47 48 private String version; 49 private long created; 50 51 private boolean justCreated=false; 52 53 58 protected WfProcessMgrImpl(SharkTransaction t,String pkgId,String version,String pDefId) throws BaseException { 59 this.justCreated = true; 60 this.name=SharkUtilities.createProcessMgrKey(pkgId,version,pDefId); 61 if (this.name==null) { 62 SharkEngineManager.getInstance().getCallbackUtilities().error("ERROR - MGR NAME NULL WHILE CRE OF PROCMGR"); 63 throw new BaseException ("Problems while determining process mgr name"); 64 } 65 66 this.packageId=pkgId; 67 this.processDefinitionId=pDefId; 68 this.version=version; 69 this.created=System.currentTimeMillis(); 70 try { 71 persist(t); 72 } catch (TransactionException tme) { 73 throw new BaseException(tme); 74 } 75 } 76 77 80 protected WfProcessMgrImpl (ProcessMgrPersistenceInterface po) { 81 restore(po); 82 } 83 84 public process_mgr_stateType process_mgr_state (SharkTransaction t) throws BaseException { 85 return state; 86 } 87 88 public void set_process_mgr_state (SharkTransaction t,process_mgr_stateType new_state) throws BaseException, TransitionNotAllowed { 89 if (!state.equals(new_state)) { 90 state=new_state; 91 try { 92 persist(t); 93 } catch (Exception ex) { 94 throw new BaseException(ex); 95 } 96 String msg="ProcessDefinition "+toString()+" - the instantiation from process definition is "; 97 if (state.equals(process_mgr_stateType.enabled)) { 98 msg+="enabled"; 99 } else { 100 msg+="disabled"; 101 } 102 SharkEngineManager.getInstance().getCallbackUtilities().info(msg); 103 } 104 } 105 106 public final String name (SharkTransaction t) throws BaseException { 107 return name; 108 } 109 110 public String description (SharkTransaction t) throws BaseException { 111 try { 112 String desc=getProcessDefinition().getProcessHeader().getDescription(); 113 114 return desc; 115 } catch (Exception ex) { 116 throw new BaseException(ex); 117 } 118 } 119 120 public String category (SharkTransaction t) throws BaseException { 121 try { 122 String cat=getProcessDefinition().getAccessLevel(); 123 return cat; 124 } catch (Exception ex) { 125 throw new BaseException(ex); 126 } 127 } 128 129 public final String version (SharkTransaction t) throws BaseException { 130 return version; 131 } 132 133 public Map context_signature (SharkTransaction t) throws BaseException { 134 try { 135 if (contextSignature==null) { 136 buildSignatures(t); 137 } 138 return new HashMap (contextSignature); 139 } catch (Exception ex) { 140 throw new BaseException(ex); 141 } 142 } 143 144 public Map result_signature (SharkTransaction t) throws BaseException { 145 try { 146 if (resultSignature==null) { 147 buildSignatures(t); 148 } 149 return new HashMap (resultSignature); 150 } catch (Exception ex) { 151 throw new BaseException(ex); 152 } 153 } 154 155 public Map input_signature (SharkTransaction t) throws BaseException { 156 try { 157 if (inputSignature==null) { 158 buildSignatures(t); 159 } 160 return new HashMap (inputSignature); 161 } catch (Exception ex) { 162 throw new BaseException(ex); 163 } 164 } 165 166 169 public WfProcessInternal create_process (SharkTransaction t,WfRequesterInternal requester) throws BaseException, 170 NotEnabled, InvalidRequester, RequesterRequired { 171 if (state.value()==process_mgr_stateType._disabled) { 172 throw new NotEnabled("Can't create process based on WfProcessMgr '"+this+"' - it is disabled!"); 173 } 174 if (requester == null) { 176 throw new RequesterRequired("Can't create process based on WfProcessMgr '"+this+"' - the requester is required!"); 177 } 178 String procId=getNextProcessKey(); 179 WfProcessInternal process = SharkEngineManager.getInstance(). 180 getObjectFactory().createProcess(t,this,requester,procId); 181 return process; 182 } 183 184 public final String process_definition_id(SharkTransaction t) throws BaseException { 185 return processDefinitionId; 186 } 187 188 public final String package_id(SharkTransaction t) throws BaseException { 189 return packageId; 190 } 191 192 public String process_definition_name(SharkTransaction t) throws BaseException { 193 try { 194 return getProcessDefinition().getName(); 195 } catch (Exception ex){ 196 throw new BaseException(ex); 197 } 198 } 199 200 private void buildSignatures(SharkTransaction t) throws BaseException { 202 contextSignature = new HashMap (); 203 resultSignature = new HashMap (); 204 inputSignature = new HashMap (); 205 206 FormalParameters fps=getProcessDefinition().getFormalParameters(); 207 Iterator it=fps.toElements().iterator(); 208 while (it.hasNext()) { 209 FormalParameter fp=(FormalParameter)it.next(); 210 String id = fp.getId(); 211 String mode=fp.getMode(); 212 String javaType=SharkUtilities.getJavaClass(fp).getName(); 213 214 if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_IN) || mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) { 215 inputSignature.put(id,javaType); 216 } 217 if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) || mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) { 218 resultSignature.put(id,javaType); 219 } 220 } 221 222 Collection dfsAndFPs=getProcessDefinition().getAllVariables().values(); 223 224 it=dfsAndFPs.iterator(); 225 while (it.hasNext()) { 226 XMLCollectionElement dfOrFp=(XMLCollectionElement)it.next(); 227 String id = dfOrFp.getId(); 228 229 String javaType=SharkUtilities.getJavaClass(dfOrFp).getName(); 230 contextSignature.put(id,javaType); 231 } 232 233 } 234 235 protected String getNextProcessKey() { 236 String pk = SharkUtilities.getNextId(SharkConstants.PROCESS__ID_NAME); 237 if (pk==null || packageId==null || processDefinitionId==null) { 240 SharkEngineManager.getInstance().getCallbackUtilities().error("PK="+pk+", packageId="+packageId+", pDefId="+processDefinitionId); 241 } 242 pk=pk+"_"+packageId+"_"+processDefinitionId; 243 if (pk.length()>100) pk=pk.substring(0,100); 244 return pk; 245 } 246 247 private WorkflowProcess getProcessDefinition () throws BaseException{ 248 if (wp==null) { 249 wp=SharkUtilities.getWorkflowProcess( 250 packageId, 251 version, 252 processDefinitionId); 253 } 254 return wp; 255 } 256 257 public String toString () { 258 return "[name="+name+",version="+version+",pkgId="+packageId+",pDefId="+processDefinitionId+"]"; 259 } 260 261 265 public boolean equals (Object obj) { 266 if (!(obj instanceof WfProcessMgrImpl)) return false; 267 return (((WfProcessMgrImpl)obj).name.equals(name)); } 269 270 public void persist (SharkTransaction t) throws TransactionException { 271 try { 272 SharkEngineManager 273 .getInstance() 274 .getInstancePersistenceManager() 275 .persist(createAndFillPersistentObject(), this.justCreated, t); 276 this.justCreated = false; 277 } catch (PersistenceException pe) { 278 throw new TransactionException(pe); 279 } 280 } 281 282 public void delete (SharkTransaction t) throws TransactionException { 283 try { 284 SharkEngineManager 285 .getInstance() 286 .getInstancePersistenceManager() 287 .deleteProcessMgr(name,t); 288 } catch (Exception ex) { 289 throw new TransactionException("Exception while deleting ProcessMgr",ex); 290 } 291 } 292 293 private ProcessMgrPersistenceInterface createAndFillPersistentObject () { 294 ProcessMgrPersistenceInterface po= 295 SharkEngineManager.getInstance().getInstancePersistenceManager().createProcessMgr(); 296 fillPersistentObject(po); 297 return po; 298 } 299 300 private void fillPersistentObject (ProcessMgrPersistenceInterface po) { 301 po.setName(this.name); 302 po.setPackageId(this.packageId); 303 po.setProcessDefinitionId(this.processDefinitionId); 304 po.setState(this.state.value()); 305 po.setVersion(this.version); 306 po.setCreated(this.created); 307 } 308 309 private void restore (ProcessMgrPersistenceInterface po) { 310 this.name=po.getName(); 311 if (this.name==null) { 312 SharkEngineManager.getInstance().getCallbackUtilities().error("ERROR - MGR NAME NULL WHILE REST OF PROCMGR"); 313 } 314 315 this.packageId=po.getPackageId(); 316 this.processDefinitionId=po.getProcessDefinitionId(); 317 this.state=process_mgr_stateType.from_int(po.getState()); 318 this.version=po.getVersion(); 319 this.created=po.getCreated(); 320 } 321 322 } 323 | Popular Tags |