| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.Management.Entities; |
| | | 3 | | using Elsa.Workflows.State; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Management.Mappers; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Maps a workflow state to a workflow instance. |
| | | 9 | | /// </summary> |
| | | 10 | | public class WorkflowStateMapper |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// [Obsolete] The property key name used to store the workflow instance name. |
| | | 14 | | /// </summary> |
| | | 15 | | [Obsolete("This constant is obsolete and retained only for backward compatibility. Avoid using it in new code.")] |
| | | 16 | | private const string WorkflowInstanceNameKey = "WorkflowInstanceName"; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Maps a workflow state to a workflow instance. |
| | | 20 | | /// </summary> |
| | | 21 | | public WorkflowInstance? Map(WorkflowState? source) |
| | | 22 | | { |
| | 327 | 23 | | if (source == null) |
| | 0 | 24 | | return null; |
| | | 25 | | |
| | 327 | 26 | | var workflowInstance = new WorkflowInstance(); |
| | 327 | 27 | | Apply(source, workflowInstance); |
| | | 28 | | |
| | 327 | 29 | | return workflowInstance; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Maps a workflow state to a workflow instance. |
| | | 34 | | /// </summary> |
| | | 35 | | public void Apply(WorkflowState source, WorkflowInstance target) |
| | | 36 | | { |
| | 327 | 37 | | target.Id = source.Id; |
| | 327 | 38 | | target.CreatedAt = source.CreatedAt; |
| | 327 | 39 | | target.DefinitionId = source.DefinitionId; |
| | 327 | 40 | | target.DefinitionVersionId = source.DefinitionVersionId; |
| | 327 | 41 | | target.Version = source.DefinitionVersion; |
| | 327 | 42 | | target.ParentWorkflowInstanceId = source.ParentWorkflowInstanceId; |
| | 327 | 43 | | target.Status = source.Status; |
| | 327 | 44 | | target.SubStatus = source.SubStatus; |
| | 327 | 45 | | target.IsExecuting = source.IsExecuting; |
| | 327 | 46 | | target.CorrelationId = source.CorrelationId; |
| | 327 | 47 | | target.Name = source.Name; |
| | 327 | 48 | | target.IncidentCount = source.Incidents.Count; |
| | 327 | 49 | | target.IsSystem = source.IsSystem; |
| | 327 | 50 | | target.UpdatedAt = source.UpdatedAt; |
| | 327 | 51 | | target.FinishedAt = source.FinishedAt; |
| | 327 | 52 | | target.WorkflowState = source; |
| | | 53 | | |
| | | 54 | | // Keep for backward compatibility with workflow instances created before the introduction of the Name property. |
| | 327 | 55 | | if (source.Properties.TryGetValue<string>(WorkflowInstanceNameKey, out var name)) |
| | 0 | 56 | | target.Name = name; |
| | 327 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Maps a workflow instance to a workflow state. |
| | | 61 | | /// </summary> |
| | | 62 | | public WorkflowState? Map(WorkflowInstance? source) |
| | | 63 | | { |
| | 0 | 64 | | if (source == null) |
| | 0 | 65 | | return null; |
| | | 66 | | |
| | 0 | 67 | | var workflowState = source.WorkflowState; |
| | 0 | 68 | | workflowState.Id = source.Id; |
| | 0 | 69 | | workflowState.CreatedAt = source.CreatedAt; |
| | 0 | 70 | | workflowState.DefinitionId = source.DefinitionId; |
| | 0 | 71 | | workflowState.DefinitionVersionId = source.DefinitionVersionId; |
| | 0 | 72 | | workflowState.DefinitionVersion = source.Version; |
| | 0 | 73 | | workflowState.ParentWorkflowInstanceId = source.ParentWorkflowInstanceId; |
| | 0 | 74 | | workflowState.Status = source.Status; |
| | 0 | 75 | | workflowState.SubStatus = source.SubStatus; |
| | 0 | 76 | | workflowState.IsExecuting = source.IsExecuting; |
| | 0 | 77 | | workflowState.CorrelationId = source.CorrelationId; |
| | 0 | 78 | | workflowState.Name = source.Name; |
| | 0 | 79 | | workflowState.UpdatedAt = source.UpdatedAt; |
| | 0 | 80 | | workflowState.FinishedAt = source.FinishedAt; |
| | 0 | 81 | | workflowState.IsSystem = source.IsSystem; |
| | | 82 | | |
| | 0 | 83 | | return workflowState; |
| | | 84 | | } |
| | | 85 | | } |