private void UpdateOrInsertBaseLangElements(WebArticle webarticle) { // ...update insert or update base elements // split text into "word"-strings to get distinct "words" of article without special characters likes spaces, line feeds etc. string[] splitStrings = Regex.Split(webarticle.PlainText.Replace("\n", String.Empty), SpecialCharactersClass.getNonLetterPattern()).Distinct().Where(s => s.Length >= 1).ToArray(); // create new list and transfer plain "word" strings to LangElement-Objects List <LangElement> articleElements = new List <LangElement>(splitStrings.Length); for (int i = 0; i < splitStrings.Length; i++) { string currentString = splitStrings[i]; // check if word exists already in database LangElement foundElement = db.LangElements.Where(l => l.Value.Equals(currentString, StringComparison.OrdinalIgnoreCase) && l.LangId == webarticle.LangId).FirstOrDefault(); if (foundElement == null) { // not found, add word to database foundElement = new LangElement() { LangId = webarticle.LangId, Value = currentString }; db.LangElements.Add(foundElement); db.SaveChanges(); } // link this word also to article to reduce loading times when reading db.WebArticleElements.AddOrUpdate(new WebArticleElement() { WebArticleId = webarticle.ID, LangElementId = foundElement.ID }); db.SaveChanges(); } }
// Map DAL.Article object to WebArticle object public static WebArticle MapArticle(DAL.Article article) { try { if (article == null) { return(null); } WebArticle art = new WebArticle() { Title = article.Title, Description = article.Description, Url = article.Url, UrlToImage = article.UrlToImage, ID = article.ID }; return(art); } catch (Exception ex) { logger.Error(ex, "Attempt to map DAL.Article " + article.ID + " to WebArticle failed: " + ex.Message); return(null); } }
public ActionResult Read(int?id) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); WebArticle webarticle = db.WebArticles.Find(id); if (webarticle == null) { return(HttpNotFound()); } string currentUserId = User.Identity.GetUserId(); ApplicationUser currentUser = db.Users.Where(u => u.Id == currentUserId).FirstOrDefault(); ArticleDecorator decorator = new ArticleDecorator(webarticle); List <ViewArticleElement> viewElements = decorator.GetAllViewElements(); ViewBag.ViewElements = viewElements; return(View(webarticle)); }
public ActionResult DeleteConfirmed(int id) { WebArticle webarticle = db.WebArticles.Find(id); db.WebArticles.Remove(webarticle); db.SaveChanges(); return(RedirectToAction("Index")); }
public JsonResult UpdatePercentage(int id, double percentage) { string currentUserId = User.Identity.GetUserId(); WebArticle a = db.WebArticles.Where(u => u.ID == id && u.UserId == currentUserId).FirstOrDefault(); if (a != null) { a.PercentageKnown = percentage; } ; db.WebArticles.AddOrUpdate(a); db.SaveChanges(); return(Json(a.PercentageKnown, JsonRequestBehavior.AllowGet)); }
// GET: /WebArticles/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } WebArticle webarticle = db.WebArticles.Find(id); if (webarticle == null) { return(HttpNotFound()); } return(View(webarticle)); }
public void Test_GetAllViewElements() { WebArticle article = new WebArticle(); article.ID = 1; article.LangId = 2; article.PlainText = "Hi, my name is Test, how is your name?"; ArticleDecorator decorator = new ArticleDecorator(article, false); // dont access database decorator.Dic.Add("Hi", new LangElement() { Value = "Hi" }); List <ViewArticleElement> viewElements = decorator.GetAllViewElements(); Assert.AreEqual(20, viewElements.Count, "wront element-count"); Assert.AreEqual("Hi", viewElements[0].Value); Assert.AreEqual(false, viewElements[0].IsNotAWord, "declared as non-word"); Assert.AreEqual(",", viewElements[1].Value); Assert.AreEqual(true, viewElements[1].IsNotAWord); Assert.AreEqual(" ", viewElements[2].Value, "no space"); Assert.AreEqual("my", viewElements[3].Value); Assert.AreEqual(" ", viewElements[4].Value); Assert.AreEqual("name", viewElements[5].Value); Assert.AreEqual(" ", viewElements[6].Value); Assert.AreEqual("is", viewElements[7].Value); Assert.AreEqual(" ", viewElements[8].Value); Assert.AreEqual("Test", viewElements[9].Value); Assert.AreEqual(",", viewElements[10].Value); Assert.AreEqual(" ", viewElements[11].Value); Assert.AreEqual("how", viewElements[12].Value); Assert.AreEqual(" ", viewElements[13].Value); Assert.AreEqual("is", viewElements[14].Value); Assert.AreEqual(" ", viewElements[15].Value); Assert.AreEqual("your", viewElements[16].Value); Assert.AreEqual(" ", viewElements[17].Value); Assert.AreEqual("name", viewElements[18].Value); Assert.AreEqual("?", viewElements[19].Value); }
public ActionResult Create([Bind(Include = "ID,LangId,Title,URL,PlainText")] WebArticle webarticle) { if (ModelState.IsValid) { string userId = User.Identity.GetUserId(); webarticle.UserId = userId; if (webarticle.URL != null) { // first, save article to get Id string title; webarticle.PlainText = WebtextExtractor.ExtractTextOnly(webarticle.URL, out title); webarticle.Title = System.Web.HttpUtility.HtmlDecode(webarticle.Title + title); if (webarticle.Title == null) { webarticle.Title = webarticle.URL; } db.WebArticles.Add(webarticle); db.SaveChanges(); // end saving article, id known nown UpdateOrInsertBaseLangElements(webarticle); } else if (webarticle.PlainText != null) { // manuall text db.WebArticles.Add(webarticle); db.SaveChanges(); UpdateOrInsertBaseLangElements(webarticle); } return(RedirectToAction("Read", new { id = webarticle.ID })); } return(View(webarticle)); }
public static SqlDataRecord ConvertToImportArticleDataRecord(this WebArticle article, string transactionId) { if (article == null) { return(null); } try { var mappingid = Guid.NewGuid().ToString().ToUpper(); var record = new SqlDataRecord(Constants.WebArticleStructure); for (var i = 0; i < Constants.WebArticleStructure.Length; i++) { switch (Constants.WebArticleStructure[i].Name) { case "ArticleID": SetStringToDataRecord(ref record, i, article.ArticleID); break; case "ArticleTitle": SetStringToDataRecord(ref record, i, article.ArticleTitle); break; case "ArticleSubtitle": SetStringToDataRecord(ref record, i, string.Empty); break; case "CoverImage": SetStringToDataRecord(ref record, i, article.CoverImage); break; case "AllowComent": SetStringToDataRecord(ref record, i, article.AllowComent); break; case "Visits": SetInt32ToDataRecord(ref record, i, article.Visits); break; case "ArticleStatus": SetStringToDataRecord(ref record, i, article.ArticleStatus); break; case "OverdueTime": SetDateTimeToDataRecord(ref record, i, article.OverdueTime); break; case "HtmlContent": SetStringToDataRecord(ref record, i, article.HtmlContent); break; case "TextContent": SetStringToDataRecord(ref record, i, article.TextContent); break; case "VisitRights": SetStringToDataRecord(ref record, i, article.VisitRights); break; case "Keyword": SetStringToDataRecord(ref record, i, article.Keyword); break; case "ArticleType": SetStringToDataRecord(ref record, i, article.ArticleType); break; case "ArticleTime": SetDateTimeToDataRecord(ref record, i, article.ArticleTime); break; case "ArticleWriter": SetStringToDataRecord(ref record, i, article.ArticleWriter); break; case "ArticleSourceName": SetStringToDataRecord(ref record, i, article.ArticleSourceName); break; case "ArticleSource": SetStringToDataRecord(ref record, i, article.OriginalUrl); break; case "IsDeleted": SetBitToDataRecord(ref record, i, article.IsDeleted); break; case "VersionNo": SetInt32ToDataRecord(ref record, i, article.VersionNo); break; case "TransactionID": SetStringToDataRecord(ref record, i, transactionId); break; case "CreatedBy": SetStringToDataRecord(ref record, i, article.CreatedBy); break; case "CreatedTime": SetDateTimeToDataRecord(ref record, i, article.CreatedTime); break; case "LastUpdatedBy": SetStringToDataRecord(ref record, i, article.LastUpdatedBy); break; case "LastUpdatedTime": SetDateTimeToDataRecord(ref record, i, article.LastUpdatedTime); break; case "ArticleVisit": SetInt32ToDataRecord(ref record, i, article.ArticleVisit); break; case "IsAnswer": SetInt32ToDataRecord(ref record, i, 0); break; case "MappingID": SetStringToDataRecord(ref record, i, mappingid); break; case "SvrID": SetStringToDataRecord(ref record, i, "*"); break; case "AppID": SetStringToDataRecord(ref record, i, "APP.Foreground.WebPortals"); break; case "ChannelID": SetStringToDataRecord(ref record, i, article.PublishToWhichChannel); break; case "Summary": SetStringToDataRecord(ref record, i, article.Summary); break; case "Images": var images = article.Images == null ? string.Empty : string.Join(",", article.Images); SetStringToDataRecord(ref record, i, images); break; case "Category": SetStringToDataRecord(ref record, i, "ContentDetails"); break; case "OpenStyle": SetStringToDataRecord(ref record, i, null); break; case "Url": SetStringToDataRecord(ref record, i, article.OpenUrl); break; case "PublishUserName": SetStringToDataRecord(ref record, i, null); break; } } return(record); } catch (Exception ex) { Logger.Error(ex); return(null); } }