using System;
using System.Linq;
using CMS.SettingsProvider;
using CMS.WorkflowEngine;
///
/// Functions for automation macro methods.
///
public class AutomationFunctions
{
#region "Public methods"
///
/// Returns true if process had passed through one/all selected automation actions.
///
/// Process instance to check
/// Automation action names separated with a semicolon
/// If true all actions must have been passed.
public static bool PassedThroughActions(object state, string actions, bool allActions)
{
AutomationStateInfo si = state as AutomationStateInfo;
if (si != null)
{
if (!String.IsNullOrEmpty(actions))
{
// Get IDs of action steps this process visited in history
string historyWhere = String.Format("HistoryStepType = {0} AND HistoryStateID = {1}", (int)WorkflowStepTypeEnum.Action, si.StateID);
var history = AutomationHistoryInfoProvider.GetAutomationHistories(historyWhere, null, 0, "HistoryStepID");
// Get action IDs of actions associated to the visited action steps
string stepWhere = SqlHelperClass.GetWhereCondition("StepID", history.Select(h => h.HistoryStepID).ToList());
var actionSteps = WorkflowStepInfoProvider.GetWorkflowSteps(stepWhere, null, 0, "StepActionID");
string[] selectedActions = actions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// Get action infos to visited actions that have selected name
string actionWhere = SqlHelperClass.GetWhereCondition("ActionID", actionSteps.Select(s => s.StepActionID).ToList());
actionWhere = SqlHelperClass.AddWhereCondition(actionWhere, SqlHelperClass.GetWhereCondition("ActionName", selectedActions));
var actionInfos = WorkflowActionInfoProvider.GetWorkflowActions(actionWhere, null, "ActionName").Items;
// Return true if all/any actions were visited in history
return allActions ? (actionInfos.Count == selectedActions.Length) : (actionInfos.Count > 0);
}
else if (allActions)
{
// No actions were selected
return true;
}
}
return false;
}
///
/// Returns true if process had passed through one/all specified automation steps.
///
/// Process instance to check
/// Automation step names separated with a semicolon
/// If true all specified steps must have been passed.
public static bool PassedThroughSteps(object state, string steps, bool allSteps)
{
AutomationStateInfo si = state as AutomationStateInfo;
if (si != null)
{
if (!String.IsNullOrEmpty(steps))
{
string[] stepArray = steps.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// Get only the step names of workflow steps visited in history that are selected by user
string where = String.Format("HistoryStateID = {0} AND {1}", si.StateID, SqlHelperClass.GetWhereCondition("HistoryStepName", stepArray));
var history = AutomationHistoryInfoProvider.GetAutomationHistories(where, null, 0, "HistoryStepName").Items;
if (history.Count > 0)
{
// At least of the selected steps was found in the history
if (!allSteps)
{
return true;
}
// Check whether all of the selected steps are in the history
return stepArray.All(s => history.Any(h => h.HistoryStepName.EqualsCSafe(s, true)));
}
}
else if (allSteps)
{
return true;
}
}
return false;
}
#endregion
}