📄 trackingprofilemanager.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Runtime.Tracking;
using System.Workflow.ComponentModel;
using System.IO;
using System.Data.SqlClient;
using System.Globalization;
using System.Data;
namespace wxwinter.wf.Service
{
public class TrackingProfileManager
{
/// <summary>
/// 生成一个自定义Profile
/// </summary>
/// <param name="version">格式 3.0.0.0</param>
/// <returns></returns>
public static TrackingProfile createProfile(string version)
{
Version ver = new Version(version);
//格式 3.0.0.0
//版本信息同时存在"TrackingProfile表Version字段"与"Profile的XML字串"中
#region <TrackingProfile>结点
//根结点
TrackingProfile profile = new TrackingProfile(); //<TrackingProfile>结点
profile.Version = ver;//此处用来写XML文件中的version="3.0.0.0"
#endregion
#region <ActivityTrackPoint>结点
//筛选Activity跟踪点的设置
//<ActivityTrackingLocation>结点
ActivityTrackingLocation NodeActivityTrackingLocation = new ActivityTrackingLocation(typeof(Activity));
foreach (ActivityExecutionStatus status in Enum.GetValues(typeof(ActivityExecutionStatus)))
{
NodeActivityTrackingLocation.ExecutionStatusEvents.Add(status);
}
//<MatchingLocations>结
NodeActivityTrackingLocation.MatchDerivedTypes = true;
//<ActivityTrackPoint>结点
ActivityTrackPoint NodeActivityTrackPoint = new ActivityTrackPoint();
//写入结
NodeActivityTrackPoint.MatchingLocations.Add(NodeActivityTrackingLocation);
profile.ActivityTrackPoints.Add(NodeActivityTrackPoint);
#endregion
#region <WorkflowTrackPoint>结点
//筛选实例跟踪点的设置
//<WorkflowTrackingLocation>结点
WorkflowTrackingLocation NodeWorkflowTrackingLocation = new WorkflowTrackingLocation();
foreach (TrackingWorkflowEvent wEvent in Enum.GetValues(typeof(TrackingWorkflowEvent)))
{
NodeWorkflowTrackingLocation.Events.Add(wEvent);
}
//<WorkflowTrackPoint>结点
WorkflowTrackPoint NodeWorkflowTrackPoint = new WorkflowTrackPoint();
//<MatchingLocation>结点
NodeWorkflowTrackPoint.MatchingLocation = NodeWorkflowTrackingLocation;
//写放结点
profile.WorkflowTrackPoints.Add(NodeWorkflowTrackPoint);
#endregion
#region <UserTrackPoint>结点
//筛选用户跟踪点的设置
//<UserTrackPoint>结点
UserTrackPoint NodeUserTrackPoint = new UserTrackPoint();
//<UserTrackingLocation>结点
UserTrackingLocation NodeUserTrackingLocation;
//对CodeActivity控件进行监视
NodeUserTrackingLocation = new UserTrackingLocation(typeof(string), typeof(System.Workflow.Activities.CodeActivity));
//************************************************************************************************
//注:要监视所有用Activity参数,包括Policy的规则活动
//使用
//{
//UserTrackingLocation NodeUserTrackingLocation1= new UserTrackingLocation();
//NodeUserTrackingLocation1.ActivityType = typeof(Activity);
//NodeUserTrackingLocation1.ArgumentType = typeof(object);
//NodeUserTrackingLocation1.MatchDerivedArgumentTypes = true;
//NodeUserTrackingLocation1.MatchDerivedActivityTypes = true;
//}
//************************************************************************************************
//<MatchingLocation>结点
NodeUserTrackingLocation.MatchDerivedActivityTypes = true;
//写入结点
NodeUserTrackPoint.MatchingLocations.Add(NodeUserTrackingLocation);
profile.UserTrackPoints.Add(NodeUserTrackPoint);
#endregion
return profile;
}
/// <summary>
/// 生成一个全监视的profile
/// </summary>
/// <param name="version">格式 3.0.0.0</param>
/// <returns></returns>
public static TrackingProfile createAllProfile(string version)
{
TrackingProfile profile = new TrackingProfile();
profile.Version = new Version(version);
//结点跟踪
ActivityTrackPoint trackPoint_Activity = new ActivityTrackPoint();
ActivityTrackingLocation location = new ActivityTrackingLocation(typeof(Activity));
location.MatchDerivedTypes = true;
foreach (ActivityExecutionStatus s in Enum.GetValues(typeof(ActivityExecutionStatus)))
{
location.ExecutionStatusEvents.Add(s);
}
trackPoint_Activity.MatchingLocations.Add(location);
profile.ActivityTrackPoints.Add(trackPoint_Activity);
//用户跟踪
UserTrackPoint trackPoint_User = new UserTrackPoint();
UserTrackingLocation UserTrackingLocation标记 = new UserTrackingLocation();
UserTrackingLocation标记.ActivityType = typeof(Activity);
UserTrackingLocation标记.ArgumentType = typeof(object);
UserTrackingLocation标记.MatchDerivedArgumentTypes = true;
UserTrackingLocation标记.MatchDerivedActivityTypes = true;
trackPoint_User.MatchingLocations.Add(UserTrackingLocation标记);
profile.UserTrackPoints.Add(trackPoint_User);
//工作流跟踪
WorkflowTrackPoint trackPoint_WF = new WorkflowTrackPoint();
trackPoint_WF.MatchingLocation = new WorkflowTrackingLocation();
foreach (TrackingWorkflowEvent workflowEvent in Enum.GetValues(typeof(TrackingWorkflowEvent)))
{
trackPoint_WF.MatchingLocation.Events.Add(workflowEvent);
}
profile.WorkflowTrackPoints.Add(trackPoint_WF);
//---------------------------
return profile;
}
/// <summary>
/// 得到Profile对象的XML字串
/// </summary>
public static string getProfileXMLString(TrackingProfile profile)
{
TrackingProfileSerializer serializer = new TrackingProfileSerializer();
StringWriter writer = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture);
serializer.Serialize(writer, profile);
string xmlstring = writer.ToString();
return xmlstring;
}
/// <summary>
/// 从磁盘文件中加载TrackingProfile对象
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static TrackingProfile loadProfileFromFile(string path)
{
FileStream fileStream = null;
fileStream = File.OpenRead(path);
StreamReader reader = new StreamReader(fileStream);
TrackingProfile profile;
TrackingProfileSerializer serializer = new TrackingProfileSerializer();
profile = serializer.Deserialize(reader);
fileStream.Close();
return profile;
}
/// <summary>
/// 从XML字符串加载TrackingProfile
/// </summary>
/// <param name="xmlstring"></param>
/// <returns></returns>
public static TrackingProfile loadProrileFromXmlstring(string xmlstring)
{
TrackingProfileSerializer serializer = new TrackingProfileSerializer();
StringReader reader = null;
reader = new StringReader(xmlstring);
TrackingProfile profile = serializer.Deserialize(reader);
return profile;
}
/// <summary>
/// 保存XML字串到磁盘
/// </summary>
public static void saveXMLStringToFile(string xmlstring, string path)
{
System.Xml.XmlDocument doc= new System.Xml.XmlDocument();
doc.LoadXml(xmlstring);
doc.Save(path);
}
/// <summary>
/// 从SQLServer中得到指定工作流的Profile
/// </summary>
public static TrackingProfile getProfileFromSqlserver(string conString, System.Type WorkflowType)
{
//调用存储过程GetTrackingProfile
string sql = conString;
TrackingProfile profile = null;
SqlDataReader rd = null;
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.GetTrackingProfile";
command.Connection = new SqlConnection(sql);
//------------------------------------------------
//参数:@TypeFullName
SqlParameter typeFullName = new SqlParameter();
typeFullName.ParameterName = "@TypeFullName";
typeFullName.SqlDbType = SqlDbType.NVarChar;
//值为要查询的工作流类的类名: typeof(wxdlzm1).ToString() 或typeof(wxdlzm1).FullName
typeFullName.SqlValue = WorkflowType.FullName; //
command.Parameters.Add(typeFullName);
//------------------------------------------------
//参数:@AssemblyFullName
SqlParameter assemblyFullName = new SqlParameter();
assemblyFullName.ParameterName = "@AssemblyFullName";
assemblyFullName.SqlDbType = SqlDbType.NVarChar;
//值为要查询工作流类的全称名: typeof(wxdlzm1).Assembly.FullName
assemblyFullName.SqlValue = WorkflowType.Assembly.FullName;
command.Parameters.Add(assemblyFullName);
//------------------------------------------------
//参数:@Version
SqlParameter versionId = new SqlParameter();
versionId.ParameterName = "@Version";
versionId.SqlDbType = SqlDbType.VarChar;
command.Parameters.Add(versionId);
//------------------------------------------------
//参数:@CreateDefault
SqlParameter createDefault = new SqlParameter();
createDefault.ParameterName = "@CreateDefault";
createDefault.SqlDbType = SqlDbType.Bit;
createDefault.SqlValue = 0;
command.Parameters.Add(createDefault);
//------------------------------------------------
command.Connection.Open();
rd = command.ExecuteReader();
if (rd.Read())
{
string xmlstring = rd[0] as string;
if (null != xmlstring)
{
profile = loadProrileFromXmlstring(xmlstring);
}
}
return profile;
}
/// <summary>
/// 从SQLServer中得到指定工作流的SQLProfile版本
/// </summary>
public static Version getProfileVerFromSqlserver(string conString, System.Type WorkflowType)
{
TrackingProfile profile = getProfileFromSqlserver(conString, WorkflowType);
if (profile != null)
{
return new Version(profile.Version.ToString());
}
else
{//如果数据库中没有,就返回"0.0.0.0"格式,表示没有,此为自定义约定
return new Version("0.0.0.0");
}
}
/// <summary>
/// 将profile字符串保存到数据库
/// </summary>
/// <param name="xmlString">profile字符串</param>
/// <param name="conString">数据库连接字串</param>
/// <param name="WorkflowType">profile所应用到的工作流</param>
/// <param name="version">版本,格式 3.0.0.0</param>
public static void saveProfileXmlStringToSQLServer(string xmlString, string conString, System.Type WorkflowType, Version version)
{
string sql = conString;
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.UpdateTrackingProfile";
command.Connection = new SqlConnection(sql);
//-------------------------------------------------------------
//参数:@TypeFullName
SqlParameter typeFullName = new SqlParameter();
typeFullName.ParameterName = "@TypeFullName";
typeFullName.SqlDbType = SqlDbType.NVarChar;
//值为要设置工作流类的类名 typeof(wxdlzm1).ToString()
typeFullName.SqlValue = WorkflowType.ToString();
command.Parameters.Add(typeFullName);
//-------------------------------------------------------------
//参数:@AssemblyFullName
SqlParameter assemblyFullName = new SqlParameter();
assemblyFullName.ParameterName = "@AssemblyFullName";
assemblyFullName.SqlDbType = SqlDbType.NVarChar;
//值为要设置工作流类的全称名typeof(wxdlzm1).Assembly.FullName
assemblyFullName.SqlValue = WorkflowType.Assembly.FullName;
command.Parameters.Add(assemblyFullName);
//-------------------------------------------------------------
//TrackingProfile表Version字段
SqlParameter versionId = new SqlParameter();
versionId.ParameterName = "@Version";
versionId.SqlDbType = SqlDbType.VarChar;
//值为要指定的片本号的字串:格式"3.0.0.7"
versionId.SqlValue = version.ToString();
command.Parameters.Add(versionId);
//-------------------------------------------------------------
//TrackingProfile表TrackingProfileXml字段
SqlParameter trackingProfile = new SqlParameter();
trackingProfile.ParameterName = "@TrackingProfileXml";
trackingProfile.SqlDbType = SqlDbType.NVarChar;
//值为TrackingProfile格式的XML字串
trackingProfile.SqlValue = xmlString;
command.Parameters.Add(trackingProfile);
//-------------------------------------------------------------
command.Connection.Open();
command.ExecuteNonQuery();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -