Пример #1
0
        private Contractor GetMasonryContractorForFunction(Func <RawMasonryLocatorResult, bool> func)
        {
            if (MasonryLocatorResultTypeToKeyDictionary.Count +
                MasonryDealerLevelToKeyDictionary.Count == 0)
            {
                InitializeMasonryDAO();
            }

            Contractor result = null;

            using (MasonryLocatorDataContext dataContext = new MasonryLocatorDataContext())
            {
                var rawResults =
                    dataContext.RawMasonryLocatorResults
                    .Where(func).ToList();
                if (rawResults.Count > 0)
                {
                    result = BuildMasonryLocatorResult(rawResults.First(), 0.0) as Contractor;
                }
            }
            return(result);
        }
Пример #2
0
        private List <ILocatorResult> GetMasonryLocatorResults(LocatorSearchOptions searchOptions)
        {
            if (MasonryLocatorResultTypeToKeyDictionary.Count +
                MasonryDealerLevelToKeyDictionary.Count == 0)
            {
                InitializeMasonryDAO();
            }

            //Disable builder search
            var locatorResultType = searchOptions.LocatorResultType & ~LocatorResultTypes.Builder;

            using (MasonryLocatorDataContext dataContext = new MasonryLocatorDataContext())
            {
                IQueryable <MasonryLocatorResultWrapper> results;

                if (searchOptions.SearchType != SearchType.Advanced)
                {
                    results =
                        (from dealer in dataContext.RawMasonryLocatorResults
                         join zipCode in dataContext.RawMasonryLocations on dealer.ShipZip equals zipCode.PostalCode
                         let distance =
                             //TODO find some way to abstract this out into an expression JGK
                             DistanceCalculationUtil.EARTH_RADIUS * (
                                 Math.Atan(
                                     Math.Sqrt(1 -
                                               Math.Pow(
                                                   Math.Sin(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Sin(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) +
                                                   Math.Cos(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Cos(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) *
                                                   Math.Cos(zipCode.Longitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN - searchOptions.Longitude / DistanceCalculationUtil.DEGREES_PER_RADIAN)
                                                   , 2))
                                     /
                                     (Math.Sin(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Sin(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) +
                                      Math.Cos(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Cos(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) *
                                      Math.Cos(zipCode.Longitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN - searchOptions.Longitude / DistanceCalculationUtil.DEGREES_PER_RADIAN))
                                     )
                                 )
                             where distance <= searchOptions.Radius
                             orderby distance, zipCode.PostalCode
                         select new MasonryLocatorResultWrapper {
                        Result = dealer, Distance = distance
                    });
                }
                else
                {
                    if (searchOptions.Latitude > 0.0)
                    {
                        results =
                            from dealer in dataContext.RawMasonryLocatorResults
                            join zipCode in dataContext.RawMasonryLocations on dealer.ShipZip equals zipCode.PostalCode
                            let distance =
                                //TODO find some way to abstract this out into an expression JGK
                                DistanceCalculationUtil.EARTH_RADIUS * (
                                    Math.Atan(
                                        Math.Sqrt(1 -
                                                  Math.Pow(
                                                      Math.Sin(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Sin(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) +
                                                      Math.Cos(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Cos(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) *
                                                      Math.Cos(zipCode.Longitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN - searchOptions.Longitude / DistanceCalculationUtil.DEGREES_PER_RADIAN)
                                                      , 2))
                                        /
                                        (Math.Sin(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Sin(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) +
                                         Math.Cos(searchOptions.Latitude / DistanceCalculationUtil.DEGREES_PER_RADIAN) * Math.Cos(zipCode.Latitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN) *
                                         Math.Cos(zipCode.Longitude.Value / DistanceCalculationUtil.DEGREES_PER_RADIAN - searchOptions.Longitude / DistanceCalculationUtil.DEGREES_PER_RADIAN))
                                        )
                                    )
                                orderby dealer.Product_Lead_Type_Code descending, dealer.Company ascending
                        select new MasonryLocatorResultWrapper {
                            Result = dealer, Distance = distance
                        };
                    }
                    else
                    {
                        results =
                            from dealer in dataContext.RawMasonryLocatorResults
                            orderby(dealer.Product_Lead_Type_Code == MasonrySelectInstaller) descending, dealer.Company ascending
                        select new MasonryLocatorResultWrapper
                        {
                            Result = dealer, Distance = 0
                        };
                    }

                    if (!String.IsNullOrEmpty(searchOptions.Company))
                    {
                        results = results.Where(lr => lr.Result.Company.Contains(searchOptions.Company));
                    }

                    if (!String.IsNullOrEmpty(searchOptions.City))
                    {
                        results = results.Where(lr => lr.Result.ShipCity.Contains(searchOptions.City));
                    }

                    if (!String.IsNullOrEmpty(searchOptions.State))
                    {
                        results = results.Where(lr => lr.Result.ShipState == searchOptions.State);
                    }
                }

                if (searchOptions.RequireEmailAddress)
                {
                    results = results.Where(lr => lr.Result.Email != null && lr.Result.Email.Length > 5);
                }

                // add where for dealer type
                if (!(LocatorResultTypes.None == locatorResultType ||     //If none is selected, return everything
                      AllMasonryLocatorResultTypes == locatorResultType)) //If everything is selected, do not apply filter.
                {
                    //if builders are ever added, this needs to have another case to support two out of three locator result types
                    results =
                        results
                        .Where(lr =>
                               lr.Result.Member_Type_Code.StartsWith(MasonryLocatorResultTypeToKeyDictionary[locatorResultType]
                                                                     ));
                }

                List <ILocatorResult> locatorResults =
                    results.ToList().ConvertAll(r => BuildMasonryLocatorResult(r.Result, r.Distance));

                // if these are dealers and not installers sort by dealer level
                if ((LocatorResultTypes.Dealer & locatorResultType) == locatorResultType)
                {
                    List <Contractor> contractors =
                        locatorResults
                        .Take(searchOptions.MaxResultsPerType)
                        .Where(lr => lr is Contractor)
                        .ToList().ConvertAll(lr => lr as Contractor);
                    List <Dealer> dealers =
                        locatorResults
                        .Take(searchOptions.MaxResultsPerType)
                        .Where(lr => lr is Dealer)
                        .ToList().ConvertAll(lr => lr as Dealer)
                        .OrderByDescending(d => d.DealerLevel).ToList();

                    locatorResults = new List <ILocatorResult>();
                    locatorResults.AddRange(contractors.ConvertAll(c => c as ILocatorResult));
                    locatorResults.AddRange(dealers.ConvertAll(d => d as ILocatorResult));
                }
                else
                {
                    locatorResults = locatorResults.Take(searchOptions.MaxResultsPerType).ToList();
                }

                return(locatorResults);
            }
        }