1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
#pragma warning disable 1634, 1691
namespace System.Workflow.Activities
{
using System;
using System.Xml.Serialization;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Workflow.ComponentModel.Compiler;
using System.Security.Principal;
using System.Workflow.Runtime.Tracking;
using System.Diagnostics;
using System.Workflow.Runtime;
[Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
public sealed class StateMachineWorkflowInstance
{
private Guid _instanceId;
private WorkflowInstance _workflowInstance;
private SqlTrackingQuery _sqlTrackingQuery;
private SqlTrackingService _sqlTrackingService;
private SqlTrackingWorkflowInstance _sqlTrackingWorkflowInstance;
private StateMachineWorkflowActivity _stateMachineWorkflow;
private WorkflowRuntime _runtime;
internal const string StateHistoryPropertyName = "StateHistory";
public StateMachineWorkflowInstance(WorkflowRuntime runtime, Guid instanceId)
{
if (runtime == null)
throw new ArgumentNullException("runtime");
if (instanceId == Guid.Empty)
throw new ArgumentNullException("instanceId");
_runtime = runtime;
_instanceId = instanceId;
_workflowInstance = runtime.GetWorkflow(instanceId);
_stateMachineWorkflow = _workflowInstance.GetWorkflowDefinition() as StateMachineWorkflowActivity;
if (_stateMachineWorkflow == null)
throw new ArgumentException(SR.GetStateMachineWorkflowRequired(), "instanceId");
}
public StateMachineWorkflowActivity StateMachineWorkflow
{
get
{
// we always get a new definition, in case a
// dynamic updated happened. The exception handling here
// is because after the workflow completes, we can no longer
// retrieve the workflow definition. In this case, we
// return the last retrieved definition
try
{
_stateMachineWorkflow = (StateMachineWorkflowActivity)this.WorkflowInstance.GetWorkflowDefinition();
}
catch (InvalidOperationException)
{
}
return _stateMachineWorkflow;
}
}
public Guid InstanceId
{
get
{
return _instanceId;
}
}
public WorkflowInstance WorkflowInstance
{
get
{
return _workflowInstance;
}
}
public StateActivity CurrentState
{
get
{
return GetCurrentState();
}
}
public string CurrentStateName
{
get
{
StateActivity currentState = this.CurrentState;
if (currentState == null)
return null;
return currentState.QualifiedName;
}
}
private static ReadOnlyCollection<StateActivity> GetLeafStates(StateActivity parentState)
{
if (parentState == null)
throw new ArgumentNullException("parentState");
List<StateActivity> leafStates = new List<StateActivity>();
Queue<StateActivity> states = new Queue<StateActivity>();
states.Enqueue(parentState);
while (states.Count > 0)
{
StateActivity parent = states.Dequeue();
foreach (Activity childActivity in parent.EnabledActivities)
{
StateActivity childState = childActivity as StateActivity;
if (childState != null)
{
if (StateMachineHelpers.IsLeafState(childState))
leafStates.Add(childState);
else
states.Enqueue(childState);
}
}
}
return leafStates.AsReadOnly();
}
public ReadOnlyCollection<StateActivity> States
{
get
{
StateMachineWorkflowActivity stateMachineWorkflow = this.StateMachineWorkflow;
#pragma warning disable 56503
if (stateMachineWorkflow == null)
throw new InvalidOperationException();
return GetLeafStates(stateMachineWorkflow);
#pragma warning restore 56503
}
}
public ReadOnlyCollection<string> PossibleStateTransitions
{
get
{
return GetPossibleStateTransitions();
}
}
public ReadOnlyCollection<string> StateHistory
{
get
{
return GetStateHistory();
}
}
public void EnqueueItem(IComparable queueName, object item)
{
EnqueueItem(queueName, item, null, null);
}
public void EnqueueItem(IComparable queueName, object item, IPendingWork pendingWork, object workItem)
{
this.WorkflowInstance.EnqueueItemOnIdle(queueName, item, pendingWork, workItem);
}
public void SetState(StateActivity targetState)
{
if (targetState == null)
throw new ArgumentNullException("targetState");
SetState(targetState.QualifiedName);
}
public void SetState(string targetStateName)
{
if (targetStateName == null)
throw new ArgumentNullException("targetStateName");
StateActivity targetState = FindActivityByQualifiedName(targetStateName) as StateActivity;
if (targetState == null)
throw new ArgumentOutOfRangeException("targetStateName");
SetStateEventArgs eventArgs = new SetStateEventArgs(targetStateName);
this.WorkflowInstance.EnqueueItemOnIdle(System.Workflow.Activities.StateMachineWorkflowActivity.SetStateQueueName, eventArgs, null, null);
}
internal Activity FindActivityByQualifiedName(string id)
{
return StateMachineHelpers.FindActivityByName(this.StateMachineWorkflow, id);
}
private StateActivity GetCurrentState()
{
ReadOnlyCollection<WorkflowQueueInfo> workflowQueuedInfos = this.WorkflowInstance.GetWorkflowQueueData();
foreach (WorkflowQueueInfo queueInfo in workflowQueuedInfos)
{
if (queueInfo.QueueName.Equals(StateMachineWorkflowActivity.SetStateQueueName))
{
if (queueInfo.SubscribedActivityNames.Count == 0)
return null;
Debug.Assert(queueInfo.SubscribedActivityNames.Count == 1);
StateMachineWorkflowActivity stateMachineWorkflow = this.StateMachineWorkflow;
StateActivity currentState = StateMachineHelpers.FindStateByName(stateMachineWorkflow, queueInfo.SubscribedActivityNames[0]);
return currentState;
}
}
return null;
}
private ReadOnlyCollection<string> GetPossibleStateTransitions()
{
List<string> targetStates = new List<string>();
ReadOnlyCollection<WorkflowQueueInfo> workflowQueuedInfos = this.WorkflowInstance.GetWorkflowQueueData();
StateMachineWorkflowActivity stateMachineWorkflow = this.StateMachineWorkflow;
foreach (WorkflowQueueInfo queueInfo in workflowQueuedInfos)
{
foreach (string subscribedActivityName in queueInfo.SubscribedActivityNames)
{
Activity subscribedActivity = StateMachineHelpers.FindActivityByName(stateMachineWorkflow, subscribedActivityName);
IEventActivity eventActivity = subscribedActivity as IEventActivity;
if (eventActivity == null)
continue;
EventDrivenActivity eventDriven = StateMachineHelpers.GetParentEventDriven(eventActivity);
Debug.Assert(eventDriven != null);
Queue<Activity> activities = new Queue<Activity>();
activities.Enqueue(eventDriven);
while (activities.Count > 0)
{
Activity activity = activities.Dequeue();
SetStateActivity setState = activity as SetStateActivity;
if (setState != null)
{
targetStates.Add(setState.TargetStateName);
}
else
{
CompositeActivity compositeActivity = activity as CompositeActivity;
if (compositeActivity != null)
{
foreach (Activity childActivity in compositeActivity.EnabledActivities)
{
activities.Enqueue(childActivity);
}
}
}
}
}
}
return targetStates.AsReadOnly();
}
private ReadOnlyCollection<string> GetStateHistory()
{
if (_sqlTrackingService == null)
{
_sqlTrackingService = _runtime.GetService<SqlTrackingService>();
if (_sqlTrackingService == null)
throw new InvalidOperationException(SR.GetSqlTrackingServiceRequired());
}
if (_sqlTrackingQuery == null)
_sqlTrackingQuery = new SqlTrackingQuery(_sqlTrackingService.ConnectionString);
StateMachineWorkflowActivity stateMachineWorkflow;
Stack<string> stateHistory = new Stack<string>();
try
{
stateMachineWorkflow = this.StateMachineWorkflow;
}
catch (InvalidOperationException)
{
return new ReadOnlyCollection<string>(stateHistory.ToArray());
}
if (_sqlTrackingWorkflowInstance == null)
{
bool result = _sqlTrackingQuery.TryGetWorkflow(_instanceId, out _sqlTrackingWorkflowInstance);
if (!result)
{
// Workflow has not started yet, so we just return an
// empty collection
return new ReadOnlyCollection<string>(stateHistory.ToArray());
}
}
_sqlTrackingWorkflowInstance.Refresh();
IList<UserTrackingRecord> events = _sqlTrackingWorkflowInstance.UserEvents;
foreach (UserTrackingRecord record in events)
{
if (record.UserDataKey != StateActivity.StateChangeTrackingDataKey)
continue;
string stateQualifiedName = record.UserData as string;
if (stateQualifiedName == null)
throw new InvalidOperationException(SR.GetInvalidUserDataInStateChangeTrackingRecord());
StateActivity state = StateMachineHelpers.FindStateByName(stateMachineWorkflow, record.QualifiedName);
if (state == null)
throw new InvalidOperationException(SR.GetInvalidUserDataInStateChangeTrackingRecord());
if (StateMachineHelpers.IsLeafState(state))
stateHistory.Push(stateQualifiedName);
}
ReadOnlyCollection<string> history = new ReadOnlyCollection<string>(stateHistory.ToArray());
return history;
}
}
}
|