Ejemplo n.º 1
0
        public static string GetPreservationName(Objects.Preservation preservation, bool isUnique)
        {
            string fiscalDocumentType = preservation.Archive.FiscalDocumentType;

            if (string.IsNullOrEmpty(fiscalDocumentType))
            {
                fiscalDocumentType = preservation.Archive != null ? preservation.Archive.Name : string.Empty;
            }

            //Normalizzazione Nome
            fiscalDocumentType = fiscalDocumentType.Replace(" ", string.Empty)
                                 .Replace(":", string.Empty)
                                 .Replace(@"\", string.Empty)
                                 .Replace("/", string.Empty)
                                 .Replace("*", string.Empty)
                                 .Replace("?", string.Empty)
                                 .Replace("\"", string.Empty)
                                 .Replace("<", string.Empty)
                                 .Replace(">", string.Empty)
                                 .Replace("|", string.Empty);

            DateTime startDate = preservation.StartDate ?? DateTime.MinValue;
            DateTime endDate   = preservation.EndDate ?? DateTime.MaxValue;

            string preservationName = $"{fiscalDocumentType}_Dal_{startDate:dd-MM-yyyy}_al_{endDate:dd-MM-yyyy}";

            if (isUnique)
            {
                preservationName = $"{preservationName}_{Guid.NewGuid()}";
            }
            return(preservationName);
        }
        private FileGruppoFile[] GetIpdaFiles(Objects.Preservation preservation, IDictionary <Guid, BindingList <DocumentAttributeValue> > fullDocumentAttributes)
        {
            logger.Info("GetIpdaFiles - INIT");
            List <FileGruppoFile> files = new List <FileGruppoFile>();
            var attributes = DbProvider.GetAttributesFromArchive(preservation.IdArchive)
                             .Where(x => !x.ConservationPosition.HasValue || x.ConservationPosition.Value > 0)
                             .OrderBy(x => x.ConservationPosition);

            foreach (var doc in preservation.Documents.OrderBy(x => x.PreservationIndex))
            {
                BindingList <DocumentAttributeValue> docAttributes = fullDocumentAttributes[doc.IdDocument];
                List <FgAttributo> attrList = new List <FgAttributo>();
                foreach (var attr in attributes)
                {
                    FgAttributo fgAttr = new FgAttributo();

                    DocumentAttributeValue currAttributeValue = docAttributes.Where(x => x.IdAttribute == attr.IdAttribute).FirstOrDefault();
                    if (currAttributeValue == null)
                    {
                        continue;
                    }

                    if (currAttributeValue.Attribute == null)
                    {
                        currAttributeValue.Attribute = attr;
                    }

                    fgAttr.nome = string.IsNullOrEmpty(attr.Description) ? attr.Name : attr.Description;

                    //L'attributo va formattato in funzione della proprieta' "Format" dell'Attributo, se valorizzata.
                    string valoreAttr;
                    if (currAttributeValue.Attribute != null && !string.IsNullOrEmpty(currAttributeValue.Attribute.Format))
                    {
                        try
                        {
                            valoreAttr = string.Format(currAttributeValue.Attribute.Format, currAttributeValue.Value);
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            valoreAttr = string.Empty;
                        }
                    }
                    else
                    {
                        valoreAttr = (currAttributeValue.Value != null) ? currAttributeValue.Value.ToString() : string.Empty;
                    }

                    fgAttr.valore = valoreAttr;
                    attrList.Add(fgAttr);
                }

                string fileName = string.Format("{0}{1}", PurgeFileName(docAttributes, string.IsNullOrEmpty(doc.PrimaryKeyValue) ? doc.PreservationIndex.GetValueOrDefault().ToString() : doc.PrimaryKeyValue), Path.GetExtension(doc.Name));

                attrList.Insert(0, new FgAttributo
                {
                    nome   = "NomeFileInArchivio",
                    valore = fileName
                });

                attrList.Add(new FgAttributo
                {
                    nome   = "ImprontaFileSHA256",
                    valore = doc.DocumentHash
                });

                files.Add(new FileGruppoFile
                {
                    id        = fileName,
                    impronta  = doc.DocumentHash,
                    extraInfo = new FgExtraInfo
                    {
                        metadati = new FgMetadati
                        {
                            attributoList = attrList.ToArray()
                        }
                    }
                });
            }
            logger.Info("GetIpdaFiles - END");
            return(files.ToArray());
        }
        private string CreatePreservationIpdaFile(Objects.Preservation preservation, string workingDir, string preservationName, string exceptions, IDictionary <Guid, BindingList <DocumentAttributeValue> > fullDocumentAttributes)
        {
            logger.InfoFormat("CreatePreservationIpdaFile - work dir {0} eccezioni {1}", workingDir, exceptions);

            try
            {
                if (string.IsNullOrEmpty(workingDir))
                {
                    throw new Exception("Working directory non configurata correttamente.");
                }

                if (preservation.Documents == null)
                {
                    throw new Exception(string.Format("Nessun documento associato alla conservazione con id {0}", preservation.IdPreservation));
                }

                if (exceptions == null)
                {
                    exceptions = string.Empty;
                }

                var user           = preservation.User;
                var sCognomeNomeCf = string.Format("{0} {1} C.F. {2}", user.Name, user.Surname, user.FiscalId);
                var nDoc           = preservation.Documents.Count();

                var dtHlp       = preservation.Documents.Select(x => x.DateMain).Min();
                var sDataMinima = dtHlp.HasValue ? dtHlp.Value.ToString("dd/MM/yyyy") : "N.D.";

                dtHlp = preservation.Documents.Select(x => x.DateMain).Max();
                var sDataMassima = dtHlp.HasValue ? dtHlp.Value.ToString("dd/MM/yyyy") : "N.D.";

                var sFileName     = Path.Combine(workingDir, "CHIUSURA_" + preservationName + ".txt");
                var sFileNameIpda = Path.Combine(workingDir, "IPDA_" + preservationName + ".xml");

                var sFileIndice    = Path.Combine(workingDir, "INDICE_" + preservationName + ".txt");
                var sFileIndiceXML = Path.Combine(workingDir, "INDICE_" + preservationName + ".xml");
                var sFileIndiceXsl = Path.Combine(workingDir, "INDICE_" + preservationName + ".xsl");

                var sFileLottoXML = Path.Combine(workingDir, "LOTTI_" + preservationName + ".xml");
                var sFileLottoXsl = Path.Combine(workingDir, "LOTTI_" + preservationName + ".xsl");

                var sFileIndiceNome = "INDICE_" + preservationName + ".txt";

                //generazione oggetto Ipda che sarà poi serializzato come file Xml
                Ipda ipda = new Ipda();

                ipda.descGenerale = new DescGenerale
                {
                    id           = preservationName,
                    applicazione = new DescApplicazione
                    {
                        nome       = "Biblos Document Server",
                        produttore = "Dgroove Srl",
                        versione   = "2015.0"
                    }
                };

                ipda.descGenerale.extraInfo = new DgExtraInfo();

                Dictionary <string, List <long> > sectional = EstraiGruppi_v1(preservation);
                List <DgmiBlocco> blocchiList = new List <DgmiBlocco>();

                foreach (var section in sectional)
                {
                    blocchiList.Add(new DgmiBlocco
                    {
                        id        = section.Key,
                        documenti = section.Value.Count().ToString(),
                        dal       = section.Value.Min().ToString(),
                        al        = section.Value.Max().ToString()
                    });
                }

                ipda.descGenerale.extraInfo.metadatiIntegrati = new DgMetadatiIntegrati
                {
                    metadati = new DgmiMetadati
                    {
                        blocchi = new DgmiBlocchi
                        {
                            bloccoList = blocchiList.ToArray()
                        },
                        documenti = new DgmiDocumenti
                        {
                            tipologia = preservation.Archive.FiscalDocumentType,
                            numero    = nDoc.ToString(),
                            dal       = sDataMinima,
                            al        = sDataMassima
                        }
                    }
                };

                List <DgMetadatiEsterni> metadatiEsterni = new List <DgMetadatiEsterni>();

                if (File.Exists(sFileName))
                {
                    metadatiEsterni.Add(new DgMetadatiEsterni
                    {
                        id        = "CHIUSURA_TXT",
                        indirizzo = Path.GetFileName(sFileName),
                        impronta  = UtilityService.GetHash(sFileName, true)
                    });
                }

                if (File.Exists(sFileIndice))
                {
                    metadatiEsterni.Add(new DgMetadatiEsterni
                    {
                        id        = "INDICE_TXT",
                        indirizzo = Path.GetFileName(sFileIndice),
                        impronta  = UtilityService.GetHash(sFileIndice, true)
                    });
                }

                if (File.Exists(sFileIndiceXML))
                {
                    metadatiEsterni.Add(new DgMetadatiEsterni
                    {
                        id        = "INDICE_XML",
                        indirizzo = Path.GetFileName(sFileIndiceXML),
                        impronta  = UtilityService.GetHash(sFileIndiceXML, true)
                    });
                }

                if (File.Exists(sFileIndiceXsl))
                {
                    metadatiEsterni.Add(new DgMetadatiEsterni
                    {
                        id        = "INDICE_XSL",
                        indirizzo = Path.GetFileName(sFileIndiceXsl),
                        impronta  = UtilityService.GetHash(sFileIndiceXsl, true)
                    });
                }

                if (File.Exists(sFileLottoXML))
                {
                    metadatiEsterni.Add(new DgMetadatiEsterni
                    {
                        id        = "LOTTI_XML",
                        indirizzo = Path.GetFileName(sFileLottoXML),
                        impronta  = UtilityService.GetHash(sFileLottoXML, true)
                    });
                }

                if (File.Exists(sFileLottoXsl))
                {
                    metadatiEsterni.Add(new DgMetadatiEsterni
                    {
                        id        = "LOTTI_XSL",
                        indirizzo = Path.GetFileName(sFileLottoXsl),
                        impronta  = UtilityService.GetHash(sFileLottoXsl, true)
                    });
                }

                ipda.descGenerale.extraInfo.metadatiEsterniList = metadatiEsterni.ToArray();

                ipda.pda = new Pda
                {
                    id        = Path.GetFileNameWithoutExtension(sFileIndice),
                    extraInfo = new PdaExtraInfo
                    {
                        metadati = new PdaMetadati()
                    }
                };

                List <PdaAttributo> attrList = DbProvider.GetAttributeByPreservationPosition(preservation.IdArchive).Select(p => new PdaAttributo {
                    nome = p.Description
                }).ToList();

                attrList.Insert(0, new PdaAttributo
                {
                    nome = "NomeFileInArchivio"
                });

                attrList.Add(new PdaAttributo
                {
                    nome = "ImprontaFileSHA256"
                });

                ipda.pda.extraInfo.metadati.attributoList = attrList.ToArray();

                //elenco dei file
                ipda.fileGruppo = new FileGruppo
                {
                    files = GetIpdaFiles(preservation, fullDocumentAttributes)
                };

                //processo
                ipda.processo = new Processo
                {
                    soggetto = new Soggetto
                    {
                        soggettoID = sCognomeNomeCf
                    },
                    tempo = new Tempo
                    {
                        rifTemporale = DateTime.Now.Date.ToString("d") + " " + DateTime.Now.ToString("HH:mm")
                    },
                    rifNormativo = ""
                };

                //sostituisce il file di chiusura
                logger.Info("Inizio serializzazione ipda");
                string xmlString = XmlFile <Ipda> .Serialize(ipda);

                logger.Info("Fine serializzazione ipda");
                preservation.CloseContent = Encoding.ASCII.GetBytes(xmlString);
                DbProvider.UpdatePreservation(preservation, true);

                //salva nella cartella di destinazione
                ipda.SaveAs(sFileNameIpda);
                logger.Info("CreatePreservationIpdaFile - ritorno al chiamante");

                return(string.Empty);
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(e.Message + " ===> " + e.Source);
            }
        }
        public string CreateAwardBatchPDVXml(Objects.AwardBatch awardBatch, Objects.Preservation preservation)
        {
            try
            {
                Ipda ipda = new Ipda()
                {
                    descGenerale = new DescGenerale()
                    {
                        id           = awardBatch.Name,
                        applicazione = new DescApplicazione()
                        {
                            nome       = "Biblos Document Server",
                            produttore = "Dgroove Srl",
                            versione   = "2015.0"
                        }
                    },
                    pda = new Pda()
                    {
                        extraInfo = new PdaExtraInfo()
                        {
                            metadati = new PdaMetadati()
                        }
                    }
                };

                List <PdaAttributo> attrList = DbProvider.GetAttributeByPreservationPosition(awardBatch.IdArchive).Select(p => new PdaAttributo {
                    nome = p.Description
                }).ToList();
                attrList.Insert(0, new PdaAttributo()
                {
                    nome = "NomeFile"
                });
                attrList.Insert(1, new PdaAttributo()
                {
                    nome = "DataVersamento"
                });
                attrList.Add(new PdaAttributo()
                {
                    nome = "ImprontaSHA"
                });
                ipda.pda.extraInfo.metadati.attributoList = attrList.ToArray();

                //TODO: Completamente da rivedere. Creare dei service specifici per creare e leggere i documenti da file system senza la necessità di chiamare ogni volta il WCFHost.
                List <FileGruppoFile>    files      = new List <FileGruppoFile>();
                BindingList <Document>   documents  = DbProvider.GetAwardBatchDocuments(awardBatch.IdAwardBatch);
                List <DocumentAttribute> attributes = DbProvider.GetAttributesFromArchive(awardBatch.IdArchive).Where(x => !x.ConservationPosition.HasValue || x.ConservationPosition.Value > 0).OrderBy(x => x.ConservationPosition).ToList();
                byte[] documentContent;
                string documentHash;
                BiblosStorageService.StorageService storageService = new BiblosStorageService.StorageService();
                foreach (Document document in documents)
                {
                    documentContent = null;
                    documentHash    = null;
                    if (preservation != null && preservation.Documents != null)
                    {
                        Document currentDocument = preservation.Documents.SingleOrDefault(x => x.IdDocument == document.IdDocument);
                        if (currentDocument != null)
                        {
                            documentHash = currentDocument.DocumentHash;
                        }
                    }

                    if (string.IsNullOrEmpty(documentHash))
                    {
                        DocumentContent currentDocumentContent = storageService.GetDocumentContent(document);
                        documentContent = currentDocumentContent.Blob;
                        documentHash    = UtilityService.GetHash(documentContent, true);
                    }

                    document.AttributeValues = DbProvider.GetFullDocumentAttributeValues(document.IdDocument);
                    List <FgAttributo> docAttrList = new List <FgAttributo>();
                    foreach (var attr in attributes)
                    {
                        FgAttributo fgAttr = new FgAttributo();

                        var currAttributeValue = document.AttributeValues.Where(x => x.IdAttribute == attr.IdAttribute).FirstOrDefault();
                        if (currAttributeValue == null)
                        {
                            continue;
                        }

                        if (currAttributeValue.Attribute == null)
                        {
                            currAttributeValue.Attribute = attr;
                        }

                        fgAttr.nome = string.IsNullOrEmpty(attr.Description) ? attr.Name : attr.Description;

                        //L'attributo va formattato in funzione della proprieta' "Format" dell'Attributo, se valorizzata.
                        string valoreAttr;
                        if (currAttributeValue.Attribute != null && !string.IsNullOrEmpty(currAttributeValue.Attribute.Format))
                        {
                            try
                            {
                                valoreAttr = string.Format(currAttributeValue.Attribute.Format, currAttributeValue.Value);
                            }
                            catch (Exception ex)
                            {
                                logger.Error(ex);
                                valoreAttr = string.Empty;
                            }
                        }
                        else
                        {
                            valoreAttr = (currAttributeValue.Value != null) ? currAttributeValue.Value.ToString() : string.Empty;
                        }

                        fgAttr.valore = valoreAttr;
                        docAttrList.Add(fgAttr);
                    }

                    docAttrList.Insert(0, new FgAttributo()
                    {
                        nome = "NomeFile", valore = document.Name
                    });
                    docAttrList.Insert(1, new FgAttributo()
                    {
                        nome = "DataVersamento", valore = document.DateMain.Value.ToString("dd/MM/yyyy")
                    });
                    docAttrList.Add(new FgAttributo()
                    {
                        nome = "ImprontaSHA", valore = documentHash
                    });

                    files.Add(new FileGruppoFile()
                    {
                        id        = document.Name,
                        impronta  = documentHash,
                        extraInfo = new FgExtraInfo()
                        {
                            metadati = new FgMetadati()
                            {
                                attributoList = docAttrList.ToArray()
                            }
                        }
                    });
                }

                ipda.fileGruppo = new FileGruppo()
                {
                    files = files.ToArray()
                };
                return(XmlFile <Ipda> .Serialize(ipda));
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                throw;
            }
        }
        private AwbBatchFile[] GetAwardBatchFiles(Objects.Preservation preservation, Guid idBatch, IDictionary <Guid, BindingList <DocumentAttributeValue> > fullDocumentAttributes)
        {
            List <AwbBatchFile> files = new List <AwbBatchFile>();
            var attributes            = DbProvider.GetAttributesFromArchive(preservation.IdArchive)
                                        .Where(x => !x.ConservationPosition.HasValue || x.ConservationPosition.Value > 0)
                                        .OrderBy(x => x.ConservationPosition);

            foreach (var doc in preservation.Documents.OrderBy(x => x.PreservationIndex))
            {
                //salta i documenti che non sono di questo lotto
                if (doc.IdAwardBatch != idBatch)
                {
                    continue;
                }

                BindingList <DocumentAttributeValue> docAttributes = fullDocumentAttributes[doc.IdDocument];
                List <AwbBatchFileAttribute>         attrList      = new List <AwbBatchFileAttribute>();
                foreach (var attr in attributes)
                {
                    AwbBatchFileAttribute awbAttr = new AwbBatchFileAttribute();

                    DocumentAttributeValue currAttributeValue = docAttributes.Where(x => x.IdAttribute == attr.IdAttribute).FirstOrDefault();
                    if (currAttributeValue == null)
                    {
                        continue;
                    }

                    if (currAttributeValue.Attribute == null)
                    {
                        currAttributeValue.Attribute = attr;
                    }

                    awbAttr.Nome = string.IsNullOrEmpty(attr.Description) ? attr.Name : attr.Description;

                    //L'attributo va formattato in funzione della proprieta' "Format" dell'Attributo, se valorizzata.
                    string valoreAttr;
                    if (currAttributeValue.Attribute != null && !string.IsNullOrEmpty(currAttributeValue.Attribute.Format))
                    {
                        try
                        {
                            valoreAttr = string.Format(currAttributeValue.Attribute.Format, currAttributeValue.Value);
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            valoreAttr = string.Empty;
                        }
                    }
                    else
                    {
                        valoreAttr = (currAttributeValue.Value != null) ? currAttributeValue.Value.ToString() : string.Empty;
                    }

                    awbAttr.Valore = valoreAttr;
                    attrList.Add(awbAttr);
                }

                string fileName = string.Format("{0}{1}", PurgeFileName(docAttributes, string.IsNullOrEmpty(doc.PrimaryKeyValue) ? doc.PreservationIndex.GetValueOrDefault().ToString() : doc.PrimaryKeyValue), Path.GetExtension(doc.Name));

                attrList.Insert(0, new AwbBatchFileAttribute
                {
                    Nome   = "NomeFileInArchivio",
                    Valore = fileName
                });

                attrList.Add(new AwbBatchFileAttribute
                {
                    Nome   = "ImprontaFileSHA256",
                    Valore = doc.DocumentHash
                });

                files.Add(new AwbBatchFile
                {
                    Attributes = attrList.ToArray()
                });
            }

            return(files.ToArray());
        }
        private string CreateAwardBatchRptFile(Objects.Preservation preservation, Company company, string workingDir, string preservationName, string exceptions, IDictionary <Guid, BindingList <DocumentAttributeValue> > fullDocumentAttributes)
        {
            logger.InfoFormat("CreateAwbReportFile - work dir {0} eccezioni {1}", workingDir, exceptions);

            try
            {
                if (string.IsNullOrEmpty(workingDir))
                {
                    throw new Exception("Working directory non configurata correttamente.");
                }

                if (preservation.Documents == null)
                {
                    throw new Exception(string.Format("Nessun documento associato alla conservazione con id {0}", preservation.IdPreservation));
                }

                if (exceptions == null)
                {
                    exceptions = string.Empty;
                }

                var nDoc = preservation.Documents.Count();

                var dtHlp       = preservation.Documents.Select(x => x.DateMain).Min();
                var sDataMinima = dtHlp.HasValue ? dtHlp.Value.ToString("dd/MM/yyyy") : "N.D.";

                dtHlp = preservation.Documents.Select(x => x.DateMain).Max();
                var sDataMassima = dtHlp.HasValue ? dtHlp.Value.ToString("dd/MM/yyyy") : "N.D.";

                var sFileNameBatch = Path.Combine(workingDir, "LOTTI_" + preservationName + ".xml");
                var sFileBatchXsl  = Path.Combine(workingDir, "LOTTI_" + preservationName + ".xsl");

                //Scrive il file XSLT di trasformazione del AwardBatch Xml
                ArchiveCompany archiveCompany = null;
                var            list           = GetArchiveCompany(preservation.Archive.IdArchive, company.CompanyName);
                if (list != null && list.Count > 0)
                {
                    archiveCompany = list[0];
                }

                File.WriteAllText(sFileBatchXsl, archiveCompany.AwardBatchXSLTFile);

                //generazione oggetto AwbReport che sarà poi serializzato come file Xml
                AwbReport report = new AwbReport();
                BindingList <Objects.AwardBatch> batches = GetPreservationAwardBatches(preservation.IdPreservation);

                //elenco dei lotti
                List <AwbBatch> batchList = new List <AwbBatch>();
                foreach (Objects.AwardBatch awardBatch in batches)
                {
                    batchList.Add(new AwbBatch
                    {
                        Descrizione = awardBatch.Name,
                        Files       = GetAwardBatchFiles(preservation, awardBatch.IdAwardBatch, fullDocumentAttributes)
                    });
                }

                //elenco dei file
                report.Batches = batchList.ToArray();

                //salva nella cartella di destinazione
                report.SaveAs(sFileNameBatch, sFileBatchXsl);
                logger.Info("CreateAwbReportFile - ritorno al chiamante");

                return(string.Empty);
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(e.Message + " ===> " + e.Source);
            }
        }