private static void DeleteOldInfos(StudyLanguageContext c, ComparisonForUser comparisonForUser) { if (comparisonForUser == null) { return; } var itemsIds = new List <long>(); foreach (ComparisonItemForUser item in comparisonForUser.Items) { long itemId = item.Id; var rulesIds = new List <long>(); foreach (ComparisonRuleForUser rule in item.Rules) { long ruleId = rule.Id; DeleteRuleExamples(c, ruleId, rule.Examples.Select(e => e.Id).ToList()); rulesIds.Add(ruleId); } DeleteRules(c, itemId, rulesIds); itemsIds.Add(itemId); } DeleteItems(c, comparisonForUser.Id, itemsIds); }
private static ComparisonForUser GetComparisonForUser(UserLanguages userLanguages, string group) { long languageId = WebSettingsConfig.Instance.GetLanguageFromId(); var comparisonsQuery = new ComparisonsQuery(languageId); ComparisonForUser comparisonForUser = comparisonsQuery.GetWithFullInfo(userLanguages, group); return(comparisonForUser); }
public void Create(string fileName) { if (!_isLoaded) { _isLoaded = true; Load(); } var reader = new StreamReader(fileName, Encoding.UTF8); XDocument xDocument = XDocument.Load(reader); XElement xComparison = xDocument.Element("comparison"); string title = GetChildElementValue(xComparison, "title"); string description = GetChildElementValue(xComparison, "description"); var comparisonForUser = new ComparisonForUser(TextFormatter.FirstUpperCharAndTrim(title), TextFormatter.FirstUpperCharAndTrim(description)); IEnumerable <XElement> info = GetChildElements(xComparison, "additionalInfo"); foreach (XElement xElement in info.Elements()) { var type = (AdditionalType)Enum.Parse(typeof(AdditionalType), xElement.Name.ToString()); string value = TextFormatter.FirstUpperCharAndTrim(xElement.Value); comparisonForUser.AddAdditional(type, value); } XElement xItems = GetChildElement(xComparison, "items"); foreach (XElement xItem in GetChildElements(xItems, "item")) { ComparisonItemForUser comparisonItem = GetComparisonItemForUser(xItem); comparisonForUser.AddItem(comparisonItem); } var comparisonsQuery = new ComparisonsQuery(_englishId); ComparisonForUser savedComparisonForUser = comparisonsQuery.GetOrCreate(comparisonForUser); if (savedComparisonForUser != null) { Console.WriteLine("Сохранена группа сравнений {0}", fileName); } else { Console.WriteLine("Не удалось сохранить группу сравнений {0}", fileName); } }
public ActionResult Download(UserLanguages userLanguages, string group, DocumentType type) { if (UserLanguages.IsInvalid(userLanguages)) { return(RedirectToAction("Index", RouteConfig.GROUPS_BY_COMPARISONS_CONTROLLER)); } ComparisonForUser comparisonForUser = GetComparisonForUser(userLanguages, group); if (comparisonForUser == null) { return(RedirectToActionPermanent("Index", RouteConfig.GROUPS_BY_COMPARISONS_CONTROLLER)); } var downloader = new ComparisonDownloader(WebSettingsConfig.Instance.DomainWithProtocol, CommonConstants.GetFontPath(Server)); var documentGenerator = downloader.Download(type, comparisonForUser.Title, comparisonForUser); return(File(documentGenerator.Generate(), documentGenerator.ContentType, documentGenerator.FileName)); }
public ActionResult Index(UserLanguages userLanguages, string group) { if (WebSettingsConfig.Instance.IsSectionForbidden(SectionId.FillDifference)) { return(RedirectToAction("Index", RouteConfig.MAIN_CONTROLLER_NAME)); } if (UserLanguages.IsInvalid(userLanguages)) { return(RedirectToAction("Index", RouteConfig.GROUPS_BY_COMPARISONS_CONTROLLER)); } ComparisonForUser comparisonForUser = GetComparisonForUser(userLanguages, group); if (comparisonForUser == null) { return(RedirectToActionPermanent("Index", RouteConfig.GROUPS_BY_COMPARISONS_CONTROLLER)); } return(View(comparisonForUser)); }
private GroupComparison GetOrCreateGroupComparison(ComparisonForUser comparisonForUser, StudyLanguageContext c) { GroupComparison groupComparison = c.GroupComparison.FirstOrDefault(e => e.Title == comparisonForUser.Title && e.LanguageId == _languageId); if (groupComparison == null) { groupComparison = new GroupComparison { Title = comparisonForUser.Title, LanguageId = _languageId }; c.GroupComparison.Add(groupComparison); } groupComparison.AdditionalInfo = comparisonForUser.GetAdditionalInfo(); groupComparison.Description = comparisonForUser.Description; groupComparison.LastModified = DateTime.Now; c.SaveChanges(); return(groupComparison); }
/// <summary> /// Получает группу сравнения по названию /// </summary> /// <param name="userLanguages">языковые настройки пользователя</param> /// <param name="title">название представления</param> /// <returns>представление или null если не найдено</returns> public ComparisonForUser GetWithFullInfo(UserLanguages userLanguages, string title) { long sourceLanguageId = userLanguages.From.Id; long translationLanguageId = userLanguages.To.Id; ComparisonForUser result = Adapter.ReadByContext(c => { var comparisonsQuery = (from gc in c.GroupComparison join ci in c.ComparisonItem on gc.Id equals ci.GroupComparisonId join cr in c.ComparisonRule on ci.Id equals cr.ComparisonItemId join cre in c.ComparisonRuleExample on cr.Id equals cre.ComparisonRuleId join st in c.SentenceTranslation on cre.SentenceTranslationId equals st.Id join s1 in c.Sentence on st.SentenceId1 equals s1.Id join s2 in c.Sentence on st.SentenceId2 equals s2.Id where gc.Title == title && gc.LanguageId == _languageId && ((s1.LanguageId == sourceLanguageId && s2.LanguageId == translationLanguageId) || (s1.LanguageId == translationLanguageId && s2.LanguageId == sourceLanguageId)) orderby ci.Order, cr.Order, cre.Order select new { gc, ci, cr, cre, st, s1, s2 }); var comparisonsInfos = comparisonsQuery.AsEnumerable(); var firstComparison = comparisonsInfos.FirstOrDefault(); if (firstComparison == null) { return(null); } var innerResult = new ComparisonForUser(firstComparison.gc); long prevComparisonItemId = IdValidator.INVALID_ID; long prevComparisonRuleId = IdValidator.INVALID_ID; ComparisonItemForUser comparisonItemForUser = null; ComparisonRuleForUser comparisonRuleForUser = null; foreach (var comparisonInfo in comparisonsInfos) { ComparisonItem comparisonItem = comparisonInfo.ci; if (prevComparisonItemId != comparisonItem.Id) { prevComparisonItemId = comparisonItem.Id; if (comparisonItemForUser != null) { innerResult.AddItem(comparisonItemForUser); } comparisonItemForUser = new ComparisonItemForUser(comparisonItem); } ComparisonRule comparisonRule = comparisonInfo.cr; if (comparisonRule.Id != prevComparisonRuleId) { prevComparisonRuleId = comparisonRule.Id; comparisonRuleForUser = new ComparisonRuleForUser(comparisonRule); comparisonItemForUser.AddRule(comparisonRuleForUser); } SourceWithTranslation sourceWithTranslation = ConverterEntities.ConvertToSourceWithTranslation(comparisonInfo.st.Id, comparisonInfo.st.Image, comparisonInfo.s1.LanguageId, comparisonInfo.s1, comparisonInfo.s2); sourceWithTranslation.IsCurrent = false; var example = new ComparisonRuleExampleForUser(comparisonInfo.cre, sourceWithTranslation); comparisonRuleForUser.AddExample(example); } if (comparisonItemForUser != null) { innerResult.AddItem(comparisonItemForUser); } return(innerResult); }); return(result); }
public ComparisonForUser GetOrCreate(ComparisonForUser comparisonForUser) { if (!comparisonForUser.IsValid()) { return(null); } bool isSuccess = true; ComparisonForUser result = null; Adapter.ActionByContext(c => { GroupComparison groupComparison = GetOrCreateGroupComparison(comparisonForUser, c); if (IdValidator.IsInvalid(groupComparison.Id)) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "ComparisonsQuery.GetOrCreate не удалось создать! Название: {0}, описание: {1}", comparisonForUser.Title, comparisonForUser.Description); isSuccess = false; return; } result = new ComparisonForUser(groupComparison); int orderItem = 1; foreach (ComparisonItemForUser comparisonItemForUser in comparisonForUser.Items) { ComparisonItem comparisonItem = GetOrCreateComparisonItem(comparisonItemForUser, groupComparison.Id, orderItem++, c); if (IdValidator.IsInvalid(comparisonItem.Id)) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "ComparisonsQuery.GetOrCreate не удалось создать пункт для сравнения! " + "Id сравнения: {0}, название {1}, перевод названия {2}, описание {3}", groupComparison.Id, comparisonItemForUser.Title, comparisonItemForUser.TitleTranslated, comparisonItemForUser.Description); isSuccess = false; continue; } var newComparisonItemForUser = new ComparisonItemForUser(comparisonItem); result.AddItem(newComparisonItemForUser); int orderRule = 1; foreach (ComparisonRuleForUser comparisonRuleForUser in comparisonItemForUser.Rules) { ComparisonRule comparisonRule = GetOrCreateComparisonRule(comparisonRuleForUser, comparisonItem.Id, orderRule++, c); long ruleId = comparisonRule.Id; if (IdValidator.IsInvalid(ruleId)) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat( "ComparisonsQuery.GetOrCreate не удалось создать правило для сравнения! " + "Id пункта сравнения: {0}, описание {1}", comparisonItem.Id, comparisonRule.Description); isSuccess = false; continue; } var newComparisonRuleForUser = new ComparisonRuleForUser(comparisonRule); newComparisonItemForUser.AddRule(newComparisonRuleForUser); isSuccess = EnumerableValidator.IsNotEmpty(comparisonRuleForUser.Examples) && CreateExamples(comparisonRuleForUser.Examples, newComparisonRuleForUser, c); } } if (isSuccess) { //удалить пункты, правила, примеры, которые не были переданы в этот раз DeleteOldInfos(c, result); } }); return(isSuccess && result != null && result.IsValid() ? result : null); }
public DocumentationGenerator Download(DocumentType docType, string fileName, ComparisonForUser comparisonForUser) { string header = comparisonForUser.Title; DocumentationGenerator documentGenerator = DocumentationGenerator.Create(_domain, _fontPath, docType, fileName); documentGenerator.AddHeader(header, false); if (comparisonForUser.HasDescription) { documentGenerator.AddParagraph(TextFormatter.AppendCharIfNeed(comparisonForUser.Description)); } foreach (ComparisonItemForUser item in comparisonForUser.Items) { string title = item.Title; if (item.HasTranslatedTitle) { title += " – " + item.TitleTranslated; } documentGenerator.AddSubheader(title); documentGenerator.AddParagraph(item.GetRuleHeader()); int ruleNumber = 1; foreach (ComparisonRuleForUser rule in item.Rules) { string ruleDescription = TextFormatter.AppendCharIfNeed(rule.Description); if (!item.IsOneRule) { ruleDescription = ruleNumber + ". " + ruleDescription; ruleNumber++; } documentGenerator.AddParagraph(ruleDescription); if (EnumerableValidator.IsNotEmpty(rule.Examples)) { foreach (ComparisonRuleExampleForUser example in @rule.Examples) { documentGenerator.AddParagraph(example.Example.Source.Text + " – " + example.Example.Translation.Text, new TextFormat { CountLeftPaddings = 1 }); if (!string.IsNullOrWhiteSpace(example.Description)) { documentGenerator.AddParagraph("Пояснение к примеру: " + TextFormatter.AppendCharIfNeed(example.Description), new TextFormat { IsItalic = true, CountLeftPaddings = 1 }); } } } } } return(documentGenerator); }
private DocumentationGenerator GetGenerator(AllMaterialsQuery allMaterialsQuery, SectionId sectionId, long id, string title) { DocumentationGenerator result; switch (sectionId) { case SectionId.GroupByWords: var groupWordsDownloader = new GroupDataDownloader(_domain, _fontPath) { Header = string.Format("Слова на тему «{0}»", title), TableHeader = "Слово" }; List <SourceWithTranslation> words = allMaterialsQuery.GetWordsByGroup(id); result = groupWordsDownloader.Download(DOCUMENT_TYPE, title, words); break; case SectionId.GroupByPhrases: var groupDataDownloader = new GroupDataDownloader(_domain, _fontPath) { Header = string.Format("Фразы на тему «{0}»", title), TableHeader = "Фраза" }; List <SourceWithTranslation> sentences = allMaterialsQuery.GetSentencesByGroup(id); result = groupDataDownloader.Download(DOCUMENT_TYPE, title, sentences); break; case SectionId.VisualDictionary: var visualDictionaryDownloader = new VisualDictionaryDownloader(_domain, _fontPath); RepresentationForUser representationForUser = allMaterialsQuery.GetVisualDictionary(title); result = visualDictionaryDownloader.Download(DOCUMENT_TYPE, title, representationForUser); break; case SectionId.FillDifference: var comparisonDownloader = new ComparisonDownloader(_domain, _fontPath); ComparisonForUser comparisonForUser = allMaterialsQuery.GetComparison(title); result = comparisonDownloader.Download(DOCUMENT_TYPE, title, comparisonForUser); break; case SectionId.Video: var videoTextDownloader = new VideoTextDownloader(_domain, _fontPath); VideoForUser videoForUser = allMaterialsQuery.GetVideo(title); result = videoTextDownloader.Download(DOCUMENT_TYPE, title, videoForUser); break; case SectionId.PopularWord: var popularWordsDownloader = new PopularWordsDownloader(_domain, _fontPath) { Header = "Минилекс слов Гуннемарка" }; List <SourceWithTranslation> popularWords = allMaterialsQuery.GetPopularWords(); result = popularWordsDownloader.Download(DOCUMENT_TYPE, title, popularWords); break; default: result = null; break; } return(result); }