public ActionResult ViewAwardRDV(Guid id) { return(ActionResultHelper.TryCatchWithLogger(() => { AwardBatch awardBatch = _preservationService.GetAwardBatch(id); if (awardBatch.IdRDVDocument.HasValue) { using (DocumentsClient client = new DocumentsClient()) { DocumentContent content = client.GetDocumentContentById(awardBatch.IdRDVDocument.Value); if (content != null) { return View(new WindowFileContentViewModel() { Content = System.Text.Encoding.UTF8.GetString(content.Blob) }); } } _loggerService.Warn(string.Format("ViewAwardRDV -> nessun documento trovato con Id {0}.", awardBatch.IdRDVDocument)); } WindowFileContentViewModel model = new WindowFileContentViewModel() { Content = string.Empty }; return View(model); }, _loggerService)); }
public ActionResult ViewAwardPDV(Guid id) { return(ActionResultHelper.TryCatchWithLogger(() => { AwardBatch awardBatch = _preservationService.GetAwardBatch(id); if (awardBatch.IdPDVDocument.HasValue) { using (DocumentsClient client = new DocumentsClient()) { DocumentContent content = client.GetDocumentContentById(awardBatch.IdPDVDocument.Value); if (content != null) { return View(new WindowFileContentViewModel() { Content = System.Text.Encoding.UTF8.GetString(content.Blob) }); } _loggerService.Warn(string.Format("ViewAwardPDV -> nessun documento trovato con Id {0}. Si procede con una nuova generazione del pacchetto di versamento.", awardBatch.IdPDVDocument)); } } string pdv = _preservationService.CreateAwardBatchPDVXml(awardBatch); SavePDVXml(pdv, awardBatch); WindowFileContentViewModel model = new WindowFileContentViewModel() { Content = pdv }; return View(model); }, _loggerService)); }
public SaveAwardBatchXMLResponseModel Process(SaveAwardBatchXMLRequestModel request) { DocumentArchive[] archives = _documentsClient.GetArchives(); DocumentAttribute[] attributes = _documentsClient.GetAttributesDefinition(request.ArchiveName); AwardBatch awardBatch = _preservationService.GetAwardBatch(request.IdAwardBatch); if (awardBatch == null) { _logger.Warn(string.Concat("GenerateAwardBatchPDVInteractor -> award batch with id ", request.IdAwardBatch, " not found")); throw new Exception(string.Concat("Award batch with id ", request.IdAwardBatch, " not found")); } DocumentArchive pdvArchive = archives.SingleOrDefault(s => s.Name.Equals(request.ArchiveName, StringComparison.InvariantCultureIgnoreCase)); if (pdvArchive == null) { _logger.Warn(string.Concat("GenerateAwardBatchPDVInteractor -> archive ", request.ArchiveName, " not found")); throw new Exception(string.Concat("Archive ", request.ArchiveName, " not found")); } Document chainDocument = new Document() { Archive = pdvArchive }; chainDocument = _documentsClient.InsertDocumentChain(chainDocument); Document document = new Document { Content = new DocumentContent() { Blob = request.Content }, Name = string.Concat(UtilityService.GetSafeFileName(awardBatch.Name), ".xml"), Archive = pdvArchive, AttributeValues = new System.ComponentModel.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)) }); document = _documentsClient.AddDocumentToChain(document, chainDocument.IdDocument, Library.Common.Enums.DocumentContentFormat.Binary); awardBatch.IdPDVDocument = document.IdDocument; _preservationService.UpdateAwardBatch(awardBatch); return(new SaveAwardBatchXMLResponseModel() { IdDocument = document.IdDocument }); }
private void SaveRDVXml(string xml, AwardBatch awardBatch) { SaveAwardBatchXMLRequestModel rdvRequestModel = new SaveAwardBatchXMLRequestModel() { IdAwardBatch = awardBatch.IdAwardBatch, ArchiveName = ConfigurationHelper.RDVArchiveName, Content = Encoding.UTF8.GetBytes(xml) }; _saveRDVXmlInteractor.Process(rdvRequestModel); }
public void UpdateAwardBatch(AwardBatch awardBatch) { try { Model.AwardBatch persistedModel = db.AwardBatch.First(x => x.IdAwardBatch == awardBatch.IdAwardBatch); persistedModel.IdPDVDocument = awardBatch.IdPDVDocument; persistedModel.IdRDVDocument = awardBatch.IdRDVDocument; persistedModel.IsRDVSigned = awardBatch.IsRDVSigned; if (requireSave) { db.SaveChanges(); } } catch (Exception ex) { logger.Error(ex); throw; } }
/// <summary> /// Sposta i documenti conservati o meno (dipende da parametro isConservated). /// appartenenti al batch passato. /// Viene salvato in IdParentBatch l'IdAwardBatch di sorgente in modo sia possibile vedere la catena di spostamento. /// </summary> /// <param name="idAwardBatch">Lotto origine</param> /// <param name="isConservated">Se 1 sposta i documenti conservati se 0 sposta i documenti da conservare</param> /// <param name="closeIfOpen">Se 1 sposta i documenti conservati se 0 sposta i documenti da conservare</param> public void MoveDocumentsAwardBatch(AwardBatch batch, bool isConservated, bool closeIfOpen) { using (DbTransaction tran = BeginNoSave()) { try { var docs = db.Document.Where(x => x.DocumentParent != null && x.IsVisible == 1 && !x.IdParentVersion.HasValue && x.IdAwardBatch == batch.IdAwardBatch && (x.IsConservated ?? 0) == (short)(isConservated == true ? 1 : 0)).ToList(); if (docs.Count() > 0) { //Crea nuovo lotto aperto ed aggiorna il parent var newBatch = GetOpenAwardBatchModel(batch.IdArchive, 1); newBatch.IdParentBatch = batch.IdAwardBatch; //aggiorna tutti i documenti foreach (var doc in docs) { doc.AwardBatch = newBatch; } db.SaveChanges(); } tran.Commit(); } catch (Exception) { try { tran.Rollback(); } catch { } throw; } } }
public ActionResult Detail(Guid id) { return(ActionResultHelper.TryCatchWithLogger(() => { AwardBatch awardBatch = _preservationService.GetAwardBatch(id); if (awardBatch == null) { throw new Exception(string.Format("Nessun pacchetto di versamento trovato con id {0}", id)); } AwardBatchDetailsViewModel model = new AwardBatchDetailsViewModel() { IdArchive = awardBatch.IdArchive, IdAwardBatch = awardBatch.IdAwardBatch, Name = awardBatch.Name, IsOpen = awardBatch.IsOpen }; return View(model); }, _loggerService)); }
public ActionResult CloseAwardBatch(Guid id) { return(ActionResultHelper.TryCatchWithLogger(() => { AwardBatch awardBatch = _preservationService.GetAwardBatch(id); if (awardBatch.IsOpen) { _preservationService.CloseAwardBatch(awardBatch); string xml = string.Empty; if (awardBatch.IdPDVDocument.HasValue) { using (DocumentsClient client = new DocumentsClient()) { DocumentContent content = client.GetDocumentContentById(awardBatch.IdPDVDocument.Value); if (content != null) { xml = Encoding.UTF8.GetString(content.Blob); } _loggerService.Warn(string.Format("CloseAwardBatch -> nessun documento trovato con Id {0}. Si procede con una nuova generazione del pacchetto di versamento.", awardBatch.IdPDVDocument)); } } if (string.IsNullOrEmpty(xml)) { xml = _preservationService.CreateAwardBatchPDVXml(awardBatch); SavePDVXml(xml, awardBatch); } DocumentArchive archive = ArchiveService.GetArchive(awardBatch.IdArchive); if (!archive.Name.Equals(ConfigurationHelper.RDVArchiveName, StringComparison.InvariantCultureIgnoreCase)) { SaveRDVXml(xml, awardBatch); } } return Content(string.Empty); }, _loggerService)); }
private string CreateRDVToSignZipToDownload(ICollection <Guid> awardBatchIds) { string destination = Path.Combine(ConfigurationHelper.GetAppDataPath(), string.Format("RDVToSign_{0:yyyyMMddHHmmss}.zip", DateTime.Now)); string folderDestination = Path.Combine(ConfigurationHelper.GetAppDataPath(), Guid.NewGuid().ToString()); try { Directory.CreateDirectory(folderDestination); using (IWritableArchive archive = ArchiveFactory.Create(ArchiveType.Zip)) using (DocumentsClient client = new DocumentsClient()) { Parallel.ForEach(awardBatchIds, (idAwardBatch) => { AwardBatch awardBatch = _preservationService.GetAwardBatch(idAwardBatch); if (!awardBatch.IdRDVDocument.HasValue) { _logger.WarnFormat("CreateRDVToSignZipToDownload -> RDV document not found for award batch {0}", idAwardBatch); return; } DocumentContent content = client.GetDocumentContentById(awardBatch.IdRDVDocument.Value); System.IO.File.WriteAllBytes(Path.Combine(folderDestination, string.Concat(idAwardBatch, "_", UtilityService.GetSafeFileName(content.Description))), content.Blob); }); archive.AddAllFromDirectory(folderDestination); archive.SaveTo(destination, CompressionType.Deflate); } } finally { if (Directory.Exists(folderDestination)) { Directory.Delete(folderDestination, true); } } return(destination); }
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); } }
private ICollection <UnZipReportViewModel> UnZipRDVSignedFile(string zipPath, Guid idArchive) { ICollection <UnZipReportViewModel> viewModel = new List <UnZipReportViewModel>(); if (System.IO.File.Exists(zipPath)) { using (Stream stream = System.IO.File.OpenRead(zipPath)) using (IReader reader = ReaderFactory.Open(stream)) using (DocumentsClient client = new DocumentsClient()) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { string fileName = Path.GetFileName(reader.Entry.Key); _logger.InfoFormat("UnZipRDVSignedFile -> Lettura file {0}", fileName); string awardBatchIdStr = fileName.Split('_').FirstOrDefault(); if (Guid.TryParse(awardBatchIdStr, out Guid idAwardBatch)) { if (!Path.GetExtension(fileName).Equals(".p7m", StringComparison.InvariantCultureIgnoreCase) && !Path.GetExtension(fileName).Equals(".tsd", StringComparison.InvariantCultureIgnoreCase)) { _logger.WarnFormat("UnZipRDVSignedFile -> Il file {0} non risulta firmato", fileName); viewModel.Add(new UnZipReportViewModel() { ReferenceId = idAwardBatch, Description = string.Format("Il file <b>{0}</b> non risulta firmato ed è stato scartato.", fileName), LogType = UnZipReportViewModel.TYPE_WARN }); continue; } AwardBatch awardBatch = _preservationService.GetAwardBatch(idAwardBatch); if (awardBatch == null) { _logger.WarnFormat("UnZipRDVSignedFile -> Nessun pacchetto di versamento trovato con id {0}", idAwardBatch); viewModel.Add(new UnZipReportViewModel() { ReferenceId = idAwardBatch, Description = string.Format("Nessun pacchetto di versamento trovato con id {0}", idAwardBatch), LogType = UnZipReportViewModel.TYPE_ERROR }); continue; } if (awardBatch.IdArchive != idArchive) { _logger.WarnFormat("UnZipRDVSignedFile -> Il pacchetto di versamento {0} non fa parte dell'archivio {0}", idAwardBatch, idArchive); viewModel.Add(new UnZipReportViewModel() { ReferenceId = idAwardBatch, Description = string.Format("Il pacchetto di versamento <b>{0}</b> non fa parte dell'archivio selezionato e verrà scartato.", awardBatch.Name), LogType = UnZipReportViewModel.TYPE_ERROR }); continue; } if (awardBatch.IsRDVSigned.HasValue && awardBatch.IsRDVSigned.Value) { _logger.WarnFormat("UnZipRDVSignedFile -> Il pacchetto di versamento con id {0} risulta già firmato.", idAwardBatch); viewModel.Add(new UnZipReportViewModel() { ReferenceId = idAwardBatch, Description = string.Format("Il pacchetto di versamento <b>{0}</b> risulta già firmato e verrà scartato.", awardBatch.Name), LogType = UnZipReportViewModel.TYPE_WARN }); continue; } if (!awardBatch.IdRDVDocument.HasValue) { _logger.WarnFormat("UnZipRDVSignedFile -> Nessun RDV presente per il pacchetto di versamento trovato con id {0}", idAwardBatch); viewModel.Add(new UnZipReportViewModel() { ReferenceId = idAwardBatch, Description = string.Format("Nessun RDV presente per il pacchetto di versamento <b>{0}</b>", awardBatch.Name), LogType = UnZipReportViewModel.TYPE_ERROR }); continue; } _logger.DebugFormat("UnZipRDVSignedFile -> CheckOut documento con id {0}", awardBatch.IdRDVDocument); Document document = client.CheckOutDocument(awardBatch.IdRDVDocument.Value, idAwardBatch.ToString(), Library.Common.Enums.DocumentContentFormat.Binary, false); using (MemoryStream ms = new MemoryStream()) { reader.WriteEntryTo(ms); document.Content = new DocumentContent(ms.ToArray()); document.Name = UtilityService.GetSafeFileName(fileName.Substring(fileName.IndexOf('_') + 1)); DocumentAttributeValue fileNameAttribute = document.AttributeValues.FirstOrDefault(x => x.Attribute.Name.Equals("Filename", StringComparison.InvariantCultureIgnoreCase)); fileNameAttribute.Value = document.Name; _logger.DebugFormat("UnZipRDVSignedFile -> CheckIn documento firmato con id {0}", awardBatch.IdRDVDocument); client.CheckInDocument(document, idAwardBatch.ToString(), Library.Common.Enums.DocumentContentFormat.Binary, null); } awardBatch.IsRDVSigned = true; _preservationService.UpdateAwardBatch(awardBatch); viewModel.Add(new UnZipReportViewModel() { ReferenceId = idAwardBatch, Description = string.Format("Pacchetto di versamento <b>{0}</b> aggiornato correttamente", awardBatch.Name), LogType = UnZipReportViewModel.TYPE_SUCCESS }); } } } } } return(viewModel); }