public WorkflowStatus GetStatus(BulkUploadJobContext bulkUploadJobContext, BulkUploadWorkflowManager workflowManager)
        {
            if (workflowManager == null)
            {
                return(new WorkflowStatus
                {
                    StatusMessage = "Operation not yet started.",
                    Complete = false,
                    CurrentStep = 0,
                    Error = false,
                    TotalSteps = 0
                });
            }

            if (workflowManager.Status.Complete && bulkUploadJobContext.ValidationResult != null && !bulkUploadJobContext.ValidationResult.Valid)
            {
                var errorMessage = $"XML Validation failed: {string.Join(Environment.NewLine, bulkUploadJobContext.ValidationResult.ValidationErrors)}";
                var status       = workflowManager.Status;
                status.Error        = true;
                status.ErrorMessage = errorMessage;
                return(status);
            }

            return(workflowManager.Status);
        }
        private BulkUploadWorkflowManager SetupWorkflowManager(BulkUploadJobContext bulkUploadJobContext, FileImportService fileImportService)
        {
            var workflowManager = new BulkUploadWorkflowManager(bulkUploadJobContext);

            workflowManager.JobStarted    += () => OnStatusUpdated(GetStatus(bulkUploadJobContext, workflowManager));
            workflowManager.JobCompleted  += () => OnStatusUpdated(GetStatus(bulkUploadJobContext, workflowManager));
            workflowManager.StepStarted   += () => OnStatusUpdated(GetStatus(bulkUploadJobContext, workflowManager));
            workflowManager.StepCompleted += () => OnStatusUpdated(GetStatus(bulkUploadJobContext, workflowManager));

            workflowManager
            .StartWith(new BulkUploadWorkflowStep
            {
                StatusMessage          = "Authenticating",
                FailureMessage         = "Authentication failed. Please ensure your credentials are valid",
                RollBackAction         = CleanUp,
                ExecuteAction          = context => Authenticate(fileImportService),
                RollbackFailureMessage = "An error occured while authenticating",
                RollbackStatusMessage  = "An error occured while authenticating"
            })

            .ContinueWith(new BulkUploadWorkflowStep
            {
                StatusMessage          = "Validating XML",
                FailureMessage         = "An error occured validating XML file content.",
                RollBackAction         = CleanUp,
                ExecuteAction          = context => ValidateBulkDataXml(context, fileImportService),
                RollbackFailureMessage = "An error occured validating XML file content",
                RollbackStatusMessage  = "An error occured validating XML file content"
            })

            .ContinueWith(new BulkUploadWorkflowStep
            {
                StatusMessage          = "Importing data",
                FailureMessage         = "Data import failed. {0}",
                RollBackAction         = CleanUp,
                ExecuteAction          = context => RunImport(fileImportService),
                RollbackFailureMessage = "Data import failed",
                RollbackStatusMessage  = "Data import failed"
            })
            .ContinueWith(new BulkUploadWorkflowStep
            {
                StatusMessage          = "Cleaning up temporary files",
                FailureMessage         = "Import completed, but there was an error removing the temporary files.  {0}",
                RollBackAction         = context => { },
                ExecuteAction          = CleanUp,
                RollbackFailureMessage = "",
                RollbackStatusMessage  = ""
            }).ContinueWith(new BulkUploadWorkflowStep
            {
                StatusMessage          = "Import completed, please check Admin App logs to see the details of your import.",
                FailureMessage         = "",
                RollBackAction         = context => { },
                ExecuteAction          = context => { },
                RollbackFailureMessage = "",
                RollbackStatusMessage  = ""
            });

            return(workflowManager);
        }