public ShowcaseItem(ShowcaseItem objectToCopy)
 {
     Active             = objectToCopy.Active;
     AddressID          = objectToCopy.AddressID;
     AgentID            = objectToCopy.AgentID;
     AvailabilityDate   = objectToCopy.AvailabilityDate;
     BuilderID          = objectToCopy.BuilderID;
     DateListed         = objectToCopy.DateListed;
     Directions         = objectToCopy.Directions;
     ElementarySchoolID = objectToCopy.ElementarySchoolID;
     EmailAddresses     = objectToCopy.EmailAddresses;
     Featured           = objectToCopy.Featured;
     HighSchoolID       = objectToCopy.HighSchoolID;
     Image            = objectToCopy.Image;
     ListPrice        = objectToCopy.ListPrice;
     MiddleSchoolID   = objectToCopy.MiddleSchoolID;
     MlsID            = objectToCopy.MlsID;
     NeighborhoodID   = objectToCopy.NeighborhoodID;
     NewHome          = objectToCopy.NewHome;
     OfficeID         = objectToCopy.OfficeID;
     OpenHouseAgentID = objectToCopy.OpenHouseAgentID;
     Rented           = objectToCopy.Rented;
     ShowcaseID       = objectToCopy.ShowcaseID;
     ShowcaseItemID   = objectToCopy.ShowcaseItemID;
     StatsSentToAgent = objectToCopy.StatsSentToAgent;
     StatsSentToOwner = objectToCopy.StatsSentToOwner;
     Summary          = objectToCopy.Summary;
     TeamID           = objectToCopy.TeamID;
     tempOldID        = objectToCopy.tempOldID;
     Title            = objectToCopy.Title;
     VirtualTourURL   = objectToCopy.VirtualTourURL;
     Website          = objectToCopy.Website;
 }
        public static void SendPropertyStatisticEmail(ShowcaseItem property, string emailAddresses = null, string subject = null, string message = null, bool updateHistoricalStats = true)
        {
            List <ShowcaseItem> properties = new List <ShowcaseItem>();

            properties.Add(property);
            SendPropertyStatisticEmail(properties, emailAddresses, subject, message, updateHistoricalStats);
        }
        public static ShowcaseItem GetByID(int ShowcaseItemID, IEnumerable <string> includeList = null)
        {
            ShowcaseItem obj = null;
            string       key = cacheKeyPrefix + ShowcaseItemID + GetCacheIncludeText(includeList);

            ShowcaseItem tmpClass = null;

            if (Cache.IsEnabled)
            {
                if (Cache.IsEmptyCacheItem(key))
                {
                    return(null);
                }
                tmpClass = Cache[key] as ShowcaseItem;
            }

            if (tmpClass != null)
            {
                obj = tmpClass;
            }
            else
            {
                using (Entities entity = new Entities())
                {
                    IQueryable <ShowcaseItem> itemQuery = AddIncludes(entity.ShowcaseItem, includeList);
                    obj = itemQuery.FirstOrDefault(n => n.ShowcaseItemID == ShowcaseItemID);
                }
                Cache.Store(key, obj);
            }

            return(obj);
        }
        public static int GetItemCountFromSearch(string filters, int showcaseID, bool newHomes)
        {
            ShowcaseItem.Filters filterList = new ShowcaseItem.Filters();
            filterList.FilterShowcaseItemActive     = true.ToString();
            filterList.FilterShowcaseItemShowcaseID = showcaseID.ToString();

            filters = HttpContext.Current.Server.UrlDecode(filters);
            string filterText = string.Empty;

            string[] queryStrings = filters.Split('&');
            foreach (string obj in queryStrings)
            {
                if (obj.StartsWith("Filters="))
                {
                    filters = obj.Replace("Filters=", "");
                }
                else if (obj.StartsWith("SearchText="))
                {
                    filterList.SearchText = obj.Replace("SearchText=", "");
                }
                else if (obj.StartsWith("AgentID="))
                {
                    filterList.FilterShowcaseItemAgentID = obj.Replace("AgentID=", "");
                }
                else if (obj.StartsWith("OpenHouse="))
                {
                    filterList.OpenHouse = Convert.ToBoolean(obj.Replace("OpenHouse=", ""));
                }
                if (newHomes)
                {
                    filterList.NewHomesOnly = true;
                }
            }

            List <ShowcaseItemForJSON> items = ShowcaseItem.GetPagedFilteredShowcaseItems(0, 1, filters, "", true, filterList);

            return(items.Any() ? items.FirstOrDefault().TotalRowCount.Value : 0);
        }
        public static int?GetCurrentShowcaseID(HttpContext requestContext = null)
        {
            if (requestContext == null)
            {
                requestContext = HttpContext.Current;
            }
            if (requestContext == null || requestContext.Items["CurrentShowcaseID"] == "null")
            {
                return(null);
            }
            if (requestContext.Items["CurrentShowcaseID"] == null)
            {
                //Frontend Check
                if (!HttpContext.Current.Request.Path.ToLower().Contains("admin/"))
                {
                    if (Settings.MultipleShowcases)
                    {
                        HttpRequest currentRequest = HttpContext.Current.Request;
                        int         temp           = 0;
                        if (currentRequest.RawUrl.ToLower().Contains("showcasewebmethods.asmx") && currentRequest.UrlReferrer != null ? !String.IsNullOrEmpty(HttpUtility.ParseQueryString(currentRequest.UrlReferrer.Query)["ShowcaseID"]) && Int32.TryParse(HttpUtility.ParseQueryString(currentRequest.UrlReferrer.Query)["ShowcaseID"], out temp) : !String.IsNullOrEmpty(currentRequest.QueryString["ShowcaseID"]) && Int32.TryParse(currentRequest.QueryString["ShowcaseID"], out temp))
                        {
                            Showcases showcaseEntity = Showcases.GetByID(temp);
                            if (showcaseEntity != null && showcaseEntity.Active)
                            {
                                requestContext.Items["CurrentShowcaseID"] = temp;
                                return(temp);
                            }
                        }
                        else if (currentRequest.RawUrl.ToLower().Contains("showcase-item.aspx") && !String.IsNullOrEmpty(currentRequest.QueryString["ID"]) && Int32.TryParse(currentRequest.QueryString["ID"], out temp))
                        {
                            Showcases showcaseEntity = Showcases.GetByID(ShowcaseItem.GetByID(temp).ShowcaseID);
                            if (showcaseEntity != null && showcaseEntity.Active)
                            {
                                requestContext.Items["CurrentShowcaseID"] = showcaseEntity.ShowcaseID;
                                return(showcaseEntity.ShowcaseID);
                            }
                        }
                    }
                    requestContext.Items["CurrentShowcaseID"] = GetDefaultShowcaseID();
                    return(GetDefaultShowcaseID());
                }
                //Backend Check
                if (Settings.MultipleShowcases)
                {
                    int showcaseID;
                    if (HttpContext.Current.Session != null && HttpContext.Current.Session["ShowcaseID"] != null && Int32.TryParse(HttpContext.Current.Session["ShowcaseID"].ToString(), out showcaseID))
                    {
                        if (Showcases.GetByID(showcaseID) == null || (!IsShowcaseAdmin() && !ShowcaseUser.ShowcaseUserGetByShowcaseID(showcaseID).Exists(s => s.UserID == Helpers.GetCurrentUserID())))
                        {
                            HttpContext.Current.Session.Remove("ShowcaseID");
                            return(null);
                        }
                        requestContext.Items["CurrentShowcaseID"] = showcaseID;
                        return(showcaseID);
                    }
                    return(null);
                }
                int?defaultID = GetDefaultShowcaseID();
                if (defaultID != null)
                {
                    SetUsersCurrentShowcaseID(defaultID.Value);
                    requestContext.Items["CurrentShowcaseID"] = defaultID;
                    return(defaultID);
                }
                if (requestContext.Items["CurrentShowcaseID"] == null)
                {
                    requestContext.Items["CurrentShowcaseID"] = 0;
                }
            }

            return(Convert.ToInt32(requestContext.Items["CurrentShowcaseID"]));
        }
Exemple #6
0
        public static void SendAlertEmails(List <SavedSearch> searches, List <WhatChanged> whatChanged = null)
        {
            Classes.Media352_MembershipProvider.User userEntity = Classes.Media352_MembershipProvider.User.GetByID(searches.FirstOrDefault().UserID, (new[] { "UserInfo" }).ToList());
            string toAddress = userEntity.Email;
            string propertyLinks = string.Empty, searchLinks = string.Empty;

            string basePropertyHtml       = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearchProperty, null, false);
            string baseFilterHtml         = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearchFilter, null, false);
            string baseFilterPropertyHtml = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearchFilterProperty, null, false);

            foreach (SavedSearch search in searches)
            {
                string msLink = search.ShowcaseID == (int)MeybohmShowcases.AikenExistingHomes || search.ShowcaseID == (int)MeybohmShowcases.AikenRentalHomes
                                                                ? "aiken/" : "augusta/";
                string searchType = search.ShowcaseID == (int)MeybohmShowcases.AikenRentalHomes || search.ShowcaseID == (int)MeybohmShowcases.AugustaRentalHomes ? "rentals" : "search";
                if (!string.IsNullOrEmpty(search.FilterString))
                {
                    string      propertiesText    = string.Empty;
                    WhatChanged whatChangedEntity = whatChanged.Find(f => f.SavedSearch.SavedSearchID == search.SavedSearchID);
                    if (whatChangedEntity != null)
                    {
                        foreach (ShowcaseItemForJSON property in whatChangedEntity.PropertiesThatChanged)
                        {
                            string whatChangedText = string.Empty;
                            List <PropertyChangeLog> propertyChanges = whatChangedEntity.ChangeLog.Where(s => s.ShowcaseItemID == property.ShowcaseItemID && !m_ChangeLogAttributesToIgnore.Any(a => a == s.Attribute)).ToList();
                            if (propertyChanges.Any())
                            {
                                if (propertyChanges.Any(c => c.Attribute == "Home Added"))
                                {
                                    whatChangedText += "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" />&nbsp; Home Added";
                                }
                                else
                                {
                                    foreach (PropertyChangeLog change in propertyChanges)
                                    {
                                        whatChangedText += "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" />&nbsp; " + change.Attribute + " Updated,";
                                    }
                                }
                            }
                            else
                            {
                                whatChangedText = "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" />&nbsp; Home Added";
                            }

                            propertiesText += baseFilterPropertyHtml.Replace("[[Address]]", property.Address + "<br />" + property.City + ", " + property.State + " " + property.Zipcode)
                                              .Replace("[[Title]]", property.Title.Replace(" Bedrooms:", "<br />Bedrooms:"))
                                              .Replace("[[Image]]", !String.IsNullOrWhiteSpace(property.Image) && !property.Image.ToLower().StartsWith("http") ? Helpers.RootPath + Globals.Settings.UploadFolder + "images/" + property.Image : property.Image)
                                              .Replace("[[WhatChanged]]", whatChangedText.TrimEnd(',').Replace(",", "<br />"));
                        }
                    }

                    searchLinks += baseFilterHtml.Replace("[[Url]]", Helpers.RootPath + msLink + searchType + "?" + search.FilterString)
                                   .Replace("[[Name]]", search.Name)
                                   .Replace("[[Properties]]", propertiesText);
                }

                if (search.ShowcaseItemID > 0)
                {
                    ShowcaseItem property = ShowcaseItem.GetByID((int)search.ShowcaseItemID, new string[] { "Address", "Address.State" });
                    if (!property.Active)
                    {
                        continue;
                    }
                    propertyLinks  = string.IsNullOrEmpty(propertyLinks) ? string.Format(@"<tr style=""background-color: #f5f5f5; padding: 10px 0; width: 100%;"">
	<td style=""text-align: center; padding: 10px 0; font-family: Arial, sans-serif; font-size: 16px; color: #999; line-height: 1.4; vertical-align: top;"" colspan=""2"">
		Updated Properties: &nbsp;{0}		
	</td>
</tr>
<tr>
	<td colspan=""2"">&nbsp;</td>
</tr>", string.Join(", ", searches.Where(s => s.ShowcaseItemID.HasValue).Select(s => s.Name))) : propertyLinks;
                    propertyLinks += basePropertyHtml.Replace("[[Title]]", property.Title.Replace(" Bedrooms:", "<br />Bedrooms:"))
                                     .Replace("[[Image]]", !String.IsNullOrWhiteSpace(property.Image) && !property.Image.ToLower().StartsWith("http") ? Helpers.RootPath + Globals.Settings.UploadFolder + "images/" + property.Image : property.Image)
                                     .Replace("[[Address]]", property.Address.Address1 + "<br />" + property.Address.City + ", " + property.Address.State.Abb + " " + property.Address.Zip)
                                     .Replace("[[DateListed]]", property.DateListed.HasValue ? property.DateListed.Value.ToShortDateString() : string.Empty)
                                     .Replace("[[NumberOfPhotos]]", Media.GetNumberOfPhotos(property.ShowcaseItemID).ToString())
                                     .Replace("[[WhatChanged]]", "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" />&nbsp; " + PropertyChangeLog.PropertyChangeLogPage(0, 1, "", "DateStamp", false, new PropertyChangeLog.Filters {
                        FilterPropertyChangeLogShowcaseItemID = search.ShowcaseItemID.ToString()
                    }).FirstOrDefault().Attribute.Replace("PhotoURL", "Photos") + " Updated")
                                     .Replace("[[PropertyLink]]", Helpers.RootPath + msLink + searchType + "?id=" + property.ShowcaseItemID);
                }
            }

            UserInfo userInfo = userEntity.UserInfo.FirstOrDefault();

            if (string.IsNullOrWhiteSpace(toAddress) || userInfo == null || (string.IsNullOrEmpty(propertyLinks) && string.IsNullOrEmpty(searchLinks)))
            {
                return;
            }
            MailMessage email = new MailMessage
            {
                IsBodyHtml = true,
                From       = new MailAddress(Globals.Settings.FromEmail)
            };

            email.To.Add(toAddress);
            email.Body = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearch, new
            {
                FirstAndLastName = userInfo.FirstAndLast,
                PropertyLinks    = propertyLinks,
                SearchLinks      = searchLinks
            });
            email.Subject = Globals.Settings.SiteTitle + " - Saved Search";

            SmtpClient smtp = new SmtpClient();

            smtp.Send(email);
        }
        public static List <ShowcaseItem> GetNewHomesForSale(string searchText, string sortField, bool sortDirection, Filters filterList = new Filters())
        {
            string cachingFilterText = GetCacheFilterText(filterList.GetCustomFilterList(), searchText);

            List <ShowcaseItem> objects;
            string baseKey  = cacheKeyPrefix + "GetNewHomesForSale_" + cachingFilterText;
            string key      = baseKey + "_" + sortField + "_" + sortDirection;
            string countKey = baseKey + "_count";

            List <ShowcaseItem> tmpList = null;
            int?tmpInt = null;

            if (Cache.IsEnabled)
            {
                tmpList = Cache[key] as List <ShowcaseItem>;
                tmpInt  = Cache[countKey] as int?;
            }

            if (tmpList != null && tmpInt.HasValue)
            {
                objects     = tmpList;
                m_ItemCount = tmpInt.Value;
            }
            else
            {
                objects = new List <ShowcaseItem>();
                Classes.Showcase.ShowcaseItemMetric.UpdateHistoricalStats(null);
                using (Entities entity = new Entities())
                {
                    IQueryable <ShowcaseItem> itemQuery = SetupWhereClause(entity.ShowcaseItem, "ShowcaseItem", filterList.GetFilterList(), string.Empty, null).Where(s => !s.SoldHome.Any());
                    if (String.IsNullOrEmpty(filterList.FilterShowcaseItemShowcaseID))
                    {
                        itemQuery = itemQuery.Where(s => s.NewHome);
                    }
                    if (sortField != "NumberOfVisits" && sortField != "NumberBedrooms" && sortField != "NumberBathrooms")
                    {
                        itemQuery = SetupOrderByClause(itemQuery, sortField, sortDirection);
                    }
                    if (!String.IsNullOrWhiteSpace(searchText))
                    {
                        int?mlsID = null;
                        int temp;
                        if (Int32.TryParse(searchText, out temp))
                        {
                            mlsID = temp;
                        }
                        itemQuery = itemQuery.Where(s => s.Address.Address1.Contains(searchText) || s.Address.Zip.Contains(searchText) || s.Builder.Name.Contains(searchText) || s.Neighborhood.Name.Contains(searchText) || (mlsID.HasValue && s.MlsID == mlsID));
                    }

                    var anonQuery = itemQuery.Select(s => new
                    {
                        ShowcaseItemEntity = s,
                        Address            = s.Address,
                        AgentInfo          = s.Agent.UserInfo.FirstOrDefault(),
                        Builder            = s.Builder,
                        Neighborhood       = s.Neighborhood,
                        NumVisits          = (int?)s.ShowcaseItemMetricHistorical.Where(m => m.ClickTypeID == (int)ClickTypes.Visit).Select(m => m.TotalCount).Sum(),
                        NumberBedrooms     = s.ShowcaseItemAttributeValue.Where(a => a.ShowcaseAttributeValue.ShowcaseAttribute.MLSAttributeName == "Bedrooms").Select(a => a.ShowcaseAttributeValue.Value).FirstOrDefault(),
                        NumberBathrooms    = s.ShowcaseItemAttributeValue.Where(a => a.ShowcaseAttributeValue.ShowcaseAttribute.MLSAttributeName == "Full Baths").Select(a => a.ShowcaseAttributeValue.Value).FirstOrDefault()
                    });

                    if (sortField == "NumberOfVisits" && sortDirection)
                    {
                        anonQuery = anonQuery.OrderBy(s => s.NumVisits);
                    }
                    else if (sortField == "NumberOfVisits")
                    {
                        anonQuery = anonQuery.OrderByDescending(s => s.NumVisits);
                    }
                    else if (sortField == "NumberBedrooms" && sortDirection)
                    {
                        anonQuery = anonQuery.OrderBy(s => s.NumberBedrooms);
                    }
                    else if (sortField == "NumberBedrooms")
                    {
                        anonQuery = anonQuery.OrderByDescending(s => s.NumberBedrooms);
                    }
                    else if (sortField == "NumberBathrooms" && sortDirection)
                    {
                        anonQuery = anonQuery.OrderBy(s => s.NumberBathrooms);
                    }
                    else if (sortField == "NumberBathrooms")
                    {
                        anonQuery = anonQuery.OrderByDescending(s => s.NumberBathrooms);
                    }

                    var items = anonQuery.ToList();
                    m_ItemCount = tmpInt.HasValue ? tmpInt.Value : anonQuery.Count();

                    foreach (var item in items)
                    {
                        ShowcaseItem obj = item.ShowcaseItemEntity;
                        obj.Address      = item.Address;
                        obj.AgentInfo    = item.AgentInfo;
                        obj.Builder      = item.Builder;
                        obj.Neighborhood = item.Neighborhood;
                        int temp;
                        if (!String.IsNullOrWhiteSpace(item.NumberBedrooms) && Int32.TryParse(item.NumberBedrooms, out temp))
                        {
                            obj.NumberBedrooms = temp;
                        }
                        if (!String.IsNullOrWhiteSpace(item.NumberBathrooms) && Int32.TryParse(item.NumberBathrooms, out temp))
                        {
                            obj.NumberBathrooms = temp;
                        }
                        if (item.NumVisits.HasValue)
                        {
                            obj.NumberOfVisits = item.NumVisits.Value;
                        }
                        objects.Add(obj);
                    }
                }

                Cache.Store(key, objects);
                Cache.Store(countKey, m_ItemCount);
            }
            return(objects);
        }
        public static List <ShowcaseItem> PageInventoryAndStatisticsReport(int startRowIndex, int maximumRows, string searchText, string sortField, bool sortDirection, Filters filterList = new Filters())
        {
            string cachingFilterText = GetCacheFilterText(filterList.GetCustomFilterList(), searchText);

            List <ShowcaseItem> objects;
            string baseKey  = cacheKeyPrefix + "PageInventoryAndStatisticsReport_" + cachingFilterText;
            string key      = baseKey + "_" + sortField + "_" + sortDirection + "_" + startRowIndex + "_" + maximumRows;
            string countKey = baseKey + "_count";

            List <ShowcaseItem> tmpList = null;
            int?tmpInt = null;

            if (Cache.IsEnabled)
            {
                tmpList = Cache[key] as List <ShowcaseItem>;
                tmpInt  = Cache[countKey] as int?;
            }

            if (tmpList != null && tmpInt.HasValue)
            {
                objects     = tmpList;
                m_ItemCount = tmpInt.Value;
            }
            else
            {
                int pageNumber = maximumRows > 0 ? 1 + startRowIndex / maximumRows : 1;
                objects = new List <ShowcaseItem>();
                Classes.Showcase.ShowcaseItemMetric.UpdateHistoricalStats(null);
                using (Entities entity = new Entities())
                {
                    IQueryable <ShowcaseItem> itemQuery = SetupWhereClause(entity.ShowcaseItem, "ShowcaseItem", filterList.GetFilterList(), searchText, null);
                    if (String.IsNullOrEmpty(filterList.FilterShowcaseItemShowcaseID))
                    {
                        itemQuery = itemQuery.Where(s => s.ShowcaseID == (int)MeybohmShowcases.AikenExistingHomes || s.ShowcaseID == (int)MeybohmShowcases.AugustaExistingHomes);
                    }
                    if (filterList.FilterBeginDate.HasValue)
                    {
                        itemQuery = itemQuery.Where(s => s.DateListed >= filterList.FilterBeginDate.Value);
                    }
                    if (filterList.FilterEndDate.HasValue)
                    {
                        itemQuery = itemQuery.Where(s => s.DateListed <= filterList.FilterEndDate.Value);
                    }
                    if (filterList.FilterListPriceMin.HasValue)
                    {
                        itemQuery = itemQuery.Where(s => s.ListPrice >= filterList.FilterListPriceMin.Value);
                    }
                    if (filterList.FilterListPriceMax.HasValue)
                    {
                        itemQuery = itemQuery.Where(s => s.ListPrice <= filterList.FilterListPriceMax.Value);
                    }
                    if (!String.IsNullOrEmpty(filterList.FilterPropertyType))
                    {
                        itemQuery = itemQuery.Where(s => s.ShowcaseItemAttributeValue.Any(a => a.ShowcaseAttributeValue.ShowcaseAttribute.MLSAttributeName == "Property Type" && a.ShowcaseAttributeValue.Value == filterList.FilterPropertyType));
                    }
                    if (sortField != "NumberOfVisits")
                    {
                        itemQuery = SetupOrderByClause(itemQuery, sortField, sortDirection);
                    }

                    var anonQuery = itemQuery.Select(s => new
                    {
                        ShowcaseItemEntity = s,
                        Address            = s.Address,
                        AgentInfo          = s.Agent.UserInfo.FirstOrDefault(),
                        NumVisits          = (int?)s.ShowcaseItemMetricHistorical.Where(m => m.ClickTypeID == (int)ClickTypes.Visit).Select(m => m.TotalCount).Sum(),
                        PropertyType       = s.ShowcaseItemAttributeValue.Where(a => a.ShowcaseAttributeValue.ShowcaseAttribute.MLSAttributeName == "Property Type").Select(a => a.ShowcaseAttributeValue.Value).FirstOrDefault()
                    });

                    if (sortField == "NumberOfVisits" && sortDirection)
                    {
                        anonQuery = anonQuery.OrderBy(s => s.NumVisits);
                    }
                    else if (sortField == "NumberOfVisits")
                    {
                        anonQuery = anonQuery.OrderByDescending(s => s.NumVisits);
                    }
                    else if (sortField == "PropertyType" && sortDirection)
                    {
                        anonQuery = anonQuery.OrderBy(s => s.PropertyType);
                    }
                    else if (sortField == "PropertyType")
                    {
                        anonQuery = anonQuery.OrderByDescending(s => s.PropertyType);
                    }

                    var items = maximumRows <= 0 ? anonQuery.ToList() : anonQuery.Skip(maximumRows * (pageNumber - 1)).Take(maximumRows).ToList();
                    m_ItemCount = tmpInt.HasValue ? tmpInt.Value : (maximumRows <= 0 || (pageNumber == 1 && items.Count < maximumRows) ? items.Count : anonQuery.Count());

                    foreach (var item in items)
                    {
                        ShowcaseItem obj = item.ShowcaseItemEntity;
                        obj.Address   = item.Address;
                        obj.AgentInfo = item.AgentInfo;
                        if (item.NumVisits.HasValue)
                        {
                            obj.NumberOfVisits = item.NumVisits.Value;
                        }
                        obj.PropertyType = item.PropertyType;
                        objects.Add(obj);
                    }
                }

                Cache.Store(key, objects);
                Cache.Store(countKey, m_ItemCount);
            }
            return(objects);
        }
        public static List <ShowcaseItem> GetPropertiesForAgent(int maximumRows, string sortField, bool sortDirection, int agentID, bool updateStats = false)
        {
            List <ShowcaseItem> objects;
            string baseKey  = cacheKeyPrefix + "GetPropertiesForAgent_" + agentID;
            string key      = baseKey + "_" + sortField + "_" + sortDirection + "_" + maximumRows;
            string countKey = baseKey + "_count";

            List <ShowcaseItem> tmpList = null;
            int?tmpInt = null;

            if (Cache.IsEnabled)
            {
                tmpList = Cache[key] as List <ShowcaseItem>;
                tmpInt  = Cache[countKey] as int?;
            }

            if (tmpList != null && tmpInt.HasValue)
            {
                objects     = tmpList;
                m_ItemCount = tmpInt.Value;
            }
            else
            {
                objects = new List <ShowcaseItem>();
                if (updateStats)
                {
                    Classes.Showcase.ShowcaseItemMetric.UpdateHistoricalStats(null);
                }
                using (Entities entity = new Entities())
                {
                    IQueryable <ShowcaseItem> itemQuery = entity.ShowcaseItem;
                    if (sortField != "NumberOfVisits")
                    {
                        itemQuery = SetupOrderByClause(itemQuery, sortField, sortDirection);
                    }

                    var anonQuery = itemQuery.Where(s => s.Active && (s.AgentID == agentID || (s.TeamID.HasValue && s.Team.UserTeam.Any(u => u.UserID == agentID)))).Select(s => new
                    {
                        ShowcaseItemEntity = s,
                        Address            = s.Address,
                        AddressState       = s.Address.State,
                        NumVisits          = (int?)s.ShowcaseItemMetricHistorical.Where(m => m.ClickTypeID == (int)ClickTypes.Visit).Select(m => m.TotalCount).Sum(),
                        PropertyType       = s.ShowcaseItemAttributeValue.Where(a => a.ShowcaseAttributeValue.ShowcaseAttribute.MLSAttributeName == "Property Type").Select(a => a.ShowcaseAttributeValue.Value).FirstOrDefault()
                    });

                    if (sortField == "NumberOfVisits" && sortDirection)
                    {
                        anonQuery = anonQuery.OrderBy(s => s.NumVisits);
                    }
                    else if (sortField == "NumberOfVisits")
                    {
                        anonQuery = anonQuery.OrderByDescending(s => s.NumVisits);
                    }

                    var items = anonQuery.Take(maximumRows).ToList();
                    foreach (var item in items)
                    {
                        ShowcaseItem obj = item.ShowcaseItemEntity;
                        obj.Address       = item.Address;
                        obj.Address.State = item.AddressState;
                        if (item.NumVisits.HasValue)
                        {
                            obj.NumberOfVisits = item.NumVisits.Value;
                        }
                        obj.PropertyType = item.PropertyType;
                        objects.Add(obj);
                    }
                    m_ItemCount = tmpInt.HasValue ? tmpInt.Value : (maximumRows <= 0 || objects.Count < maximumRows ? objects.Count : anonQuery.Count());
                }

                Cache.Store(key, objects);
                Cache.Store(countKey, m_ItemCount);
            }

            return(objects);
        }