/// <summary> /// Builds workflow and validates from workflow template /// </summary> /// <param name="workflowDefinition"></param> /// <param name="sagaStepBehaviorFactory"></param> public Workflow(WorkflowDefinition workflowDefinition, ISagaStepBehaviorFactory sagaStepBehaviorFactory, IJsonSchemaDefinitionFactory jsonSchemaDefinitionFactory) { WorkflowDefinition = workflowDefinition; Name = workflowDefinition.Name; Version = workflowDefinition.Version; _sagaStepBehaviorFactory = sagaStepBehaviorFactory; //Add all edges starting from "START" with their nodes foreach (WorkflowDefinition.Edge edge in workflowDefinition.Edges.Where(e => e.FromNodeKey.Equals(DoubleLinkedDirectedGraph <NodeData, EdgeData> .START_NODE_KEY))) { WorkflowDefinition.Node toNode = workflowDefinition.Nodes.Where(n => n.NodeKey.Equals(edge.ToNodeKey)).FirstOrDefault(); CheckIfSagaStepBehaviorExists(toNode); JsonSchema dataJsonSchema = jsonSchemaDefinitionFactory.GetJsonSchemaDefinition(toNode.DataJsonSchemaModuleKey, toNode.DataJsonSchemaKey, toNode.DataJsonSchemaVersion).JsonSchema; _internalWorkflow.InsertFromStart(toNode.NodeKey, new NodeData(toNode.Description, dataJsonSchema, toNode.BehaviorModuleKey, toNode.BehaviorKey, toNode.BehaviorVersion, toNode.BehaviorConfiguration)); } //Add all remaining edges with their nodes List <WorkflowDefinition.Edge> nonInsertedEdges = workflowDefinition.Edges.Where(e => !e.FromNodeKey.Equals(DoubleLinkedDirectedGraph <NodeData, EdgeData> .START_NODE_KEY)).ToList(); while (nonInsertedEdges.Count > 0) { //Select first that have an included nodekey to avoid problems WorkflowDefinition.Edge edge = nonInsertedEdges.Where(f => _internalWorkflow.AlreadyAddedNodeKeys.Contains(f.FromNodeKey)).First(); WorkflowDefinition.Node toNode = workflowDefinition.Nodes.Where(n => n.NodeKey.Equals(edge.ToNodeKey)).FirstOrDefault(); CheckIfSagaStepBehaviorExists(toNode); JsonSchema dataJsonSchema = jsonSchemaDefinitionFactory.GetJsonSchemaDefinition(toNode.DataJsonSchemaModuleKey, toNode.DataJsonSchemaKey, toNode.DataJsonSchemaVersion).JsonSchema; _internalWorkflow.Insert(edge.FromNodeKey, toNode.NodeKey, edge.Description, new NodeData(toNode.Description, dataJsonSchema, toNode.BehaviorModuleKey, toNode.BehaviorKey, toNode.BehaviorVersion, toNode.BehaviorConfiguration)); nonInsertedEdges.Remove(edge); } _internalWorkflow.FinishGraph(); }
private void CheckIfSagaStepBehaviorExists(WorkflowDefinition.Node toNode) { ISagaStepBehavior sagaStepBehavior = _sagaStepBehaviorFactory.GetSagaStepBehavior(toNode.BehaviorModuleKey, toNode.BehaviorKey, toNode.BehaviorVersion, toNode.BehaviorConfiguration, Name); if (sagaStepBehavior == null) { throw new WorkflowStartupException($"Workflow {Name}.{Version} cannot find SagaStepBehavior {toNode.BehaviorKey}.{toNode.BehaviorVersion}"); } }