Used to pass around the user's search criteria (convenience class)
예제 #1
0
        /// <summary>
        /// Creates and dispatches a PageManager to load a search page,
        /// parse, extract, validate and return results using the 
        /// parameter callback EventHandlers
        /// </summary>
        /// <param name="pageNumber">Which search page number to load</param>
        /// <param name="criteria">The user's search criteria</param>
        /// <param name="pageLoadMethod">Supply the method to retrieve html</param>
        /// <param name="updateProgressMethod">Callback for progress updates</param>
        /// <param name="workerCompleteMethod">Callback when work complete</param>
        public PageManager(int pageNumber,
            SearchCriteria criteria,
            Func<int, string, string> pageLoadMethod, // see explanation above
            ProgressChangedEventHandler updateProgressMethod,
            RunWorkerCompletedEventHandler workerCompleteMethod)
        {
            if (pageNumber < 1)
            {
                string msg = "Supplied page number ({0}) was < 0!";
                msg = string.Format(msg,pageNumber);
                throw new ArgumentOutOfRangeException(msg);
            }

            if (pageLoadMethod == null)
            {
                string msg = "Provided a null method to obtain page HTML!";
                throw new InvalidOperationException(msg);
            }

            ProgressChanged += updateProgressMethod; // Callback
            RunWorkerCompleted += workerCompleteMethod;  // Callback
            DoWork += Work;

            _pageLoadMethod = pageLoadMethod;
            _searchCriteria = criteria;
            _pageNumber = pageNumber;

            WorkerReportsProgress = true;
            WorkerSupportsCancellation = true;
        }
예제 #2
0
        public static bool ValidateReviewCount(SearchCriteria criteria, int count)
        {
            if (count < criteria.MinNumberReviews)
            {
                return false;
            }

            return true;
        }
예제 #3
0
        public static bool ValidatePriceRange(SearchCriteria criteria, DoubleRange priceRange)
        {
            // If the user specified a price range, but the item has no price information, fail
            if (criteria.PriceRange.HasRangeSpecified && !priceRange.HasRangeSpecified) { return false; }

            if (criteria.PriceRange != null && criteria.PriceRange.HasRangeSpecified)
            {
                if (priceRange == null) return false;
                if (!criteria.PriceRange.Overlaps(priceRange)) return false;
            }

            return true;
        }
예제 #4
0
        private bool _working; // Is the main search thread still active?

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Create and start work on a new search. Spawns PageManagers.
        /// </summary>
        /// <param name="searchCriteria">User-supplied search criteria</param>
        /// <param name="threadCount">Number of pages to search asynchronously</param>
        public SearchManager(SearchCriteria searchCriteria, int threadCount)
        {
            if (threadCount < 2)
            {
                string msg = "Application misconfigured. ";
                msg += "Set the MAX_THREADS constant to a value > 1";
                throw new ArgumentException(msg);
            }

            _searchCriteria = searchCriteria;
            _threadCount = threadCount;
            DoWork += Work;

            WorkerReportsProgress = true;
            WorkerSupportsCancellation = true;
        }
예제 #5
0
        public static bool ValidateItemName(SearchCriteria criteria, string itemName)
        {
            if (criteria.MatchAllSearchTerms)
            {
                if (itemName == null) { return false; }

                // Make sure every search term is present in the item name
                string[] terms = criteria.SearchText.ToLower().Split(' ');
                for (int i = 0; i < terms.Count(); i++)
                {
                    if (!itemName.ToLower().Contains(terms[i]))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
예제 #6
0
        public static bool ValidateReviewDistribution(SearchCriteria criteria, ScoreDistribution scoreDistribution)
        {
            // Test each of the review percentage criteria
            if (scoreDistribution != null)
            {
                if (!criteria.ScoreDistribution.OneStar.Contains(scoreDistribution.OneStar))
                { return false; }

                if (!criteria.ScoreDistribution.TwoStar.Contains(scoreDistribution.TwoStar))
                { return false; }

                if (!criteria.ScoreDistribution.ThreeStar.Contains(scoreDistribution.ThreeStar))
                { return false; }

                if (!criteria.ScoreDistribution.FourStar.Contains(scoreDistribution.FourStar))
                { return false; }

                if (!criteria.ScoreDistribution.FiveStar.Contains(scoreDistribution.FiveStar))
                { return false; }
            }

            return true;
        }