public override async Task Execute(CommandModel commandModel) { try { if (!(commandModel is CommandInsertPreservationPDV)) { _logger.Error($"Command is not of type {nameof(CommandInsertPreservationPDV)}"); return; } CommandInsertPreservationPDV @command = commandModel as CommandInsertPreservationPDV; await SendNotification(command.ReferenceId, $"Inizio salvataggio PDV per il lotto di versamento con id {command.IdAwardBatch}", NotifyLevel.Info); DocumentArchive pdvArchive = ArchiveService.GetArchiveByName(command.PDVArchive); if (pdvArchive == null) { _logger.Error($"Archive with name {command.PDVArchive} not found"); throw new Exception($"Non è stato trovato un archivio con nome {command.PDVArchive}"); } ICollection <DocumentAttribute> attributes = AttributeService.GetAttributesFromArchive(pdvArchive.IdArchive); AwardBatch awardBatch = _preservationService.GetAwardBatch(command.IdAwardBatch); if (awardBatch == null) { _logger.Error($"Award batch with id {command.IdAwardBatch} not found"); throw new Exception($"Non è stato trovato un lotto di versamento con id {command.IdAwardBatch}"); } Document document = new Document { Content = new DocumentContent() { Blob = Convert.FromBase64String(command.Content) }, Name = string.Concat(UtilityService.GetSafeFileName(awardBatch.Name), ".xml"), Archive = pdvArchive, AttributeValues = new BindingList <DocumentAttributeValue>() }; document.AttributeValues.Add(new DocumentAttributeValue() { Value = document.Name, Attribute = attributes.Single(f => f.Name.Equals("Filename", StringComparison.InvariantCultureIgnoreCase)) }); document.AttributeValues.Add(new DocumentAttributeValue() { Value = awardBatch.Name, Attribute = attributes.Single(f => f.Name.Equals("Signature", StringComparison.InvariantCultureIgnoreCase)) }); using (var clientChannel = WCFUtility.GetClientConfigChannel <IDocuments>(ServerService.WCF_Document_HostName)) { document = (clientChannel as IDocuments).AddDocumentToChain(document, null, DocumentContentFormat.Binary); } awardBatch.IdPDVDocument = document.IdDocument; _preservationService.UpdateAwardBatch(awardBatch); _logger.Info($"Saved PDV with id {awardBatch.IdPDVDocument} for awardbatch {awardBatch.IdAwardBatch}"); await SendNotification(command.ReferenceId, $"PDV salvato con id {awardBatch.IdPDVDocument} per il lotto di versamento con id {awardBatch.IdAwardBatch}", NotifyLevel.Info); } catch (Exception ex) { _logger.Error("Error on insert PDV", ex); await SendNotification(commandModel.ReferenceId, $"Errore nella fase di inserimento PDV", NotifyLevel.Error); } }
public override async Task Execute(CommandModel commandModel) { PreservationTask preservationTask = null; try { _commandModel = commandModel; if (!(commandModel is CommandExecutePreservation)) { _logger.Error($"Command is not of type {nameof(CommandExecutePreservation)}"); await SendNotification(commandModel.ReferenceId, "E' avvenuto un errore durante la gestione del comando di esecuzione conservazione", NotifyLevel.Error, true); return; } CommandExecutePreservation @command = commandModel as CommandExecutePreservation; if (command.IdTask == Guid.Empty) { _logger.Error($"Command with idTask not defined"); await SendNotification(command.ReferenceId, "Non è stato definito un ID Task per l'esecuzione della conservazione", NotifyLevel.Error, true); return; } preservationTask = _preservationService.GetPreservationTask(command.IdTask); if (preservationTask == null) { _logger.Error($"Task {command.IdTask} not found"); await SendNotification(command.ReferenceId, $"Non è stato trovato un Task con id {command.IdTask}", NotifyLevel.Error, true); return; } PreservationInfoResponse response = _preservationService.CreatePreservation(preservationTask); if (response.HasErros) { await SendNotification(command.ReferenceId, $"Errore nell'attività di conservazione del task con id {command.IdTask}: {response.Error.Message}", NotifyLevel.Error, true); return; } if (preservationTask.TaskType.Type == PreservationTaskTypes.Preservation && command.AutoGenerateNextTask) { try { _preservationService.AutoGenerateNextTask(preservationTask); } catch (Exception ex) { _logger.Warn($"E' avvenuto un errore durante la generazione del task di conservazione successivo al task {command.IdTask}", ex); await SendNotification(command.ReferenceId, $"Non è stato possibile generare il task di conservazione successivo al task {command.IdTask}", NotifyLevel.Warning); } } if (response.AwardBatchesXml != null && response.AwardBatchesXml.Count > 0) { foreach (KeyValuePair <Guid, string> awardBatchXml in response.AwardBatchesXml) { string serializedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes(awardBatchXml.Value)); if (!string.IsNullOrEmpty(command.PDVArchive)) { CommandInsertPreservationPDV pdvCommand = new CommandInsertPreservationPDV() { PDVArchive = command.PDVArchive, ReferenceId = command.ReferenceId, IdAwardBatch = awardBatchXml.Key, Content = serializedContent }; await Mediator.Send(pdvCommand); } if (!string.IsNullOrEmpty(command.RDVArchive)) { CommandInsertPreservationRDV rdvCommand = new CommandInsertPreservationRDV() { RDVArchive = command.RDVArchive, ReferenceId = command.ReferenceId, IdAwardBatch = awardBatchXml.Key, Content = serializedContent }; await Mediator.Send(rdvCommand); } } } await SendNotification(command.ReferenceId, $"Processo di conservazione terminato", NotifyLevel.Info, true); } catch (Exception ex) { _logger.Error($"Error on execute preservation task", ex); await SendNotification(commandModel.ReferenceId, $"E' avvenuto un errore durante l'attività richiesta", NotifyLevel.Error, true); } finally { if (preservationTask != null) { _preservationService.UnlockTask(preservationTask); } } }