internal WorkflowTree ExecuteWorkflow(WorkflowTree workflow, Entity primaryEntity, PluginContext pluginContext, Core core) { var provider = new MockupServiceProviderAndFactory(core, pluginContext, new TracingService()); var service = provider.CreateAdminOrganizationService(new MockupServiceSettings(true, true, MockupServiceSettings.Role.SDK)); return(workflow.Execute(primaryEntity, core.TimeOffset, service, provider, provider.GetService(typeof(ITracingService)) as ITracingService)); }
private void Execute(Entity workflow, string operation, object entityObject, Entity preImage, Entity postImage, PluginContext pluginContext, Core core) { // Check if it is supposed to execute. Returns preemptively, if it should not. var entity = entityObject as Entity; var entityRef = entityObject as EntityReference; var guid = entity?.Id ?? entityRef.Id; var logicalName = entity?.LogicalName ?? entityRef.LogicalName; checkInfiniteRecursion(pluginContext); var isCreate = operation.ToLower() == EventOperationStrings.Create; var isUpdate = operation.ToLower() == EventOperationStrings.Update; var isDelete = operation.ToLower() == EventOperationStrings.Delete; var triggerFields = new HashSet <string>(); if (workflow.GetAttributeValue <string>("triggeronupdateattributelist") != null) { foreach (var field in workflow.GetAttributeValue <string>("triggeronupdateattributelist").Split(',')) { triggerFields.Add(field); } } var thisStage = isCreate ? workflow.GetOptionSetValue <workflow_stage>("createstage") : (isDelete ? workflow.GetOptionSetValue <workflow_stage>("deletestage") : workflow.GetOptionSetValue <workflow_stage>("updatestage")); if (thisStage == null) { thisStage = workflow_stage.Postoperation; } var thisPluginContext = createPluginContext(pluginContext, workflow, thisStage, guid, logicalName); var parsedWorkflow = ParseWorkflow(workflow); WorkflowTree postExecution = null; if (thisStage == workflow_stage.Preoperation) { postExecution = ExecuteWorkflow(parsedWorkflow, preImage.CloneEntity(), thisPluginContext, core); } else { postExecution = ExecuteWorkflow(parsedWorkflow, postImage.CloneEntity(), thisPluginContext, core); } if (postExecution.Variables["Wait"] != null) { waitingWorkflows.Add(postExecution.Variables["Wait"] as WaitInfo); } }
private void ExecuteIfMatch(Entity workflow, EventOperation operation, ExecutionStage stage, object entityObject, Entity preImage, Entity postImage, PluginContext pluginContext, Core core) { // Check if it is supposed to execute. Returns preemptively, if it should not. if (workflow.LogicalName != "workflow") { return; } var entity = entityObject as Entity; var entityRef = entityObject as EntityReference; var guid = (entity != null) ? entity.Id : entityRef.Id; var logicalName = (entity != null) ? entity.LogicalName : entityRef.LogicalName; if (workflow.GetAttributeValue <string>("primaryentity") != "" && workflow.GetAttributeValue <string>("primaryentity") != logicalName) { return; } if (pluginContext.Depth > 8) { throw new FaultException( "This workflow job was canceled because the workflow that started it included an infinite loop." + " Correct the workflow logic and try again."); } var isCreate = operation == EventOperation.Create; var isUpdate = operation == EventOperation.Update; var isDelete = operation == EventOperation.Delete; if (!isCreate && !isUpdate && !isDelete) { return; } if (isCreate && (!workflow.GetAttributeValue <bool?>("triggeroncreate").HasValue || !workflow.GetAttributeValue <bool?>("triggeroncreate").Value)) { return; } if (isDelete && (!workflow.GetAttributeValue <bool?>("triggerondelete").HasValue || !workflow.GetAttributeValue <bool?>("triggerondelete").Value)) { return; } var triggerFields = new HashSet <string>(); if (workflow.GetAttributeValue <string>("triggeronupdateattributelist") != null) { foreach (var field in workflow.GetAttributeValue <string>("triggeronupdateattributelist").Split(',')) { triggerFields.Add(field); } } if (isUpdate && ( workflow.GetAttributeValue <string>("triggeronupdateattributelist") == null || workflow.GetAttributeValue <string>("triggeronupdateattributelist") == "" || !entity.Attributes.Any(a => workflow.GetAttributeValue <string>("triggeronupdateattributelist").Split(',').Any(f => a.Key == f)))) { return; } var thisStage = isCreate ? workflow.GetOptionSetValue <workflow_stage>("createstage") : (isDelete ? workflow.GetOptionSetValue <workflow_stage>("deletestage") : workflow.GetOptionSetValue <workflow_stage>("updatestage")); if (thisStage == null) { thisStage = workflow_stage.Postoperation; } if ((int)thisStage != (int)stage) { return; } // Create the plugin context var thisPluginContext = pluginContext.Clone(); thisPluginContext.Mode = ((int)workflow.GetOptionSetValue <Workflow_Mode>("mode") + 1) % 2; thisPluginContext.Stage = thisStage.HasValue ? (int)thisStage : (int)workflow_stage.Postoperation; thisPluginContext.PrimaryEntityId = guid; thisPluginContext.PrimaryEntityName = logicalName; var parsedWorkflow = ParseWorkflow(workflow); if (parsedWorkflow == null) { return; } WorkflowTree postExecution = null; if (thisStage == workflow_stage.Preoperation) { postExecution = ExecuteWorkflow(parsedWorkflow, preImage, thisPluginContext, core); } else { postExecution = ExecuteWorkflow(parsedWorkflow, postImage, thisPluginContext, core); } if (postExecution.Variables["Wait"] != null) { waitingWorkflows.Add(postExecution.Variables["Wait"] as WaitInfo); } }
public WorkflowExecutionContext(WorkflowTree workflow, PluginContext pluginContext, EntityReference primaryRef) { this.workflow = workflow; this.pluginContext = pluginContext; this.primaryRef = primaryRef; }
private void ExecuteIfMatch(Entity workflow, EventOperation operation, ExecutionStage stage, object entityObject, Entity preImage, Entity postImage, PluginContext pluginContext, Core core) { // Check if it is supposed to execute. Returns preemptively, if it should not. if (workflow.LogicalName != "workflow") { return; } var entity = entityObject as Entity; var entityRef = entityObject as EntityReference; var guid = entity?.Id ?? entityRef.Id; var logicalName = entity?.LogicalName ?? entityRef.LogicalName; if (workflow.GetAttributeValue <string>("primaryentity") != "" && workflow.GetAttributeValue <string>("primaryentity") != logicalName) { return; } checkInfiniteRecursion(pluginContext); var isCreate = operation == EventOperation.Create; var isUpdate = operation == EventOperation.Update; var isDelete = operation == EventOperation.Delete; if (!isCreate && !isUpdate && !isDelete) { return; } if (isCreate && (!workflow.GetAttributeValue <bool?>("triggeroncreate").HasValue || !workflow.GetAttributeValue <bool?>("triggeroncreate").Value)) { return; } if (isDelete && (!workflow.GetAttributeValue <bool?>("triggerondelete").HasValue || !workflow.GetAttributeValue <bool?>("triggerondelete").Value)) { return; } var triggerFields = new HashSet <string>(); if (workflow.GetAttributeValue <string>("triggeronupdateattributelist") != null) { foreach (var field in workflow.GetAttributeValue <string>("triggeronupdateattributelist").Split(',')) { triggerFields.Add(field); } } if (isUpdate && ( workflow.GetAttributeValue <string>("triggeronupdateattributelist") == null || workflow.GetAttributeValue <string>("triggeronupdateattributelist") == "" || !entity.Attributes.Any(a => workflow.GetAttributeValue <string>("triggeronupdateattributelist").Split(',').Any(f => a.Key == f)))) { return; } var thisStage = isCreate ? workflow.GetOptionSetValue <workflow_stage>("createstage") : (isDelete ? workflow.GetOptionSetValue <workflow_stage>("deletestage") : workflow.GetOptionSetValue <workflow_stage>("updatestage")); if (thisStage == null) { thisStage = workflow_stage.Postoperation; } if ((int)thisStage != (int)stage) { return; } var thisPluginContext = createPluginContext(pluginContext, workflow, thisStage, guid, logicalName); var parsedWorkflow = ParseWorkflow(workflow); if (parsedWorkflow == null) { return; } WorkflowTree postExecution = null; if (thisStage == workflow_stage.Preoperation) { postExecution = ExecuteWorkflow(parsedWorkflow, preImage.CloneEntity(), thisPluginContext, core); } else { postExecution = ExecuteWorkflow(parsedWorkflow, postImage.CloneEntity(), thisPluginContext, core); } if (postExecution.Variables["Wait"] != null) { waitingWorkflows.Add(postExecution.Variables["Wait"] as WaitInfo); } }