封装K2.net 2003中的K2ROM

在用K2.net 2003开发Workflow时,我们常常要封装一个K2ROM以对K2流程进行处理.下面就是一个K2流程要用到的最基本的一些操作.

using System;
using System.Collections;
using SourceCode.K2ROM;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace K2ROM
{
    
/// <summary>
    
/// Summary description for K2Helper.
    
/// </summary>

    public class K2Helper
    
{
        
public K2Helper()
        
{
            
//
            
// TODO: Add constructor logic here
            
//
        }



        
public static string GetConnectionString(string domain, string userName, string password)
        
{
            
return string.Format(@"[;];Authenication=Windows;Domain={0};User={1};Password={2}", domain, userName, "Dell1234");
        }



        
/// <summary>
        
/// Authenication
        
/// </summary>

        public static string ConnectionString = "[;];Authenication=Windows;Domain=mstech;User=administrator;Password=Satyam123241";


        
/// <summary>
        
/// Getting the K2 server name.
        
/// </summary>

        public static string ServerName = ConfigurationManager.AppSettings["K2Server"].ToString();


        
/// <summary>
        
/// Start a process instance in k2 server.
        
/// </summary>
        
/// <param name="processName">Process name</param>
        
/// <param name="param">Process instance data,stored as a hashtable</param>
        
/// <returns>Return process instance</returns>

        public static ProcessInstance StartProcessInstance(string processName, Hashtable param)
        
{
            Connection con 
= new Connection();

            con.Open(ServerName);
            ProcessInstance instance 
= con.CreateProcessInstance(processName);
            IDictionaryEnumerator list 
= param.GetEnumerator();
            
while (list.MoveNext())
                instance.DataFields[list.Key.ToString()].Value 
= list.Value;
            con.StartProcessInstance(instance);
            con.Close();
            
return instance;

        }

        
/// <summary>
        
/// Submit the result to K2 server.
        
/// </summary>
        
/// <param name="item">WorklistItem</param>
        
/// <param name="isSubmit">Stop or Finish</param>
        
/// <returns></returns>

        public static bool UpdateWorkItem(WorklistItem item, bool isSubmit)
        
{
            
if (item == null)
                
throw new NullReferenceException("WorklitItem item is null!");

            
if (isSubmit)
                item.Finish();
            
else
                item.Update();
            
return true;
        }



        
/// <summary>
        
/// Redirect the work item to specified person.
        
/// </summary>
        
/// <param name="item"></param>
        
/// <param name="userName"></param>
        
/// <returns></returns>

        public static bool RedirectWorkItem(WorklistItem item, string userName)
        
{
            
if (item != null)
            
{
                item.Redirect(userName);
                
return true;
            }

            
return false;
        }



        
/// <summary>
        
/// Get WorklistItem by serialnumber.
        
/// </summary>
        
/// <param name="serialNumber"></param>
        
/// <returns></returns>

        public static WorklistItem GetWorkistItem(string serialNumber)
        
{
            Connection con 
= new Connection();
            con.Open(ServerName);

            WorklistItem item 
= con.OpenWorklistItem(serialNumber, "ASP");
            
return item;

        }


        
/// <summary>
        
/// Open a server item
        
/// </summary>
        
/// <param name="serialNumber"></param>
        
/// <returns></returns>

        public static ServerItem GetServerItem(string serialNumber)
        
{
            Connection con 
= new Connection();
            con.Open(ServerName);
            
return con.OpenServerItem(serialNumber);
        }



        
/// <summary>
        
/// convert worklist to dataset
        
/// </summary>
        
/// <param name="list"></param>
        
/// <returns></returns>

        public static DataTable GetWorklistDataTable(Worklist list)
        
{
            
if (list == null || list.Count == 0)
                
return null;
            DataTable dt 
= CreatWorkListTable();
            
foreach (WorklistItem item in list)
            
{
                DataRow dr 
= dt.NewRow();
                dr[
"ProcInstID"= item.ProcessInstance.ID;
                dr[
"StartDate"= item.ProcessInstance.StartDate;
                dr[
"SerialNumber"= item.SerialNumber;
                
//The id implement alternant opration between K2 DB and Location DB.
                dr["WorkItemID"= item.ProcessInstance.DataFields["WorkItemID"].Value.ToString();
                dt.Rows.Add(dr);

            }

            
return dt;
        }


        
/// <summary>
        
/// Creat a datatable to store WorkList.
        
/// </summary>
        
/// <returns></returns>

        public static DataTable CreatWorkListTable()
        
{
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
"ProcInstID");
            dt.Columns.Add(
"StartDate");
            dt.Columns.Add(
"SerialNumber");
            dt.Columns.Add(
"WorkItemID");
            
return dt;
        }


        
/// <summary>
        
/// Get Worklist by criteria.
        
/// </summary>
        
/// <param name="criteria"></param>
        
/// <returns></returns>

        public static Worklist GetWorklist(WorklistCriteria criteria)
        
{
            Connection con 
= new Connection();
            con.Open(ServerName);
            
return con.OpenWorklist(criteria);
        }


        
/// <summary>
        
/// Get Worklist by process name.
        
/// </summary>
        
/// <param name="processName"></param>
        
/// <returns></returns>

        public static Worklist GetWorklist(string processName)
        
{
            WorklistCriteria criteria 
= new WorklistCriteria();
            criteria.AddFilterField(WCField.ProcessName, WCCompare.Equal, processName);
            
return GetWorklist(criteria);
        }


        
/// <summary>
        
/// Get Worklist by process name and activity name.
        
/// </summary>
        
/// <param name="processName"></param>
        
/// <param name="activityName"></param>
        
/// <returns></returns>

        public static Worklist GetWorklist(string processName, string activityName)
        
{

            WorklistCriteria criteria 
= new WorklistCriteria();
            criteria.AddFilterField(WCField.ProcessName, WCCompare.Equal, processName);
            criteria.AddFilterField(WCField.ActivityName, WCCompare.Equal, activityName);
            
return GetWorklist(criteria);
        }


        
/// <summary>
        
/// Stop a process instance by process ID.
        
/// </summary>
        
/// <param name="ProcessID"></param>
        
/// <returns></returns>

        public static bool StopProcessInstance(int ProcessID)
        
{
            SourceCode.K2Mng.K2Manager mgr 
= K2Helper.GetK2Manager();
            
bool flag = mgr.StopProcessInstances(ProcessID);
            mgr.Logout();
            
return flag;
        }


        
/// <summary>
        
/// Access K2 service as admin.
        
/// </summary>
        
/// <returns></returns>

        public static SourceCode.K2Mng.K2Manager GetK2Manager()
        
{
            SourceCode.K2Mng.K2Manager mgr 
= new SourceCode.K2Mng.K2Manager();
            
try
            
{
                mgr.Login(ConfigurationManager.AppSettings[
"K2Server"], int.Parse(ConfigurationManager.AppSettings["K2AdminPort"]), ConfigurationManager.AppSettings["K2AdminConnectionString"]);
            }

            
catch (Exception ex)
            
{
                
throw new Exception("Access to K2 Server denied!", ex);
            }

            
return mgr;
        }

    }

}

posted on 2007-07-13 15:27  残叶  阅读(1508)  评论(1编辑  收藏  举报

导航