public void setDefinitionFiles()// throws IOException, FPDLParserException,EngineException 
        {
            if (DefinitionFiles != null && workflowDefinitionMap == null)
            {
                workflowDefinitionMap = new Dictionary<String, WorkflowDefinition>();
                Dom4JFPDLParser parser = new Dom4JFPDLParser();

                for (int i = 0; i < DefinitionFiles.Count; i++)
                {
                    Stream inStream = new FileStream(DefinitionFiles[i].Trim(), FileMode.Open);
                    if (inStream == null)
                    {
                        throw new IOException("没有找到名称为" + DefinitionFiles[i] + "的流程定义文件");
                    }
                    WorkflowProcess workflowProcess = parser.parse(inStream);

                    WorkflowDefinition workflowDef = new WorkflowDefinition();
                    workflowDef.Version = 1;

                    workflowDef.setWorkflowProcess(workflowProcess);

                    String latestVersionKey = workflowProcess.Id + "_V_" + workflowDef.Version;
                    workflowDefinitionMap.Add(latestVersionKey, workflowDef);
                    latestVersionKeyMap.Add(workflowProcess.Id, latestVersionKey);
                }
            }
        }
        public static WorkflowDefinition GetWorkflowDefinition(IDataReader dr)
        {
            WorkflowDefinition workFlowDefinition = new WorkflowDefinition();
            workFlowDefinition.Id=Convert.ToString(dr["id"]);
            workFlowDefinition.DefinitionType=Convert.ToString(dr["definition_type"]);
            workFlowDefinition.ProcessId=Convert.ToString(dr["process_id"]);
            workFlowDefinition.Name=Convert.ToString(dr["name"]);
            workFlowDefinition.DisplayName=Convert.ToString(dr["display_name"]);

            workFlowDefinition.Description=Convert.ToString(dr["description"]);
            workFlowDefinition.Version=Convert.ToInt32(dr["version"]);
            workFlowDefinition.State=Convert.ToInt32(dr["state"]) == 1 ? true : false;
            workFlowDefinition.UploadUser=Convert.ToString(dr["upload_user"]);
            if (!(dr["upload_time"] is DBNull)) workFlowDefinition.UploadTime=Convert.ToDateTime(dr["upload_time"]);

            workFlowDefinition.PublishUser=Convert.ToString(dr["publish_user"]);
            if (!(dr["publish_time"] is DBNull)) workFlowDefinition.PublishTime=Convert.ToDateTime(dr["publish_time"]);
            // 读取blob大字段
            workFlowDefinition.ProcessContent=Convert.ToString(dr["process_content"]);
            return workFlowDefinition;
        }
        /// <summary>
        /// 保存流程定义,如果同一个ProcessId的流程定义已经存在,则版本号自动加1。
        /// </summary>
        /// <param name="workflowProcessXml">保存的WorkflowProcess XML 文本.</param>
        /// <param name="version">保存的版本,当版本为小于等于0时,添加新流程,如存在相同ProcessId则版本号加1</param>
        /// <param name="isState">是否发布</param>
        public bool SaveOrUpdateWorkflowProcess(string workflowProcessXml, int version, bool isState)
        {
            Dom4JFPDLParser parser = new Dom4JFPDLParser();
            MemoryStream msin = new MemoryStream(Encoding.UTF8.GetBytes(workflowProcessXml));
            WorkflowProcess workflowProcess = parser.parse(msin);
            if (workflowProcess == null) return false;

            WorkflowDefinition wd = RuntimeContextFactory.getRuntimeContext().PersistenceService.FindWorkflowDefinitionByProcessIdAndVersionNumber(workflowProcess.Id, version);
            if (wd == null)
            {
                wd = new WorkflowDefinition();
                wd.PublishTime = DateTime.Now;
                wd.PublishUser = "******";
            }

            wd.UploadTime = DateTime.Now;
            wd.UploadUser = "******";
            wd.State = isState;

            wd.Name = workflowProcess.Name;
            wd.DisplayName = workflowProcess.DisplayName;
            wd.Description = workflowProcess.Description;
            wd.setWorkflowProcess(workflowProcess);

            if (RuntimeContextFactory.getRuntimeContext().PersistenceService.SaveOrUpdateWorkflowDefinition(wd))
            {
                return true;
            }

            return false;
        }
        /******************************************************************************/
        /************                                                        **********/
        /************            存取流程定义文件 相关的持久化方法             **********/
        /************            Persistence methods for workflow definition **********/
        /************                                                        **********/
        /******************************************************************************/

        /// <summary>
        /// Save or update the workflow definition. The version will be increased automatically when insert a new record.
        /// 保存流程定义,如果同一个ProcessId的流程定义已经存在,则版本号自动加1。
        /// </summary>
        public bool SaveOrUpdateWorkflowDefinition(WorkflowDefinition workflowDef)
        {

            if (String.IsNullOrEmpty(workflowDef.Id))
            {
                Int32 latestVersion = FindTheLatestVersionNumberIgnoreState(workflowDef.ProcessId);
                if (latestVersion > 0)
                {
                    workflowDef.Version = latestVersion + 1;
                }
                else
                {
                    workflowDef.Version = 1;
                }
                workflowDef.Id = Guid.NewGuid().ToString("N");
                string insert = "INSERT INTO T_FF_DF_WORKFLOWDEF (" +
                    "ID, DEFINITION_TYPE, PROCESS_ID, NAME, DISPLAY_NAME, " +
                    "DESCRIPTION, VERSION, STATE, UPLOAD_USER, UPLOAD_TIME, " +
                    "PUBLISH_USER, PUBLISH_TIME, PROCESS_CONTENT )VALUES(@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13)";

                SqlParameter[] insertParms = { 
					SqlServerHelper.NewSqlParameter("@1", SqlDbType.VarChar, 50, workflowDef.Id), 
					SqlServerHelper.NewSqlParameter("@2", SqlDbType.VarChar, 50, workflowDef.DefinitionType), 
					SqlServerHelper.NewSqlParameter("@3", SqlDbType.VarChar, 100, workflowDef.ProcessId), 
					SqlServerHelper.NewSqlParameter("@4", SqlDbType.VarChar, 100, workflowDef.Name), 
					SqlServerHelper.NewSqlParameter("@5", SqlDbType.VarChar, 128, workflowDef.DisplayName), 
					SqlServerHelper.NewSqlParameter("@6", SqlDbType.VarChar, 1024, workflowDef.Description), 
					SqlServerHelper.NewSqlParameter("@7", SqlDbType.Int, workflowDef.Version), 
					SqlServerHelper.NewSqlParameter("@8", SqlDbType.SmallInt, SqlServerHelper.OraBit(workflowDef.State) ), 
					SqlServerHelper.NewSqlParameter("@9", SqlDbType.VarChar, 50, workflowDef.UploadUser), 
					SqlServerHelper.NewSqlParameter("@10", SqlDbType.DateTime, 11, workflowDef.UploadTime), 
					SqlServerHelper.NewSqlParameter("@11", SqlDbType.VarChar, 50, workflowDef.PublishUser), 
					SqlServerHelper.NewSqlParameter("@12", SqlDbType.DateTime, 11, workflowDef.PublishTime), 
					SqlServerHelper.NewSqlParameter("@13", SqlDbType.NText, workflowDef.ProcessContent)
				};
                if (SqlServerHelper.ExecuteNonQuery(connectionString, CommandType.Text, insert, insertParms) != 1)
                    return false;
                else return true;
            }
            else
            {
                string update = "UPDATE T_FF_DF_WORKFLOWDEF SET " +
                    "PROCESS_ID=@3, NAME=@4, DISPLAY_NAME=@5, DESCRIPTION=@6, " +
                    "STATE=@8, UPLOAD_USER=@9, UPLOAD_TIME=@10, PROCESS_CONTENT=@13 " +
                    "WHERE ID=@1";
                SqlParameter[] updateParms = { 
    				SqlServerHelper.NewSqlParameter("@3", SqlDbType.VarChar, 100, workflowDef.ProcessId), 
    				SqlServerHelper.NewSqlParameter("@4", SqlDbType.VarChar, 100, workflowDef.Name), 
    				SqlServerHelper.NewSqlParameter("@5", SqlDbType.VarChar, 128, workflowDef.DisplayName), 
    				SqlServerHelper.NewSqlParameter("@6", SqlDbType.VarChar, 1024, workflowDef.Description), 
    				SqlServerHelper.NewSqlParameter("@8", SqlDbType.SmallInt, SqlServerHelper.OraBit(workflowDef.State)), 
    				SqlServerHelper.NewSqlParameter("@9", SqlDbType.VarChar, 50, workflowDef.UploadUser), 
    				SqlServerHelper.NewSqlParameter("@10", SqlDbType.DateTime, 11, workflowDef.UploadTime),
    				SqlServerHelper.NewSqlParameter("@13", SqlDbType.Text,workflowDef.ProcessContent),
    				SqlServerHelper.NewSqlParameter("@1", SqlDbType.VarChar, 50, workflowDef.Id)
    			};
                if (SqlServerHelper.ExecuteNonQuery(connectionString, CommandType.Text, update, updateParms) != 1)
                    return false;
                else return true;
            }
        }
        /******************************************************************************/
        /************                                                        **********/
        /************            存取流程定义文件 相关的持久化方法             **********/
        /************            Persistence methods for workflow definition **********/
        /************                                                        **********/
        /******************************************************************************/

        /// <summary>
        /// Save or update the workflow definition. The version will be increased automatically when insert a new record.
        /// 保存流程定义,如果同一个ProcessId的流程定义已经存在,则版本号自动加1。
        /// </summary>
        public bool SaveOrUpdateWorkflowDefinition(WorkflowDefinition workflowDef) {

            if (String.IsNullOrEmpty(workflowDef.Id))
            {
                Int32 latestVersion = FindTheLatestVersionNumberIgnoreState(workflowDef.ProcessId);
                if (latestVersion > 0)
                {
                    workflowDef.Version=latestVersion + 1;
                }
                else
                {
                    workflowDef.Version=1;
                }
                workflowDef.Id = Guid.NewGuid().ToString().Replace("-", "");
                string insert = "INSERT INTO T_FF_DF_WORKFLOWDEF (" +
                    "ID, DEFINITION_TYPE, PROCESS_ID, NAME, DISPLAY_NAME, " +
                    "DESCRIPTION, VERSION, STATE, UPLOAD_USER, UPLOAD_TIME, " +
                    "PUBLISH_USER, PUBLISH_TIME, PROCESS_CONTENT )VALUES(:1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13)";
                
                OracleParameter[] insertParms = { 
					OracleHelper.NewOracleParameter(":1", OracleType.VarChar, 50, workflowDef.Id), 
					OracleHelper.NewOracleParameter(":2", OracleType.VarChar, 50, workflowDef.DefinitionType), 
					OracleHelper.NewOracleParameter(":3", OracleType.VarChar, 100, workflowDef.ProcessId), 
					OracleHelper.NewOracleParameter(":4", OracleType.VarChar, 100, workflowDef.Name), 
					OracleHelper.NewOracleParameter(":5", OracleType.VarChar, 128, workflowDef.DisplayName), 
					OracleHelper.NewOracleParameter(":6", OracleType.VarChar, 1024, workflowDef.Description), 
					OracleHelper.NewOracleParameter(":7", OracleType.Int32, workflowDef.Version), 
					OracleHelper.NewOracleParameter(":8", OracleType.Int16, OracleHelper.OraBit(workflowDef.State) ), 
					OracleHelper.NewOracleParameter(":9", OracleType.VarChar, 50, workflowDef.UploadUser), 
					OracleHelper.NewOracleParameter(":10", OracleType.Timestamp, 11, workflowDef.UploadTime), 
					OracleHelper.NewOracleParameter(":11", OracleType.VarChar, 50, workflowDef.PublishUser), 
					OracleHelper.NewOracleParameter(":12", OracleType.Timestamp, 11, workflowDef.PublishTime), 
					OracleHelper.NewOracleParameter(":13", OracleType.NVarChar, workflowDef.ProcessContent)
				};
                if (OracleHelper.ExecuteNonQuery(connectionString, CommandType.Text, insert, insertParms) != 1)
                    return false;
                else return true;
            }
            else
            {
                string update = "UPDATE T_FF_DF_WORKFLOWDEF SET " +
                    "PROCESS_ID=:3, NAME=:4, DISPLAY_NAME=:5, DESCRIPTION=:6, " +
                    "STATE=:8, UPLOAD_USER=:9, UPLOAD_TIME=:10, PROCESS_CONTENT=:13 " +
                    "WHERE ID=:1";
                OracleParameter[] updateParms = { 
    				OracleHelper.NewOracleParameter(":3", OracleType.VarChar, 100, workflowDef.ProcessId), 
    				OracleHelper.NewOracleParameter(":4", OracleType.VarChar, 100, workflowDef.Name), 
    				OracleHelper.NewOracleParameter(":5", OracleType.VarChar, 128, workflowDef.DisplayName), 
    				OracleHelper.NewOracleParameter(":6", OracleType.VarChar, 1024, workflowDef.Description), 
    				OracleHelper.NewOracleParameter(":8", OracleType.Int16, OracleHelper.OraBit(workflowDef.State)), 
    				OracleHelper.NewOracleParameter(":9", OracleType.VarChar, 50, workflowDef.UploadUser), 
    				OracleHelper.NewOracleParameter(":10", OracleType.Timestamp, 11, workflowDef.UploadTime),
    				OracleHelper.NewOracleParameter(":13", OracleType.NVarChar,workflowDef.ProcessContent),
    				OracleHelper.NewOracleParameter(":1", OracleType.VarChar, 50, workflowDef.Id)
    			};
                if (OracleHelper.ExecuteNonQuery(connectionString, CommandType.Text, update, updateParms) != 1)
                    return false;
                else return true;
            }
        }
        public void ok_Click(object sender, AjaxEventArgs e)
        {
            WorkflowDefinition wd = new WorkflowDefinition();
            //如是修改。
            string id = this.HProcessId.Value.ToString().Trim();
            if (!string.IsNullOrEmpty(id))
            {
                wd = RuntimeContextExamples.GetRuntimeContext().PersistenceService.FindWorkflowDefinitionById(id);
            }
            else
            {
                wd.PublishTime = DateTime.Now;
                wd.PublishUser = "******";
            }
            wd.UploadTime = DateTime.Now;
            wd.UploadUser = "******";
            wd.State = Boolean.Parse(this.state.SelectedItem.Value);

            if (this.BasicField.HasFile)
            {
                //string filename = this.Server.MapPath("~/WorkFlowTemp/" + BasicField.PostedFile.FileName);
                //BasicField.PostedFile.SaveAs(filename);

                WorkflowProcess workflowProcess;
                using (Stream inStream = BasicField.PostedFile.InputStream)//new FileStream(filename, FileMode.Open))
                {
                    if (inStream == null)
                    {
                        throw new IOException("没有上传流程定义文件!");
                    }
                    Dom4JFPDLParser parser = new Dom4JFPDLParser();
                    workflowProcess = parser.parse(inStream);
                }
                if (workflowProcess != null)
                {
                    if (string.IsNullOrEmpty(id)) wd.ProcessId = workflowProcess.Id;
                    wd.Name = workflowProcess.Name;
                    wd.DisplayName = workflowProcess.DisplayName;
                    wd.Description = workflowProcess.Description;
                    wd.setWorkflowProcess(workflowProcess);
                    //wd.ProcessContent = File.ReadAllText(filename);// twd.ProcessContent;
                }
                else
                {
                    Ext.Msg.Show(new MessageBox.Config
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO,
                        Title = "Success",
                        Message = "错误的流程文件。"
                    });
                    return;
                }
            }

            if (RuntimeContextExamples.GetRuntimeContext().PersistenceService.SaveOrUpdateWorkflowDefinition(wd))
            {
                WorkflowEdit.Hide();

                Ext.Msg.Show(new MessageBox.Config
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "Success",
                    Message = "保存成功。"
                });

                query_Click(null, null);
            }
            else
            {
                Ext.Msg.Show(new MessageBox.Config
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "Success",
                    Message = "保存出错。"
                });
            }
        }
 public WorkflowSessionIProcessInstanceCreateProcessInstance(String creatorId, WorkflowProcess wfProcess, 
     WorkflowDefinition workflowDef, String parentProcessInstanceId,String parentTaskInstanceId)
 {
     this.creatorId = creatorId;
     this.wfProcess = wfProcess;
     this.workflowDef = workflowDef;
     this.parentProcessInstanceId = parentProcessInstanceId;
     this.parentTaskInstanceId = parentTaskInstanceId;
 }