diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs index 50ab591d..97f739a4 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-BreakPoem.cs @@ -18,6 +18,9 @@ namespace RMuseum.Services.Implementation { public async Task> _BreakPoemAsync(RMuseumDbContext context, int poemId, int vOrder, Guid userId, GanjoorPoemCompleteViewModel poem, GanjoorPage parentPage, string poemTitleStaticPart) { + await context.GanjoorPoems.Include(p => p.GanjoorMetre).Where(p => p.Id == poemId).SingleOrDefaultAsync();//waring killer + return new RServiceResult(0, "_BreakPoemAsync is disabled for now because of dependence to _UpdatePageAsync"); + /* try { if (poem.Next == null) @@ -357,7 +360,7 @@ namespace RMuseum.Services.Implementation { return new RServiceResult(0, exp.ToString()); } - + */ } diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-UpdatePage.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-UpdatePage.cs index d2ba278b..849117b8 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-UpdatePage.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-UpdatePage.cs @@ -43,19 +43,6 @@ namespace RMuseum.Services.Implementation HtmlText = dbPage.HtmlText, }; - GanjoorPoem dbPoem = null; - - if (dbPage.GanjoorPageType == GanjoorPageType.PoemPage) - { - dbPoem = await context.GanjoorPoems.Include(p => p.GanjoorMetre).Where(p => p.Id == id).SingleOrDefaultAsync(); - - snapshot.SourceName = dbPoem.SourceName; - snapshot.SourceUrlSlug = dbPoem.SourceUrlSlug; - snapshot.Rhythm = dbPoem.GanjoorMetre == null ? null : dbPoem.GanjoorMetre.Rhythm; - snapshot.RhymeLetters = dbPoem.RhymeLetters; - snapshot.OldTag = dbPoem.OldTag; - snapshot.OldTagPageUrl = dbPoem.OldTagPageUrl; - } context.GanjoorPageSnapshots.Add(snapshot); await context.SaveChangesAsync(); @@ -195,532 +182,7 @@ namespace RMuseum.Services.Implementation } context.GanjoorPages.Update(dbPage); - - if (dbPoem != null) - { - dbPoem.SourceName = pageData.SourceName; - dbPoem.SourceUrlSlug = pageData.SourceUrlSlug; - - - dbPoem.OldTag = pageData.OldTag; - dbPoem.OldTagPageUrl = pageData.OldTagPageUrl; - dbPoem.MixedModeOrder = pageData.MixedModeOrder; - dbPoem.Language = pageData.Language; - dbPoem.Published = pageData.Published; - - - dbPoem.HtmlText = pageData.HtmlText; - dbPoem.Title = pageData.Title; - dbPoem.UrlSlug = pageData.UrlSlug; - dbPoem.FullUrl = dbPage.FullUrl; - dbPoem.FullTitle = dbPoem.FullTitle; - - List verses = _extractVersesFromPoemHtmlText(id, pageData.HtmlText); - - dbPoem.PlainText = PreparePlainText(verses); - - context.GanjoorPoems.Update(dbPoem); - - var sections = await context.GanjoorPoemSections.Where(s => s.PoemId == dbPoem.Id && s.SectionType == PoemSectionType.WholePoem).OrderBy(s => s.VerseType).ToListAsync(); - GanjoorPoemSection mainSection = sections.Count > 0 ? sections[0] : null; - - var oldVerses = await context.GanjoorVerses.Where(v => v.PoemId == id).ToListAsync(); - - if (oldVerses.Count <= verses.Count) - { - for (int v = 0; v < oldVerses.Count; v++) - { - oldVerses[v].Text = verses[v].Text; - oldVerses[v].VersePosition = verses[v].VersePosition; - oldVerses[v].VOrder = verses[v].VOrder; - context.GanjoorVerses.Update(oldVerses[v]); - } - - for (int v = oldVerses.Count; v < verses.Count; v++) - { - if(mainSection != null) - { - if (verses[v].VersePosition != VersePosition.Comment && verses[v].VersePosition != VersePosition.Paragraph) - verses[v].SectionIndex = mainSection.Index; - } - context.GanjoorVerses.Add(verses[v]); - } - } - else - { - for (int v = 0; v < verses.Count; v++) - { - oldVerses[v].Text = verses[v].Text; - oldVerses[v].VersePosition = verses[v].VersePosition; - oldVerses[v].VOrder = verses[v].VOrder; - context.GanjoorVerses.Update(oldVerses[v]); - } - - for (int v = verses.Count; v < oldVerses.Count; v++) - { - context.GanjoorVerses.Remove(oldVerses[v]); - } - } - await context.SaveChangesAsync(); - - await _FillPoemCoupletIndices(context, id); - - if (mainSection != null) - { - int? oldMetreId = mainSection.GanjoorMetreId; - string oldRhymeLetters = mainSection.RhymeLetters; - if (string.IsNullOrEmpty(pageData.Rhythm)) - { - mainSection.GanjoorMetreId = null; - } - else - { - var metre = await context.GanjoorMetres.Where(m => m.Rhythm == pageData.Rhythm).SingleOrDefaultAsync(); - if (metre == null) - { - metre = new GanjoorMetre() - { - Rhythm = pageData.Rhythm, - VerseCount = 0 - }; - context.GanjoorMetres.Add(metre); - await context.SaveChangesAsync(); - } - mainSection.GanjoorMetreId = metre.Id; - } - mainSection.RhymeLetters = pageData.RhymeLetters; - - if (oldMetreId != mainSection.GanjoorMetreId || oldRhymeLetters != mainSection.RhymeLetters) - { - context.Update(mainSection); - await context.SaveChangesAsync(); - - _backgroundTaskQueue.QueueBackgroundWorkItem - ( - async token => - { - using (RMuseumDbContext inlineContext = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so context might be already been freed/collected by GC - { - if (oldMetreId != null && !string.IsNullOrEmpty(oldRhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)oldMetreId, oldRhymeLetters); - await inlineContext.SaveChangesAsync(); - } - - if (mainSection.GanjoorMetreId != null && !string.IsNullOrEmpty(mainSection.RhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)mainSection.GanjoorMetreId, mainSection.RhymeLetters); - await inlineContext.SaveChangesAsync(); - } - } - }); - } - } - - GanjoorPoemSection secondSection = sections.Count > 1 ? sections[1] : null; - if(!string.IsNullOrEmpty(pageData.RhymeLetters) && !string.IsNullOrEmpty(pageData.Rhythm2) && secondSection == null) - { - var allSections = await context.GanjoorPoemSections.AsNoTracking().Where(s => s.PoemId == id).ToListAsync(); - var tempVerses = await context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == id).ToListAsync(); - var maxIndex = allSections.Max(s => s.Index); - secondSection = new GanjoorPoemSection() - { - PoemId = mainSection.PoemId, - PoetId = mainSection.PoetId, - SectionType = PoemSectionType.WholePoem, - VerseType = tempVerses.Where(v => v.SectionIndex != null).Any() == false ? VersePoemSectionType.Second : tempVerses.Where(v => v.ThirdSectionIndex != null).Any() == false ? VersePoemSectionType.Third : VersePoemSectionType.Forth, - Index = maxIndex + 1, - Number = maxIndex + 2, - GanjoorMetreId = null, - RhymeLetters = null, - HtmlText = mainSection.HtmlText, - PlainText = mainSection.PlainText - }; - context.Add(secondSection); - var mainSectionVerses = await context.GanjoorVerses.Where(v => v.PoemId == mainSection.PoemId && v.SectionIndex == mainSection.Index).ToListAsync(); - foreach (var verse in mainSectionVerses) - { - switch (secondSection.VerseType) - { - case VersePoemSectionType.Second: - verse.SecondSectionIndex = secondSection.Index; - break; - case VersePoemSectionType.Third: - verse.ThirdSectionIndex = secondSection.Index; - break; - case VersePoemSectionType.Forth: - verse.ForthSectionIndex = secondSection.Index; - break; - } - } - context.UpdateRange(mainSectionVerses); - await _context.SaveChangesAsync(); - } - if (secondSection != null) - { - int? oldMetreId = secondSection.GanjoorMetreId; - string oldRhymeLetters = secondSection.RhymeLetters; - if (string.IsNullOrEmpty(pageData.Rhythm2)) - { - secondSection.GanjoorMetreId = null; - } - else - { - var metre = await context.GanjoorMetres.Where(m => m.Rhythm == pageData.Rhythm2).SingleOrDefaultAsync(); - if (metre == null) - { - metre = new GanjoorMetre() - { - Rhythm = pageData.Rhythm2, - VerseCount = 0 - }; - context.GanjoorMetres.Add(metre); - await context.SaveChangesAsync(); - } - secondSection.GanjoorMetreId = metre.Id; - } - secondSection.RhymeLetters = pageData.RhymeLetters; - - if (oldMetreId != secondSection.GanjoorMetreId || oldRhymeLetters != secondSection.RhymeLetters) - { - - if(string.IsNullOrEmpty(secondSection.RhymeLetters)) - { - context.Remove(secondSection); - } - else - { - context.Update(secondSection); - } - await context.SaveChangesAsync(); - - _backgroundTaskQueue.QueueBackgroundWorkItem - ( - async token => - { - using (RMuseumDbContext inlineContext = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so context might be already been freed/collected by GC - { - if (oldMetreId != null && !string.IsNullOrEmpty(oldRhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)oldMetreId, oldRhymeLetters); - await inlineContext.SaveChangesAsync(); - } - - if (secondSection.GanjoorMetreId != null && !string.IsNullOrEmpty(secondSection.RhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)secondSection.GanjoorMetreId, secondSection.RhymeLetters); - await inlineContext.SaveChangesAsync(); - } - } - }); - } - } - - GanjoorPoemSection thirdSection = sections.Count > 2 ? sections[2] : null; - if (!string.IsNullOrEmpty(pageData.RhymeLetters) && !string.IsNullOrEmpty(pageData.Rhythm3) && thirdSection == null) - { - var allSections = await context.GanjoorPoemSections.AsNoTracking().Where(s => s.PoemId == id).ToListAsync(); - var tempVerses = await context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == id).ToListAsync(); - var maxIndex = allSections.Max(s => s.Index); - thirdSection = new GanjoorPoemSection() - { - PoemId = mainSection.PoemId, - PoetId = mainSection.PoetId, - SectionType = PoemSectionType.WholePoem, - VerseType = tempVerses.Where(v => v.SectionIndex != null).Any() == false ? VersePoemSectionType.Second : tempVerses.Where(v => v.ThirdSectionIndex != null).Any() == false ? VersePoemSectionType.Third : VersePoemSectionType.Forth, - Index = maxIndex + 1, - Number = maxIndex + 2, - GanjoorMetreId = null, - RhymeLetters = null, - HtmlText = mainSection.HtmlText, - PlainText = mainSection.PlainText - }; - context.Add(thirdSection); - var mainSectionVerses = await context.GanjoorVerses.Where(v => v.PoemId == mainSection.PoemId && v.SectionIndex == mainSection.Index).ToListAsync(); - foreach (var verse in mainSectionVerses) - { - switch (secondSection.VerseType) - { - case VersePoemSectionType.Second: - verse.SecondSectionIndex = thirdSection.Index; - break; - case VersePoemSectionType.Third: - verse.ThirdSectionIndex = thirdSection.Index; - break; - case VersePoemSectionType.Forth: - verse.ForthSectionIndex = thirdSection.Index; - break; - } - } - context.UpdateRange(mainSectionVerses); - await _context.SaveChangesAsync(); - } - if (thirdSection != null) - { - int? oldMetreId = thirdSection.GanjoorMetreId; - string oldRhymeLetters = thirdSection.RhymeLetters; - if (string.IsNullOrEmpty(pageData.Rhythm3)) - { - thirdSection.GanjoorMetreId = null; - } - else - { - var metre = await context.GanjoorMetres.Where(m => m.Rhythm == pageData.Rhythm3).SingleOrDefaultAsync(); - if (metre == null) - { - metre = new GanjoorMetre() - { - Rhythm = pageData.Rhythm3, - VerseCount = 0 - }; - context.GanjoorMetres.Add(metre); - await context.SaveChangesAsync(); - } - thirdSection.GanjoorMetreId = metre.Id; - } - thirdSection.RhymeLetters = pageData.RhymeLetters; - - if (oldMetreId != thirdSection.GanjoorMetreId || oldRhymeLetters != thirdSection.RhymeLetters) - { - if (string.IsNullOrEmpty(thirdSection.RhymeLetters)) - { - context.Remove(thirdSection); - } - else - { - context.Update(thirdSection); - } - await context.SaveChangesAsync(); - - _backgroundTaskQueue.QueueBackgroundWorkItem - ( - async token => - { - using (RMuseumDbContext inlineContext = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so context might be already been freed/collected by GC - { - if (oldMetreId != null && !string.IsNullOrEmpty(oldRhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)oldMetreId, oldRhymeLetters); - await inlineContext.SaveChangesAsync(); - } - - if (thirdSection.GanjoorMetreId != null && !string.IsNullOrEmpty(thirdSection.RhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)thirdSection.GanjoorMetreId, thirdSection.RhymeLetters); - await inlineContext.SaveChangesAsync(); - } - } - }); - } - } - - if(mainSection != null) - { - var nonCommentVerses = await context.GanjoorVerses.Where(v => v.PoemId == id && v.VersePosition != VersePosition.Comment).OrderBy(v => v.VOrder).ToListAsync(); - if (!nonCommentVerses.Where(v => v.VersePosition == VersePosition.Paragraph || v.VersePosition == VersePosition.Single).Any()) - { - if (string.IsNullOrEmpty(mainSection.RhymeLetters)) - { - if (nonCommentVerses.Where(v => - v.VersePosition == VersePosition.CenteredVerse1 - || - v.VersePosition == VersePosition.CenteredVerse2 - ).Any()) - { - //multi-band poem - //TODO: fix for future - } - else - if (_IsMasnavi(nonCommentVerses)) - { - var oldCoupletSections = await context.GanjoorPoemSections.Where(s => s.PoemId == id && s.SectionType == PoemSectionType.Couplet).ToListAsync(); - context.RemoveRange(oldCoupletSections); - await context.SaveChangesAsync(); - _backgroundTaskQueue.QueueBackgroundWorkItem - ( - async token => - { - using (RMuseumDbContext inlineContext = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so context might be already been freed/collected by GC - { - foreach (var oldCoupletSection in oldCoupletSections) - { - if (oldCoupletSection.GanjoorMetreId != null && !string.IsNullOrEmpty(oldCoupletSection.RhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)oldCoupletSection.GanjoorMetreId, oldCoupletSection.RhymeLetters); - await inlineContext.SaveChangesAsync(); - } - } - } - }); - var allSections = await context.GanjoorPoemSections.AsNoTracking().Where(s => s.PoemId == id).ToListAsync(); - var index = allSections.Max(s => s.Index); - for (int v = 0; v < nonCommentVerses.Count; v += 2) - { - index++; - var rightVerse = nonCommentVerses[v]; - var leftVerse = nonCommentVerses[v + 1]; - List coupletVerses = new List(); - coupletVerses.Add(rightVerse); - coupletVerses.Add(leftVerse); - var res = LanguageUtils.FindRhyme(coupletVerses); - - GanjoorPoemSection verseSection = new GanjoorPoemSection() - { - PoemId = id, - PoetId = mainSection.PoetId, - SectionType = PoemSectionType.Couplet, - VerseType = VersePoemSectionType.Second, - Index = index, - Number = index + 1,//couplet number - GanjoorMetreId = mainSection.GanjoorMetreId, - RhymeLetters = res.Rhyme - }; - - rightVerse.SecondSectionIndex = verseSection.Index; - leftVerse.SecondSectionIndex = verseSection.Index; - - var rl = new List(); rl.Add(rightVerse); rl.Add(leftVerse); - verseSection.HtmlText = PrepareHtmlText(rl); - verseSection.PlainText = PreparePlainText(rl); - - context.Add(verseSection); - - if (!string.IsNullOrEmpty(pageData.Rhythm2)) - { - index++; - var metre = await context.GanjoorMetres.Where(m => m.Rhythm == pageData.Rhythm2).SingleOrDefaultAsync(); - if (metre == null) - { - metre = new GanjoorMetre() - { - Rhythm = pageData.Rhythm2, - VerseCount = 0 - }; - context.GanjoorMetres.Add(metre); - await context.SaveChangesAsync(); - } - var secondMeterId = metre.Id; - - GanjoorPoemSection secondVerseSection = new GanjoorPoemSection() - { - PoemId = id, - PoetId = mainSection.PoetId, - SectionType = PoemSectionType.Couplet, - VerseType = VersePoemSectionType.Third, - Index = index, - Number = index + 1,//couplet number - GanjoorMetreId = secondMeterId, - RhymeLetters = res.Rhyme, - HtmlText = verseSection.HtmlText, - PlainText = verseSection.PlainText - }; - context.Add(secondVerseSection); - - rightVerse.ThirdSectionIndex = secondVerseSection.Index; - leftVerse.ThirdSectionIndex = secondVerseSection.Index; - } - - if (!string.IsNullOrEmpty(pageData.Rhythm3)) - { - index++; - var metre = await context.GanjoorMetres.Where(m => m.Rhythm == pageData.Rhythm3).SingleOrDefaultAsync(); - if (metre == null) - { - metre = new GanjoorMetre() - { - Rhythm = pageData.Rhythm2, - VerseCount = 0 - }; - context.GanjoorMetres.Add(metre); - await context.SaveChangesAsync(); - } - var thirdMeterId = metre.Id; - - GanjoorPoemSection thirdVerseSection = new GanjoorPoemSection() - { - PoemId = id, - PoetId = mainSection.PoetId, - SectionType = PoemSectionType.Couplet, - VerseType = VersePoemSectionType.Forth, - Index = index, - Number = index + 1,//couplet number - GanjoorMetreId = thirdMeterId, - RhymeLetters = res.Rhyme, - HtmlText = verseSection.HtmlText, - PlainText = verseSection.PlainText - }; - context.Add(thirdVerseSection); - - rightVerse.ForthSectionIndex = thirdVerseSection.Index; - leftVerse.ForthSectionIndex = thirdVerseSection.Index; - } - } - context.UpdateRange(nonCommentVerses); - await context.SaveChangesAsync(); - - var newCoupletSections = await context.GanjoorPoemSections.Where(s => s.PoemId == id && s.SectionType == PoemSectionType.Couplet).ToListAsync(); - - _backgroundTaskQueue.QueueBackgroundWorkItem - ( - async token => - { - using (RMuseumDbContext inlineContext = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so context might be already been freed/collected by GC - { - foreach (var newCoupletSection in newCoupletSections) - { - if (newCoupletSection.GanjoorMetreId != null && !string.IsNullOrEmpty(newCoupletSection.RhymeLetters)) - { - await _UpdateRelatedSections(inlineContext, (int)newCoupletSection.GanjoorMetreId, newCoupletSection.RhymeLetters); - await inlineContext.SaveChangesAsync(); - } - } - } - }); - - } - } - } - - } - - var mySections = await context.GanjoorPoemSections.Where(s => s.PoemId == id).ToListAsync(); - foreach (var section in mySections) - { - var verseSections = await context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == id - && - ( - (section.VerseType == VersePoemSectionType.First && v.SectionIndex == section.Index) - || - (section.VerseType == VersePoemSectionType.Second && v.SecondSectionIndex == section.Index) - || - (section.VerseType == VersePoemSectionType.Third && v.ThirdSectionIndex == section.Index) - || - (section.VerseType == VersePoemSectionType.Forth && v.ForthSectionIndex == section.Index) - ) - ).OrderBy(v => v.VOrder).ToListAsync(); - section.HtmlText = PrepareHtmlText(verseSections); - section.PlainText = PreparePlainText(verseSections); - } - context.UpdateRange(mySections); - await context.SaveChangesAsync(); - - - - var excerptsInRelatedCaches = await context.GanjoorCachedRelatedSections.Where(p => p.TargetPoemId == id).ToListAsync(); - if (excerptsInRelatedCaches.Count > 0) - { - foreach (var excerptsInRelatedCache in excerptsInRelatedCaches) - { - var targetSection = mySections.Where(s => s.Index == excerptsInRelatedCache.TargetSectionIndex).SingleOrDefault(); - if(targetSection != null) - { - excerptsInRelatedCache.HtmlExcerpt = GetPoemHtmlExcerpt(targetSection.HtmlText); - } - } - context.GanjoorCachedRelatedSections.UpdateRange(excerptsInRelatedCaches); - } - - } + await context.SaveChangesAsync(); CacheCleanForPageByUrl(dbPage.FullUrl);