Пример #1
0
        public void FillByCSV(string fileName, Action <int, string, string, bool> callBack)
        {
            var csvReader = new CsvReader(fileName);

            SetLanguages();
            var wordsQuery = new WordsQuery();

            int i = 0;

            do
            {
                string[] line = csvReader.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (line.Length < 2)
                {
                    continue;
                }

                string   source       = line[0].Trim();
                string[] translations = line[1].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string translation in translations)
                {
                    WordWithTranslation wordWithTranslation = wordsQuery.GetOrCreate(
                        new PronunciationForUser(IdValidator.INVALID_ID, source, false, _englishLanguageId),
                        new PronunciationForUser(IdValidator.INVALID_ID, translation, false, _russianLanguageId), null,
                        WordType.PhrasalVerb);
                    callBack(++i, source, translation, wordWithTranslation != null);
                }
            } while (true);
        }
Пример #2
0
        public bool Create(WordWithTranslation wordWithTranslation)
        {
            bool result = true;

            foreach (PronunciationForUser translation in wordWithTranslation.Translations)
            {
                WordWithTranslation createdWordWithTranslation = GetOrCreate(wordWithTranslation.Source, translation,
                                                                             null, wordWithTranslation.WordType);
                result &= createdWordWithTranslation != null;
            }
            return(result);
        }
Пример #3
0
        /// <summary>
        /// Создает слова для группы
        /// </summary>
        /// <param name="groupForUser">группа, к которой нужно добавить слово</param>
        /// <param name="source">слово</param>
        /// <param name="translation">перевод</param>
        /// <param name="image">изображение для слова</param>
        /// <param name="rating">рейтинг</param>
        /// <returns>созданные слова для группы, или ничего</returns>
        public SourceWithTranslation GetOrCreate(GroupForUser groupForUser,
                                                 PronunciationForUser source,
                                                 PronunciationForUser translation,
                                                 byte[] image,
                                                 int?rating)
        {
            var wordsQuery = new WordsQuery();
            WordWithTranslation wordWithTranslation = wordsQuery.GetOrCreate(source, translation, image,
                                                                             WordType.Default, null);

            if (wordWithTranslation == null)
            {
                return(null);
            }
            SourceWithTranslation result = Adapter.ReadByContext(c => {
                var wordsWithTranslationsQuery = (from w1 in c.Word
                                                  join wt in c.WordTranslation on w1.Id equals wt.WordId1
                                                  join gw in c.GroupWord on wt.Id equals gw.WordTranslationId
                                                  join w2 in c.Word on wt.WordId2 equals w2.Id
                                                  where gw.GroupId == groupForUser.Id && wt.Id == wordWithTranslation.Id
                                                  select new { gw, wt, w1, w2 });
                var firstRecord = wordsWithTranslationsQuery.AsEnumerable().FirstOrDefault();
                if (firstRecord == null)
                {
                    return(null);
                }
                //сохранить возможно изменившийся рейтинг
                GroupWord groupWord = firstRecord.gw;
                groupWord.Rating    = rating;
                c.SaveChanges();

                SourceWithTranslation innerResult = ConverterEntities.ConvertToSourceWithTranslation(firstRecord.wt.Id,
                                                                                                     firstRecord.wt.
                                                                                                     Image,
                                                                                                     wordWithTranslation
                                                                                                     .Source.
                                                                                                     LanguageId,
                                                                                                     firstRecord.w1,
                                                                                                     firstRecord.w2);
                return(innerResult);
            });

            if (result == null)
            {
                result = Create(groupForUser, wordWithTranslation, rating);
            }
            return(result);
        }
Пример #4
0
        private bool CreateWordWithTranslation(string englishWord,
                                               IEnumerable <string> translatedWords,
                                               WordType wordType)
        {
            string sourceText          = englishWord;
            var    wordWithTranslation = new WordWithTranslation(GetWord(_englishLanguageId, sourceText));

            foreach (string translatedWord in translatedWords)
            {
                wordWithTranslation.AddTranslation(GetWord(_russianLanguageId, translatedWord));
            }

            bool result = _words.Create(wordWithTranslation);

            return(result);
        }
Пример #5
0
        /// <summary>
        /// Создает слова по типу популярности
        /// </summary>
        /// <param name="source">слово</param>
        /// <param name="translation">перевод</param>
        /// <param name="type">тип популярности</param>
        /// <returns>созданные слова для группы, или ничего</returns>
        public SourceWithTranslation GetOrCreate(PronunciationForUser source,
                                                 PronunciationForUser translation,
                                                 PopularWordType type)
        {
            var wordsQuery = new WordsQuery();
            WordWithTranslation wordWithTranslation = wordsQuery.GetOrCreate(source, translation, null, WordType.Default,
                                                                             null);

            if (wordWithTranslation == null)
            {
                return(null);
            }
            var parsedType = (int)type;
            SourceWithTranslation result = Adapter.ReadByContext(c => {
                var wordsWithTranslationsQuery = (from w1 in c.Word
                                                  join wt in c.WordTranslation on w1.Id equals wt.WordId1
                                                  join pw in c.PopularWord on wt.Id equals pw.WordTranslationId
                                                  join w2 in c.Word on wt.WordId2 equals w2.Id
                                                  where pw.Type == parsedType && wt.Id == wordWithTranslation.Id
                                                  select new { wt, w1, w2 });
                var firstRecord = wordsWithTranslationsQuery.AsEnumerable().FirstOrDefault();
                if (firstRecord == null)
                {
                    return(null);
                }

                SourceWithTranslation innerResult = ConverterEntities.ConvertToSourceWithTranslation(firstRecord.wt.Id,
                                                                                                     firstRecord.wt.
                                                                                                     Image,
                                                                                                     wordWithTranslation
                                                                                                     .Source.
                                                                                                     LanguageId,
                                                                                                     firstRecord.w1,
                                                                                                     firstRecord.w2);
                return(innerResult);
            });

            if (result == null)
            {
                result = Create(wordWithTranslation, parsedType);
            }
            return(result);
        }
Пример #6
0
        private SourceWithTranslation Create(WordWithTranslation wordWithTranslation, int type)
        {
            SourceWithTranslation result = null;

            Adapter.ActionByContext(context => {
                var popularWord = new PopularWord
                {
                    WordTranslationId = wordWithTranslation.Id, Type = type
                };
                context.PopularWord.Add(popularWord);
                context.SaveChanges();
                if (IdValidator.IsValid(popularWord.Id))
                {
                    result = new SourceWithTranslation();
                    result.Set(popularWord.Id, wordWithTranslation.Source, wordWithTranslation.Translations[0]);
                }
            });
            return(result);
        }
Пример #7
0
        private SourceWithTranslation Create(GroupForUser groupForUser,
                                             WordWithTranslation wordWithTranslation,
                                             int?rating)
        {
            SourceWithTranslation result = null;

            Adapter.ActionByContext(context => {
                var groupWord = new GroupWord
                {
                    WordTranslationId = wordWithTranslation.Id, GroupId = groupForUser.Id, Rating = rating
                };
                context.GroupWord.Add(groupWord);
                context.SaveChanges();
                if (IdValidator.IsValid(groupWord.Id))
                {
                    result = new SourceWithTranslation();
                    result.Set(groupWord.Id, wordWithTranslation.Source, wordWithTranslation.Translations[0]);
                }
            });
            return(result);
        }
Пример #8
0
        public long GetIdByWordsForUser(PronunciationForUser source, PronunciationForUser translation)
        {
            WordWithTranslation wrd = GetOrCreate(source, translation, null);

            return(wrd != null ? wrd.Id : IdValidator.INVALID_ID);
        }
Пример #9
0
        public WordWithTranslation GetOrCreate(PronunciationForUser source,
                                               PronunciationForUser translation,
                                               byte[] image,
                                               WordType wordType = WordType.Default,
                                               int?rating        = null)
        {
            //TODO: проверять корректность параметров
            WordWithTranslation result = null;

            Adapter.ActionByContext(context => {
                IEnumerable <Word> sourceWords = FindWords(context, new List <PronunciationForUser> {
                    source
                });
                IEnumerable <Word> translateWords = FindWords(context, new List <PronunciationForUser> {
                    translation
                });

                Word sourceWord      = GetOrAddWord(source, wordType, sourceWords, context);
                Word translationWord = GetOrAddWord(translation, wordType, translateWords, context);
                context.SaveChanges();

                if (sourceWord == null || IdValidator.IsInvalid(sourceWord.Id) || translationWord == null ||
                    IdValidator.IsInvalid(translationWord.Id))
                {
                    //все слова добавлены - если нужно, добавить связь между новым словом и переводом для него
                    LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat(
                        "WordsQuery.GetOrCreate can't add text {0} with translation {1}, image {2}, wordType {3}, rating {4}",
                        source.Text, translation.Text,
                        image != null ? image.Length.ToString(CultureInfo.InvariantCulture) : "<NULL>", wordType, rating);
                    result = null;
                    return;
                }

                WordTranslation wordTranslation = GetWordTranslation(context, sourceWord, translationWord);
                if (wordTranslation != null)
                {
                    //обновить возможно изменившийся рейтинг и изображение
                    wordTranslation.Image  = image;
                    wordTranslation.Rating = rating;
                    context.SaveChanges();
                }
                else
                {
                    wordTranslation = Create(image, rating, context, sourceWord, translationWord);
                }

                if (IdValidator.IsValid(wordTranslation.Id))
                {
                    result = new WordWithTranslation(sourceWord)
                    {
                        Id = wordTranslation.Id
                    };
                    result.AddTranslation(translationWord);
                }
                else
                {
                    LoggerWrapper.LogTo(LoggerName.Errors).ErrorFormat(
                        "WordsQuery.GetOrCreate can't add/get wordTranslation for text {0} with translation {1}, image {2}, wordType {3}, rating {4}",
                        source.Text, translation.Text,
                        image != null ? image.Length.ToString(CultureInfo.InvariantCulture) : "<NULL>", wordType, rating);
                }
            });
            return(result);
        }