Skip to content

Commit d3cf22d

Browse files
Thierry Habarthabarthierry-hue
authored andcommitted
Ticket #7 : support processtask
1 parent f512fb7 commit d3cf22d

30 files changed

Lines changed: 784 additions & 64 deletions

Doc.docx

15.4 KB
Binary file not shown.

src/CaseManagement.CMMN/Apis/CaseInstancesController.cs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
using CaseManagement.CMMN.CaseInstance.CommandHandlers;
22
using CaseManagement.CMMN.CaseInstance.Commands;
3+
using CaseManagement.CMMN.CaseInstance.Exceptions;
34
using CaseManagement.CMMN.Domains;
45
using CaseManagement.CMMN.Extensions;
56
using CaseManagement.Workflow.Domains;
7+
using CaseManagement.Workflow.Domains.Process.Exceptions;
68
using CaseManagement.Workflow.Persistence;
79
using CaseManagement.Workflow.Persistence.Parameters;
810
using CaseManagement.Workflow.Persistence.Responses;
911
using Microsoft.AspNetCore.Http;
1012
using Microsoft.AspNetCore.Mvc;
1113
using Newtonsoft.Json.Linq;
1214
using System;
15+
using System.Collections.Generic;
1316
using System.Linq;
1417
using System.Net;
1518
using System.Threading.Tasks;
@@ -59,10 +62,38 @@ public async Task<IActionResult> Launch(string id)
5962
}
6063

6164
[HttpPost("{id}/confirm/{elt}")]
62-
public async Task<IActionResult> ConfirmForm(string id, string elt, [FromBody] ConfirmFormCommand confirmForm)
65+
public async Task<IActionResult> ConfirmForm(string id, string elt, [FromBody] JObject jObj)
6366
{
64-
await _confirmFormCommandHandler.Handle(new ConfirmFormCommand { CaseInstanceId = id, CaseElementInstanceId = elt });
65-
return new OkResult();
67+
try
68+
{
69+
await _confirmFormCommandHandler.Handle(new ConfirmFormCommand { CaseInstanceId = id, CaseElementInstanceId = elt, Content = jObj });
70+
return new OkResult();
71+
}
72+
catch(UnknownCaseInstanceException)
73+
{
74+
return ToError(new Dictionary<string, string>
75+
{
76+
{ "bad_request", "case instance doesn't exist" }
77+
}, HttpStatusCode.NotFound, Request);
78+
}
79+
catch (UnknownCaseInstanceElementException)
80+
{
81+
return ToError(new Dictionary<string, string>
82+
{
83+
{ "bad_request", "case instance element doesn't exist" }
84+
}, HttpStatusCode.NotFound, Request);
85+
}
86+
catch (ProcessFlowInstanceDomainException ex)
87+
{
88+
return ToError(ex.Errors, HttpStatusCode.BadRequest, Request);
89+
}
90+
catch(Exception ex)
91+
{
92+
return ToError(new Dictionary<string, string>
93+
{
94+
{ "invalid_request", ex.Message }
95+
}, HttpStatusCode.BadRequest, Request);
96+
}
6697
}
6798

6899
[HttpGet("{id}")]
@@ -175,6 +206,7 @@ private static JObject ToDto(ProcessFlowInstance flowInstance)
175206
{ "create_datetime", flowInstance.CreateDateTime },
176207
{ "template_id", flowInstance.ProcessFlowTemplateId },
177208
{ "name", flowInstance.ProcessFlowName },
209+
{ "context", ToDto(flowInstance.ExecutionContext) },
178210
{ "status", Enum.GetName(typeof(ProcessFlowInstanceStatus), flowInstance.Status).ToLowerInvariant() }
179211
};
180212
var planItems = new JArray();
@@ -187,6 +219,17 @@ private static JObject ToDto(ProcessFlowInstance flowInstance)
187219
return result;
188220
}
189221

222+
private static JObject ToDto(ProcessFlowInstanceExecutionContext context)
223+
{
224+
var jObj = new JObject();
225+
foreach (var kvp in context.Variables)
226+
{
227+
jObj.Add(kvp.Key, kvp.Value);
228+
}
229+
230+
return jObj;
231+
}
232+
190233
private static JObject ToDto(CMMNPlanItem planItem)
191234
{
192235
return new JObject
@@ -196,5 +239,24 @@ private static JObject ToDto(CMMNPlanItem planItem)
196239
{ "status", Enum.GetName(typeof(ProcessFlowInstanceElementStatus), planItem.Status).ToLowerInvariant() }
197240
};
198241
}
242+
243+
private static ActionResult ToError(ICollection<KeyValuePair<string, string>> errors, HttpStatusCode statusCode, HttpRequest request)
244+
{
245+
var problemDetails = new ValidationProblemDetails
246+
{
247+
Instance = request.Path,
248+
Status = (int)statusCode,
249+
Detail = "Please refer to the errors property for additional details."
250+
};
251+
foreach (var kvp in errors.GroupBy(e => e.Key))
252+
{
253+
problemDetails.Errors.Add(kvp.Key, kvp.Select(s => s.Value).ToArray());
254+
}
255+
256+
return new BadRequestObjectResult(problemDetails)
257+
{
258+
StatusCode = (int)statusCode
259+
};
260+
}
199261
}
200262
}

src/CaseManagement.CMMN/CaseInstance/CommandHandlers/ConfirmFormCommandHandler.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using CaseManagement.CMMN.CaseInstance.Commands;
2+
using CaseManagement.CMMN.CaseInstance.Exceptions;
23
using CaseManagement.CMMN.Domains;
34
using CaseManagement.Workflow.Domains;
45
using CaseManagement.Workflow.Infrastructure.EvtBus;
56
using CaseManagement.Workflow.Infrastructure.EvtStore;
7+
using CaseManagement.Workflow.Persistence;
68
using Microsoft.Extensions.Options;
79
using NEventStore;
810
using System.Linq;
@@ -12,10 +14,12 @@ namespace CaseManagement.CMMN.CaseInstance.CommandHandlers
1214
{
1315
public class ConfirmFormCommandHandler : BaseCommandHandler<ProcessFlowInstance>, IConfirmFormCommandHandler
1416
{
17+
private readonly IFormQueryRepository _formQueryRepository;
1518
private readonly IEventStoreRepository<ProcessFlowInstance> _eventStoreRepository;
1619

17-
public ConfirmFormCommandHandler(IStoreEvents storeEvents, IEventBus eventBus, IEventStoreRepository<ProcessFlowInstance> eventStoreRepository, IAggregateSnapshotStore<ProcessFlowInstance> aggregateSnapshotStore, IOptions<SnapshotConfiguration> snapshotConfiguration) : base(storeEvents, eventBus, aggregateSnapshotStore, snapshotConfiguration)
20+
public ConfirmFormCommandHandler(IFormQueryRepository formQueryRepository, IStoreEvents storeEvents, IEventBus eventBus, IEventStoreRepository<ProcessFlowInstance> eventStoreRepository, IAggregateSnapshotStore<ProcessFlowInstance> aggregateSnapshotStore, IOptions<SnapshotConfiguration> snapshotConfiguration) : base(storeEvents, eventBus, aggregateSnapshotStore, snapshotConfiguration)
1821
{
22+
_formQueryRepository = formQueryRepository;
1923
_eventStoreRepository = eventStoreRepository;
2024
}
2125

@@ -24,16 +28,28 @@ public async Task<bool> Handle(ConfirmFormCommand confirmFormCommand)
2428
var caseInstance = await _eventStoreRepository.GetLastAggregate(confirmFormCommand.CaseInstanceId, ProcessFlowInstance.GetStreamName(confirmFormCommand.CaseInstanceId));
2529
if (caseInstance == null || string.IsNullOrWhiteSpace(caseInstance.Id))
2630
{
27-
// TODO : THROW EXCEPTION.
31+
throw new UnknownCaseInstanceException(confirmFormCommand.CaseInstanceId);
2832
}
2933

3034
var flowInstanceElt = caseInstance.Elements.FirstOrDefault(e => e.Id == confirmFormCommand.CaseElementInstanceId) as CMMNPlanItem;
3135
if (flowInstanceElt == null)
3236
{
33-
// TODO : THROW EXCEPTION.
37+
throw new UnknownCaseInstanceElementException(caseInstance.Id, confirmFormCommand.CaseElementInstanceId);
3438
}
3539

36-
caseInstance.ConfirmForm(confirmFormCommand.CaseElementInstanceId);
40+
var humanTask = flowInstanceElt.PlanItemDefinition as CMMNHumanTask;
41+
if (humanTask == null)
42+
{
43+
throw new NotSupportedTaskException("Task must be a HumanTask");
44+
}
45+
46+
var form = await _formQueryRepository.FindFormById(humanTask.FormId);
47+
if (form == null)
48+
{
49+
throw new UnknownFormException(humanTask.FormId);
50+
}
51+
52+
caseInstance.ConfirmForm(confirmFormCommand.CaseElementInstanceId, form, confirmFormCommand.Content);
3753
await Commit(caseInstance, caseInstance.GetStreamName());
3854
return true;
3955
}

src/CaseManagement.CMMN/CaseInstance/CommandHandlers/CreateCaseInstanceCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static CMMNTask BuildTask(tTask task)
112112

113113
private static CMMNPlanItemDefinition BuildProcessTask(tProcessTask processTask)
114114
{
115-
var result = new CMMNProcessTask(processTask.name) { AssemblyQualifiedName = processTask.assemblyQualifiedName, IsBlocking = processTask.isBlocking };
115+
var result = new CMMNProcessTask(processTask.name) { IsBlocking = processTask.isBlocking };
116116
if (processTask.parameterMapping != null)
117117
{
118118
foreach(var pm in processTask.parameterMapping)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System.Runtime.Serialization;
1+
using Newtonsoft.Json.Linq;
22

33
namespace CaseManagement.CMMN.CaseInstance.Commands
44
{
5-
[DataContract]
65
public class ConfirmFormCommand
76
{
87
public string CaseInstanceId { get; set; }
98
public string CaseElementInstanceId { get; set; }
9+
public JObject Content { get; set; }
1010
}
1111
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace CaseManagement.CMMN.CaseInstance.Exceptions
4+
{
5+
public class NotSupportedTaskException : Exception
6+
{
7+
public NotSupportedTaskException(string message) : base(message) { }
8+
}
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace CaseManagement.CMMN.CaseInstance.Exceptions
4+
{
5+
public class UnknownCaseInstanceElementException : Exception
6+
{
7+
public UnknownCaseInstanceElementException(string caseInstanceId, string caseInstanceElementId)
8+
{
9+
CaseInstanceId = caseInstanceId;
10+
CaseInstanceElementId = caseInstanceElementId;
11+
}
12+
13+
public string CaseInstanceId { get; set; }
14+
public string CaseInstanceElementId { get; set; }
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace CaseManagement.CMMN.CaseInstance.Exceptions
4+
{
5+
public class UnknownCaseInstanceException : Exception
6+
{
7+
public UnknownCaseInstanceException(string caseInstanceId)
8+
{
9+
CaseInstanceId = caseInstanceId;
10+
}
11+
12+
public string CaseInstanceId { get; set; }
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace CaseManagement.CMMN.CaseInstance.Exceptions
4+
{
5+
public class UnknownFormException : Exception
6+
{
7+
public UnknownFormException(string formId)
8+
{
9+
FormId = formId;
10+
}
11+
12+
public string FormId { get; set; }
13+
}
14+
}

src/CaseManagement.CMMN/CaseInstance/Processors/CMMNHumanTaskProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override async Task<bool> Run(CMMNPlanItem cmmnPlanItem, ProcessFlowInsta
2828
}
2929

3030
var form = await _formQueryRepository.FindFormById(humanTask.FormId);
31-
cmmnPlanItem.SetFormInstance(form);
31+
cmmnPlanItem.SetFormInstance(ProcessFlowInstanceElementForm.New(form.Id));
3232
if (humanTask.IsBlocking)
3333
{
3434
return false;

0 commit comments

Comments
 (0)