Beispiel #1
0
        protected void ProcessChannel(Channel channel)
        {
            //export data
            CommonParser <CanonChannelMonitor> export = ImportFactory.GetParser(
                (Canon.Data.Enums.ChannelTypeEnum)channel.InfoType,
                channel.ChannelId,
                channel.Url,
                CanonConfigManager.UploadDataFolder,
                channel.AdditionalCommand);

            export.Logger = logger;
            if (!export.ExportToDb())
            {
                return;
            }

            logger.Info(string.Format("Export to DB finished."));

            try
            {
                CanonChannelMapping map = new CanonChannelMapping(channel.ChannelId);

                map.CleanNotExistingProducts();

                map.UpdateRelevances();

                logger.Info(string.Format("Relevance points were updated."));

                map.MarkRecommendeedRelevances();

                logger.Info(string.Format("The best relevanced pairs were chosen."));

                //Create new mapping rules for all channels
                map.CreateNewMappingRules();
                logger.Info(string.Format("New mapping rules has been created."));

                //Update main monitor
                map.UpdateMonitor();
                logger.Info(string.Format("Main monitor has been updated."));
            }
            catch (Exception ex)
            {
                //into db log
                int id = CanonProductsLog.AddImportLog(ChannelLogEnum.ChannelError,
                                                       channel.ChannelId, export.TryedRecords, export.SuccessRecords);
                CanonProductsLog.AddImportErrorsLog(id, ChannelErrorEnum.ChannelMappingError, string.Empty);

                logger.Fatal(ex);
            }

            //into db log
            CanonProductsLog.AddImportLog(ChannelLogEnum.ImportOk,
                                          channel.ChannelId, export.TryedRecords, export.SuccessRecords);

            logger.Info(string.Format("Export finished: {0}, {1}, {2}",
                                      channel.ChannelId, channel.ChannelName, channel.Url));
        }
Beispiel #2
0
        /// <summary>
        /// Exports data from file into database
        /// </summary>
        /// <returns></returns>
        public override bool ExportToDb()
        {
            ErrorMessages.Clear();
            T tmp = Activator.CreateInstance <T>();

            tmp.CleanTodaysData(this._channelId);

            //download and save xml file
            try
            {
                this.DownloadXmlFile();

                if (Logger != null)
                {
                    Logger.Debug("File download complete.");
                }
            }
            catch (Exception ex)
            {
                //into db log
                int id = CanonProductsLog.AddImportLog(ChannelLogEnum.ChannelError, this._channelId, 0, 0);
                CanonProductsLog.AddImportErrorsLog(id, ChannelErrorEnum.ChannelIsNotAvailable, string.Empty);

                //into internal log
                ErrorMessages.Add(new ImportErrorMessage("XmlChannelDownloadError",
                                                         new string[] { _channelId.ToString() }));

                //into log
                if (Logger != null)
                {
                    Logger.Error(string.Format("Url {0}, channel {1}, exception {2}", _url, _channelId, ex.ToString()));
                }
                return(false);
            }
            string resultFilename = this.DownloadedFilename;

            //parse downloaded file
            List <T> list = new List <T>();

            try
            {
                list = this.ImportFile();
                if (Logger != null)
                {
                    Logger.Debug("File parsing complete.");
                }
            }
            catch (Exception ex)
            {
                //into db log
                int id = CanonProductsLog.AddImportLog(ChannelLogEnum.ChannelError, this._channelId, 0, 0);
                CanonProductsLog.AddImportErrorsLog(id, ChannelErrorEnum.FeedFormatIsWrong, string.Empty);

                //into internal log
                ErrorMessages.Add(new ImportErrorMessage("XmlChannelParseError",
                                                         new string[] { _channelId.ToString() }));
                //into log
                if (Logger != null)
                {
                    Logger.Error(string.Format("Url {0}, channel {1}, exception {2}", _url, _channelId, ex.ToString()));
                }
                return(false);
            }

            //export file into database
            if (Logger != null)
            {
                Logger.Debug(string.Format("Parsed {0} products.", list.Count));
            }
            this.TryedRecords = list.Count;
            int exportedCount = 0;

            foreach (T product in list)
            {
                try
                {
                    if (product.InsertNewRecord())
                    {
                        exportedCount++;
                    }
                }
                catch (Exception ex)
                {
                    //into db log
                    int id = CanonProductsLog.AddImportLog(ChannelLogEnum.ProductError, this._channelId, 0, 0);
                    CanonProductsLog.AddImportErrorsLog(id, ChannelErrorEnum.ProductParsingError, product.ProductName);

                    //add message about error
                    ErrorMessages.Add(new ImportErrorMessage("GeneralRecordImportError",
                                                             new string[] { product.ProductName }));
                    //into log
                    if (Logger != null)
                    {
                        Logger.Error(string.Format("File {0}, product {1} exception {2}", _url, product.ProductName, ex.ToString()));
                    }
                }
            }
            if (Logger != null)
            {
                Logger.Debug(string.Format("Exported {0} products successfully.", exportedCount));
            }

            this.SuccessRecords = exportedCount;
            //if exported files are empty return error
            if (exportedCount == 0)
            {
                //into db log
                int id = CanonProductsLog.AddImportLog(ChannelLogEnum.ChannelError, this._channelId, 0, 0);
                CanonProductsLog.AddImportErrorsLog(id, ChannelErrorEnum.ChannelIsEmpty, string.Empty);

                ErrorMessages.Add(new ImportErrorMessage("XmlChannelEmptyError"));
                //into log
                if (Logger != null)
                {
                    Logger.Error(string.Format("File {0}, error {1}", _url, _channelId));
                }
                return(false);
            }
            return(true);
        }