Пример #1
0
        /// <summary>
        /// Create a feed
        /// </summary>
        /// <param name="options">Feed configuration options</param>
        /// <returns>true if feed creation is successful, false otherwise</returns>
        public bool CreateFeed(FeedOptions options)
        {
            string feedFile = Path.Combine(options.FeedDataPath, options.FeedFileName);

            if (File.Exists(feedFile) && !options.OverwriteFeedFile)
            {
                UpdateStatus(100, "Feed File Already Exists. You should either chose to overwrite the feed file or provide a different name.", false);
                return(false);
            }

            string criteria = "VisibilityId<>" + (short)CatalogVisibility.Private;

            if (!options.IncludeAllProducts)
            {
                criteria = criteria + " AND ExcludeFromFeed=0";
            }

            string headerRow = GetHeaderRow();

            if (!headerRow.EndsWith("\r\n"))
            {
                headerRow += "\r\n";
            }

            try
            {
                using (StreamWriter feedWriter = File.CreateText(feedFile))
                {
                    feedWriter.Write(headerRow);
                    feedWriter.Close();
                }
            }
            catch (System.UnauthorizedAccessException accessException)
            {
                UpdateStatus(100, "Access restricted on feed data folder. In order to create feeds, the current user (" + Misc.GetProcessIdentity() + ") needs write access to feeds data folder.", false);
                UpdateStatus(100, "Access exception : " + accessException.Message, false);
                return(false);
            }

            bool needTokenReset = false;

            if (Token.Instance.Store == null)
            {
                Store store = StoreDataSource.Load(options.StoreId);
                Token.Instance.InitStoreContext(store);
                needTokenReset = true;
            }

            try
            {
                using (StreamWriter feedWriter = File.AppendText(feedFile))
                {
                    ProductCollection products;
                    int count      = ProductDataSource.CountForCriteria(criteria);
                    int startIndex = 0;
                    while (startIndex < count)
                    {
                        // DETERMINE HOW MANY ROWS LEFT TO INCLUDE IN FEED
                        int rowsRemaining = count - startIndex;

                        // ONLY PROCESS 1000 ROWS AT A TIME
                        int maxRows = (rowsRemaining > 1000) ? 1000 : rowsRemaining;

                        // CALCULATE PROGRESS PERCENTAGE AND DISPLAY PROGRESS
                        int percentDone = startIndex / count * 100;
                        UpdateStatus(percentDone, "Generating feed for products from " + startIndex + " to " + (startIndex + maxRows) + " out of " + count + " products.");

                        // GET THE ROWS TO BE OUTPUT
                        products = ProductDataSource.LoadForCriteria(criteria, maxRows, startIndex);

                        // GENERATE THE FEED DATA
                        string feedData = GetFeedData(products);

                        // WRITE DATA TO THE FEED FILE
                        if (!feedData.EndsWith("\r\n"))
                        {
                            feedData += "\r\n";
                        }
                        feedWriter.Write(feedData);
                        feedWriter.Flush();

                        // LOOP TO THE NEXT BLOCK OF DATA
                        startIndex += 1000;
                    }

                    // CLOSE THE FEED FILE
                    feedWriter.Close();
                }
            }
            catch (Exception e)
            {
                Logger.Warn("Error Feed Creator Thread : " + e.Message, e);
                UpdateStatus(100, "Error while creating feed file." + e.Message, false);
                return(false);
            }
            finally
            {
                if (needTokenReset)
                {
                    Token.ResetInstance();
                }
            }
            UpdateStatus(100, string.Format("Feed file generated at {0}.", options.FeedDataPath), true);
            return(true);
        }