protected bool ProcessPriceFile(string InFile, out string ExtrFile, ulong sourceTypeId)
 {
     ExtrFile = InFile;
     if (ArchiveHelper.IsArchive(InFile))
     {
         if ((drCurrent[SourcesTableColumns.colExtrMask] is String) &&
             !String.IsNullOrEmpty(drCurrent[SourcesTableColumns.colExtrMask].ToString()))
         {
             ExtrFile = PriceProcessor.FileHelper.FindFromArhive(
                 InFile + ExtrDirSuffix, (string)drCurrent[SourcesTableColumns.colExtrMask]);
         }
         else
         {
             ExtrFile = String.Empty;
         }
     }
     if (String.IsNullOrEmpty(ExtrFile))
     {
         DownloadLogEntity.Log(sourceTypeId, CurrPriceItemId, String.Format(
                                   "Не удалось найти файл в архиве. Маска файла в архиве : '{0}'",
                                   drCurrent[SourcesTableColumns.colExtrMask]));
         return(false);
     }
     return(true);
 }
        protected void LogDownloaderFail(ulong sourceTypeId, string message, string archFileName)
        {
            var downloadLogId = DownloadLogEntity.Log(sourceTypeId, CurrPriceItemId, message, DownPriceResultCode.ErrorProcess, archFileName, null);

            if (downloadLogId == 0)
            {
                throw new Exception(String.Format("При логировании прайс-листа {0} получили 0 значение в ID;", CurrPriceItemId));
            }

            CopyToHistory(downloadLogId);
        }
        protected void LogDownloadedPrice(ulong sourceTypeId, string archFileName, string extrFileName)
        {
            With.DeadlockWraper((c, t) => {
                var downloadLogId = DownloadLogEntity.Log(sourceTypeId, (uint)CurrPriceItemId,
                                                          null, null,
                                                          DownPriceResultCode.SuccessDownload, archFileName,
                                                          (String.IsNullOrEmpty(extrFileName)) ? null : Path.GetFileName(extrFileName), c);

                var downloadedFileName = Path.Combine(Settings.Default.InboundPath, "d" + CurrPriceItemId + "_" + downloadLogId + GetExt());
                var item = CreatePriceProcessItem(downloadedFileName);
                item.CopyToInbound(extrFileName, c, t);
                CopyToHistory(downloadLogId);
            });

            using (NDC.Push(CurrPriceItemId.ToString()))
                _logger.InfoFormat("Price {0} - {1} скачан/распакован",
                                   drCurrent[SourcesTableColumns.colShortName],
                                   drCurrent[SourcesTableColumns.colPriceName]);
        }
        protected virtual void SendUnrecLetter(Mime m, AddressList from, EMailSourceHandlerException exception)
        {
            try {
                var attachments = m.Attachments.Where(a => !String.IsNullOrEmpty(a.GetFilename())).Aggregate("", (s, a) => s + String.Format("\"{0}\"\r\n", a.GetFilename()));
                var ms          = new MemoryStream(m.ToByteData());
#if !DEBUG
                SmtpClientEx.QuickSendSmartHost(
                    Settings.Default.SMTPHost,
                    25,
                    Environment.MachineName,
                    Settings.Default.ServiceMail,
                    new[] { Settings.Default.UnrecLetterMail },
                    ms);
#endif
                FailMailSend(m.MainEntity.Subject, from.ToAddressListString(),
                             m.MainEntity.To.ToAddressListString(), m.MainEntity.Date, ms, attachments, exception.Message);
                DownloadLogEntity.Log((ulong)PriceSourceType.EMail, String.Format("Письмо не распознано.Причина : {0}; Тема :{1}; От : {2}",
                                                                                  exception.Message, m.MainEntity.Subject, from.ToAddressListString()));
            }
            catch (Exception exMatch) {
                _logger.Error("Не удалось отправить нераспознанное письмо", exMatch);
            }
        }
        public override void ProcessData()
        {
            //набор строк похожих источников
            FillSourcesTable();
            while (dtSources.Rows.Count > 0)
            {
                DataRow[] drLS          = null;
                var       currentSource = dtSources.Rows[0];
                var       priceSource   = new PriceSource(currentSource);
                if (!IsReadyForDownload(priceSource))
                {
                    currentSource.Delete();
                    dtSources.AcceptChanges();
                    continue;
                }
                try {
                    drLS = GetLikeSources(priceSource);
                    try {
                        CurrFileName = String.Empty;
                        GetFileFromSource(priceSource);
                        priceSource.UpdateLastCheck();
                    }
                    catch (PathSourceHandlerException pathException) {
                        FailedSources.Add(priceSource.PriceItemId);
                        DownloadLogEntity.Log(priceSource.SourceTypeId, priceSource.PriceItemId, pathException.ToString(), pathException.ErrorMessage);
                    }
                    catch (Exception e) {
                        FailedSources.Add(priceSource.PriceItemId);
                        DownloadLogEntity.Log(priceSource.SourceTypeId, priceSource.PriceItemId, e.ToString());
                    }

                    if (!String.IsNullOrEmpty(CurrFileName))
                    {
                        var correctArchive = FileHelper.ProcessArchiveIfNeeded(CurrFileName, ExtrDirSuffix, priceSource.ArchivePassword);
                        foreach (var drS in drLS)
                        {
                            SetCurrentPriceCode(drS);
                            string extractFile = null;
                            try {
                                if (!correctArchive)
                                {
                                    throw new PricePreprocessingException("Не удалось распаковать файл '" + Path.GetFileName(CurrFileName) + "'. Файл поврежден", CurrFileName);
                                }

                                if (!ProcessPriceFile(CurrFileName, out extractFile, priceSource.SourceTypeId))
                                {
                                    throw new PricePreprocessingException("Не удалось обработать файл '" + Path.GetFileName(CurrFileName) + "'", CurrFileName);
                                }

                                LogDownloadedPrice(priceSource.SourceTypeId, Path.GetFileName(CurrFileName), extractFile);
                                FileProcessed();
                            }
                            catch (PricePreprocessingException e) {
                                LogDownloaderFail(priceSource.SourceTypeId, e.Message, e.FileName);
                                FileProcessed();
                            }
                            catch (Exception e) {
                                LogDownloaderFail(priceSource.SourceTypeId, e.Message, extractFile);
                            }
                            finally {
                                drS.Delete();
                            }
                        }
                        Cleanup();
                    }
                    else
                    {
                        foreach (var drDel in drLS)
                        {
                            drDel.Delete();
                        }
                    }
                }
                catch (Exception ex) {
                    var error = String.Empty;
                    if (drLS != null && drLS.Length > 1)
                    {
                        error += String.Join(", ", drLS.Select(r => r[SourcesTableColumns.colPriceCode].ToString()).ToArray());
                        drLS.Each(r => FileHelper.Safe(r.Delete));
                        error = "Источники : " + error;
                    }
                    else
                    {
                        error = String.Format("Источник : {0}", currentSource[SourcesTableColumns.colPriceCode]);
                        FileHelper.Safe(currentSource.Delete);
                    }
                    Log(ex, error);
                }
                finally {
                    FileHelper.Safe(() => dtSources.AcceptChanges());
                }
            }
        }