11using CaseManagement . CMMN . CaseInstance . CommandHandlers ;
22using CaseManagement . CMMN . CaseInstance . Commands ;
3+ using CaseManagement . CMMN . CaseInstance . Exceptions ;
34using CaseManagement . CMMN . Domains ;
5+ using CaseManagement . CMMN . Persistence ;
6+ using CaseManagement . Workflow . Infrastructure ;
7+ using Microsoft . AspNetCore . Http ;
48using Microsoft . AspNetCore . Mvc ;
59using Newtonsoft . Json . Linq ;
610using System ;
11+ using System . Collections . Generic ;
12+ using System . Linq ;
713using System . Net ;
814using System . Threading . Tasks ;
915
@@ -14,11 +20,21 @@ public class CaseInstancesController : Controller
1420 {
1521 private readonly ICreateCaseInstanceCommandHandler _createCaseInstanceCommandHandler ;
1622 private readonly ILaunchCaseInstanceCommandHandler _launchCaseInstanceCommandHandler ;
23+ private readonly ISuspendCommandHandler _suspendCommandHandler ;
24+ private readonly IResumeCommandHandler _resumeCommandHandler ;
25+ private readonly ITerminateCommandHandler _terminateCommandHandler ;
26+ private readonly IReactivateCommandHandler _reactivateCommandHandler ;
27+ private readonly ICMMNWorkflowInstanceQueryRepository _cmmnWorkflowInstanceQueryRepository ;
1728
18- public CaseInstancesController ( ICreateCaseInstanceCommandHandler createCaseInstanceCommandHandler , ILaunchCaseInstanceCommandHandler launchCaseInstanceCommandHandler )
29+ public CaseInstancesController ( ICreateCaseInstanceCommandHandler createCaseInstanceCommandHandler , ILaunchCaseInstanceCommandHandler launchCaseInstanceCommandHandler , ISuspendCommandHandler suspendCommandHandler , IResumeCommandHandler resumeCommandHandler , ITerminateCommandHandler terminateCommandHandler , IReactivateCommandHandler reactivateCommandHandler , ICMMNWorkflowInstanceQueryRepository cmmnWorkflowInstanceQueryRepository )
1930 {
2031 _createCaseInstanceCommandHandler = createCaseInstanceCommandHandler ;
2132 _launchCaseInstanceCommandHandler = launchCaseInstanceCommandHandler ;
33+ _suspendCommandHandler = suspendCommandHandler ;
34+ _resumeCommandHandler = resumeCommandHandler ;
35+ _terminateCommandHandler = terminateCommandHandler ;
36+ _reactivateCommandHandler = reactivateCommandHandler ;
37+ _cmmnWorkflowInstanceQueryRepository = cmmnWorkflowInstanceQueryRepository ;
2238 }
2339
2440 /*
@@ -49,6 +65,256 @@ public async Task<IActionResult> Launch(string id)
4965 return new OkResult ( ) ;
5066 }
5167
68+ [ HttpGet ( "{id}" ) ]
69+ public async Task < IActionResult > Get ( string id )
70+ {
71+ var flowInstance = await _cmmnWorkflowInstanceQueryRepository . FindFlowInstanceById ( id ) ;
72+ if ( flowInstance == null )
73+ {
74+ return new NotFoundResult ( ) ;
75+ }
76+
77+ return new OkObjectResult ( ToDto ( flowInstance ) ) ;
78+ }
79+
80+ [ HttpGet ( "{id}/suspend" ) ]
81+ public async Task < IActionResult > Suspend ( string id )
82+ {
83+ try
84+ {
85+ await _suspendCommandHandler . Handle ( new SuspendCommand ( id , null ) ) ;
86+ return new OkResult ( ) ;
87+ }
88+ catch ( UnknownCaseInstanceException )
89+ {
90+ return ToError ( new Dictionary < string , string >
91+ {
92+ { "bad_request" , "case instance doesn't exist" }
93+ } , HttpStatusCode . NotFound , Request ) ;
94+ }
95+ catch ( UnknownCaseInstanceElementException )
96+ {
97+ return ToError ( new Dictionary < string , string >
98+ {
99+ { "bad_request" , "case instance element doesn't exist" }
100+ } , HttpStatusCode . NotFound , Request ) ;
101+ }
102+ catch ( AggregateValidationException ex )
103+ {
104+ return ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
105+ }
106+ catch ( Exception ex )
107+ {
108+ return ToError ( new Dictionary < string , string >
109+ {
110+ { "invalid_request" , ex . Message }
111+ } , HttpStatusCode . BadRequest , Request ) ;
112+ }
113+ }
114+
115+ [ HttpGet ( "{id}/suspend/{elt}" ) ]
116+ public async Task < IActionResult > Suspend ( string id , string elt )
117+ {
118+ try
119+ {
120+ await _suspendCommandHandler . Handle ( new SuspendCommand ( id , elt ) ) ;
121+ return new OkResult ( ) ;
122+ }
123+ catch ( UnknownCaseInstanceException )
124+ {
125+ return ToError ( new Dictionary < string , string >
126+ {
127+ { "bad_request" , "case instance doesn't exist" }
128+ } , HttpStatusCode . NotFound , Request ) ;
129+ }
130+ catch ( UnknownCaseInstanceElementException )
131+ {
132+ return ToError ( new Dictionary < string , string >
133+ {
134+ { "bad_request" , "case instance element doesn't exist" }
135+ } , HttpStatusCode . NotFound , Request ) ;
136+ }
137+ catch ( AggregateValidationException ex )
138+ {
139+ return ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
140+ }
141+ catch ( Exception ex )
142+ {
143+ return ToError ( new Dictionary < string , string >
144+ {
145+ { "invalid_request" , ex . Message }
146+ } , HttpStatusCode . BadRequest , Request ) ;
147+ }
148+ }
149+
150+ [ HttpGet ( "{id}/reactivate" ) ]
151+ public async Task < IActionResult > Reactivate ( string id )
152+ {
153+ try
154+ {
155+ await _reactivateCommandHandler . Handle ( new ReactivateCommand ( id , null ) ) ;
156+ return new OkResult ( ) ;
157+ }
158+ catch ( UnknownCaseInstanceException )
159+ {
160+ return ToError ( new Dictionary < string , string >
161+ {
162+ { "bad_request" , "case instance doesn't exist" }
163+ } , HttpStatusCode . NotFound , Request ) ;
164+ }
165+ catch ( AggregateValidationException ex )
166+ {
167+ return ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
168+ }
169+ catch ( Exception ex )
170+ {
171+ return ToError ( new Dictionary < string , string >
172+ {
173+ { "invalid_request" , ex . Message }
174+ } , HttpStatusCode . BadRequest , Request ) ;
175+ }
176+ }
177+
178+ [ HttpGet ( "{id}/reactivate/{elt}" ) ]
179+ public async Task < IActionResult > Reactivate ( string id , string elt )
180+ {
181+ try
182+ {
183+ await _reactivateCommandHandler . Handle ( new ReactivateCommand ( id , elt ) ) ;
184+ return new OkResult ( ) ;
185+ }
186+ catch ( UnknownCaseInstanceException )
187+ {
188+ return ToError ( new Dictionary < string , string >
189+ {
190+ { "bad_request" , "case instance doesn't exist" }
191+ } , HttpStatusCode . NotFound , Request ) ;
192+ }
193+ catch ( UnknownCaseInstanceElementException )
194+ {
195+ return ToError ( new Dictionary < string , string >
196+ {
197+ { "bad_request" , "case instance element doesn't exist" }
198+ } , HttpStatusCode . NotFound , Request ) ;
199+ }
200+ catch ( AggregateValidationException ex )
201+ {
202+ return ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
203+ }
204+ catch ( Exception ex )
205+ {
206+ return ToError ( new Dictionary < string , string >
207+ {
208+ { "invalid_request" , ex . Message }
209+ } , HttpStatusCode . BadRequest , Request ) ;
210+ }
211+ }
212+
213+ [ HttpGet ( "{id}/resume/{elt}" ) ]
214+ public async Task < IActionResult > Resume ( string id , string elt )
215+ {
216+ try
217+ {
218+ await _resumeCommandHandler . Handle ( new ResumeCommand ( id , elt ) ) ;
219+ return new OkResult ( ) ;
220+ }
221+ catch ( UnknownCaseInstanceException )
222+ {
223+ return ToError ( new Dictionary < string , string >
224+ {
225+ { "bad_request" , "case instance doesn't exist" }
226+ } , HttpStatusCode . NotFound , Request ) ;
227+ }
228+ catch ( UnknownCaseInstanceElementException )
229+ {
230+ return ToError ( new Dictionary < string , string >
231+ {
232+ { "bad_request" , "case instance element doesn't exist" }
233+ } , HttpStatusCode . NotFound , Request ) ;
234+ }
235+ catch ( AggregateValidationException ex )
236+ {
237+ return ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
238+ }
239+ catch ( Exception ex )
240+ {
241+ return ToError ( new Dictionary < string , string >
242+ {
243+ { "invalid_request" , ex . Message }
244+ } , HttpStatusCode . BadRequest , Request ) ;
245+ }
246+ }
247+
248+ [ HttpGet ( "{id}/terminate" ) ]
249+ public async Task < IActionResult > Terminate ( string id )
250+ {
251+ try
252+ {
253+ await _terminateCommandHandler . Handle ( new TerminateCommand ( id , null ) ) ;
254+ return new OkResult ( ) ;
255+ }
256+ catch ( UnknownCaseInstanceException )
257+ {
258+ return ToError ( new Dictionary < string , string >
259+ {
260+ { "bad_request" , "case instance doesn't exist" }
261+ } , HttpStatusCode . NotFound , Request ) ;
262+ }
263+ catch ( UnknownCaseInstanceElementException )
264+ {
265+ return ToError ( new Dictionary < string , string >
266+ {
267+ { "bad_request" , "case instance element doesn't exist" }
268+ } , HttpStatusCode . NotFound , Request ) ;
269+ }
270+ catch ( AggregateValidationException ex )
271+ {
272+ return ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
273+ }
274+ catch ( Exception ex )
275+ {
276+ return ToError ( new Dictionary < string , string >
277+ {
278+ { "invalid_request" , ex . Message }
279+ } , HttpStatusCode . BadRequest , Request ) ;
280+ }
281+ }
282+
283+ [ HttpGet ( "{id}/terminate/{elt}" ) ]
284+ public async Task < IActionResult > Terminate ( string id , string elt )
285+ {
286+ try
287+ {
288+ await _terminateCommandHandler . Handle ( new TerminateCommand ( id , elt ) ) ;
289+ return new OkResult ( ) ;
290+ }
291+ catch ( UnknownCaseInstanceException )
292+ {
293+ return ToError ( new Dictionary < string , string >
294+ {
295+ { "bad_request" , "case instance doesn't exist" }
296+ } , HttpStatusCode . NotFound , Request ) ;
297+ }
298+ catch ( UnknownCaseInstanceElementException )
299+ {
300+ return ToError ( new Dictionary < string , string >
301+ {
302+ { "bad_request" , "case instance element doesn't exist" }
303+ } , HttpStatusCode . NotFound , Request ) ;
304+ }
305+ catch ( AggregateValidationException ex )
306+ {
307+ return ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
308+ }
309+ catch ( Exception ex )
310+ {
311+ return ToError ( new Dictionary < string , string >
312+ {
313+ { "invalid_request" , ex . Message }
314+ } , HttpStatusCode . BadRequest , Request ) ;
315+ }
316+ }
317+
52318 /*
53319 [HttpGet("{id}/stop")]
54320 public async Task<IActionResult> Stop(string id)
@@ -162,18 +428,6 @@ public async Task<IActionResult> Terminate(string id, string elt)
162428 }
163429 }
164430
165- [HttpGet("{id}")]
166- public async Task<IActionResult> Get(string id)
167- {
168- var flowInstance = await _processFlowInstanceQueryRepository.FindFlowInstanceById(id);
169- if (flowInstance == null)
170- {
171- return new NotFoundResult();
172- }
173-
174- return new OkObjectResult(ToDto(flowInstance));
175- }
176-
177431 [HttpGet("{id}/steps/.search")]
178432 public async Task<IActionResult> SearchExecutionSteps(string id)
179433 {
@@ -347,6 +601,25 @@ private static JObject ToDto(CMMNWorkflowInstanceExecutionContext context)
347601 return jObj ;
348602 }
349603
604+ private static ActionResult ToError ( ICollection < KeyValuePair < string , string > > errors , HttpStatusCode statusCode , HttpRequest request )
605+ {
606+ var problemDetails = new ValidationProblemDetails
607+ {
608+ Instance = request . Path ,
609+ Status = ( int ) statusCode ,
610+ Detail = "Please refer to the errors property for additional details."
611+ } ;
612+ foreach ( var kvp in errors . GroupBy ( e => e . Key ) )
613+ {
614+ problemDetails . Errors . Add ( kvp . Key , kvp . Select ( s => s . Value ) . ToArray ( ) ) ;
615+ }
616+
617+ return new BadRequestObjectResult ( problemDetails )
618+ {
619+ StatusCode = ( int ) statusCode
620+ } ;
621+ }
622+
350623 /*
351624
352625 private static JObject ToDto(CMMNCaseFileItem fileItem)
@@ -366,25 +639,6 @@ private static JObject ToDto(CMMNCaseFileItem fileItem)
366639 result.Add("metadata", metadata);
367640 return result;
368641 }
369-
370- private static ActionResult ToError(ICollection<KeyValuePair<string, string>> errors, HttpStatusCode statusCode, HttpRequest request)
371- {
372- var problemDetails = new ValidationProblemDetails
373- {
374- Instance = request.Path,
375- Status = (int)statusCode,
376- Detail = "Please refer to the errors property for additional details."
377- };
378- foreach (var kvp in errors.GroupBy(e => e.Key))
379- {
380- problemDetails.Errors.Add(kvp.Key, kvp.Select(s => s.Value).ToArray());
381- }
382-
383- return new BadRequestObjectResult(problemDetails)
384- {
385- StatusCode = (int)statusCode
386- };
387- }
388642 */
389643 }
390644}
0 commit comments