Skip to content

Commit

Permalink
Add WorkfowType Version
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzx86 committed May 13, 2024
1 parent 2f2d13e commit c4300c4
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public async Task<IActionResult> Index(WorkflowTypeIndexOptions options, PagerPa

options ??= new WorkflowTypeIndexOptions();

var query = _session.Query<WorkflowType, WorkflowTypeIndex>();
var query = _session.Query<WorkflowType, WorkflowTypeIndex>()
.Where(x => x.Latest);

if (!string.IsNullOrWhiteSpace(options.Search))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class WorkflowIndex : MapIndex
{
public long DocumentId { get; set; }
public string WorkflowTypeId { get; set; }
public string WorkflowTypeVersionId { get; set; }
public string WorkflowId { get; set; }
public int WorkflowStatus { get; set; }
public DateTime CreatedUtc { get; set; }
Expand All @@ -22,6 +23,7 @@ public class WorkflowBlockingActivitiesIndex : MapIndex
public string WorkflowTypeId { get; set; }
public string WorkflowId { get; set; }
public string WorkflowCorrelationId { get; set; }
public string WorkflowTypeVersionId { get; set; }
}

public class WorkflowIndexProvider : IndexProvider<Workflow>
Expand All @@ -33,6 +35,7 @@ public override void Describe(DescribeContext<Workflow> context)
new WorkflowIndex
{
WorkflowTypeId = workflow.WorkflowTypeId,
WorkflowTypeVersionId = workflow.WorkflowTypeVersionId,
WorkflowId = workflow.WorkflowId,
CreatedUtc = workflow.CreatedUtc,
WorkflowStatus = (int)workflow.Status
Expand All @@ -45,6 +48,7 @@ public override void Describe(DescribeContext<Workflow> context)
new WorkflowBlockingActivitiesIndex
{
ActivityId = x.ActivityId,
WorkflowTypeVersionId = workflow.WorkflowTypeVersionId,
ActivityName = x.Name,
ActivityIsStart = x.IsStart,
WorkflowTypeId = workflow.WorkflowTypeId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using OrchardCore.Workflows.Models;
using YesSql.Indexes;
Expand All @@ -11,6 +12,13 @@ public class WorkflowTypeIndex : MapIndex
public string Name { get; set; }
public bool IsEnabled { get; set; }
public bool HasStart { get; set; }
public string DisplayName { get; set; }
public string WorkflowTypeVersionId { get; set; }
public bool Latest { get; set; }
public string UpdatedBy { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedUtc { get; set; }
public DateTime ModifiedUtc { get; set; }
}

public class WorkflowTypeStartActivitiesIndex : MapIndex
Expand All @@ -33,7 +41,14 @@ public override void Describe(DescribeContext<WorkflowType> context)
WorkflowTypeId = workflowType.WorkflowTypeId,
Name = workflowType.Name,
IsEnabled = workflowType.IsEnabled,
HasStart = workflowType.Activities.Any(x => x.IsStart)
HasStart = workflowType.Activities.Any(x => x.IsStart),
DisplayName = workflowType.DisplayName,
WorkflowTypeVersionId = workflowType.WorkflowTypeVersionId,
Latest = workflowType.Latest,
CreatedUtc = workflowType.CreatedUtc,
ModifiedUtc = workflowType.ModifiedUtc,
UpdatedBy = workflowType.UpdatedBy,
CreatedBy = workflowType.CreatedBy
}
);

Expand Down
107 changes: 107 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Workflows/Migrations.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
using System;
using System.Threading.Tasks;
using OrchardCore.Data.Migration;
using OrchardCore.Modules;
using OrchardCore.Workflows.Indexes;
using OrchardCore.Workflows.Models;
using OrchardCore.Workflows.Services;
using YesSql;
using YesSql.Sql;

namespace OrchardCore.Workflows
{
public class Migrations : DataMigration
{
private readonly IWorkflowStore _workflowStore;
private readonly IWorkflowTypeStore _workflowTypeStore;
private readonly ISession _session;
private readonly IClock _clock;

public Migrations(IWorkflowStore workflowStore, IWorkflowTypeStore workflowTypeStore, ISession session, IClock clock)
{
_workflowStore = workflowStore;
_workflowTypeStore = workflowTypeStore;
_session = session;
_clock = clock;
}

public async Task<int> CreateAsync()
{
await SchemaBuilder.CreateMapIndexTableAsync<WorkflowTypeIndex>(table => table
Expand Down Expand Up @@ -147,5 +164,95 @@ public async Task<int> UpdateFrom2Async()

return 3;
}
public async Task<int> UpdateFrom3Async()
{
await SchemaBuilder.AlterIndexTableAsync<WorkflowTypeIndex>(table =>
{
table.AddColumn<string>("DisplayName", c => c.WithLength(255));
table.AddColumn<string>("WorkflowTypeVersionId", c => c.WithLength(26));
table.AddColumn<bool>("Latest");
table.AddColumn<bool>("UpdatedBy");
table.AddColumn<DateTime>("CreatedUtc");
table.AddColumn<DateTime>("ModifiedUtc");
});

await SchemaBuilder.AlterIndexTableAsync<WorkflowTypeIndex>(table => table
.CreateIndex("IDX_WorkflowTypeIndex_DocumentId",
"DocumentId",
"WorkflowTypeId",
"Name",
"HasStart",
"IsEnabled",
"WorkflowTypeVersionId",
"DisplayName",
"Latest",
"CreatedUtc",
"ModifiedUtc",
"UpdatedBy")
);

//await SchemaBuilder.AlterIndexTableAsync<WorkflowTypeStartActivitiesIndex>(table =>
//{
// table.AddColumn<string>("WorkflowTypeVersionId", c => c.WithLength(26));
// table.CreateIndex("IDX_WorkflowTypeStartActivitiesIndex_DocumentId",
// "DocumentId",
// "WorkflowTypeId",
// "WorkflowTypeVersionId",
// "StartActivityId",
// "StartActivityName",
// "IsEnabled");
//});

await SchemaBuilder.AlterIndexTableAsync<WorkflowIndex>(table =>
{
table.AddColumn<string>("WorkflowTypeVersionId", c => c.WithLength(26));
table.CreateIndex("IDX_WorkflowIndex_DocumentId",
"DocumentId",
"WorkflowTypeId",
"WorkflowTypeVersionId",
"WorkflowId",
"WorkflowStatus",
"CreatedUtc");
});


await SchemaBuilder.AlterIndexTableAsync<WorkflowBlockingActivitiesIndex>(table =>
{
table.AddColumn<string>("WorkflowTypeVersionId", c => c.WithLength(26));
});

await SchemaBuilder.AlterIndexTableAsync<WorkflowBlockingActivitiesIndex>(table => table
.CreateIndex("IDX_WFBlockingActivities_DocumentId_ActivityId",
"DocumentId",
"ActivityId",
"WorkflowTypeId",
"WorkflowTypeVersionId",
"WorkflowId"));

await SchemaBuilder.AlterIndexTableAsync<WorkflowBlockingActivitiesIndex>(table => table
.CreateIndex("IDX_WFBlockingActivities_DocumentId_ActivityName",
"DocumentId",
"ActivityName",
"WorkflowTypeId",
"WorkflowTypeVersionId",
"WorkflowCorrelationId"));

var existsedTypes = await _session.Query<WorkflowType, WorkflowTypeIndex>().ListAsync();

foreach (var workflowType in existsedTypes)
{
workflowType.DisplayName = workflowType.Name;
workflowType.Latest = true;
workflowType.CreatedUtc = _clock.UtcNow;
workflowType.ModifiedUtc = _clock.UtcNow;

await _workflowTypeStore.SaveAsync(workflowType);
}

return 4;
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public string GenerateUniqueId(WorkflowType workflowType)
{
return _idGenerator.GenerateUniqueId();
}

public string GenerateVersionUniqueId(WorkflowType workflowType)
{
return _idGenerator.GenerateUniqueId();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using OrchardCore.Modules;
using OrchardCore.Workflows.Indexes;
Expand All @@ -11,15 +13,20 @@ namespace OrchardCore.Workflows.Services
{
public class WorkflowTypeStore : IWorkflowTypeStore
{
private readonly ISession _session;
private readonly YesSql.ISession _session;
private readonly IEnumerable<IWorkflowTypeEventHandler> _handlers;
private readonly ILogger _logger;

public WorkflowTypeStore(ISession session, IEnumerable<IWorkflowTypeEventHandler> handlers, ILogger<WorkflowTypeStore> logger)
private readonly IClock _clock;
private readonly IWorkflowTypeIdGenerator _idGenerator;
private readonly IHttpContextAccessor _contextAccessor;
public WorkflowTypeStore(YesSql.ISession session, IEnumerable<IWorkflowTypeEventHandler> handlers, ILogger<WorkflowTypeStore> logger, IWorkflowTypeIdGenerator idGenerator, IClock clock, IHttpContextAccessor contextAccessor)
{
_session = session;
_handlers = handlers;
_logger = logger;
_idGenerator = idGenerator;
_clock = clock;
_contextAccessor = contextAccessor;
}

public Task<WorkflowType> GetAsync(long id)
Expand All @@ -34,52 +41,64 @@ public Task<IEnumerable<WorkflowType>> GetAsync(IEnumerable<long> ids)

public Task<WorkflowType> GetAsync(string workflowTypeId)
{
return _session.Query<WorkflowType, WorkflowTypeIndex>(x => x.WorkflowTypeId == workflowTypeId).FirstOrDefaultAsync();
return _session.Query<WorkflowType, WorkflowTypeIndex>(x => x.WorkflowTypeId == workflowTypeId && x.Latest == true)
.FirstOrDefaultAsync();
}

public Task<IEnumerable<WorkflowType>> ListAsync()
{
return _session.Query<WorkflowType, WorkflowTypeIndex>().ListAsync();
return _session.Query<WorkflowType, WorkflowTypeIndex>(x => x.Latest).ListAsync();
}

public Task<IEnumerable<WorkflowType>> GetByStartActivityAsync(string activityName)
public async Task<IEnumerable<WorkflowType>> GetByStartActivityAsync(string activityName)
{
return _session
return await _session
.Query<WorkflowType, WorkflowTypeStartActivitiesIndex>(index =>
index.StartActivityName == activityName &&
index.IsEnabled)
.With<WorkflowTypeIndex>(x => x.Latest)
.ListAsync();
}

public async Task SaveAsync(WorkflowType workflowType)
{
var isNew = workflowType.Id == 0;
var existsedEntity = await GetAsync(workflowType.WorkflowTypeId);
var isNew = existsedEntity is null;

// reset to new entity.
workflowType.Id = 0;
workflowType.WorkflowTypeVersionId = _idGenerator.GenerateVersionUniqueId(workflowType);
workflowType.DisplayName ??= workflowType.Name;
workflowType.Latest = true;
workflowType.ModifiedUtc = _clock.UtcNow;
workflowType.UpdatedBy = _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);

await _session.SaveAsync(workflowType);

if (isNew)
{
workflowType.CreatedUtc = _clock.UtcNow;
workflowType.CreatedBy = workflowType.UpdatedBy;

var context = new WorkflowTypeCreatedContext(workflowType);
await _handlers.InvokeAsync((handler, context) => handler.CreatedAsync(context), context, _logger);
}
else
{
existsedEntity.Latest = false;
await _session.SaveAsync(existsedEntity);

var context = new WorkflowTypeUpdatedContext(workflowType);
await _handlers.InvokeAsync((handler, context) => handler.UpdatedAsync(context), context, _logger);
}
}

public async Task DeleteAsync(WorkflowType workflowType)
{
// Delete workflows first.
var workflows = await _session.Query<Workflow, WorkflowIndex>(x => x.WorkflowTypeId == workflowType.WorkflowTypeId).ListAsync();

foreach (var workflow in workflows)
{
_session.Delete(workflow);
}

// Then delete the workflow type.
_session.Delete(workflowType);
workflowType.Latest = false;
workflowType.ModifiedUtc = _clock.UtcNow;
workflowType.UpdatedBy = _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
await _session.SaveAsync(workflowType);
var context = new WorkflowTypeDeletedContext(workflowType);
await _handlers.InvokeAsync((handler, context) => handler.DeletedAsync(context), context, _logger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ public class Workflow
/// Whether this workflow instance needs to be resumed atomically.
/// </summary>
public bool IsAtomic => LockTimeout > 0 && LockExpiration > 0;

public string WorkflowTypeVersionId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using OrchardCore.Entities;

Expand All @@ -15,11 +16,23 @@ public class WorkflowType : Entity
/// </summary>
public string WorkflowTypeId { get; set; }

/// <summary>
/// Workflow type version.
/// </summary>
public string WorkflowTypeVersionId { get; set; }

/// <summary>
/// Is the latest version.
/// </summary>
public bool Latest { get; set; }

/// <summary>
/// The name of this workflow type.
/// </summary>
public string Name { get; set; }

public string DisplayName { get; set; }

/// <summary>
/// Whether this workflow definition is enabled or not.
/// </summary>
Expand Down Expand Up @@ -54,5 +67,9 @@ public class WorkflowType : Entity
/// A complete list of the transitions between the activities on this workflow.
/// </summary>
public IList<Transition> Transitions { get; set; } = [];
public DateTime CreatedUtc { get; set; }
public DateTime ModifiedUtc { get; set; }
public string UpdatedBy { get; set; }
public string CreatedBy { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace OrchardCore.Workflows.Services
public interface IWorkflowTypeIdGenerator
{
string GenerateUniqueId(WorkflowType workflowType);
string GenerateVersionUniqueId(WorkflowType workflowType);
}
}

0 comments on commit c4300c4

Please sign in to comment.