예제 #1
0
        /// <summary>
        /// Metodo per il salvataggio di un DataSet in Serie AVCP
        /// </summary>
        /// <param name="pub">Oggetto da salvare</param>
        /// <param name="item">DocumentSeriesItem in cui deve essere salvato</param>
        /// <param name="username">Nome dell'operatore che esegue il savataggio</param>
        /// <returns>Restituisce l'oggetto contenente tutte le informazioni sull'importazione eseguita</returns>
        public SetDataSetResult SetDataSetPub(pubblicazione pub, DocumentSeriesItem item, string username, bool saveAlways)
        {
            if (pub == null)
            {
                throw new ArgumentNullException("pub", "Oggetto [pubblicazione] nullo");
            }

            // Creo l'oggetto di ritorno
            var tor = new SetDataSetResult();

            tor.Item = item;

            try
            {
                // Recupero il documento principale
                tor.Chain = FacadeFactory.Instance.DocumentSeriesItemFacade.GetMainChainInfo(tor.Item);
            }
            catch (Exception ex)
            {
                throw new SetDataSetResultException(tor, "Errore in fase di recupero documento catena principale.", ex);
            }

            try
            {
                // Verifico data ultimo aggiornamento
                tor.Step = 100;

                if (!saveAlways && !tor.Chain.Documents.IsNullOrEmpty() && tor.Chain.Attributes.ContainsKey(AttributeDataUltimoAggiornamento))
                {
                    DateTime lastUpdate = DateTime.MinValue;
                    if (!DateTime.TryParse(tor.Chain.Attributes[AttributeDataUltimoAggiornamento], out lastUpdate))
                    {
                        tor.Chain.AddAttribute(AttributeDataUltimoAggiornamento, pub.metadata.dataUltimoAggiornamentoDataset.ToString("s"));
                    }
                    tor.LastUpdate = lastUpdate;
                    FileLogger.Debug(LoggerName, string.Format("tor.LastUpdate = {0}", tor.LastUpdate.Date));
                    FileLogger.Debug(LoggerName, string.Format("pub.metadata.dataUltimoAggiornamentoDataset.Date = {0}", pub.metadata.dataUltimoAggiornamentoDataset.Date));

                    // Aggiorno solo se la data in arrivo è maggiore di quella salvata
                    if (pub.metadata.dataUltimoAggiornamentoDataset.Date <= tor.LastUpdate.Date)
                    {
                        tor.Updated = false;
                        return(tor);
                    }
                }
                tor.Updated = true;
            }
            catch (Exception ex)
            {
                throw new SetDataSetResultException(tor, "Errore in fase di verifica data ultimo aggiornamento.", ex);
            }

            try
            {
                // Aggiornamento necessario, eseguo eventaule Flush del documento
                tor.Step = 200;
                if (!tor.Chain.Documents.IsNullOrEmpty())
                {
                    FlushDocuments(tor.Item);
                    tor.Flushed = true;
                    // Ricarico la nuova catena creata
                    tor.Chain = FacadeFactory.Instance.DocumentSeriesItemFacade.GetMainChainInfo(tor.Item);
                }
            }
            catch (Exception ex)
            {
                throw new SetDataSetResultException(tor, "Errore in fase di Flush del documento.", ex);
            }

            try
            {
                // Salvo il documento nella catena
                tor.Step = 300;
                tor.SerializedDataSet = AVCPHelper.Serialize(pub);
                var doc = new MemoryDocumentInfo(tor.SerializedDataSet.ToBytes(), "dataset.xml", "");
                tor.Chain.AddDocument(doc);
            }
            catch (Exception ex)
            {
                throw new SetDataSetResultException(tor, "Errore in fase di salvataggio del documento in catena.", ex);
            }

            try
            {
                // Riporto i Metadati in Serie Documentale
                tor.Step = 400;

                tor.Chain.AddAttribute(AttributeUrlFile, string.Format(DocSuiteContext.Current.ProtocolEnv.AVCPDatasetUrlMask, tor.Item.Id));
                if (pub.metadata.dataUltimoAggiornamentoDataset != DateTime.MinValue)
                {
                    tor.Chain.AddAttribute(AttributeDataUltimoAggiornamento, pub.metadata.dataUltimoAggiornamentoDataset.ToString("s"));
                }
                if (pub.metadata.licenza != null)
                {
                    tor.Chain.AddAttribute(AttributeLicenza, pub.metadata.licenza.ToString());
                }
                tor.Chain.AddAttribute(AttributeAbstract, pub.metadata.@abstract);
                tor.Chain.AddAttribute(AttributeAnnoRiferimento, pub.metadata.annoRiferimento.ToString());
                tor.Chain.AddAttribute(AttributeTitolo, pub.metadata.titolo);
                if (pub.metadata.dataPubblicazioneDataset != DateTime.MinValue)
                {
                    tor.Chain.AddAttribute(AttributeDataPubblicazione, pub.metadata.dataPubblicazioneDataset.ToString("s"));
                }
                tor.Chain.AddAttribute(AttributeEntePubblicatore, pub.metadata.entePubblicatore);
            }
            catch (Exception ex)
            {
                throw new SetDataSetResultException(tor, "Errore in fase di impostazione attributi in catena.", ex);
            }

            try
            {
                // Eseguo il salvataggio dell'Item
                tor.Step = 500;
                FacadeFactory.Instance.DocumentSeriesItemFacade.UpdateDocumentSeriesItem(tor.Item, tor.Chain, username, $"Modificata registrazione AVCP {tor.Item.Year:0000}/{tor.Item.Number:0000000}");

                // Invio comando di update alle WebApi
                if (tor.Item.Status == DocumentSeriesItemStatus.Active)
                {
                    FacadeFactory.Instance.DocumentSeriesItemFacade.SendUpdateDocumentSeriesItemCommand(tor.Item);
                }

                tor.Saved = true;
            }
            catch (Exception ex)
            {
                throw new SetDataSetResultException(tor, "Errore in fase di salvataggio finale.", ex);
            }

            return(tor);
        }
예제 #2
0
 public SetDataSetResultException(SetDataSetResult res, string message, Exception inner)
     : base(message, inner)
 {
     PartialResult = res;
 }