private Notification GetPECNotRead()
        {
            try
            {
                Notification            model          = new Notification();
                CommonUtil              cutil          = new CommonUtil();
                NHibernatePECMailFinder _pecMailFinder = new NHibernatePECMailFinder();
                _pecMailFinder.Actives            = true;
                _pecMailFinder.Direction          = (short)PECMailDirection.Ingoing;
                _pecMailFinder.NotRead            = true;
                _pecMailFinder.CheckSecurityRight = true;
                cutil.ApplyPecMailFinderSecurity(ref _pecMailFinder);

                model.NotificationName  = NotificationType.PECDaLeggere.ToString();
                model.NotificationCount = _pecMailFinder.Count();
                return(model);
            }
            catch (Exception ex)
            {
                FileLogger.Error(LogName.WebAPIClientLog, ex.Message, ex);
                throw;
            }
        }
Example #2
0
        private void RecoverEnvelopeAttachments(IEnumerable <PECMailBox> boxes)
        {
            var boxName = string.Empty;

            try
            {
                foreach (var box in boxes)
                {
                    boxName = string.Format("{0} - {1}", box.Id, box.MailBoxName);

                    if (CancelRequest())
                    {
                        FileLogger.Info(Name, "Chiusura modulo invocata dall'utente.");
                        return;
                    }

                    FileLogger.Info(Name, "Recupero Envelope in corso...");
                    // Recupero l'elenco delle PEC da riprocessare
                    NHibernatePECMailFinder finder = new NHibernatePECMailFinder
                    {
                        Actives      = true,
                        MailDateFrom = !Parameters.RecoverEnvelopeAttachmentStartDate.Equals(default(DateTime)) ? (DateTime?)Parameters.RecoverEnvelopeAttachmentStartDate : null,
                        MailDateTo   = !Parameters.RecoverEnvelopeAttachmentEndDate.Equals(default(DateTime)) ? (DateTime?)Parameters.RecoverEnvelopeAttachmentEndDate : null,
                        MailboxIds   = new[] { box.Id },
                    };
                    List <PECMail> pecMails = finder.DoSearch().Where(pecMail =>
                                                                      pecMail.Direction != (short)DocSuiteWeb.Data.PECMailDirection.Outgoing &&
                                                                      pecMail.IDMailContent != Guid.Empty &&
                                                                      !pecMail.LogEntries.Any(pml => pml.Type.Eq(PECMailLogType.Reprocessed.ToString()))).
                                              Take(box.Configuration.MaxReadForSession).ToList();
                    FileLogger.Info(Name, string.Format("Trovate {0} pec da riprocessare", pecMails.Count));
                    foreach (PECMail pecMail in pecMails)
                    {
                        PECMail currentPecMail = pecMail;
                        // Recupero la PEC dal server di conservazione
                        BiblosDocumentInfo originalEml = FacadeFactory.Instance.PECMailFacade.GetPecMailContent(currentPecMail);
                        if (originalEml == null)
                        {
                            continue;
                        }
                        BiblosDocumentInfo originalEnvelope = new BiblosDocumentInfo(currentPecMail.Location.DocumentServer, currentPecMail.IDEnvelope);

                        FileLogger.Info(Name, string.Format("Estrazione eml originale GUID_Chain_EML {0} PEC [{1}] da riprocessare - EML name [{2}]", currentPecMail.IDMailContent, currentPecMail.Id, originalEml.Name));
                        try
                        {
                            Guid  originalEnvelopeId = currentPecMail.IDEnvelope;
                            IMail envelope           = new MailBuilder().CreateFromEml(originalEml.Stream);
                            FileLogger.Debug(Name, "Generata busta da EML");
                            envelope.RemoveAttachments();
                            FileLogger.Debug(Name, "Rimossi allegati con successo, archiviazione in corso...");
                            MailStoreFacade.Factory.PECMailFacade.ArchiveEnvelope(ref currentPecMail,
                                                                                  envelope.Render(), originalEnvelope.Name);
                            MailStoreFacade.Factory.PECMailFacade.UpdateNoLastChange(ref currentPecMail);
                            FileLogger.Info(Name,
                                            string.Format(
                                                "Aggiornamento Envelope avvenuto correttamente: nuovo GUID_Chain [{0}]",
                                                currentPecMail.IDEnvelope));
                            MailStoreFacade.Factory.PECMailLogFacade.InsertLog(ref currentPecMail, string.Format("Ricalcolata busta: guidPrecedente [{0}] -> guidRicalcolato [{1}]", originalEnvelopeId, currentPecMail.IDEnvelope), PECMailLogType.Reprocessed);
                        }
                        catch (Exception ex)
                        {
                            FileLogger.Error(Name, "Errore in fase di ricalcolo busta.", ex);
                            SendMessage(
                                string.Format("Errore in fase di ricalcolo busta per la PEC {1} - Guid: [{2}].{0}Stacktrace: {3}", Environment.NewLine, pecMail.Id, pecMail.IDEnvelope, FullStacktrace(ex)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                FileLogger.Error(Name, string.Format("Errore in [RecoverEnvelopeAttachments] - Casella [{0}].", boxName), ex);
                SendMessage(string.Format("Errore in [RecoverEnvelopeAttachments] - Casella [{0}]. \nErrore: {1} \nStacktrace: {2}", boxName, ex.Message, FullStacktrace(ex)));
            }
            finally
            {
                // Libero le risorse
                NHibernateManager.NHibernateSessionManager.Instance.CloseTransactionAndSessions();
                GC.Collect(GC.MaxGeneration);
            }
        }