Example #1
0
        public static bool Save(CategoryEditVM model, ProviderCurrentMember currentMember)
        {
            bool returnValue = false;
            ProviderCategory aCategory;
            if (ProviderCategory.Exists(model.Id))
            {
                aCategory = new ProviderCategory(model.Id);
            }
            else
            {
                aCategory = new ProviderCategory();
            }

            if (currentMember.CanEdit(aCategory))
            {
                aCategory.Title = model.Title;
                aCategory.EditDate = DateTime.UtcNow;
                aCategory.IsHidden = model.IsHidden;
                aCategory.ParentId = model.ParentId;
                try
                {
                    aCategory.Save();
                    InsideWordWebStaticCache.Instance.ClearCache();
                    returnValue = true;
                }
                catch (Exception caughtException)
                {
                    returnValue = false;
                }
            }

            return returnValue;
        }
Example #2
0
 public static bool Delete(IList<NumBitVM> model, ProviderCurrentMember currentMember)
 {
     bool returnStatus = true;
     foreach (NumBitVM pair in model)
     {
         if (pair.Bit && ProviderCategory.Exists(pair.Num))
         {
             ProviderCategory aCategory = new ProviderCategory(pair.Num);
             if (currentMember.CanEdit(aCategory))
             {
                 returnStatus &= aCategory.Delete();
             }
         }
     }
     InsideWordWebStaticCache.Instance.ClearCache();
     return returnStatus;
 }
Example #3
0
 public IndexVM(ProviderCategory aCategory, List<BlurbVM> blurbList, int currentPage, bool isLastPage)
 {
     if (aCategory.IsRoot)
     {
         PageTitle = string.Empty;
         ShowTitle = false;
     }
     else
     {
         ShowTitle = true;
         PageTitle = aCategory.Title;
     }
     NextPage = currentPage + 1;
     Category = aCategory;
     BlurbList = blurbList;
     IsLastPage = isLastPage;
 }
Example #4
0
        public virtual ActionResult CategoryEdit(long? categoryId)
        {
            ActionResult returnValue = null;

            if (categoryId.HasValue)
            {
                if (ProviderCategory.Exists(categoryId.Value))
                {
                    ProviderCategory aCategory = new ProviderCategory(categoryId.Value);
                    returnValue = View(new CategoryEditVM(aCategory));
                }
                else
                {
                    returnValue = RedirectToAction( MVC.Error.Index(404) );
                }
            }

            if (returnValue == null)
            {
                returnValue = View(new CategoryEditVM());
            }

            return returnValue;
        }
Example #5
0
        public static List<ProviderArticle> LoadRankedBlurbBy(ProviderCategory aCategory, int skipAmount, int takeAmount)
        {
            if (_loadRankedBlurbBy == null)
            {
                // compile this query
                int sparrowScoreType = (int)ProviderArticleScore.ScoreTypeEnum.SparrowRank;
                _loadRankedBlurbBy = CompiledQuery.Compile<InsideWordEntities, string, int, int, IQueryable<Article>>(
                    (ctx, categoryPath, skip, take) => ctx.Articles
                                    .Include("Member.AlternateMemberIds")
                                    .Include("Photos.Thumbnails")
                                    .Include("ArticleScores")
                                    .Where(article => article.Categories.Any(category => category.FullPath.StartsWith(categoryPath))
                                                    && !article.IsHidden
                                                    && article.IsPublished)
                                    .OrderByDescending(article => article.ArticleScores
                                                                         .Where(aScore => aScore.ScoreType == sparrowScoreType)
                                                                         .FirstOrDefault()
                                                                         .Score)
                                    .Skip(skip)
                                    .Take(take)
                );
            }

            //Checking the article's categories against the the FUllPath will ensure that we include the subcategories
            List<ProviderArticle> articleList = _loadRankedBlurbBy.Invoke(DbCtx.Instance, aCategory.FullPath, skipAmount, takeAmount)
                                                                   .ToList()
                                                                   .ConvertAll(_converterEntityToProvider);
            return articleList;
        }
Example #6
0
 public SimpleCategoryVM(ProviderCategory aCategory)
 {
     Id = aCategory.Id;
     Title = aCategory.Title;
 }
Example #7
0
 public MemberServerDataVM(ProviderCurrentMember currentMember, List<AlternateCategoryMapVM> map, ProviderCategory treeRoot)
 {
     StatusCode = (int)StatusEnum.success;
     Map = map;
     CategoryTree = new CategoryVM(treeRoot);
 }
Example #8
0
 public CategoryVM(ProviderCategory aCategory)
 {
     Id = aCategory.Id;
     Title = aCategory.Title;
     Children = new List<CategoryVM>();
     foreach (ProviderCategory childCategory in aCategory.Children(true))
     {
         Children.Add(new CategoryVM(childCategory));
     }
 }
Example #9
0
        public CategoryEditVM(ProviderCategory aCategory)
        {
            Id = (aCategory.Id.HasValue)?aCategory.Id.Value:-1;
            Title = aCategory.Title;
            ParentId = (aCategory.ParentId.HasValue)? aCategory.ParentId.Value : -1;
            IsHidden = aCategory.IsHidden;

            PotentialParentList = new SelectList( ProviderCategory.LoadAll()
                                                                  .Where(category => category.Id != Id)
                                                                  .ToList(),
                                                  "Id",
                                                  "Title",
                                                  new ProviderCategory(Id));
        }
Example #10
0
        public static List<AlternateCategoryMapVM> MapCategories(ProviderCurrentMember currentMember, List<AlternateCategoryMapVM> memberToIWMap)
        {
            List<AlternateCategoryMapVM> IWToMemberMap = new List<AlternateCategoryMapVM>();
            ProviderCategory defaultCategory = new ProviderCategory(InsideWordSettingsDictionary.Instance.DefaultCategoryId.Value);
            List<ProviderAlternateCategoryId> alternateList = currentMember.AlternateCategoryList;
            List<ProviderCategory> iwCategoryList = ProviderCategory.LoadAll();
            ProviderAlternateCategoryId alternateCategory = null;
            ProviderCategory iwCategory = null;
            long? bestIWMatch = null;
            bool mapHasChanged = false;

            foreach (AlternateCategoryMapVM aMap in memberToIWMap)
            {
                mapHasChanged = false;
                alternateCategory = null;
                iwCategory = null;

                if (alternateList.Exists(altId => altId.AlternateId == aMap.AlternateId))
                {
                    // The alternate category is already in our system so just load it
                    alternateCategory = alternateList.Find(altId => altId.AlternateId == aMap.AlternateId);
                }
                else
                {
                    // The alternate category is not in our system so do a bit more work
                    alternateCategory = new ProviderAlternateCategoryId();
                    alternateCategory.MemberId = currentMember.Id.Value;
                    alternateCategory.AlternateId = aMap.AlternateId;
                }

                // ORDER OF "IF" STATEMENTS MATTERS HERE
                if (alternateCategory.OverrideFlag)
                {
                    // This is an override so use the server value
                    iwCategory = iwCategoryList.Find(aCategory => aCategory.Id == alternateCategory.CategoryId);
                }
                else if (aMap.MapId.HasValue && iwCategoryList.Exists(aCategory => aCategory.Id == aMap.MapId.Value))
                {
                    // the map is preset by the member so use that
                    iwCategory = iwCategoryList.Find(aCategory => aCategory.Id == aMap.MapId.Value);
                }
                else if (alternateCategory.CategoryId.HasValue)
                {
                    // the map is not preset by the member, but we have a server value so use the server value
                    iwCategory = iwCategoryList.Find(aCategory => aCategory.Id == alternateCategory.CategoryId);
                }
                else
                {
                    // this category map doesn't exist at all so find the best match
                    bestIWMatch = ProviderAlternateCategoryId.BestMatch(aMap.AlternateTitle);
                    if (bestIWMatch.HasValue)
                    {
                        iwCategory = iwCategoryList.Find(aCategory => aCategory.Id.Value == bestIWMatch.Value);
                    }
                    else if (iwCategoryList.Exists(aCategory => !aCategory.IsRoot &&
                                                                aCategory.Title
                                                                        .ToLowerInvariant()
                                                                        .CompareTo(aMap.AlternateTitle.ToLowerInvariant()) == 0))
                    {
                        iwCategory = iwCategoryList.Find(aCategory => aCategory.Title
                                                                        .ToLowerInvariant()
                                                                        .CompareTo(aMap.AlternateTitle.ToLowerInvariant()) == 0);
                    }
                    else
                    {
                        iwCategory = defaultCategory;
                    }
                }

                if (!alternateCategory.CategoryId.HasValue || alternateCategory.CategoryId.Value != iwCategory.Id.Value)
                {
                    alternateCategory.CategoryId = iwCategory.Id.Value;
                    mapHasChanged = true;
                }

                // finish and save the changes to the alternate category
                alternateCategory.OverrideFlag = false; // always reset the override flag here
                alternateCategory.DisplayName = aMap.AlternateTitle;
                alternateCategory.Save();

                IWToMemberMap.Add(new AlternateCategoryMapVM { AlternateTitle = iwCategory.Title, AlternateId = iwCategory.Id.Value, MapId = aMap.AlternateId });

                // determine if we need to refresh the articles on the server
                // Do this last to avoid race condition
                if (!alternateCategory.IsNew && mapHasChanged)
                {
                    // The category map changed. Re-categorize all articles related to this alternate category.
                    AsyncRefreshArticleManager.Instance.AddBy(AsyncRefreshArticleManager.LoadEnum.AltCategory, alternateCategory.Id.Value);
                }
            }

            return IWToMemberMap;
        }
Example #11
0
        public static bool SaveAlternateCategories(AlternateCategoryListVM model, ProviderMember aMember)
        {
            List<ProviderAlternateCategoryId> alternateList = aMember.AlternateCategoryList;
            ProviderCategory defaultCategory = new ProviderCategory(InsideWordSettingsDictionary.Instance.DefaultCategoryId.Value);
            foreach(AlternateCategoryMapVM aMap in model.AlternateCategoryMapList)
            {
                ProviderAlternateCategoryId altCategory = null;
                if (alternateList.Exists(altId => altId.AlternateId == aMap.AlternateId))
                {
                    altCategory = alternateList.Find(altId => altId.AlternateId == aMap.AlternateId);
                }
                else
                {
                    altCategory = new ProviderAlternateCategoryId();
                    altCategory.AlternateId = aMap.AlternateId;
                    altCategory.MemberId = aMember.Id.Value;
                }

                if (aMap.MapId.HasValue)
                {
                    if (aMap.MapId == -1)
                    {
                        aMap.MapId = defaultCategory.Id;
                    }

                    if (altCategory.CategoryId != aMap.MapId)
                    {
                        altCategory.CategoryId = aMap.MapId;
                        if (!altCategory.IsNew)
                        {
                            // The category map changed. Re-categorize all articles related to this alternate category.
                            AsyncRefreshArticleManager.Instance.AddBy(AsyncRefreshArticleManager.LoadEnum.AltCategory, altCategory.Id.Value);
                        }
                    }
                }

                if (!string.IsNullOrWhiteSpace(aMap.AlternateTitle))
                {
                    altCategory.DisplayName = aMap.AlternateTitle;
                }
                altCategory.OverrideFlag = model.OverrideFlag;
                altCategory.Save();
            }

            return true;
        }
Example #12
0
        public virtual JsonResult Category(long? categoryId)
        {
            ProviderCategory aCategory = new ProviderCategory();
            if (!categoryId.HasValue || !aCategory.Load(categoryId.Value))
            {
                aCategory = ProviderCategory.Root;
            }

            return Json(new CategoryVM(aCategory), JsonRequestBehavior.AllowGet);
        }
Example #13
0
        public static List<BlurbVM> LoadCachedRankedBlurbs(ProviderCategory aCategory, int page)
        {
            string cacheKey = _rankedBlurbListKey + aCategory.Id.Value;
            List<BlurbVM> returnList = new List<BlurbVM>();
            if (HttpContext.Current.Cache[cacheKey] != null)
            {
                returnList = (List<BlurbVM>)HttpContext.Current.Cache[cacheKey];
            }

            int requestStartRange = page * BLURBS_PER_PAGE;
            int requestEndRange = Math.Min(requestStartRange + BLURBS_PER_PAGE, MAX_BLURBS);
            if (returnList.Count < requestEndRange && requestEndRange < MAX_BLURBS)
            {
                int loadAmount = Math.Max(BATCH_LOAD_AMOUNT, requestEndRange - returnList.Count);
                List<BlurbVM> blurbList = ProviderArticle.LoadRankedBlurbBy(aCategory, returnList.Count, loadAmount)
                                                         .ConvertAll<BlurbVM>(anArticle => new BlurbVM(anArticle));

                // Distinct destroys the score order so we can't use that function unfortunately : (
                foreach (BlurbVM aBlurb in blurbList)
                {
                    if (!returnList.Contains(aBlurb))
                    {
                        returnList.Add(aBlurb);
                    }
                }

                DateTime expiration = DateTime.UtcNow.AddSeconds(BLURB_CACHE_SECONDS);
                HttpContext.Current.Cache.Remove(cacheKey);
                HttpContext.Current.Cache.Add(cacheKey,
                                              returnList,
                                              null,
                                              expiration,
                                              Cache.NoSlidingExpiration,
                                              CacheItemPriority.Low,
                                              null);
            }
            int skip = Math.Min(requestStartRange, returnList.Count);
            int take = Math.Min(BLURBS_PER_PAGE, returnList.Count - skip);
            return returnList.GetRange(skip, take);
        }