using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Xml;
using Neo.Afx.ComponentModel;
using Neo.Afx.Services.Forms;
using Neo.Afx.Services.Workflows.Entities;
using Neo.Afx.ViewModels;
namespace Neo.Afx.Services.Workflows
{
///
/// Workflow database context container
///
public class WorkflowsDatabase : Database
{
///
/// Initializes a new instance of the class.
///
public WorkflowsDatabase()
: base(new WorkflowContext(), 120) //set the command timeout to 2 minutes
{
}
#region GetActiveWorkflowTemplates
///
/// Gets the active workflow templates.
///
///
public List GetActiveWorkflowTemplates()
{
return (from wt in Context.Vw_WorkflowTemplates
where wt.WorkflowTemplateStatusID == 1 //change to enum. 1=Active
select wt).ToList();
}
///
/// Gets the active workflow templates.
///
/// The workflow ID.
///
public List GetActiveWorkflowTemplates(long workflowId)
{
return (from wt in Context.Vw_WorkflowTemplates
where wt.WorkflowTemplateStatusID == 1 //change to enum. 1=Active
&& wt.WorkflowID == workflowId
select wt).ToList();
}
#endregion
#region GetWorkflowInstances
///
/// Gets the workflow instances.
///
///
public List GetWorkflowInstances(long workflowID, string workflowSteps, string workflowStatusList, long userID)
{
return Context.Database.SqlQuery(
"EXEC Wf.Pr_WorkflowInstanceSearch @WorkflowID, @WorkflowSteps, @WorkflowInstanceStatus, @UserID",
new SqlParameter("WorkflowID", workflowID),
new SqlParameter("WorkflowSteps", workflowSteps),
new SqlParameter("WorkflowInstanceStatus", workflowStatusList),
new SqlParameter("UserID", userID)).ToList();
}
///
/// Returns all workflows for the specified entity and item
///
///
///
///
public List GetWorkflowInstances(long entityID, long itemID)
{
return Context.Vw_WorkflowInstances.Where(a => a.EntityID == entityID && a.ItemID == itemID).ToList();
}
#endregion
#region GetTasks
///
/// Gets the tasks
///
/// The assigned to user ID.
/// The workflow instance task status ID.
/// The workflow ID.
///
public List GetTasks(long workflowTemplateStepID, WorkflowInstanceTaskStatusEnum workflowInstanceTaskStatusID)
{
return (from t in Context.Vw_WorkflowInstanceTasks
where t.WorkflowInstanceTaskStatusID == (short)workflowInstanceTaskStatusID
&& t.WorkflowTemplateStepID == workflowTemplateStepID
select t).ToList();
}
///
/// Gets the tasks.
///
/// The assigned to user ID.
/// The workflow instance task status ID.
///
public List GetTasks(long assignedToUserID, short workflowInstanceTaskStatusID)
{
return (from t in Context.Vw_WorkflowInstanceTasks
where t.AssignedToID == assignedToUserID
&& t.WorkflowInstanceTaskStatusID == workflowInstanceTaskStatusID
select t).ToList();
}
///
/// Gets the tasks
///
/// The assigned to user ID.
/// The workflow instance task status ID.
/// The workflow ID.
///
public List GetTasks(long assignedToUserID, short workflowInstanceTaskStatusID, long workflowID)
{
return (from t in Context.Vw_WorkflowInstanceTasks
where t.AssignedToID == assignedToUserID
&& t.WorkflowInstanceTaskStatusID == workflowInstanceTaskStatusID
&& t.WorkflowID == workflowID
select t).ToList();
}
///
/// Gets the tasks
///
/// The workflow Instance ID.
///
public List GetTasks(long workflowInstanceID)
{
return (from t in Context.Vw_WorkflowInstanceTasks
where t.WorkflowInstanceID == workflowInstanceID
select t).ToList();
}
///
/// Gets the tasks
///
/// The entity ID.
/// The item ID.
///
public List GetTasks(long entityID, long itemID)
{
return (from t in Context.Vw_WorkflowInstanceTasks
where t.WorkflowInstanceEntityID == entityID
&& t.WorkflowInstanceItemID == itemID
select t).ToList();
}
#endregion
#region GetWorkflow
///
/// Gets the workflow.
///
/// The workflow ID.
/// The user ID.
///
public Workflow GetWorkflow(long workflowID, long userID)
{
return (from w in Context.Workflows.Include("WorkflowEntity")
where w.WorkflowID == workflowID
select w).FirstOrDefault();
}
#endregion
#region GetWorkflowTemplate
///
/// Gets the workflow template.
///
/// The workflow template ID.
/// The user ID.
///
public WorkflowTemplate GetWorkflowTemplate(long workflowTemplateID, long userID)
{
return (from wt in Context.WorkflowTemplates.Include("WorkflowTemplateDefaults").Include("WorkflowTemplateSteps").Include("WorkflowTemplateParticipants").Include("WorkflowTemplateParticipants.WorkflowTemplateParticipantRoles")
where wt.WorkflowTemplateID == workflowTemplateID
select wt).FirstOrDefault();
}
#endregion
#region GetActiveWorkflowTemplate
///
/// Gets the active workflow template.
///
/// The workflow ID.
/// The user ID.
///
public WorkflowTemplate GetActiveWorkflowTemplate(long workflowID, long userID)
{
var obj = (from wt in Context.WorkflowTemplates.Include("WorkflowTemplateDefaults").Include("WorkflowTemplateSteps").Include("WorkflowTemplateParticipants").Include("WorkflowTemplateParticipants.WorkflowTemplateParticipantRoles")
where wt.WorkflowID == workflowID
&& wt.WorkflowTemplateStatusID == 1 //active
select wt).FirstOrDefault();
obj.WorkflowTemplateDefaults = obj.WorkflowTemplateDefaults.ToList();
obj.WorkflowTemplateSteps = obj.WorkflowTemplateSteps.ToList();
obj.WorkflowTemplateParticipants = obj.WorkflowTemplateParticipants.ToList();
return obj;
}
#endregion
#region GetWorkflowInstance
///
/// Gets the workflow instance.
///
/// The workflow instance ID.
/// The user ID.
///
public WorkflowInstance GetWorkflowInstance(long workflowInstanceID, long userID)
{
if(workflowInstanceID == -1)
{
var workflowTemplateStep = new WorkflowTemplateStep()
{
Step = 1,
WorkflowTemplateStepTypeID = 2
};
var tasks = new List();
tasks.Add(new WorkflowInstanceTask()
{
CreatedDate = DateTime.Now,
AssignedToID = userID,
CreatedByID = userID,
WorkflowTemplateStepID = 1,
WorkflowInstanceTaskStatusID = 1,
WorkflowTemplateStep = workflowTemplateStep,
AssignedTo = new WorkflowUser()
{
KnownAs = ""
},
WorkflowInstanceTaskStatus = new WorkflowInstanceTaskStatus()
{
Description = ""
},
CompletedBy = new WorkflowUser()
{
KnownAs = ""
}
});
return new WorkflowInstance()
{
CreatedByID = userID,
WorkflowTemplateID = 1,
WorkflowTemplateStepID = 1,
WorkflowTemplateStep = workflowTemplateStep,
WorkflowInstanceTasks = tasks,
WorkflowInstanceStatus = new WorkflowInstanceStatus()
{
WorkflowInstanceStatusID = 1,
Description = "New Request"
}
};
}
var results = (from wi in Context.WorkflowInstances.Include("WorkflowInstanceStatus").Include("WorkflowTemplateStep").Include("WorkflowInstanceTasks").Include("WorkflowInstanceTasks.AssignedTo").Include("WorkflowInstanceTasks.CompletedBy").Include("WorkflowInstanceTasks.WorkflowInstanceTaskStatus").Include("WorkflowInstanceTasks.WorkflowTemplateStep").Include("WorkflowInstanceParticipants").Include("WorkflowInstanceParticipants.WorkflowTemplateParticipant").Include("WorkflowInstanceParticipants.WorkflowTemplateParticipant.WorkflowTemplateParticipantRoles")
where wi.WorkflowInstanceID == workflowInstanceID
select wi).FirstOrDefault();
results.WorkflowInstanceTasks = results.WorkflowInstanceTasks.OrderBy(a => a.CreatedDate).ToList();
return results;
}
#endregion
#region GetWorkflowInstance
///
/// Gets the workflow instance based on the entity and itemid.
///
///
public WorkflowInstance GetWorkflowInstance(int entityID, long itemID)
{
var instance = (from wi in Context.WorkflowInstances.Include("WorkflowInstanceStatus").Include("WorkflowTemplateStep").Include("WorkflowInstanceTasks").Include("WorkflowInstanceTasks.AssignedTo").Include("WorkflowInstanceTasks.CompletedBy").Include("WorkflowInstanceTasks.WorkflowInstanceTaskStatus").Include("WorkflowInstanceTasks.WorkflowTemplateStep").Include("WorkflowInstanceParticipants").Include("WorkflowInstanceParticipants.WorkflowTemplateParticipant").Include("WorkflowInstanceParticipants.WorkflowTemplateParticipant.WorkflowTemplateParticipantRoles")
where wi.EntityID == entityID && wi.ItemID == itemID
select wi).FirstOrDefault();
instance.WorkflowInstanceTasks = instance.WorkflowInstanceTasks.OrderBy(a => a.CreatedDate).ToList();
return instance;
}
#endregion
#region GetWorkflowInstanceView
///
/// Gets the workflow instance view based on the entity and itemid.
///
///
public Vw_WorkflowInstance GetWorkflowInstanceView(long entityID, long itemID)
{
return (from wi in Context.Vw_WorkflowInstances
where wi.EntityID == entityID && wi.ItemID == itemID
select wi).FirstOrDefault();
}
#endregion
#region GetWorkflowInstanceRoute
///
/// Gets the workflow instance route.
///
/// The workflow template ID.
/// The workflow instance ID.
/// The user ID.
///
public WorkflowInstanceRoute GetWorkflowInstanceRoute(long workflowTemplateID, long workflowInstanceID, long userID)
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(Context.Database.SqlQuery("EXEC Wf.Pr_WorkflowInstanceRouteGet @workflowTemplateID, @workflowInstanceID, @userID", new SqlParameter("WorkflowTemplateID", workflowTemplateID), new SqlParameter("WorkflowInstanceID", workflowInstanceID), new SqlParameter("UserID", userID)).FirstOrDefault());
return new WorkflowInstanceRoute(xmlDoc);
}
#endregion
#region SubmitWorkflowTaskAction
///
/// Submits the workflow task action.
///
/// The workflow instance task ID.
/// The workflow instance task status ID.
/// Name of the completed by user.
/// The request correction workflow template step ID.
/// The comments.
///
public string SubmitWorkflowTaskAction(long workflowInstanceTaskID, int workflowInstanceTaskStatusID, string completedByUserName, long requestCorrectionWorkflowTemplateStepID, string comments)
{
string errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceTaskAction @WorkflowInstanceTaskID, @WorkflowInstanceTaskStatusID, @CompletedByUserName, @RequestCorrectionWorkflowTemplateStepID, @Comments, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowInstanceTaskID", workflowInstanceTaskID),
new SqlParameter("WorkflowInstanceTaskStatusID", workflowInstanceTaskStatusID),
new SqlParameter("CompletedByUserName", completedByUserName),
new SqlParameter("RequestCorrectionWorkflowTemplateStepID", requestCorrectionWorkflowTemplateStepID),
new SqlParameter("Comments", comments),
errorParam);
if(!string.IsNullOrEmpty(errorParam.Value.ToStringSafe()))
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region SubmitWorkflowInstanceErrorAction
///
/// Submits the workflow instance error action.
///
/// The workflow instance ID.
/// The error action id. (1=Retry, 2=Override, 3=Request Correction)
/// Name of the completed by user.
/// The request correction workflow template step ID.
/// The comments.
///
public string SubmitWorkflowInstanceErrorAction(long workflowInstanceID, int errorActionId, string completedByUserName, long requestCorrectionWorkflowTemplateStepID, string comments)
{
var errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceErrorAction @WorkflowInstanceID, @WorkflowInstanceErrorActionID, @CompletedByUserName, @RequestCorrectionWorkflowTemplateStepID, @Comments, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowInstanceID", workflowInstanceID),
new SqlParameter("WorkflowInstanceErrorActionID", errorActionId),
new SqlParameter("CompletedByUserName", completedByUserName),
new SqlParameter("RequestCorrectionWorkflowTemplateStepID", requestCorrectionWorkflowTemplateStepID),
new SqlParameter("Comments", comments),
errorParam);
if(!string.IsNullOrEmpty(errorParam.Value.ToStringSafe()))
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region StartWorkflowInstance
///
/// Starts the workflow instance.
///
/// The workflow ID.
/// The entity ID.
/// The item ID.
/// The workflow participants.
/// Name of the user.
///
public string StartWorkflowInstance(int workflowID, int entityID, long itemID, List workflowParticipants, string referenceNumber, string userName)
{
var errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
var participantList = workflowParticipants.Aggregate("", (current, p) => current + (p.WorkflowTemplateParticipantID + "_" + p.UserID + ','));
if(participantList.Length > 0)
{
participantList = participantList.Substring(0, participantList.Length - 1);
}
var returnVal = Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceStart @WorkflowID, @EntityID, @ItemID, @WorkflowParticipants, @UserName, @ReferenceNumber, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowID", workflowID),
new SqlParameter("EntityID", entityID),
new SqlParameter("ItemID", itemID),
new SqlParameter("WorkflowParticipants", participantList),
new SqlParameter("ReferenceNumber", referenceNumber),
new SqlParameter("UserName", userName),
errorParam);
if(returnVal <= 0)
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region SaveWorkflowInstance
///
/// Saves the workflow instance.
///
/// The workflow instance ID.
/// The workflow participants.
/// Name of the user.
///
public string SaveWorkflowInstance(long workflowInstanceID, List workflowParticipants, string userName)
{
var errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
var participantList = workflowParticipants.Aggregate("", (current, p) => current + (p.WorkflowTemplateParticipantID + "_" + p.UserID + ','));
if(participantList.Length > 0)
{
participantList = participantList.Substring(0, participantList.Length - 1);
}
var returnVal = Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceSave @WorkflowInstanceID, @WorkflowParticipants, @UserName, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowInstanceID", workflowInstanceID),
new SqlParameter("WorkflowParticipants", participantList),
new SqlParameter("UserName", userName),
errorParam);
if(returnVal <= 0)
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region ReAssignWorkflowInstanceTask
///
/// Res the assign workflow instance task.
///
/// The workflow instance task ID.
/// The reassigned to ID.
/// The comments.
/// Name of the user.
///
public string ReAssignWorkflowInstanceTask(long workflowInstanceTaskID, long reassignedToID, string comments, string userName)
{
var errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
var returnVal = 0;
try
{
returnVal = Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceTaskReAssign @WorkflowInstanceTaskID, @ReassignedByUserName, @ReassignedTo, @Comments, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowInstanceTaskID", workflowInstanceTaskID),
new SqlParameter("ReassignedByUserName", userName),
new SqlParameter("ReassignedTo", reassignedToID),
new SqlParameter("Comments", comments),
errorParam);
}
catch(Exception ex)
{
return ex.Message;
}
if(returnVal <= 0)
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region EscalateWorkflowInstanceTask
///
/// Escalate a workflow instance task (or return it to the original user when done).
///
///
///
///
///
///
///
public string EscalateWorkflowInstanceTask(long workflowInstanceTaskID, long escalateToID, bool returnEscalation, string comments, string userName)
{
var errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
var returnVal = 0;
try
{
returnVal = Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceTaskEscalation @WorkflowInstanceTaskID, @EscalationByUserName, @EscalateTo, @ReturnEscalation, @Comments, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowInstanceTaskID", workflowInstanceTaskID),
new SqlParameter("EscalationByUserName", userName),
new SqlParameter("EscalateTo", escalateToID),
new SqlParameter("ReturnEscalation", returnEscalation),
new SqlParameter("Comments", comments),
errorParam);
}
catch (Exception ex)
{
return ex.Message;
}
if (returnVal <= 0)
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region CancelWorkflow
///
/// Cancel's a workflow
///
public string CancelWorkflow(long workflowInstanceID, string completedByUserName, string comments)
{
string errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
var returnVal = Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceCancel @WorkflowInstanceID, @WorkflowCancelledByUserName, @Comments, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowInstanceID", workflowInstanceID),
new SqlParameter("WorkflowCancelledByUserName", completedByUserName),
new SqlParameter("Comments", comments),
errorParam);
if(returnVal <= 0)
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region CheckWorkflowInstanceSecurity
///
/// Checks if a user has access to a workflow.
///
/// The workflow ID.
/// The item ID.
/// Name of the completed by user.
///
public bool CheckWorkflowInstanceSecurity(long workflowID, long itemID, string completedByUserName)
{
var returnVal = Context.Database.SqlQuery(
"EXEC Wf.Pr_WorkflowInstanceCheckSecurity @WorkflowID, @ItemID, @CompletedByUserName",
new SqlParameter("WorkflowID", workflowID),
new SqlParameter("ItemID", itemID),
new SqlParameter("CompletedByUserName", completedByUserName)).FirstOrDefault();
return (returnVal == 1) ? true : false;
}
#endregion
#region GetWorkflowConsoleViewModel
public WorkflowConsoleViewModelBase GetWorkflowConsoleViewModel(long workflowid, long workflowInstanceID, long itemID, long userID, IEnumerable roles)
{
return GetWorkflowConsoleViewModel(workflowid, workflowInstanceID, itemID, userID, roles, null);
}
///
/// Gets a populated console view model.
///
/// The workflowid.
/// The workflow instance ID.
/// The item ID.
/// The user ID.
/// The roles.
/// The workflowbuddies.
/// WorkflowConsoleViewModelBase with the following pupulated:
/// ControllerName
/// UserID
/// UserRoles
/// ItemID
/// WorkflowID
/// WorkflowFriendlyName
/// WorkflowTemplateDefaults
/// EntityID
/// EntityKey
/// WorkflowInstance
/// WorkflowInstanceRoute
/// FormChecklistResults
/// Ui.canReAssignTasks
///
public WorkflowConsoleViewModelBase GetWorkflowConsoleViewModel(long workflowid, long workflowInstanceID, long itemID, long userID, IEnumerable roles, List> workflowBuddies)
{
var workflowDatabase = new WorkflowsDatabase();
var formDatabase = new FormsDatabase();
Workflow workflow;
WorkflowTemplate workflowTemplate;
WorkflowInstance workflowInstance;
////var checklistResults = new List();
if(workflowInstanceID != -1)
{
workflowInstance = workflowDatabase.GetWorkflowInstance(workflowInstanceID, userID);
workflowTemplate = workflowDatabase.GetWorkflowTemplate(workflowInstance.WorkflowTemplateID, userID);
workflow = workflowDatabase.GetWorkflow(workflowTemplate.WorkflowID, userID);
if (workflowBuddies != null)
{
foreach (var t in workflowInstance.WorkflowInstanceTasks.Where(a => a.WorkflowInstanceTaskStatusID == 1)) //1=Outstanding
{
foreach (var w in workflowBuddies)
{
if (workflow.WorkflowID == w.Key && t.AssignedToID == w.Value)
{
t.canReAssignToSelf = true; //users can re-asisgn tasks to themselves if they have been set up as a buddy of the user that has the current task assigned
}
}
}
}
foreach(var wip in workflowInstance.WorkflowInstanceParticipants)
{
wip.WorkflowTemplateParticipantFieldName = wip.WorkflowTemplateParticipant.FieldName;
wip.WorkflowTemplateParticipantDisplayName = wip.WorkflowTemplateParticipant.DisplayName;
wip.IsVisible = wip.WorkflowTemplateParticipant.IsVisible;
wip.WorkflowTemplateParticipantStaticParameters = new List>()
{
new KeyValuePair("RoleIDs", string.Join(",", wip.WorkflowTemplateParticipant.WorkflowTemplateParticipantRoles.Select(s => s.RoleID.ToString()).ToArray()))
};
}
////checklistResults.AddRange(formDatabase.GetChecklistResults(workflow.EntityID, itemID));
}
else
{
workflow = workflowDatabase.GetWorkflow(workflowid, userID);
workflowTemplate = workflowDatabase.GetActiveWorkflowTemplate(workflowid, userID);
workflowInstance = workflowDatabase.GetWorkflowInstance(workflowInstanceID, userID);
workflowInstance.WorkflowTemplate = workflowTemplate;
workflowInstance.WorkflowInstanceParticipants = new List();
foreach(var wtp in workflowTemplate.WorkflowTemplateParticipants.OrderBy(a => a.OrderBy))
{
workflowInstance.WorkflowInstanceParticipants.Add(new WorkflowInstanceParticipant()
{
WorkflowInstanceID = -1,
UserID = wtp.DefaultUserID,
WorkflowTemplateParticipantID = wtp.WorkflowTemplateParticipantID,
WorkflowTemplateParticipantFieldName = wtp.FieldName,
WorkflowTemplateParticipantDisplayName = wtp.DisplayName,
IsVisible = wtp.IsVisible,
WorkflowTemplateParticipantStaticParameters = new List>()
{
new KeyValuePair("RoleIDs", string.Join(",", wtp.WorkflowTemplateParticipantRoles.Select(s => s.RoleID.ToString()).ToArray()))
}
});
}
////foreach(var ctq in formDatabase.GetChecklistTemplateQuestionsAll(5, workflowTemplate.WorkflowTemplateID))
////{
//// checklistResults.Add(new Forms.Entities.ChecklistResult()
//// {
//// EntityID = workflow.EntityID,
//// ItemID = itemID,
//// ChecklistTemplateQuestionID = ctq.ChecklistTemplateQuestionID,
//// ChecklistTemplateQuestion = ctq
//// });
////}
SetDefaultParticipant(workflowInstance.WorkflowInstanceParticipants, "Initiator", userID);
}
var workflowInstanceRoute = workflowDatabase.GetWorkflowInstanceRoute(workflowTemplate.WorkflowTemplateID, workflowInstanceID, userID);
itemID = (itemID != -1) ? itemID : (new Random().Next(2000000, 3000000) * -1);
////var isAdmin = false;
////foreach(var adminRole in workflow.AdministratorRoles.Split(','))
////{
//// var roleID = adminRole.ToInt16();
//// var matchedRoles = roles.Where(a => a == roleID);
//// if(matchedRoles.Count() > 0)
//// {
//// isAdmin = true;
//// break;
//// }
////}
return new WorkflowConsoleViewModelBase
{
ControllerName = workflow.WorkflowControllerName,
DashboardControllerName = workflow.DashboardControllerName,
UserID = userID,
UserRoles = roles,
////UserIsAdmin = isAdmin,
ItemID = itemID,
WorkflowID = workflow.WorkflowID,
WorkflowFriendlyName = workflow.WorkflowFriendlyName,
WorkflowTemplateDefaults = workflowTemplate.WorkflowTemplateDefaults,
DocumentStoreLocationID = workflowTemplate.DocumentStoreLocationID,
EntityID = workflow.EntityID,
EntityKey = workflow.WorkflowEntity.EntityKey,
WorkflowInstance = workflowInstance,
WorkflowInstanceRoute = workflowInstanceRoute
////FormChecklistResults = checklistResults,
////Ui = new WorkflowConsoleUIProperties()
////{
//// canReAssignTasks = isAdmin
////}
};
}
#endregion
#region SetDefaultParticipant
///
/// Set's the userID property in the participants list based on a specified key (WorkflowTemplateParticipantFieldName)
///
///
///
///
private static void SetDefaultParticipant(IEnumerable participants, string key, long value)
{
foreach(var p in participants.Where(p => p.WorkflowTemplateParticipantFieldName.ToLower() == key.ToLower()))
{
p.UserID = value;
}
}
#endregion
#region CanActionWorkflowInstanceTask
///
/// Determines whether this instance [can action workflow instance task] the specified workflow instance task ID.
///
/// The workflow instance task ID.
/// The user ID.
///
public bool? CanActionWorkflowInstanceTask(long workflowInstanceTaskID, long userID)
{
return Context.Pr_WorkflowInstanceTaskCanAction(workflowInstanceTaskID, userID).FirstOrDefault();
}
#endregion
#region GetWorkflowInstanceTaskStepsForCorrection
public List GetWorkflowInstanceTaskStepsForCorrection(long workflowInstanceTaskID)
{
return Context.Pr_WorkflowInstanceTaskStepsGetForCorrection(workflowInstanceTaskID).ToList();
}
#endregion
#region GetWorkflowInstanceTaskEscalationUsers
public List GetWorkflowInstanceTaskEscalationUsers(long workflowInstanceTaskID, bool includeUserSettings = false)
{
return Context.Pr_WorkflowInstanceTaskEscalationUsersGet(workflowInstanceTaskID, includeUserSettings).ToList();
}
#endregion
#region IsWorkflowBuddy
///
/// Determines whether this specified user is a buddy of another user
///
///
public bool IsWorkflowBuddy(long workflowID, long assignedToUserID, long buddyUserID)
{
var b = (from t in Context.WorkflowBuddies
where t.UserID == assignedToUserID
&& t.WorkflowID == workflowID
&& t.BuddyUserID == buddyUserID
select t).FirstOrDefault();
return (b != null);
}
#endregion
#region ReAssignWorkflowInstanceGroupTask
///
/// Reassigns a group task from the system user to the current user, if the user belongs to the role linked to the step
///
/// The workflow instance ID.
/// The reassigned to ID.
/// The comments.
/// The user id of the user performing the re-assign.
///
public string ReAssignWorkflowInstanceGroupTask(long workflowInstanceID, long reassignedToID, string comments, long userID)
{
var errorMessage = "";
var errorParam = new SqlParameter("@ErrorMessage", SqlDbType.VarChar, 1000)
{
Direction = ParameterDirection.Output
};
var returnVal = 0;
try
{
returnVal = Context.Database.ExecuteSqlCommand(
"EXEC Wf.Pr_WorkflowInstanceTaskReAssignGroupTask @WorkflowInstanceID, @ReassignedToID, @UserID, @Comments, @ErrorMessage OUTPUT",
new SqlParameter("WorkflowInstanceID", workflowInstanceID),
new SqlParameter("ReassignedToID", reassignedToID),
new SqlParameter("UserID", userID),
new SqlParameter("Comments", comments),
errorParam);
}
catch (Exception ex)
{
return ex.Message;
}
if (returnVal <= 0)
{
errorMessage = CleanError(errorParam.Value.ToString());
}
return errorMessage;
}
#endregion
#region CleanError
///
/// Cleans the error.
///
/// The error message.
///
static string CleanError(string err)
{
if(err.Contains("was deadlocked on lock"))
{
err = "Error completing action. Please try again.";
}
return err;
}
#endregion
#region GetWorkflowInstanceTaskView
public Vw_WorkflowInstanceTask GetWorkflowInstanceTaskView(long workflowInstanceTaskID)
{
return Context.Vw_WorkflowInstanceTasks.Where(a => a.WorkflowInstanceTaskID == workflowInstanceTaskID).FirstOrDefault();
}
#endregion
#region UpdateWorkflowInstanceDocumentID
public void UpdateWorkflowInstanceDocumentID(long workflowInstanceDocumentID, long documentID, long userID)
{
Context.Pr_WorkflowInstanceDocumentIdUpdate(workflowInstanceDocumentID, documentID, userID);
}
#endregion
#region UpdateWorkflowInstanceDocumentExpiryDate
public void UpdateWorkflowInstanceDocumentExpiryDate(long workflowInstanceDocumentID, DateTime expiryDate, long userID)
{
Context.Pr_WorkflowInstanceDocumentExpiryDateUpdate(workflowInstanceDocumentID, expiryDate, userID);
}
#endregion
#region UpdateWorkflowInstanceDocumentTitle
public void UpdateWorkflowInstanceDocumentTitle(long workflowInstanceDocumentID, string title, long userID)
{
Context.Pr_WorkflowInstanceDocumentTitleUpdate(workflowInstanceDocumentID, title, userID);
}
#endregion
#region GetWorkflowInstanceDocuments
public List GetWorkflowInstanceDocuments(long workflowInstanceID, long userID)
{
return Context.Pr_WorkflowInstanceDocumentsGet(workflowInstanceID, userID).ToList();
}
#endregion
#region GetReferenceNumber
public string GetReferenceNumber(long entityID, long itemID)
{
var wi = Context.WorkflowInstances.Where(a => a.EntityID == entityID && a.ItemID == itemID).FirstOrDefault();
return (wi != null) ? wi.ReferenceNumber : null;
}
#endregion
#region PerformCanSubmitToNextStepChecks
public List PerformCanSubmitToNextStepChecks(long workflowInstanceID, long? taskID, long userID)
{
return Context.Pr_WorkflowInstanceSubmitToNextStepChecks(workflowInstanceID, taskID, userID).ToList();
}
#endregion
#region ExecutePostFormSave
public void ExecutePostFormSave(long workflowInstanceID, long userID)
{
Context.Pr_WorkflowInstancePostFormSave(workflowInstanceID, userID);
}
#endregion
#region WorkflowInstanceTaskCanAssignToMe
///
/// Gets the workflow instance based on the entity and itemid.
///
///
public bool WorkflowInstanceTaskCanAssignToMe(long workflowInstanceTaskID, long userID)
{
bool? result = Context.Pr_WorkflowInstanceTaskCanAssignToMe(workflowInstanceTaskID, userID).FirstOrDefault();
return (result == null) ? false : result.Value;
}
#endregion
#region WorkflowInstanceRetryFailedTask
///
/// Gets the workflow instance based on the entity and itemid.
///
///
public void WorkflowInstanceRetryFailedTask(long workflowInstanceID,long workflowInstanceTaskID, string comments, long userID)
{
Context.Pr_WorkflowInstanceRetryFailedTask(workflowInstanceID, workflowInstanceTaskID, comments, userID);
}
#endregion
}
}