public void InitiateWorkflowRequestHandler(InitiateWorkflowRequest message)
        {
            InitiateWorkflowResponse response = null;
            try
            {
                response = new InitiateWorkflowResponse();
                using (var dataContext = new DataContext(message.TenantId, message.AuditContext))
                {
                    response.AuditContext = message.AuditContext;
                    response.CorrelationId = message.CorrelationId;
                    response.TenantId = message.TenantId;

                    var newWorkflow = makeNewWorkflowInstance(message, dataContext, _bus);
                    response.WorkflowInstanceId = newWorkflow.Id;
                    _bus.Reply(response);

                    Utils.PublishWorkflowState(_bus, newWorkflow, message.AuditContext);
                    Utils.PublishNewTasks(_bus, newWorkflow, message.AuditContext);
                }
            }
            catch (Exception exc)
            {
                response = null;
                _bus.Reply(response, 0);
            }
        }
 private static WorkflowInstance makeNewWorkflowInstance(InitiateWorkflowRequest message, DataContext dataContext, IMessagingService bus)
 {
     var objType = message.TargetObjectType;
     var newWorkflow = new WorkflowInstance();
     newWorkflow.TargetObjectId = message.TargetObjectId;
     newWorkflow.TargetObjectTenantId = message.TenantId;
     newWorkflow.WorkflowTemplate = Utils.GetWorkflowTemplate(dataContext, null, objType, message.WorkflowTemplateName);
     newWorkflow.StartDate = DateTime.UtcNow;
     newWorkflow.Completed = false;
     newWorkflow.CurrentState = new State()
     {
         LastState = false,
         MetaData = null,
         StateTemplate = newWorkflow.WorkflowTemplate.States.FirstOrDefault(x => x.FirstState),
         TransitionDate = DateTime.UtcNow,
         TransitionedBy = message.AuditContext,
         TransitionEvent = new WorkflowEvent()
         {
             EventDate = DateTime.UtcNow,
             EventType = newWorkflow.WorkflowTemplate.EventsTypes.FirstOrDefault(x => x.Event == "Start_Workflow"),
             ExecutedBy = message.AuditContext,
             StateParent = newWorkflow.CurrentState,
             WorkflowInstance = newWorkflow,
         },
         Tasks = new List<Models.Task>(),
     };
     newWorkflow.CurrentState.StateTemplate.Tasks.Where(x => x.FirstTask == true).ToList().ForEach(x =>
     {
         newWorkflow.CurrentState.Tasks.Add(new Models.Task()
         {
             IsCurrentTask = true,
             State = newWorkflow.CurrentState,
             TaskTemplate = x,
             TransitionDate = DateTime.UtcNow,
             TransitionedBy = message.AuditContext,
             WorkflowInstance = newWorkflow,
         });
     }
     );
     newWorkflow.TargetObjectType = message.TargetObjectType;
     dataContext.WorkflowInstances.Add(newWorkflow);
     dataContext.SaveChanges();
     return newWorkflow;
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            IMessagingService bus = IoCContext.Current.Container.Resolve<IMessagingService>();
            IDictionary<string, string> settings = new Dictionary<string, string>();
            settings.Add("EndpointName", "PocketBoss_Test_Client");
            settings.Add("HandlersAssembly", "PocketBoss.TestingCLI.exe");
            bus.OpenConnection(settings);
            bus.AddSubscription<WorkflowStateNotification>();
            bus.AddSubscription<WorkflowTaskNotification>();

            System.Console.WriteLine("Press enter to start workflow");
            var templatesRequest = new GetWorkflowTemplatesRequest()
            {
                AuditContext = "Console App",
                CorrelationId = Guid.NewGuid(),
                TargetObjectType = "PocketBoss.Samples.User",
                TenantId = System.Guid.Empty
            };

            GetWorkflowTemplatesResponse templateLookupData = null;
            long workflowTemplateId;

            templateLookupData = bus.Send<GetWorkflowTemplatesRequest, GetWorkflowTemplatesResponse>(templatesRequest).Result;

            System.Console.WriteLine("Workflows Registered:" + templateLookupData.WorkflowTemplates.Count());

            if (!templateLookupData.WorkflowTemplates.Exists(t => t.WorkflowTemplateName.Contains("Sample Workflow")))
            {
                System.Console.WriteLine("Test Workflow not seeded");
                System.Console.Read();
                return;
            }
            else
            {
                workflowTemplateId = templateLookupData.WorkflowTemplates[0].WorkflowTemplateId;
            }

            while (Console.ReadLine() != null)
            {
                WorkflowTemplate workflowTemplate = new WorkflowTemplate();
                GetSingleWorkflowTemplateRequest templateRequest = new GetSingleWorkflowTemplateRequest()
                {
                    AuditContext = "Console App",
                    CorrelationId = Guid.NewGuid(),
                    TenantId = System.Guid.Empty,
                    WorkflowTemplateId = workflowTemplateId
                };
                GetSingleWorkflowTemplateResponse searchResponse = bus.Send<GetSingleWorkflowTemplateRequest, GetSingleWorkflowTemplateResponse>(templateRequest).Result;
                workflowTemplate = searchResponse.WorkflowTemplate;

                var startWorkflowInstance = new InitiateWorkflowRequest()
                {
                    AuditContext = "Console App",
                    CorrelationId = Guid.NewGuid(),
                    TargetObjectId = DateTime.Now.Ticks.ToString(),
                    TargetObjectType = workflowTemplate.TargetObjectType,
                    TenantId = System.Guid.Empty,
                    WorkflowTemplateName = workflowTemplate.WorkflowName
                };

                long workflowInstanceId = 0;
                InitiateWorkflowResponse initializeResponse = bus.Send<InitiateWorkflowRequest, InitiateWorkflowResponse>(startWorkflowInstance).Result;
                workflowInstanceId = initializeResponse.WorkflowInstanceId;
                System.Console.WriteLine("Workflow InstanceId = " + workflowInstanceId.ToString());
            }
        }