public static void SaveWorkflowUserAndAdvance(AssetWorkflowUser awu) { // If rejecting, comments are mandatory if (awu.AssetWorkflowUserStatus == AssetWorkflowUserStatus.Rejected && StringUtils.IsBlank(awu.Comments)) { throw new AssetWorkflowUserException("Comments are required if rejecting an asset", awu); } // Update modification date awu.LastUpdate = DateTime.Now; // Now save it AssetWorkflowUser.Update(awu); // If workflow item is still pending, our work is done here if (awu.AssetWorkflowUserStatus == AssetWorkflowUserStatus.Pending) { return; } // If workflow item was rejected, don't continue if (awu.AssetWorkflowUserStatus == AssetWorkflowUserStatus.Rejected) { RejectAndCompleteWorkflow(awu.AssetWorkflow, awu); return; } // Get a list of all users in the same asset workflow AssetWorkflowUserFinder finder = new AssetWorkflowUserFinder { AssetWorkflowId = awu.AssetWorkflowId }; finder.SortExpressions.Add(new AscendingSort(AssetWorkflowUser.Columns.Position)); List <AssetWorkflowUser> assetWorkflowUserList = AssetWorkflowUser.FindMany(finder); // Now we need to figure out who's next, or whether we're all done, so get list of all users after the current one who are still waiting List <AssetWorkflowUser> remainingUsers = assetWorkflowUserList.FindAll(o => (o.AssetWorkflowUserStatus == AssetWorkflowUserStatus.Waiting) && (o.Position > awu.Position)); if (remainingUsers.Count == 0) { // No users left, so we can complete this workflow CompleteWorkflow(awu.AssetWorkflow); return; } // Otherwise, get the next user in the workflow AssetWorkflowUser nextUserInWorkflow = remainingUsers[0]; // and notify them NotifyUser(nextUserInWorkflow); }
private static void NotifyUser(AssetWorkflowUser assetWorkflowUser) { // First set this user to pending assetWorkflowUser.AssetWorkflowUserStatus = AssetWorkflowUserStatus.Pending; // Save them AssetWorkflowUser.Update(assetWorkflowUser); // Now trigger event to send nofication if (AssetWorkflowUserSelected != null) { AssetWorkflowUserSelected(null, new AssetWorkflowUserEventArgs(assetWorkflowUser)); } }
/// <summary> /// Submits an asset to a workflow, and starts the approval process /// </summary> public static void SubmitAssetToWorkflow(Asset asset, User submittedByUser) { m_Logger.DebugFormat("Asset: {0} submitted to workflow", asset.AssetId); // First ensure we have a workflow if (asset.WorkflowId.GetValueOrDefault() == 0) { throw new SystemException("Asset submitted to workflow, but workflow is not specified"); } // First get all of the users in the workflow WorkflowUserFinder finder = new WorkflowUserFinder { WorkflowId = asset.WorkflowId.GetValueOrDefault() }; finder.SortExpressions.Add(new AscendingSort(WorkflowUser.Columns.Position)); List <WorkflowUser> workflowUserList = WorkflowUser.FindMany(finder); // Ensure workflow has users if (workflowUserList.Count == 0) { throw new SystemException("Asset submitted to workflow, but workflow does not have any users"); } // Create a new asset workflow AssetWorkflow aw = AssetWorkflow.New(); aw.AssetId = asset.AssetId.GetValueOrDefault(); aw.IsComplete = false; aw.SubmittedByUserId = submittedByUser.UserId.GetValueOrDefault(); aw.CreateDate = DateTime.Now; AssetWorkflow.Update(aw); // Now add all the users to it for (int position = 0; position < workflowUserList.Count; position++) { // Get the current workflow user WorkflowUser wfu = workflowUserList[position]; // Ensure it's not a duplicate if (aw.AssetWorkflowUserList.Any(awfu => awfu.UserId == wfu.UserId)) { continue; } // Set-up the asset workflow user AssetWorkflowUser awu = AssetWorkflowUser.New(); awu.AssetWorkflowId = aw.AssetWorkflowId.GetValueOrDefault(); awu.UserId = wfu.UserId; awu.Position = position + 1; awu.AssetWorkflowUserStatus = AssetWorkflowUserStatus.Waiting; awu.CreateDate = DateTime.Now; awu.LastUpdate = DateTime.Now; // Save the workflow user to the DB AssetWorkflowUser.Update(awu); // Add the user to the workflow aw.AssetWorkflowUserList.Add(awu); } if (AssetSubmittedToWorkflow != null) { AssetSubmittedToWorkflow(null, new AssetEventArgs(asset)); } // Now notify the next user. First get them AssetWorkflowUser nextUserInWorkflow = aw.AssetWorkflowUserList[0]; // The send notification NotifyUser(nextUserInWorkflow); }