public async Task Update(CasePlanInstanceAggregate workflowInstance, CancellationToken token) { using (var lck = await _dbContext.Lock()) { var record = await _dbContext.CasePlanInstances .Include(_ => _.Roles).ThenInclude(_ => _.Claims) .Include(_ => _.Files) .Include(_ => _.WorkerTasks) .Include(_ => _.Children).ThenInclude(_ => _.Children) .FirstOrDefaultAsync(_ => _.Id == workflowInstance.AggregateId, token); if (record == null) { return; } _dbContext.Roles.RemoveRange(record.Roles); _dbContext.CasePlanInstanceWorkerTaskLst.RemoveRange(record.WorkerTasks); _dbContext.CasePlanElementInstanceLst.RemoveRange(record.Children); _dbContext.CasePlanInstanceFileItemLst.RemoveRange(record.Files); record.CaseState = (int?)workflowInstance.State; record.Version = workflowInstance.Version; record.Name = workflowInstance.Name; record.ExecutionContext = workflowInstance.ExecutionContext == null ? null : JsonConvert.SerializeObject(workflowInstance.ExecutionContext); record.Roles = workflowInstance.Roles.Select(_ => _.ToModel(workflowInstance.AggregateId)).ToList(); record.Files = workflowInstance.Files.Select(_ => _.ToModel(workflowInstance.AggregateId)).ToList(); record.WorkerTasks = workflowInstance.WorkerTasks.Select(_ => _.ToModel(workflowInstance.AggregateId)).ToList(); record.Children = workflowInstance.Children == null ? new List <CasePlanElementInstanceModel>() : workflowInstance.Children.Select(_ => _.ToModel(workflowInstance.AggregateId)).ToList(); record.UpdateDateTime = workflowInstance.UpdateDateTime; } }
public static CasePlanInstanceResult ToDto(CasePlanInstanceAggregate casePlanInstance) { var children = casePlanInstance.GetFlatListCasePlanItems().Select(_ => CasePlanItemInstanceResult.ToDto(_)).ToList(); foreach (var elt in casePlanInstance.Children.Where(_ => _.Type == CasePlanElementInstanceTypes.STAGE)) { foreach (var childEltId in elt.Children.Select(_ => _.EltId)) { foreach (var child in children.Where(_ => _.EltId == childEltId)) { child.ParentEltId = elt.EltId; } } } return(new CasePlanInstanceResult { Id = casePlanInstance.AggregateId, CaseFileId = casePlanInstance.CaseFileId, CasePlanId = casePlanInstance.CasePlanId, Name = casePlanInstance.Name, State = casePlanInstance.State == null ? string.Empty : Enum.GetName(typeof(CaseStates), casePlanInstance.State), Roles = casePlanInstance.Roles.Select(_ => CasePlanInstanceRoleResult.ToDto(_)).ToList(), CreateDateTime = casePlanInstance.CreateDateTime, UpdateDateTime = casePlanInstance.UpdateDateTime, Children = children, Files = casePlanInstance.Files.Select(_ => CasePlanInstanceFileItemResult.ToDTO(_)).ToList(), WorkerTasks = casePlanInstance.WorkerTasks.Select(_ => WorkerTaskResult.ToDTO(_)).ToList(), ExecutionContext = casePlanInstance.ExecutionContext == null ? new Dictionary <string, string>() : casePlanInstance.ExecutionContext.Variables.ToDictionary(k => k.Key, k => k.Value) }); }
public Task Add(CasePlanInstanceAggregate workflowInstance, CancellationToken token) { var record = workflowInstance.ToModel(); _dbContext.CasePlanInstances.Add(record); return(Task.CompletedTask); }
protected async Task Publish <TEvt>(CasePlanInstanceAggregate aggregate, CancellationToken cancellationToken) where TEvt : DomainEvent { var domainEvts = aggregate.DomainEvents.Where(e => e.GetType() == typeof(TEvt)).Cast <TEvt>(); foreach (var domainEvt in domainEvts) { await _busControl.Publish(domainEvt, cancellationToken); } }
public async Task Handle(CasePlanInstanceCreatedEvent message, CancellationToken token) { var record = CasePlanInstanceAggregate.New(new List <DomainEvent> { message }); await _casePlanInstanceCommandRepository.Add(record, token); await _casePlanInstanceCommandRepository.SaveChanges(token); }
public Task <bool> TryAddCaseFileItem(CaseEltInstance caseFileItem, CasePlanInstanceAggregate casePlanInstance, CancellationToken token) { if (casePlanInstance.IsFileExists(caseFileItem.Id)) { return(Task.FromResult(false)); } casePlanInstance.TryAddCaseFileItem(caseFileItem.Id, CaseFileItemType, "value"); return(Task.FromResult(true)); }
public ServerBuilder SetCasePlanInstance(CasePlanInstanceAggregate casePlanInstance) { var casePlanInstances = new ConcurrentBag <CasePlanInstanceAggregate> { casePlanInstance }; _services.TryUpdateSingleton <ICasePlanInstanceCommandRepository>(new InMemoryCaseInstanceCommandRepository(casePlanInstances)); _services.TryUpdateSingleton <ICasePlanInstanceQueryRepository>(new InMemoryCaseInstanceQueryRepository(casePlanInstances)); return(this); }
public new CasePlanInstanceAggregate Build() { var files = new List <CaseFileItemInstance>(); foreach (var builder in _builders) { files.Add(builder.Build() as CaseFileItemInstance); } var result = CasePlanInstanceAggregate.New(CasePlanInstanceId, base.InternalBuild() as StageElementInstance, files); return(result); }
public static CasePlanInstanceResult ToDto(CasePlanInstanceAggregate casePlanInstance) { return(new CasePlanInstanceResult { Id = casePlanInstance.AggregateId, CasePlanId = casePlanInstance.CasePlanId, Name = casePlanInstance.Name, State = casePlanInstance.State == null ? string.Empty : Enum.GetName(typeof(CaseStates), casePlanInstance.State), Roles = casePlanInstance.Roles.Select(_ => CasePlanInstanceRoleResult.ToDto(_)).ToList(), CreateDateTime = casePlanInstance.CreateDateTime, UpdateDateTime = casePlanInstance.UpdateDateTime, Children = casePlanInstance.GetFlatListCasePlanItems().Select(_ => CasePlanItemInstanceResult.ToDto(_)).ToList(), Files = casePlanInstance.Files.Select(_ => CasePlanInstanceFileItemResult.ToDTO(_)).ToList(), ExecutionContext = casePlanInstance.ExecutionContext == null ? new Dictionary <string, string>() : casePlanInstance.ExecutionContext.Variables.ToDictionary(k => k.Key, k => k.Value) }); }
public async Task <CasePlanInstanceResult> Handle(CreateCaseInstanceCommand request, CancellationToken cancellationToken) { var workflowDefinition = await _casePlanCommandRepository.Get(request.CasePlanId, cancellationToken); if (workflowDefinition == null) { throw new UnknownCasePlanException(request.CasePlanId); } var casePlanInstance = CasePlanInstanceAggregate.New(Guid.NewGuid().ToString(), workflowDefinition, request.NameIdentifier, request.Parameters); await _casePlanInstanceCommandRepository.Add(casePlanInstance, cancellationToken); await _casePlanInstanceCommandRepository.SaveChanges(cancellationToken); await _busControl.Publish(casePlanInstance.DomainEvents.First(s => s is CasePlanInstanceCreatedEvent) as CasePlanInstanceCreatedEvent, cancellationToken); return(CasePlanInstanceResult.ToDto(casePlanInstance)); }
public async Task Execute(CasePlanInstanceAggregate casePlanInstance, CancellationToken cancellationToken) { var terminateSub = await _subscriberRepository.TrySubscribe(casePlanInstance.AggregateId, CMMNConstants.ExternalTransitionNames.Terminate, cancellationToken); if (casePlanInstance.State == null) { casePlanInstance.MakeTransition(CMMNTransitions.Create); } if (casePlanInstance.State == CaseStates.Active) { var executionContext = new CMMNExecutionContext { Instance = casePlanInstance }; foreach (var fileItem in casePlanInstance.FileItems) { await _processorFactory.Execute(executionContext, fileItem, cancellationToken); } await _processorFactory.Execute(executionContext, casePlanInstance.StageContent, cancellationToken); if (casePlanInstance.StageContent.TakeStageState == TaskStageStates.Completed) { casePlanInstance.MakeTransition(CMMNTransitions.Complete, false); return; } if (casePlanInstance.StageContent.TakeStageState == TaskStageStates.Terminated) { casePlanInstance.MakeTransition(CMMNTransitions.Terminate, false); return; } if (terminateSub.IsCaptured) { await _subscriberRepository.TryReset(casePlanInstance.AggregateId, null, CMMNConstants.ExternalTransitionNames.Terminate, cancellationToken); casePlanInstance.MakeTransition(CMMNTransitions.Terminate); } } await Publish <CaseInstanceWorkerTaskAddedEvent>(casePlanInstance, cancellationToken); await Publish <CaseInstanceWorkerTaskRemovedEvent>(casePlanInstance, cancellationToken); }
public async Task <CasePlanInstanceResult> Handle(CreateCaseInstanceCommand request, CancellationToken cancellationToken) { var workflowDefinition = await _casePlanQueryRepository.Get(request.CasePlanId, cancellationToken); if (workflowDefinition == null) { throw new UnknownCasePlanException(request.CasePlanId); } var permissions = request.Permissions == null ? new List <CasePlanInstanceRole>() : request.Permissions.Select(_ => new CasePlanInstanceRole { Id = _.RoleId, Claims = _.Claims.ToList() }).ToList(); var casePlanInstance = CasePlanInstanceAggregate.New(Guid.NewGuid().ToString(), workflowDefinition, request.NameIdentifier, permissions, request.Parameters); await _commitAggregateHelper.Commit(casePlanInstance, casePlanInstance.GetStreamName(), cancellationToken); return(CasePlanInstanceResult.ToDto(casePlanInstance)); }
public static CasePlanInstanceModel ToModel(this CasePlanInstanceAggregate record) { var result = new CasePlanInstanceModel { Id = record.AggregateId, Version = record.Version, CasePlanId = record.CasePlanId, Name = record.Name, CaseState = record.State == null ? null : (int?)record.State, ExecutionContext = record.ExecutionContext == null ? null : JsonConvert.SerializeObject(record.ExecutionContext), Roles = record.Roles.Select(_ => ToModel(_, record.AggregateId, record.CaseOwner)).ToList(), Files = record.Files.Select(_ => ToModel(_, record.AggregateId)).ToList(), WorkerTasks = record.WorkerTasks.Select(_ => ToModel(_, record.AggregateId)).ToList(), Children = record.Children == null ? new List <CasePlanElementInstanceModel>() : record.Children.Select(_ => _.ToModel(record.AggregateId)).ToList(), CreateDateTime = record.CreateDateTime, UpdateDateTime = record.UpdateDateTime }; return(result); }
public async Task Execute(CasePlanInstanceAggregate casePlanInstance, CancellationToken cancellationToken) { var terminateSub = await _subscriberRepository.TrySubscribe(casePlanInstance.AggregateId, CMMNConstants.ExternalTransitionNames.Terminate, cancellationToken); if (casePlanInstance.State == null) { casePlanInstance.MakeTransition(CMMNTransitions.Create); } if (casePlanInstance.State == CaseStates.Active) { var executionContext = new CMMNExecutionContext { CasePlanInstance = casePlanInstance }; foreach (var child in casePlanInstance.Children) { await _processorFactory.Execute(executionContext, child, child.GetType(), cancellationToken); } if (casePlanInstance.StageContent.State == TaskStageStates.Completed) { casePlanInstance.MakeTransition(CMMNTransitions.Complete, false); return; } if (casePlanInstance.StageContent.State == TaskStageStates.Terminated) { casePlanInstance.MakeTransition(CMMNTransitions.Terminate, false); return; } if (terminateSub.IsCaptured) { casePlanInstance.MakeTransition(CMMNTransitions.Terminate); } } }
public async Task <bool> Handle(ReactivateCommand reactivateCommand, CancellationToken token) { var caseInstance = await _eventStoreRepository.GetLastAggregate <CasePlanInstanceAggregate>(reactivateCommand.CaseInstanceId, CasePlanInstanceAggregate.GetStreamName(reactivateCommand.CaseInstanceId)); if (caseInstance == null || string.IsNullOrWhiteSpace(caseInstance.AggregateId)) { throw new UnknownCasePlanInstanceException(reactivateCommand.CaseInstanceId); } if (!string.IsNullOrWhiteSpace(reactivateCommand.CaseInstanceElementId)) { var elt = caseInstance.GetChild(reactivateCommand.CaseInstanceElementId); if (elt == null) { throw new UnknownCasePlanElementInstanceException(reactivateCommand.CaseInstanceId, reactivateCommand.CaseInstanceElementId); } caseInstance.MakeTransition(elt, CMMNTransitions.Reactivate); await _messageBroker.QueueExternalEvent(CMMNConstants.ExternalTransitionNames.Reactivate, reactivateCommand.CaseInstanceId, reactivateCommand.CaseInstanceElementId, token); return(true); } caseInstance.MakeTransition(CMMNTransitions.Reactivate); await _messageBroker.QueueExternalEvent(CMMNConstants.ExternalTransitionNames.Reactivate, reactivateCommand.CaseInstanceId, null, token); return(true); }
public Task RegisterCasePlanInstance(CasePlanInstanceAggregate casePlanInstance, CancellationToken token) { return(_casePlanInstanceCommandRepository.Add(casePlanInstance, token)); }
public async Task <bool> Handle(CloseCommand closeCommand, CancellationToken token) { var caseInstance = await _eventStoreRepository.GetLastAggregate <CasePlanInstanceAggregate>(closeCommand.CasePlanInstanceId, CasePlanInstanceAggregate.GetStreamName(closeCommand.CasePlanInstanceId)); if (caseInstance == null || string.IsNullOrWhiteSpace(caseInstance.AggregateId)) { throw new UnknownCasePlanInstanceException(closeCommand.CasePlanInstanceId); } caseInstance.MakeTransition(CMMNTransitions.Close); await _messageBroker.QueueExternalEvent(CMMNConstants.ExternalTransitionNames.Close, closeCommand.CasePlanInstanceId, null, closeCommand.Parameters, token); return(true); }
public Task Update(CasePlanInstanceAggregate workflowInstance, CancellationToken token) { _dbContext.CasePlanInstances.Update(workflowInstance); return(Task.CompletedTask); }
public async Task <bool> Handle(ReenableCommand command, CancellationToken token) { var casePlanInstance = await _eventStoreRepository.GetLastAggregate <CasePlanInstanceAggregate>(command.CasePlanInstanceId, CasePlanInstanceAggregate.GetStreamName(command.CasePlanInstanceId)); if (casePlanInstance == null) { throw new UnknownCasePlanInstanceException(command.CasePlanInstanceId); } var elt = casePlanInstance.GetChild(command.CasePlanElementInstanceId); if (elt == null) { throw new UnknownCasePlanElementInstanceException(command.CasePlanInstanceId, command.CasePlanElementInstanceId); } casePlanInstance.MakeTransition(elt, CMMNTransitions.Reenable); await _messageBroker.QueueExternalEvent(CMMNConstants.ExternalTransitionNames.Reenable, command.CasePlanInstanceId, command.CasePlanElementInstanceId, command.Parameters, token); return(true); }
public async Task <bool> Handle(UpdatePermissionsRoleCommand updatePermissionsRoleCommand, CancellationToken token) { var caseInstance = await _eventStoreRepository.GetLastAggregate <CasePlanInstanceAggregate>(updatePermissionsRoleCommand.CasePlanInstanceId, CasePlanInstanceAggregate.GetStreamName(updatePermissionsRoleCommand.CasePlanInstanceId)); if (caseInstance == null || string.IsNullOrWhiteSpace(caseInstance.AggregateId)) { throw new UnknownCasePlanInstanceException(updatePermissionsRoleCommand.CasePlanInstanceId); } caseInstance.UpdatePermission(updatePermissionsRoleCommand.RoleId, updatePermissionsRoleCommand.Claims.ToList()); await _commitAggregateHelper.Commit(caseInstance, caseInstance.GetStreamName(), token); return(true); }
public Task RegisterCasePlanInstance(CasePlanInstanceAggregate casePlanInstance, CancellationToken token) { return(_caseJobServer.RegisterCasePlanInstance(casePlanInstance, token)); }
protected override async Task ProcessMessage(CasePlanInstanceNotification notification, CancellationToken cancellationToken) { string lockName = $"caseplaninstance:{notification.Id}"; if (!await _distributedLock.TryAcquireLock(lockName, cancellationToken)) { return; } var casePlanInstance = await _eventStoreRepository.GetLastAggregate <CasePlanInstanceAggregate>(notification.CasePlanInstanceId, CasePlanInstanceAggregate.GetStreamName(notification.CasePlanInstanceId)); try { if (casePlanInstance == null || string.IsNullOrWhiteSpace(casePlanInstance.AggregateId)) { throw new InvalidOperationException($"case plan instance '{notification.CasePlanInstanceId}' doesn't exist"); } await _casePlanInstanceProcessor.Execute(casePlanInstance, cancellationToken); await _commitAggregateHelper.Commit(casePlanInstance, casePlanInstance.GetStreamName(), cancellationToken); } finally { await _distributedLock.ReleaseLock(lockName, cancellationToken); } }
public async Task RegisterCasePlanInstance(CasePlanInstanceAggregate casePlanInstance, CancellationToken token) { await _commitAggregateHelper.Commit(casePlanInstance, casePlanInstance.GetStreamName(), token); }