001: package org.enhydra.shark;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.Map;
007:
008: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
009: import org.enhydra.shark.api.client.wfmodel.InvalidRequester;
010: import org.enhydra.shark.api.client.wfmodel.NotEnabled;
011: import org.enhydra.shark.api.client.wfmodel.RequesterRequired;
012: import org.enhydra.shark.api.client.wfmodel.TransitionNotAllowed;
013: import org.enhydra.shark.api.client.wfmodel.process_mgr_stateType;
014: import org.enhydra.shark.api.common.SharkConstants;
015: import org.enhydra.shark.api.internal.instancepersistence.ProcessMgrPersistenceObject;
016: import org.enhydra.shark.api.internal.working.WfProcessInternal;
017: import org.enhydra.shark.api.internal.working.WfProcessMgrInternal;
018: import org.enhydra.shark.api.internal.working.WfRequesterInternal;
019: import org.enhydra.shark.utilities.MiscUtilities;
020: import org.enhydra.shark.xpdl.XMLCollectionElement;
021: import org.enhydra.shark.xpdl.XPDLConstants;
022: import org.enhydra.shark.xpdl.elements.FormalParameter;
023: import org.enhydra.shark.xpdl.elements.FormalParameters;
024: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
025:
026: /**
027: * WfProcessMgrImpl - Workflow Process Manager implementation
028: *
029: * @author Sasa Bojanic
030: * @author Vladimir Puskas
031: */
032: public class WfProcessMgrImpl implements WfProcessMgrInternal {
033:
034: protected WorkflowProcess wp;
035:
036: protected String packageId;
037:
038: protected String processDefinitionId;
039:
040: protected String name;
041:
042: protected process_mgr_stateType state = process_mgr_stateType.enabled;
043:
044: protected Map contextSignature;
045:
046: protected Map resultSignature;
047:
048: protected Map inputSignature;
049:
050: protected String version;
051:
052: protected long created;
053:
054: protected boolean justCreated = false;
055:
056: /**
057: * Creates new WfProcessMgrImpl
058: *
059: * @param pkgId The Id of package where process definition exists.
060: * @param pDefId The Id of process definition.
061: */
062: protected WfProcessMgrImpl(WMSessionHandle shandle, String pkgId,
063: String version, String pDefId) throws Exception {
064: this .justCreated = true;
065: this .name = MiscUtilities.createProcessMgrKey(pkgId, version,
066: pDefId);
067: if (this .name == null) {
068: SharkEngineManager
069: .getInstance()
070: .getCallbackUtilities()
071: .error(shandle,
072: "ERROR - MGR NAME NULL WHILE CRE OF PROCMGR");
073: throw new Exception(
074: "Problems while determining process mgr name");
075: }
076:
077: this .packageId = pkgId;
078: this .processDefinitionId = pDefId;
079: this .version = version;
080: this .created = System.currentTimeMillis();
081:
082: persist(shandle);
083: }
084:
085: /**
086: * Used to create object when restoring it from database.
087: */
088: protected WfProcessMgrImpl(ProcessMgrPersistenceObject po) {
089: restore(po);
090: }
091:
092: public process_mgr_stateType process_mgr_state(
093: WMSessionHandle shandle) throws Exception {
094: return state;
095: }
096:
097: public void set_process_mgr_state(WMSessionHandle shandle,
098: process_mgr_stateType new_state) throws Exception,
099: TransitionNotAllowed {
100: if (!state.equals(new_state)) {
101: state = new_state;
102:
103: persist(shandle);
104:
105: String msg = "ProcessDefinition "
106: + toString()
107: + " - the instantiation from process definition is ";
108:
109: if (state.equals(process_mgr_stateType.enabled)) {
110: msg += "enabled";
111: } else {
112: msg += "disabled";
113: }
114:
115: SharkEngineManager.getInstance().getCallbackUtilities()
116: .info(shandle, msg);
117: }
118: }
119:
120: public String name(WMSessionHandle shandle) throws Exception {
121: return name;
122: }
123:
124: public String description(WMSessionHandle shandle) throws Exception {
125: String desc = getProcessDefinition(shandle).getProcessHeader()
126: .getDescription();
127:
128: return desc;
129: }
130:
131: public String category(WMSessionHandle shandle) throws Exception {
132: String cat = getProcessDefinition(shandle).getAccessLevel();
133: return cat;
134: }
135:
136: public String version(WMSessionHandle shandle) throws Exception {
137: return version;
138: }
139:
140: public Map context_signature(WMSessionHandle shandle)
141: throws Exception {
142: if (contextSignature == null) {
143: buildSignatures(shandle);
144: }
145: return new HashMap(contextSignature);
146: }
147:
148: public Map result_signature(WMSessionHandle shandle)
149: throws Exception {
150: if (resultSignature == null) {
151: buildSignatures(shandle);
152: }
153:
154: return new HashMap(resultSignature);
155: }
156:
157: public Map input_signature(WMSessionHandle shandle)
158: throws Exception {
159: if (inputSignature == null) {
160: buildSignatures(shandle);
161: }
162:
163: return new HashMap(inputSignature);
164: }
165:
166: /**
167: * Create a WfProcess object
168: */
169: public WfProcessInternal create_process(WMSessionHandle shandle,
170: WfRequesterInternal requester) throws Exception,
171: NotEnabled, InvalidRequester, RequesterRequired {
172: if (state.value() == process_mgr_stateType._disabled) {
173: throw new NotEnabled(
174: "Can't create process based on WfProcessMgr '"
175: + this + "' - it is disabled!");
176: }
177: // This can be changed - we can allow that some processes do not have to have
178: // requester
179: if (requester == null) {
180: throw new RequesterRequired(
181: "Can't create process based on WfProcessMgr '"
182: + this + "' - the requester is required!");
183: }
184: String procId = getNextProcessKey(shandle);
185: WfProcessInternal process = SharkEngineManager.getInstance()
186: .getObjectFactory().createProcess(shandle, this ,
187: requester, procId);
188:
189: return process;
190: }
191:
192: public String process_definition_id(WMSessionHandle shandle)
193: throws Exception {
194: return processDefinitionId;
195: }
196:
197: public String package_id(WMSessionHandle shandle) throws Exception {
198: return packageId;
199: }
200:
201: public String process_definition_name(WMSessionHandle shandle)
202: throws Exception {
203: return getProcessDefinition(shandle).getName();
204: }
205:
206: // Constructs the context/result signatures from the formalParameters
207: protected void buildSignatures(WMSessionHandle shandle)
208: throws Exception {
209: contextSignature = new HashMap();
210: resultSignature = new HashMap();
211: inputSignature = new HashMap();
212:
213: FormalParameters fps = getProcessDefinition(shandle)
214: .getFormalParameters();
215: Iterator it = fps.toElements().iterator();
216: while (it.hasNext()) {
217: FormalParameter fp = (FormalParameter) it.next();
218: String id = fp.getId();
219: String mode = fp.getMode();
220: String javaType = SharkUtilities.getJavaClass(fp).getName();
221:
222: if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_IN)
223: || mode
224: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
225: inputSignature.put(id, javaType);
226: }
227: if (mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT)
228: || mode
229: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
230: resultSignature.put(id, javaType);
231: }
232: }
233:
234: Collection dfsAndFPs = getProcessDefinition(shandle)
235: .getAllVariables().values();
236:
237: it = dfsAndFPs.iterator();
238: while (it.hasNext()) {
239: XMLCollectionElement dfOrFp = (XMLCollectionElement) it
240: .next();
241: String id = dfOrFp.getId();
242:
243: String javaType = SharkUtilities.getJavaClass(dfOrFp)
244: .getName();
245: contextSignature.put(id, javaType);
246: }
247:
248: }
249:
250: protected String getNextProcessKey(WMSessionHandle shandle) {
251: String pk = SharkUtilities
252: .getNextId(SharkConstants.PROCESS__ID_NAME);
253: // calculate 20 spaces for the Long, and do it twice because of
254: // the activities Id, also calculate 3 spaces for underscore
255: if (pk == null || packageId == null
256: || processDefinitionId == null) {
257: SharkEngineManager
258: .getInstance()
259: .getCallbackUtilities()
260: .error(
261: shandle,
262: "PK=" + pk + ", packageId=" + packageId
263: + ", pDefId=" + processDefinitionId);
264: }
265: pk = pk + "_" + packageId + "_" + processDefinitionId;
266: if (pk.length() > 100)
267: pk = pk.substring(0, 100);
268: return pk;
269: }
270:
271: protected WorkflowProcess getProcessDefinition(
272: WMSessionHandle shandle) throws Exception {
273: if (wp == null) {
274: wp = SharkUtilities.getWorkflowProcess(shandle, packageId,
275: version, processDefinitionId);
276: }
277: return wp;
278: }
279:
280: public String toString() {
281: return "[name=" + name + ",version=" + version + ",pkgId="
282: + packageId + ",pDefId=" + processDefinitionId + "]";
283: }
284:
285: /**
286: * It is assumed that there can't be two or more processes mgrs having the same package
287: * id and process definition id.
288: */
289: public boolean equals(Object obj) {
290: if (!(obj instanceof WfProcessMgrImpl))
291: return false;
292: return (((WfProcessMgrImpl) obj).name.equals(name)); // &&
293: // mgr.version().equals(version()));
294: }
295:
296: public int hashCode() {
297: return name.hashCode();
298: }
299:
300: public void persist(WMSessionHandle shandle) throws Exception {
301: SharkEngineManager.getInstance()
302: .getInstancePersistenceManager().persist(shandle,
303: createAndFillPersistentObject(),
304: this .justCreated);
305: this .justCreated = false;
306: }
307:
308: public void delete(WMSessionHandle shandle) throws Exception {
309: SharkEngineManager.getInstance()
310: .getInstancePersistenceManager().deleteProcessMgr(
311: shandle, name);
312: }
313:
314: protected ProcessMgrPersistenceObject createAndFillPersistentObject() {
315: ProcessMgrPersistenceObject po = new ProcessMgrPersistenceObject();
316: fillPersistentObject(po);
317: return po;
318: }
319:
320: protected void fillPersistentObject(ProcessMgrPersistenceObject po) {
321: po.setName(this .name);
322: po.setPackageId(this .packageId);
323: po.setProcessDefinitionId(this .processDefinitionId);
324: po.setState(this .state.value());
325: po.setVersion(this .version);
326: po.setCreated(this .created);
327: }
328:
329: protected void restore(ProcessMgrPersistenceObject po) {
330: this.name = po.getName();
331: this.packageId = po.getPackageId();
332: this.processDefinitionId = po.getProcessDefinitionId();
333: this.state = process_mgr_stateType.from_int(po.getState());
334: this.version = po.getVersion();
335: this.created = po.getCreated();
336: }
337:
338: }
|