OpenAuth.Net/OpenAuth.Mvc/Models/WorkflowInit.cs
2016-09-07 16:58:08 +08:00

110 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Configuration;
using System.Xml.Linq;
using OpenAuth.App;
using OpenAuth.Domain;
using OptimaJet.Workflow.Core.Builder;
using OptimaJet.Workflow.Core.Bus;
using OptimaJet.Workflow.Core.Persistence;
using OptimaJet.Workflow.Core.Runtime;
using OptimaJet.Workflow.DbPersistence;
namespace OpenAuth.Mvc.Models
{
public static class WorkflowInit
{
private static volatile WorkflowRuntime _runtime;
private static readonly object _sync = new object();
public static WorkflowRuntime Runtime
{
get
{
if (_runtime == null)
{
lock (_sync)
{
if (_runtime == null)
{
var connectionString = ConfigurationManager.ConnectionStrings["WorkFlow"].ConnectionString;
var builder = new WorkflowBuilder<XElement>(
new MSSQLProvider(connectionString),
new OptimaJet.Workflow.Core.Parser.XmlWorkflowParser(),
new MSSQLProvider(connectionString)
).WithDefaultCache();
_runtime = new WorkflowRuntime(new Guid("{8D38DB8F-F3D5-4F26-A989-4FDD40F32D9D}"))
.WithBuilder(builder)
.WithRuleProvider(new WorkflowRuleProvider())
.WithActionProvider(new WorkflowActionProvider())
.WithPersistenceProvider(new MSSQLProvider(connectionString))
.WithTimerManager(new TimerManager())
.WithBus(new NullBus())
.SwitchAutoUpdateSchemeBeforeGetAvailableCommandsOn()
.Start();
_runtime.ProcessStatusChanged += _runtime_ProcessStatusChanged;
}
}
}
return _runtime;
}
}
private static void _runtime_ProcessStatusChanged(object sender, ProcessStatusChangedEventArgs e)
{
if (e.NewStatus != ProcessStatus.Idled && e.NewStatus != ProcessStatus.Finalized)
return;
if (string.IsNullOrEmpty(e.SchemeCode))
return;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ת<CCAC><D7AA><EFBFBD><EFBFBD>¼
WorkflowActionProvider.DeleteEmptyPreHistory(e.ProcessId);
_runtime.PreExecuteFromCurrentActivity(e.ProcessId);
//<2F><><EFBFBD><EFBFBD>֪ͨ<CDA8>б<EFBFBD>
UpdateInbox(e);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
UpdateApplyState(e);
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
/// </summary>
private static void UpdateApplyState(ProcessStatusChangedEventArgs e)
{
var nextState = WorkflowInit.Runtime.GetLocalizedStateName(e.ProcessId, e.ProcessInstance.CurrentState);
var _app = AutofacExt.GetFromFac<CommonApplyApp>();
_app.ChangeState(e.ProcessId, e.ProcessInstance.CurrentState, nextState);
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>֪ͨ<CDA8>б<EFBFBD>
/// </summary>
private static void UpdateInbox(ProcessStatusChangedEventArgs e)
{
var inboxApp = AutofacExt.GetFromFac<WorkflowInboxApp>();
inboxApp.DeleteAllByProcess(e.ProcessId);
if (e.NewStatus != ProcessStatus.Finalized)
{
var newActors = Runtime.GetAllActorsForDirectCommandTransitions(e.ProcessId);
foreach (var newActor in newActors)
{
var newInboxItem = new Relevance()
{
Id = Guid.NewGuid(),
SecondId = new Guid(newActor),
FirstId = e.ProcessId,
Key = "ProcessUser"
};
inboxApp.Add(newInboxItem);
}
}
}
}
}