To continue my series of Workflow utility methods, today I'm presenting a way to retrieve the next Activity Definition, relative to the current Activity in the Workflow Process.
Alternatively, you can retrieve a next ActivityDefinition by name, where name is the ActivityDefinition title that we are looking for:
For the code-above we need to know the CurrentWorkItem, which is the WorkItem of the current item in the workflow.
The code above is part of YAWF (Yet Another Workflow Framework).
/// <summary>
/// Return the next activity
definition, if there is one.
/// </summary>
/// <returns></returns>
protected ActivityDefinition
GetNextActivityDefinition() {
ActivityInstance activityInstance =
CurrentWorkItem.Activity as ActivityInstance;
TridionActivityDefinition activityDefinition =
activityInstance.ActivityDefinition as TridionActivityDefinition;
ProcessDefinition processDefinition =
activityDefinition.ProcessDefinition;
IList<ActivityDefinition>
activityDefinitions = new List<ActivityDefinition>(processDefinition.ActivityDefinitions);
IList<TridionActivityDefinition>
nextActivities = null;
foreach (TridionActivityDefinition
tridionActivityDefinition in
activityDefinitions) {
if
(tridionActivityDefinition.Id.Equals(activityDefinition.Id)) {
nextActivities
= tridionActivityDefinition.NextActivityDefinitions;
if (nextActivities == null
|| nextActivities.Count == 0) {
Logger.Warn("GetNextActivityDefinition:
Current Activity does not have a next Activity. It is the last one.");
}
else if
(nextActivities.Count > 1) {
Logger.Warn("GetNextActivityDefinition:
Current Activity is a decision. It has more than 1 next Activity.");
}
else {
return nextActivities[0];
}
}
}
return null;
}
/// <summary>
/// Return the next activity
definition by name, if there is one.
/// </summary>
/// <returns></returns>
protected ActivityDefinition
GetNextActivityDefinition(String name) {
ActivityInstance activityInstance =
CurrentWorkItem.Activity as ActivityInstance;
TridionActivityDefinition activityDefinition =
activityInstance.ActivityDefinition as TridionActivityDefinition;
IList<TridionActivityDefinition>
nextActivities = activityDefinition.NextActivityDefinitions;
if (nextActivities == null)
{
Logger.Warn("GetNextActivityDefinition:
Current Activity does not have a next Activity. It is the last one.");
} else {
foreach (ActivityDefinition
activity in nextActivities) {
if (activity.Title.Equals(name)) {
Logger.Debug("GetNextActivityDefinition:
Found activity " + activity + " by
name.");
return activity;
}
}
}
Logger.Warn("GetNextActivityDefinition:
Cannot find next Activity by name.");
return null;
}
For the code-above we need to know the CurrentWorkItem, which is the WorkItem of the current item in the workflow.
The code above is part of YAWF (Yet Another Workflow Framework).
Comments