Exemplo n.º 1
0
 public TaskInstance(ProcessInstance workflowProcessInsatnce)
 {
     this.State = TaskInstanceStateEnum.INITIALIZED;
     this.Suspended = false;
     this.CanBeWithdrawn = true;
     this.processInsatance = workflowProcessInsatnce;
 }
        /// <summary>
        /// 返回ProcessInstance,共14个字段
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static ProcessInstance GetProcessInstance(IDataReader dr)
        {
            ProcessInstance processInstance = new ProcessInstance();

            processInstance.Id=Convert.ToString(dr["id"]);
            processInstance.ProcessId=Convert.ToString(dr["process_id"]); 
            processInstance.Version=Convert.ToInt32(dr["version"]); 
            processInstance.Name=Convert.ToString(dr["name"]); 
            processInstance.DisplayName=Convert.ToString(dr["display_name"]); 

            processInstance.State= (ProcessInstanceEnum)Convert.ToInt32(dr["state"]); 
            processInstance.Suspended=Convert.ToInt32(dr["suspended"]) == 1; 
            processInstance.CreatorId=Convert.ToString(dr["creator_id"]); 
            if (!(dr["created_time"] is DBNull)) processInstance.CreatedTime=Convert.ToDateTime(dr["created_time"]); 
            if (!(dr["started_time"] is DBNull)) processInstance.StartedTime=Convert.ToDateTime(dr["started_time"]); 

            if (!(dr["expired_time"] is DBNull)) processInstance.ExpiredTime=Convert.ToDateTime(dr["expired_time"]); 
            if (!(dr["end_time"] is DBNull)) processInstance.EndTime=Convert.ToDateTime(dr["end_time"]); 

            processInstance.ParentProcessInstanceId=Convert.ToString(dr["parent_processinstance_id"]);
            processInstance.ParentTaskInstanceId=Convert.ToString(dr["parent_taskinstance_id"]);

            return processInstance;
        }
        /// <summary>
        /// 恢复流程实例
        /// </summary>
        public bool RestoreProcessInstance(ProcessInstance processInstance)
        {
            SqlTransaction transaction = SqlServerHelper.GetSqlTransaction(this.connectionString);
            try
            {
                String sql = " update t_ff_rt_processinstance set suspended=0 where id=@1 ";
                int count = SqlServerHelper.ExecuteNonQuery(transaction, CommandType.Text, sql,
                    SqlServerHelper.NewSqlParameter("@1", SqlDbType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }

                // 恢复对应的TaskInstance
                String sql2 = " update t_ff_rt_taskinstance set suspended=0 where processinstance_id=@1";
                count = SqlServerHelper.ExecuteNonQuery(transaction, CommandType.Text, sql2,
                    SqlServerHelper.NewSqlParameter("@1", SqlDbType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }

                processInstance.Suspended = false;

                transaction.Commit();
                return true;

            }
            catch
            {
                transaction.Rollback();
                return false;
            }
            finally
            {
                if (transaction.Connection.State != ConnectionState.Closed)
                {
                    transaction.Connection.Close();
                    transaction.Dispose();
                }
            }
        }
        /// <summary>
        /// 终止流程实例。将流程实例、活动的TaskInstance、活动的WorkItem的状态设置为CANCELED;并删除所有的token
        /// </summary>
        public bool AbortProcessInstance(ProcessInstance processInstance)
        {
            SqlTransaction transaction = SqlServerHelper.GetSqlTransaction(this.connectionString);
            try
            {
                // 更新流程状态,设置为canceled
                DateTime now = this.RuntimeContext.CalendarService.getSysDate();
                String processSql = "update t_ff_rt_processinstance set state=" + (int)ProcessInstanceEnum.CANCELED
                        + ",end_time=@1 where id=@2 ";
                int count = SqlServerHelper.ExecuteNonQuery(transaction, CommandType.Text, processSql,
                    SqlServerHelper.NewSqlParameter("@1", SqlDbType.DateTime, 11, now),
                    SqlServerHelper.NewSqlParameter("@2", SqlDbType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }


                // 更新所有的任务实例状态为canceled
                String taskSql = " update t_ff_rt_taskinstance set state=" + (int)TaskInstanceStateEnum.CANCELED
                        + ",end_time=@1,can_be_withdrawn=0 " + "  where processinstance_id=@2 and (state=0 or state=1)";
                count = SqlServerHelper.ExecuteNonQuery(transaction, CommandType.Text, taskSql,
                    SqlServerHelper.NewSqlParameter("@1", SqlDbType.DateTime, 11, now),
                    SqlServerHelper.NewSqlParameter("@2", SqlDbType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }

                // 更新所有工作项的状态为canceled
                String workItemSql = " update t_ff_rt_workitem set state="
                        + (int)WorkItemEnum.CANCELED
                        + ",end_time=@1  "
                        + " where taskinstance_id in (select a.id  from t_ff_rt_taskinstance a,t_ff_rt_workitem b where a.id=b.taskinstance_id and a.processinstance_id=@2 ) and (state=0 or state=1) ";
                count = SqlServerHelper.ExecuteNonQuery(transaction, CommandType.Text, workItemSql,
                    SqlServerHelper.NewSqlParameter("@1", SqlDbType.DateTime, 11, now),
                    SqlServerHelper.NewSqlParameter("@2", SqlDbType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }

                // 删除所有的token
                String tokenSql = " delete from t_ff_rt_token where processinstance_id=@1  ";
                count = SqlServerHelper.ExecuteNonQuery(transaction, CommandType.Text, tokenSql,
                    SqlServerHelper.NewSqlParameter("@1", SqlDbType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }

                // 数据库操作成功后,更新对象的状态
                processInstance.State = ProcessInstanceEnum.CANCELED;

                transaction.Commit();
                return true;

            }
            catch
            {
                transaction.Rollback();
                return false;
            }
            finally
            {
                if (transaction.Connection.State != ConnectionState.Closed)
                {
                    transaction.Connection.Close();
                    transaction.Dispose();
                }
            }
        }
        /// <summary>
        /// 挂起流程实例
        /// </summary>
        public bool SuspendProcessInstance(ProcessInstance processInstance) {
            OracleTransaction transaction = OracleHelper.GetOracleTransaction(this.connectionString);
            try
            {

                String sql = " update t_ff_rt_processinstance set suspended=1 where id=:1 ";
                int count = OracleHelper.ExecuteNonQuery(transaction, CommandType.Text, sql,
                    OracleHelper.NewOracleParameter(":1", OracleType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }

                // 挂起对应的TaskInstance
                String sql2 = " update t_ff_rt_taskinstance set suspended=1 where processinstance_id=:1 ";
                count = OracleHelper.ExecuteNonQuery(transaction, CommandType.Text, sql2,
                    OracleHelper.NewOracleParameter(":1", OracleType.VarChar, 50, processInstance.Id)
                    );
                if (count <= 0)
                {
                    transaction.Rollback();
                    return false;
                }

                processInstance.Suspended=true;

                transaction.Commit();
                return true;

            }
            catch
            {
                transaction.Rollback();
                return false;
            }
            finally
            {
                if (transaction.Connection.State != ConnectionState.Closed)
                {
                    transaction.Connection.Close();
                    transaction.Dispose();
                }
            }
        }
        public object doInWorkflowSession(RuntimeContext ctx)
        {
            WorkflowDefinition workflowDef = ctx.DefinitionService.GetTheLatestVersionOfWorkflowDefinition(wfprocessId);
            WorkflowProcess wfProcess = null;

            wfProcess = workflowDef.getWorkflowProcess();

            if (wfProcess == null)
            {
                throw new Exception("Workflow process NOT found,id=[" + wfprocessId + "]");
            }

            ProcessInstance processInstance = new ProcessInstance();
            processInstance.CreatorId=creatorId;
            processInstance.ProcessId=wfProcess.Id;
            processInstance.Version=workflowDef.Version;
            processInstance.DisplayName=wfProcess.DisplayName;
            processInstance.Name=wfProcess.Name;
            processInstance.State=ProcessInstanceEnum.INITIALIZED;
            processInstance.CreatedTime=ctx.CalendarService.getSysDate();
            processInstance.ParentProcessInstanceId=parentProcessInstanceId;
            processInstance.ParentTaskInstanceId=parentTaskInstanceId;

            ctx.PersistenceService.SaveOrUpdateProcessInstance(
                    processInstance);

            // 初始化流程变量
            List<DataField> datafields = wfProcess.DataFields;
            for (int i = 0; datafields != null && i < datafields.Count; i++)
            {
                DataField df = (DataField)datafields[i];
                if (df.DataType == DataTypeEnum.STRING)
                {
                    if (df.InitialValue != null)
                    {
                        processInstance.setProcessInstanceVariable(df.Name, df.InitialValue);
                    }
                    else
                    {
                        processInstance.setProcessInstanceVariable(df.Name, "");
                    }
                }
                else if (df.DataType == DataTypeEnum.INTEGER)
                {
                    if (df.InitialValue != null)
                    {
                        try
                        {
                            Int32 intValue = Int32.Parse(df.InitialValue);
                            processInstance.setProcessInstanceVariable(df.Name, intValue);
                        }
                        catch (Exception )
                        {
                        }
                    }
                    else
                    {
                        processInstance.setProcessInstanceVariable(df.Name, (Int32)0);
                    }
                }
                else if (df.DataType == DataTypeEnum.LONG)
                {
                    if (df.InitialValue != null)
                    {
                        try
                        {
                            long longValue = long.Parse(df.InitialValue);
                            processInstance.setProcessInstanceVariable(df.Name, longValue);
                        }
                        catch (Exception )
                        {
                        }
                    }
                    else
                    {
                        processInstance.setProcessInstanceVariable(df.Name, (long)0);
                    }
                }
                else if (df.DataType == DataTypeEnum.FLOAT)
                {
                    if (df.InitialValue != null)
                    {
                        float floatValue = float.Parse(df.InitialValue);
                        processInstance.setProcessInstanceVariable(df.Name, floatValue);
                    }
                    else
                    {
                        processInstance.setProcessInstanceVariable(df.Name, (float)0);
                    }
                }
                else if (df.DataType == DataTypeEnum.DOUBLE)
                {
                    if (df.InitialValue != null)
                    {
                        Double doubleValue = Double.Parse(df.InitialValue);
                        processInstance.setProcessInstanceVariable(df
                                .Name, doubleValue);
                    }
                    else
                    {
                        processInstance.setProcessInstanceVariable(df.Name, (Double)0);
                    }
                }
                else if (df.DataType == DataTypeEnum.BOOLEAN)
                {
                    if (df.InitialValue != null)
                    {
                        Boolean booleanValue = Boolean.Parse(df.InitialValue);
                        processInstance.setProcessInstanceVariable(df.Name, booleanValue);
                    }
                    else
                    {
                        processInstance.setProcessInstanceVariable(df.Name, false);
                    }
                }
                else if (df.DataType == DataTypeEnum.DATETIME)
                {
                    // TODO 需要完善一下
                    if (df.InitialValue != null
                            && df.DataPattern != null)
                    {
                        try
                        {
                            //SimpleDateFormat dFormat = new SimpleDateFormat(df.DataPattern);
                            DateTime dateTmp = DateTime.Parse(df.InitialValue);
                            processInstance.setProcessInstanceVariable(df.Name, dateTmp);
                        }
                        catch (Exception )
                        {
                            processInstance.setProcessInstanceVariable(df.Name, null);
                            //e.printStackTrace();
                        }
                    }
                    else
                    {
                        processInstance.setProcessInstanceVariable(df.Name, null);
                    }
                }
            }

            ctx.PersistenceService.SaveOrUpdateProcessInstance(
                    processInstance);

            return processInstance;
        }
        public Object doInWorkflowSession(RuntimeContext ctx)
        {
            ProcessInstance processInstance = new ProcessInstance();
            processInstance.CreatorId=creatorId;
            processInstance.ProcessId=wfProcess.Id;
            processInstance.Version=workflowDef.Version;
            processInstance.DisplayName = wfProcess.DisplayName;
            processInstance.Name = wfProcess.Name;
            processInstance.State = ProcessInstanceEnum.INITIALIZED;
            processInstance.CreatedTime=ctx.CalendarService.getSysDate();
            processInstance.ParentProcessInstanceId=parentProcessInstanceId;
            processInstance.ParentTaskInstanceId=parentTaskInstanceId;

            ctx.PersistenceService.SaveOrUpdateProcessInstance(processInstance);

            return processInstance;
        }
Exemplo n.º 8
0
 public JoinPoint(ProcessInstance processInstance)
 {
     this._processInstance = processInstance;
 }