Пример #1
0
        public static PetOwnerList GetPetOwnerList(Guid providerGroupId)
        {
            PetOwnerList result = null;

            // Step 1. Get httpContext
            HttpContext httpContext = HttpContext.Current;

            // Step 2. Get cache object
            if (httpContext != null)
            {
                // Mark Active
                httpContext.Cache.Add(GetActiveProviderGroupCacheKey(providerGroupId), true, null, Cache.NoAbsoluteExpiration, new TimeSpan(GetActiveProviderGroupSlidingExpirationInMinutes()), CacheItemPriority.Default, null);

                result = httpContext.Cache[GetPetOwnerListCacheKey(providerGroupId)] as PetOwnerList;
                if (result != null)
                {
                    return(result);
                }
            }

            // TODO: Log the CACHE MISS


            // Step 3. Get PetList if necessary
            return(GetPetOwnerListInner(providerGroupId));
        }
Пример #2
0
        public PetOwnerList ExecuteList()
        {
            using (DataContext context = DataContext.CreateForMessage(this))
            {
                PetOwnerList result = new PetOwnerList()
                {
                    ProviderGroupId = this.ProviderGroupId
                };

                result.OwnerList = context.OwnerSet
                                   .Include("PetCollection")
                                   .Where(x => x.ProviderGroupId == this.ProviderGroupId)
                                   .Select(x => new OwnerProjection()
                {
                    Id = x.Id, FirstName = x.FirstName, LastName = x.LastName, PhoneNumber = x.PhoneNumber ?? String.Empty, PetList = x.PetCollection.Select(y => new PetProjection()
                    {
                        Id = y.Id, Name = y.Name ?? string.Empty
                    }).ToList()
                })
                                   .OrderBy(x => x.LastName)
                                   .ThenBy(x => x.FirstName)
                                   .ToList();

                // Assign owner, add pet list
                result.OwnerList.ForEach(x => x.PetList.ForEach(y => {
                    y.Owner = x;
                    result.PetList.Add(y);
                }));

                // Sort pet list
                result.PetList.Sort((x, y) => x.Name.CompareTo(y.Name));

                return(result);
            }
        }
Пример #3
0
        public static List <OwnerLink> QueryOwner(Guid providerGroupId, string query)
        {
            List <OwnerLink> result = new List <OwnerLink>();

            // Get the list
            PetOwnerList petOwnerList = GetPetOwnerList(providerGroupId);

            // tokenize
            var tokenList = query.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (tokenList.Count == 0)
            {
                return(result);
            }

            petOwnerList.OwnerList.ForEach(x =>
            {
                OwnerLink ownerLink = new OwnerLink()
                {
                    Id = x.Id, FirstName = x.FirstName, LastName = x.LastName
                };

                if (x.FirstName.StartsWith(tokenList.First(), StringComparison.InvariantCultureIgnoreCase))
                {
                    ownerLink.Score = ownerLink.Score + 10;
                }

                if (x.LastName.StartsWith(tokenList.Last(), StringComparison.InvariantCultureIgnoreCase))
                {
                    ownerLink.Score = ownerLink.Score + 12;
                }

                if (ownerLink.Score > 0)
                {
                    result.Add(ownerLink);
                }
            });

            result.Sort((x, y) =>
            {
                int score = y.Score.CompareTo(x.Score);
                if (score != 0)
                {
                    return(score);
                }

                int lastName = x.LastName.CompareTo(y.LastName);
                if (lastName != 0)
                {
                    return(lastName);
                }

                return(x.FirstName.CompareTo(y.FirstName));
            });

            return(result);
        }
Пример #4
0
        public static List <PetOwnerLink> QueryPetOwner(Guid providerGroupId, string query)
        {
            List <PetOwnerLink> result = new List <PetOwnerLink>();

            // Get the list
            PetOwnerList petOwnerList = GetPetOwnerList(providerGroupId);

            // tokenize
            var tokenList = query.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (tokenList.Count == 0)
            {
                return(result);
            }

            // query
            petOwnerList.OwnerList.ForEach(o =>
            {
                if (o.PetList.Count == 0) // Owners without a pet
                {
                    PetOwnerLink petOwnerLink = new PetOwnerLink()
                    {
                        Id = null, Pet = null, OwnerId = o.Id, FirstName = o.FirstName, LastName = o.LastName
                    };


                    if (o.FirstName.StartsWith(tokenList.First(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        petOwnerLink.Score = petOwnerLink.Score + 10;
                    }

                    if (o.LastName.StartsWith(tokenList.Last(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        petOwnerLink.Score = petOwnerLink.Score + 12;
                    }

                    if (petOwnerLink.Score > 0)
                    {
                        // TODO: figure out how to re-enable

                        //result.Add(petOwnerLink);
                    }
                }

                o.PetList.ForEach(p =>
                {
                    PetOwnerLink petOwnerLink = new PetOwnerLink()
                    {
                        Id = p.Id, Pet = p.Name, OwnerId = o.Id, FirstName = o.FirstName, LastName = o.LastName, Breed = "Unknown"
                    };

                    // first token score
                    int firstTokenScore = 0;
                    if (o.FirstName.StartsWith(tokenList.First(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        firstTokenScore = firstTokenScore + 8;
                    }
                    else if (o.LastName.StartsWith(tokenList.First(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        firstTokenScore = firstTokenScore + 12;
                    }
                    else if (p.Name.StartsWith(tokenList.First(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        firstTokenScore = firstTokenScore + 10;
                    }
                    else if (query.All(char.IsNumber))
                    {
                        if (query.Length == 4 && p.Owner.PhoneNumber.EndsWith(query))
                        {
                            firstTokenScore = firstTokenScore + 8;
                        }
                        else if (query.Length == 5 && p.Owner.PhoneNumber.EndsWith(query))
                        {
                            firstTokenScore = firstTokenScore + 8;
                        }
                        else if (query.Length == 7 && p.Owner.PhoneNumber.EndsWith(query))
                        {
                            firstTokenScore = firstTokenScore + 10;
                        }
                        else if (query.Length == 10 && p.Owner.PhoneNumber.StartsWith(query))
                        {
                            firstTokenScore = firstTokenScore + 12;
                        }
                    }

                    int lastTokenScore = 0;
                    if (o.LastName.StartsWith(tokenList.Last(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        lastTokenScore = lastTokenScore + 12;
                    }

                    if (tokenList.Count == 1)
                    {
                        if (firstTokenScore > 0)
                        {
                            petOwnerLink.Score = firstTokenScore;
                            result.Add(petOwnerLink);
                        }
                    }
                    else
                    {
                        if (firstTokenScore > 0 && lastTokenScore > 0)
                        {
                            petOwnerLink.Score = firstTokenScore + lastTokenScore;
                            result.Add(petOwnerLink);
                        }
                    }
                }
                                  );
            });

            result.Sort((x, y) =>
            {
                int score = y.Score.CompareTo(x.Score);
                if (score != 0)
                {
                    return(score);
                }

                int owner = x.LastFirst.CompareTo(y.LastFirst);
                if (score != 0)
                {
                    return(score);
                }

                return(x.Pet.CompareTo(y.Pet));
            });

            return(result);
        }