protected override void AfterSave(Resolution entity)
        {
            try
            {
                _logger.WriteDebug(new LogMessage(string.Concat("VecompSoftware.DocSuite.Private.WebAPI.Controllers.Entity.Resolutions.AfterSave with entity UniqueId ", entity.UniqueId)), LogCategories);
                Resolution resolution = _unitOfWork.Repository <Resolution>().GetFullByUniqueId(entity.UniqueId).SingleOrDefault();
                if (resolution != null)
                {
                    IList <CategoryFascicle> categoryFascicles = resolution.Category.CategoryFascicles.Where(f => (f.DSWEnvironment == (int)DSWEnvironmentType.Resolution || f.DSWEnvironment == 0)).ToList();
                    CategoryFascicle         categoryFascicle  = categoryFascicles.FirstOrDefault();
                    if (categoryFascicles != null && categoryFascicles.Count > 1)
                    {
                        categoryFascicle = categoryFascicles.FirstOrDefault(f => f.FascicleType == FascicleType.Period);
                    }
                    _mapper.FileResolution  = _unitOfWork.Repository <FileResolution>().GetByResolution(resolution.EntityId).SingleOrDefault();
                    _mapper.ResolutionRoles = _unitOfWork.Repository <ResolutionRole>().GetByResolution(resolution.EntityId);
                    IIdentityContext identity        = new IdentityContext(_currentIdentity.FullUserName);
                    ResolutionModel  resolutionModel = _mapper.Map(resolution, new ResolutionModel());

                    ICommandUpdateResolution commandUpdate = new CommandUpdateResolution(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty, identity, resolutionModel, categoryFascicle, null);

                    ServiceBusMessage message = _cqrsMapper.Map(commandUpdate, new ServiceBusMessage());
                    if (string.IsNullOrEmpty(message.ChannelName))
                    {
                        throw new DSWException(string.Concat("Queue name to command [", commandUpdate.ToString(), "] is not mapped"), null, DSWExceptionCode.SC_Mapper);
                    }

                    Task.Run(async() =>
                    {
                        await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                    }).Wait();
                }
                base.AfterSave(entity);
            }
            catch (DSWException ex)
            {
                _logger.WriteError(ex, LogCategories);
            }
        }
示例#2
0
        protected override void AfterSave(Fascicle entity)
        {
            try
            {
                _logger.WriteDebug(new LogMessage($"VecompSoftware.DocSuite.Private.WebAPI.Controllers.Entity.Fascicles.AfterSave with entity UniqueId {entity.UniqueId}"), LogCategories);

                if (CurrentDeleteActionType.HasValue && CurrentDeleteActionType == DeleteActionType.CancelFascicle)
                {
                    IQueryable <FascicleDocumentUnit> documentUnits = _unitOfWork.Repository <FascicleDocumentUnit>().GetByFascicle(entity.UniqueId);

                    ServiceBusMessage message;
                    foreach (FascicleDocumentUnit item in documentUnits)
                    {
                        if (item.DocumentUnit.Environment == (int)DSWEnvironmentType.Protocol)
                        {
                            Protocol protocol = _unitOfWork.Repository <Protocol>().GetByUniqueId(item.DocumentUnit.UniqueId).Single();
                            message = GenerateMessage(item.DocumentUnit.Category, (int)DSWEnvironmentType.Protocol,
                                                      (categoryFascicle) => new CommandUpdateProtocol(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, item.DocumentUnit.TenantAOO.UniqueId, null, null, null,
                                                                                                      new IdentityContext(_currentIdentity.FullUserName), protocol, categoryFascicle, null));
                            Task.Run(async() =>
                            {
                                await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                            }).Wait();
                        }
                        if (item.DocumentUnit.Environment == (int)DSWEnvironmentType.Resolution)
                        {
                            Resolution      resolution      = _unitOfWork.Repository <Resolution>().GetByUniqueId(item.DocumentUnit.UniqueId).Single();
                            ResolutionModel resolutionModel = _mapper.Map(resolution, new ResolutionModel());
                            _mapper.FileResolution  = _unitOfWork.Repository <FileResolution>().GetByResolution(item.DocumentUnit.EntityId).SingleOrDefault();
                            _mapper.ResolutionRoles = _unitOfWork.Repository <ResolutionRole>().GetByResolution(item.DocumentUnit.EntityId);
                            message = GenerateMessage(item.DocumentUnit.Category, (int)DSWEnvironmentType.Resolution,
                                                      (categoryFascicle) => new CommandUpdateResolution(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, item.DocumentUnit.TenantAOO.UniqueId,
                                                                                                        new IdentityContext(_currentIdentity.FullUserName), resolutionModel, categoryFascicle, null));
                            Task.Run(async() =>
                            {
                                await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                            }).Wait();
                        }
                        if (item.DocumentUnit.Environment == (int)DSWEnvironmentType.DocumentSeries)
                        {
                            DocumentSeriesItem documentSeriesItem = _unitOfWork.Repository <DocumentSeriesItem>().GetFullByUniqueId(item.DocumentUnit.UniqueId).SingleOrDefault();
                            message = GenerateMessage(item.DocumentUnit.Category, (int)DSWEnvironmentType.DocumentSeries,
                                                      (categoryFascicle) => new CommandUpdateDocumentSeriesItem(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, item.DocumentUnit.TenantAOO.UniqueId, null,
                                                                                                                new IdentityContext(_currentIdentity.FullUserName), documentSeriesItem, categoryFascicle, null));
                            Task.Run(async() =>
                            {
                                await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                            }).Wait();
                        }
                        if (item.DocumentUnit.Environment >= 100)
                        {
                            UDSBuildModel commandModel = _mapperUnitOfwork.Repository <IDomainMapper <DocumentUnit, UDSBuildModel> >().Map(item.DocumentUnit, new UDSBuildModel());
                            commandModel.UniqueId         = item.DocumentUnit.UniqueId;
                            commandModel.RegistrationDate = item.DocumentUnit.RegistrationDate;
                            commandModel.RegistrationUser = item.DocumentUnit.RegistrationUser;
                            message = GenerateMessage(item.DocumentUnit.Category, item.DocumentUnit.Environment,
                                                      (categoryFascicle) => new CommandCQRSUpdateUDSData(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, item.DocumentUnit.TenantAOO.UniqueId,
                                                                                                         new IdentityContext(_currentIdentity.FullUserName), commandModel, categoryFascicle, null, null, null, null));
                            Task.Run(async() =>
                            {
                                await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                            }).Wait();
                        }
                    }
                }
                if (CurrentInsertActionType.HasValue || CurrentUpdateActionType.HasValue)
                {
                    if (CurrentInsertActionType.HasValue)
                    {
                        Task.Run(async() =>
                        {
                            FascicleFolder fascicleNode = new FascicleFolder
                            {
                                UniqueId = Guid.NewGuid(),
                                Fascicle = entity,
                                Name     = "Fascicolo",
                                Category = entity.Category,
                                Status   = FascicleFolderStatus.Active,
                                Typology = FascicleFolderTypology.Fascicle
                            };
                            _unitOfWork.Repository <FascicleFolder>().Insert(fascicleNode);
                            await _unitOfWork.SaveChangesAsync();
                            _logger.WriteDebug(new LogMessage($"Created principal folder from Fascicle.UniqueId {entity.UniqueId}"), LogCategories);
                            Fascicle fascicleTemplateModel = null;
                            if (entity.FascicleTemplate != null && !string.IsNullOrEmpty(entity.FascicleTemplate.JsonModel))
                            {
                                fascicleTemplateModel = JsonConvert.DeserializeObject <Fascicle>(entity.FascicleTemplate.JsonModel, Defaults.DefaultJsonSerializer);
                                if (fascicleTemplateModel != null && fascicleTemplateModel.FascicleFolders.Any())
                                {
                                    _logger.WriteDebug(new LogMessage($"Evaluating fascicle template {entity.FascicleTemplate.UniqueId} with {fascicleTemplateModel.FascicleFolders.Count} folders from Fascicle.UniqueId {entity.UniqueId}"), LogCategories);
                                    IEnumerable <FascicleFolder> results = fascicleTemplateModel.FascicleFolders.Where(f => f.Typology == FascicleFolderTypology.SubFascicle);
                                    await RecursiveInsertFascicleFoldersAsync(entity, fascicleNode, results, results.Where(f => !f.ParentInsertId.HasValue));
                                }
                            }
                        }).Wait();
                    }
                    IIdentityContext identity = new IdentityContext(_currentIdentity.FullUserName);
                    Fascicle         fascicle = _unitOfWork.Repository <Fascicle>().GetByUniqueId(entity.UniqueId);
                    ICQRS            command  = new CommandCreateFascicle(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty, identity, fascicle);
                    if (CurrentUpdateActionType.HasValue)
                    {
                        command = new CommandUpdateFascicle(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty, identity, fascicle);
                    }

                    foreach (IWorkflowAction workflowAction in WorkflowActions)
                    {
                        workflowAction.IdWorkflowActivity = IdWorkflowActivity;
                        command.WorkflowActions.Add(workflowAction);
                    }
                    ServiceBusMessage message = _cqrsMapper.Map(command, new ServiceBusMessage());
                    if (message == null || string.IsNullOrEmpty(message.ChannelName))
                    {
                        throw new DSWException($"Queue name to command [{command}] is not mapped", null, DSWExceptionCode.SC_Mapper);
                    }
                    Task.Run(async() =>
                    {
                        await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                    }).Wait();
                }
            }
            catch (DSWException ex)
            {
                _logger.WriteError(ex, LogCategories);
            }
            base.AfterSave(entity);
        }
        protected override void AfterSave(FascicleDocumentUnit entity)
        {
            try
            {
                _logger.WriteDebug(new LogMessage(string.Concat("VecompSoftware.DocSuite.Private.WebAPI.Controllers.Entity.Fascicles.FascicleDocumentUnit.AfterSave with entity UniqueId ", entity.UniqueId)), LogCategories);
                ServiceBusMessage message = null;

                if (entity.ReferenceType == ReferenceType.Fascicle)
                {
                    switch (entity.DocumentUnit.Environment)
                    {
                    case 1:
                    {
                        Collaboration collaboration             = null;
                        Guid?         collaborationUniqueId     = null;
                        int?          collaborationId           = null;
                        string        collaborationTemplateName = string.Empty;
                        Protocol      protocol = _unitOfWork.Repository <Protocol>().GetByUniqueIdWithRole(entity.DocumentUnit.UniqueId).SingleOrDefault();
                        if (protocol != null)
                        {
                            collaboration = _unitOfWork.Repository <Collaboration>().GetByProtocol(protocol.Year, protocol.Number).SingleOrDefault();
                            if (collaboration != null)
                            {
                                collaborationId           = collaboration.EntityId;
                                collaborationUniqueId     = collaboration.UniqueId;
                                collaborationTemplateName = collaboration.TemplateName;
                            }
                        }
                        message = GenerateMessage(entity.DocumentUnit.Category, (int)DSWEnvironmentType.Protocol,
                                                  (categoryFascicle) => new CommandUpdateProtocol(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, protocol.TenantAOO.UniqueId,
                                                                                                  collaborationUniqueId, collaborationId, collaborationTemplateName, new IdentityContext(_currentIdentity.FullUserName), protocol, categoryFascicle, null));
                        Task.Run(async() =>
                            {
                                await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                            }).Wait();
                        break;
                    }

                    case 2:
                    {
                        Resolution resolution = _unitOfWork.Repository <Resolution>().GetFullByUniqueId(entity.DocumentUnit.UniqueId).SingleOrDefault();
                        if (resolution.AdoptionDate.HasValue)
                        {
                            ResolutionModel resolutionModel = _mapper.Map(resolution, new ResolutionModel());
                            _mapper.FileResolution  = _unitOfWork.Repository <FileResolution>().GetByResolution(resolution.EntityId).SingleOrDefault();
                            _mapper.ResolutionRoles = _unitOfWork.Repository <ResolutionRole>().GetByResolution(resolution.EntityId);
                            message = GenerateMessage(entity.DocumentUnit.Category, (int)DSWEnvironmentType.Resolution,
                                                      (categoryFascicle) => new CommandUpdateResolution(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty,
                                                                                                        new IdentityContext(_currentIdentity.FullUserName), resolutionModel, categoryFascicle, null));
                            Task.Run(async() =>
                                {
                                    await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                                }).Wait();
                        }
                        break;
                    }

                    case 4:
                    {
                        DocumentSeriesItem documentSeriesItem = _unitOfWork.Repository <DocumentSeriesItem>().GetFullByUniqueId(entity.DocumentUnit.UniqueId).SingleOrDefault();
                        message = GenerateMessage(entity.DocumentUnit.Category, (int)DSWEnvironmentType.DocumentSeries,
                                                  (categoryFascicle) => new CommandUpdateDocumentSeriesItem(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty, null,
                                                                                                            new IdentityContext(_currentIdentity.FullUserName), documentSeriesItem, categoryFascicle, null));
                        Task.Run(async() =>
                            {
                                await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                            }).Wait();
                        break;
                    }

                    default:
                    {
                        if (entity.DocumentUnit.Environment >= 100)
                        {
                            UDSBuildModel commandModel = _mapperUnitOfwork.Repository <IDomainMapper <DocumentUnit, UDSBuildModel> >().Map(entity.DocumentUnit, new UDSBuildModel());
                            commandModel.UniqueId         = entity.DocumentUnit.UniqueId;
                            commandModel.UDSId            = entity.DocumentUnit.UniqueId;
                            commandModel.RegistrationDate = entity.DocumentUnit.RegistrationDate;
                            commandModel.RegistrationUser = entity.DocumentUnit.RegistrationUser;
                            message = GenerateMessage(entity.DocumentUnit.Category, entity.DocumentUnit.Environment,
                                                      (categoryFascicle) => new CommandCQRSUpdateUDSData(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, entity.DocumentUnit.TenantAOO?.UniqueId ?? Guid.Empty,
                                                                                                         new IdentityContext(_currentIdentity.FullUserName), commandModel, categoryFascicle, entity.DocumentUnit, null, null, null));
                            Task.Run(async() =>
                                {
                                    await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                                }).Wait();
                        }
                        break;
                    }
                    }
                }

                if (CurrentInsertActionType.HasValue)
                {
                    message = GenerateMessage(entity.DocumentUnit.Category, (int)DSWEnvironmentType.Fascicle,
                                              (categoryFascicle) =>
                    {
                        return(new CommandCreateFascicleDocumentUnit(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty,
                                                                     new IdentityContext(_currentIdentity.FullUserName), entity));
                    });
                    Task.Run(async() =>
                    {
                        await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                    }).Wait();
                }

                if (CurrentDeleteActionType.HasValue)
                {
                    message = GenerateMessage(entity.DocumentUnit.Category, (int)DSWEnvironmentType.Fascicle,
                                              (categoryFascicle) =>
                    {
                        return(new CommandDeleteFascicleDocumentUnit(_parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty,
                                                                     new IdentityContext(_currentIdentity.FullUserName), entity));
                    });
                    Task.Run(async() =>
                    {
                        await _queueService.SubscribeQueue(message.ChannelName).SendToQueueAsync(message);
                    }).Wait();
                }
            }
            catch (DSWException ex)
            {
                _logger.WriteError(ex, LogCategories);
            }
            base.AfterSave(entity);
        }