package org.enhydra.shark;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
import org.enhydra.shark.api.client.wfmodel.InvalidRequester;
import org.enhydra.shark.api.client.wfmodel.NotEnabled;
import org.enhydra.shark.api.client.wfmodel.RequesterRequired;
import org.enhydra.shark.api.client.wfmodel.TransitionNotAllowed;
import org.enhydra.shark.api.client.wfmodel.process_mgr_stateType;
import org.enhydra.shark.api.common.SharkConstants;
import org.enhydra.shark.api.internal.instancepersistence.ProcessMgrPersistenceObject;
import org.enhydra.shark.api.internal.working.WfProcessInternal;
import org.enhydra.shark.api.internal.working.WfProcessMgrInternal;
import org.enhydra.shark.api.internal.working.WfRequesterInternal;
import org.enhydra.shark.utilities.MiscUtilities;
import org.enhydra.shark.xpdl.XMLCollectionElement;
import org.enhydra.shark.xpdl.XPDLConstants;
import org.enhydra.shark.xpdl.elements.FormalParameter;
import org.enhydra.shark.xpdl.elements.FormalParameters;
import org.enhydra.shark.xpdl.elements.WorkflowProcess;
/**
* WfProcessMgrImpl - Workflow Process Manager implementation
*
* @author Sasa Bojanic
* @author Vladimir Puskas
*/
public class WfProcessMgrImpl implements WfProcessMgrInternal {
protected WorkflowProcess wp;
protected String packageId;
protected String processDefinitionId;
protected String name;
protected process_mgr_stateType state = process_mgr_stateType.enabled;
protected Map contextSignature;
protected Map resultSignature;
protected Map inputSignature;
protected String version;
protected long created;
protected boolean justCreated = false;
/**
* Creates new WfProcessMgrImpl
*
* @param pkgId The Id of package where process definition exists.
* @param pDefId The Id of process definition.
*/
protected WfProcessMgrImpl(WMSessionHandle shandle,
String pkgId,
String version,
String pDefId) throws Exception {
this.justCreated = true;
this.name = MiscUtilities.createProcessMgrKey(pkgId, version, pDefId);
if (this.name == null) {
SharkEngineManager.getInstance()
.getCallbackUtilities()
.error(shandle, "ERROR - MGR NAME NULL WHILE CRE OF PROCMGR");
throw new Exception("Problems while determining process mgr name");
}
this.packageId = pkgId;
this.processDefinitionId = pDefId;
this.version = version;
this.created = System.currentTimeMillis();
persist(shandle);
}
/**
* Used to create object when restoring it from database.
*/
protected WfProcessMgrImpl(ProcessMgrPersistenceObject po) {
restore(po);
}
public process_mgr_stateType process_mgr_state(WMSessionHandle shandle)
throws Exception {
return state;
}
public void set_process_mgr_state(WMSessionHandle shandle,
process_mgr_stateType new_state)
throws Exception,
TransitionNotAllowed {
if (!state.equals(new_state)) {
state = new_state;
persist(shandle);
String msg = "ProcessDefinition "
+ toString() + " - the instantiation from process definition is ";
if (state.equals(process_mgr_stateType.enabled)) {
msg += "enabled";
} else {
msg += "disabled";
}
SharkEngineManager.getInstance().getCallbackUtilities().info(shandle, msg);
}
}
public String name(WMSessionHandle shandle) throws Exception {
return name;
}
public String description(WMSessionHandle shandle) throws Exception {
String desc = getProcessDefinition(shandle).getProcessHeader().getDescription();
return desc;
}
public String category(WMSessionHandle shandle) throws Exception {
String cat = getProcessDefinition(shandle).getAccessLevel();
return cat;
}
public String version(WMSessionHandle shandle) throws Exception {
return version;
}
public Map context_signature(WMSessionHandle shandle) throws Exception {
if (contextSignature == null) {
buildSignatures(shandle);
}
return new HashMap(contextSignature);
}
public Map result_signature(WMSessionHandle shandle) throws Exception {
if (resultSignature == null) {
buildSignatures(shandle);
}
return new HashMap(resultSignature);
}
public Map input_signature(WMSessionHandle shandle) throws Exception {
if (inputSignature == null) {
buildSignatures(shandle);
}
return new HashMap(inputSignature);
}
/**
* Create a WfProcess object
*/
public WfProcessInternal create_process(WMSessionHandle shandle,
WfRequesterInternal requester)
throws Exception,
NotEnabled,
InvalidRequester,
RequesterRequired {
if (state.value() == process_mgr_stateType._disabled) {
throw new NotEnabled("Can't create process based on WfProcessMgr '"
+ this + "' - it is disabled!");
}
// This can be changed - we can allow that some processes do not have to have
// requester
if (requester == null) {
throw new RequesterRequired("Can't create process based on WfProcessMgr '"
+ this + "' - the requester is required!");
}
String procId = getNextProcessKey(shandle);
WfProcessInternal process = SharkEngineManager.getInstance()
.getObjectFactory()
.createProcess(shandle, this, requester, procId);
return process;
}
public String process_definition_id(WMSessionHandle shandle) throws Exception {
return processDefinitionId;
}
public String package_id(WMSessionHandle shandle) throws Exception {
return packageId;
}
public String process_definition_name(WMSessionHandle shandle) throws Exception {
return getProcessDefinition(shandle).getName();
}
// Constructs the context/result signatures from the formalParameters
protected void buildSignatures(WMSessionHandle shandle) throws Exception {
contextSignature = new HashMap();
resultSignature = new HashMap();
inputSignature = new HashMap();
FormalParameters fps = getProcessDefinition(shandle).getFormalParameters();
Iterator it = fps.toElements().iterator();
while (it.hasNext()) {
FormalParameter fp = (FormalParameter) it.next();
String id = fp.getId();
String mode = fp.getMode();
String javaType = SharkUtilities.getJavaClass(fp).getName();
if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_IN)
|| mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
inputSignature.put(id, javaType);
}
if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT)
|| mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
resultSignature.put(id, javaType);
}
}
Collection dfsAndFPs = getProcessDefinition(shandle).getAllVariables().values();
it = dfsAndFPs.iterator();
while (it.hasNext()) {
XMLCollectionElement dfOrFp = (XMLCollectionElement) it.next();
String id = dfOrFp.getId();
String javaType = SharkUtilities.getJavaClass(dfOrFp).getName();
contextSignature.put(id, javaType);
}
}
protected String getNextProcessKey(WMSessionHandle shandle) {
String pk = SharkUtilities.getNextId(SharkConstants.PROCESS__ID_NAME);
// calculate 20 spaces for the Long, and do it twice because of
// the activities Id, also calculate 3 spaces for underscore
if (pk == null || packageId == null || processDefinitionId == null) {
SharkEngineManager.getInstance()
.getCallbackUtilities()
.error(shandle,
"PK="
+ pk + ", packageId=" + packageId + ", pDefId="
+ processDefinitionId);
}
pk = pk + "_" + packageId + "_" + processDefinitionId;
if (pk.length() > 100)
pk = pk.substring(0, 100);
return pk;
}
protected WorkflowProcess getProcessDefinition(WMSessionHandle shandle)
throws Exception {
if (wp == null) {
wp = SharkUtilities.getWorkflowProcess(shandle,
packageId,
version,
processDefinitionId);
}
return wp;
}
public String toString() {
return "[name="
+ name + ",version=" + version + ",pkgId=" + packageId + ",pDefId="
+ processDefinitionId + "]";
}
/**
* It is assumed that there can't be two or more processes mgrs having the same package
* id and process definition id.
*/
public boolean equals(Object obj) {
if (!(obj instanceof WfProcessMgrImpl))
return false;
return (((WfProcessMgrImpl) obj).name.equals(name)); // &&
// mgr.version().equals(version()));
}
public int hashCode() {
return name.hashCode();
}
public void persist(WMSessionHandle shandle) throws Exception {
SharkEngineManager.getInstance()
.getInstancePersistenceManager()
.persist(shandle, createAndFillPersistentObject(), this.justCreated);
this.justCreated = false;
}
public void delete(WMSessionHandle shandle) throws Exception {
SharkEngineManager.getInstance()
.getInstancePersistenceManager()
.deleteProcessMgr(shandle, name);
}
protected ProcessMgrPersistenceObject createAndFillPersistentObject() {
ProcessMgrPersistenceObject po = new ProcessMgrPersistenceObject();
fillPersistentObject(po);
return po;
}
protected void fillPersistentObject(ProcessMgrPersistenceObject po) {
po.setName(this.name);
po.setPackageId(this.packageId);
po.setProcessDefinitionId(this.processDefinitionId);
po.setState(this.state.value());
po.setVersion(this.version);
po.setCreated(this.created);
}
protected void restore(ProcessMgrPersistenceObject po) {
this.name = po.getName();
this.packageId = po.getPackageId();
this.processDefinitionId = po.getProcessDefinitionId();
this.state = process_mgr_stateType.from_int(po.getState());
this.version = po.getVersion();
this.created = po.getCreated();
}
}
|