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; }
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; }
public virtual JsonResult PublishArticle(APIArticleVM model) { string from = "APILOGINFO - " + HttpContext.Request.UserHostAddress; InsideWordWebLog.Instance.Buffer(from, "PublishArticle( "+model+" )"); ApiMsgVM returnMessage = new ApiMsgVM( (int)ApiMsgVM.StatusEnum.failure ); ProviderCurrentMember currentMember = ProviderCurrentMember.Instance; if (!currentMember.IsLoggedOn) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "You must be logged in to use this command. Use the API function " + Url.Action(MVC.API.Login()) + " to login."; } else if (!ModelState.IsValid) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "Could not parse the data."; } else if (string.IsNullOrWhiteSpace(model.Title) || string.IsNullOrWhiteSpace(model.Text)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "title and text cannot be blank."; } else { ProviderArticle anArticle = null; ProviderAlternateArticleId altId = new ProviderAlternateArticleId(); if (model.Id.HasValue) { anArticle = new ProviderArticle(model.Id.Value); } else if (!string.IsNullOrWhiteSpace(model.AltId) && altId.Load(model.AltId, currentMember.Id.Value)) { anArticle = new ProviderArticle(altId.ArticleId); } else { anArticle = new ProviderArticle(); } if (!model.CategoryId.HasValue && anArticle.IsNew) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "New articles must have a Category Id"; } else if (model.CategoryId.HasValue && !ProviderCategory.Exists(model.CategoryId.Value)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "The category id " + model.CategoryId + " does not exist."; } else if (!currentMember.CanEdit(anArticle)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "You do not have the rights to edit article with InsideWord id " + anArticle.Id; if (string.IsNullOrWhiteSpace(model.AltId)) { returnMessage.StatusMessage += " and alternate id " + model.AltId; } } else { if (anArticle.IsNew) { anArticle.CreateDate = DateTime.UtcNow; } // The edit date is controlled by InsideWord and not the ones using the API // this is because edit date is actually rather important. anArticle.EditDate = DateTime.UtcNow; if (model.IsPublished.HasValue) { anArticle.IsPublished = model.IsPublished.Value; } else if (anArticle.IsNew) { anArticle.IsPublished = true; } anArticle.Blurb = model.Blurb; anArticle.Title = model.Title; anArticle.RawText = model.Text; anArticle.MemberId = currentMember.Id.Value; anArticle.RemoveAllCategories(); if (model.CategoryId.HasValue) { anArticle.AddCategory(model.CategoryId.Value); } else { anArticle.AddCategory(InsideWordSettingsDictionary.Instance.DefaultCategoryId.Value); } if (model.AlternateCategoryId.HasValue) { List<ProviderAlternateCategoryId> altCategoryList = currentMember.AlternateCategoryList; ProviderAlternateCategoryId alternateCategory = null; if (altCategoryList.Exists(altCat => altCat.AlternateId == model.AlternateCategoryId)) { alternateCategory = altCategoryList.Find(altCat => altCat.AlternateId == model.AlternateCategoryId); } else { // Doesn't seem to exist. This must be a new category we don't have any info on yet because the update from the client hasn't occurred yet. // Save this as an incomplete alternate category alternateCategory = new ProviderAlternateCategoryId(); alternateCategory.AlternateId = model.AlternateCategoryId.Value; alternateCategory.MemberId = currentMember.Id.Value; alternateCategory.Save(); } anArticle.RemoveAllAlternateCategories(); anArticle.AddAlternateCategory(alternateCategory.Id.Value); } // Associate the photos, but don't bother returning any errors. List<string> errorList = new List<string>(); ArticleBL.AssociatePhotos(model.Text, anArticle, ref errorList); if (errorList.Count > 0) { InsideWordWebLog.Instance.Log.Debug(string.Join(", ", errorList)); } anArticle.Save(); // now that the article is saved check if the model came in with an alternate id and if it did then try and create and attach. if (!string.IsNullOrWhiteSpace(model.AltId) && ProviderAlternateArticleId.Exists(model.AltId, currentMember.Id.Value) == null) { altId.MemberId = currentMember.Id.Value; altId.AlternateId = model.AltId; altId.ArticleId = anArticle.Id.Value; altId.Save(); } returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.success; returnMessage.StatusMessage = "Article has been submitted successfully."; returnMessage.Content = anArticle.Id.ToString(); } } InsideWordWebLog.Instance.Buffer(from, "Done PublishArticle - " + returnMessage); return Json(returnMessage); }