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());
        }
        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;
            }
        }