Methods / Methods for Process Instances |
Merges 2 or more process instances into one process instance.
These process instances should be based on the same process definition.
string MergeProcessInstances(IWFWorkflowService svc, string[] processInstanceIDs) { IWFWorkflowService svc = GetWorkflowService(); // suspends all of process instances to be merged. foreach (string id in processInstanceIDs) { svc.SuspendProcInst(id); } // query process instances string inExpr = ShUtil.Merge(processInstanceIDs, true); WFQueryExpr queryExpr = new WFQueryExpr("PROC_INST_ID", SQLExpr.IN, WFAny.Create(inExpr), true); WFBaseProcessInstance[] pis = svc.QueryProcInsts(queryExpr); // merge custom attributes NameValue[] mergedCustomAttributes = GetMergeCustomAttributes(api, pis); string procInstID = UUID.GetID(); string procInstName = pis[0].DefName + "_" + DateTime.Now.Tostring() + " - Merged"; string workObjectID = procInstID + " - Merged"; WFProcessMergingInstruction instruction = new WFProcessMergingInstruction(); instruction.MergingProcessInstanceIDs = processInstanceIDs; instruction.MergedProcessInstance = new WFProcessMergingInstruction.MergedProcessParameter( procInstID, procInstName, workObjectID, null, mergedCustomAttributes); instruction.Validate(); return svc.MergeProcInsts(instruction); } // sample code for merging custom attributes private NameValue[] GetMergeCustomAttributes(IWFWorkflowService svc, WFBaseProcessInstance[] pis) { List<string> workObjectIDs = new List<string>(); foreach (WFBaseProcessInstance pi in pis) { workObjectIDs.Add(pi.WorkObjectID); } KeyValue[] items = svc.GetCustomAttrsEx(workObjectIDs.ToArray()); Dictionary<string, System.Xml.XmlDocument> dss = new Dictionary<string, System.Xml.XmlDocument>(); foreach (KeyValue item in items) { WFCustomAttributes ds = new WFCustomAttributes(); ds.AttrXml = item.Value; System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.LoadXml(ds["//"] as string); dss[item.Key] = xmlDoc; } // master processInstanceID and workObjectID string masterProcessInstanceID = pis[0].ProcInstID; string masterWorkObjectID = pis[0].WorkObjectID; System.Xml.XmlDocument masterXmlDoc = dss[masterWorkObjectID]; System.Xml.XmlNamespaceManager nsm = ShUtil.GetNamespaces(masterXmlDoc); System.Xml.XmlNode titleNode = masterXmlDoc.SelectSingleNode("/pd:issueTracking/pd:issueTitle", nsm); titleNode.InnerText = "Title - Merged"; System.Xml.XmlNode descriptionNode = masterXmlDoc.SelectSingleNode("/pd:issueTracking/pd:description", nsm); descriptionNode.InnerText = "Description - Merged"; System.Xml.XmlNode parent = masterXmlDoc.SelectSingleNode("/pd:issueTracking/pd:Persons", nsm); // merge rest of children foreach (string key in dss.Keys) { if (key == masterWorkObjectID) continue; System.Xml.XmlDocument secondaryXmlDoc = dss[key]; System.Xml.XmlNamespaceManager nsmgr = ShUtil.GetNamespaces(secondaryXmlDoc); System.Xml.XmlNode node = secondaryXmlDoc.SelectSingleNode("/pd:issueTracking/pd:Persons", nsmgr); parent.InnerXml += node.InnerXml; } return NameValue.Array("//", masterXmlDoc.OuterXml); } #endregion