Ejemplo n.º 1
0
        /// <summary>
        /// Called when the AddToSrsCommand is fired.
        /// Opens the SRS entry window.
        /// </summary>
        private void OnAddToSrs()
        {
            // Prepare the new entry.
            SrsEntry entry = new SrsEntry();

            entry.LoadFromKanji(_kanjiEntity.DbKanji);

            // Show the modal entry edition window.
            EditSrsEntryWindow wnd = new EditSrsEntryWindow(entry);

            wnd.ShowDialog();

            // When it is closed, get the result.
            ExtendedSrsEntry result = wnd.Result;

            if (wnd.IsSaved && result != null &&
                result.AssociatedKanji == _kanjiEntity.DbKanji.Character)
            {
                // The result exists and is still associated with this kanji.
                // We can use it in this ViewModel.
                SrsEntry = result;
            }
        }
        /// <summary>
        /// Reads an SRS item from a field row using the parameters of the view model.
        /// </summary>
        /// <param name="row">Row to read.</param>
        /// <param name="log">Log under the form of a stringbuilder to inform the user about how everything goes.</param>
        /// <returns>The SRS item read if successful. A null value otherwise.</returns>
        private SrsEntry ReadEntry(List <string> row, VocabDao vocabDao, KanjiDao kanjiDao, StringBuilder log)
        {
            try
            {
                SrsEntry entry        = new SrsEntry();
                string   kanjiReading = row[_kanjiReadingColumn];
                if (string.IsNullOrEmpty(kanjiReading))
                {
                    log.AppendFormat("Empty kanji reading. Skipping.");
                    return(null);
                }

                // Figure out item type.
                CsvItemType itemType = ReadItemType(row);
                if (itemType == CsvItemType.Kanji ||
                    (itemType == CsvItemType.Unspecified &&
                     (_noTypeBehavior == CsvImportNoTypeBehavior.AllKanji ||
                      (_noTypeBehavior == CsvImportNoTypeBehavior.Auto && kanjiReading.Length == 1))))
                {
                    // Three solutions to set as kanji:
                    // 1. Item is manually specified as kanji in source data.
                    // 2. Item is not specified and no type behavior is set to AllKanji.
                    // 3. Item is not specified and no type behavior is set to Auto and the length of kanjiReading is exactly 1.
                    entry.AssociatedKanji = kanjiReading;
                    log.AppendFormat("Kanji: \"{0}\". ", kanjiReading);
                    itemType = CsvItemType.Kanji;
                }
                else
                {
                    // All other cases will lead to vocab.
                    entry.AssociatedVocab = kanjiReading;
                    log.AppendFormat("Vocab: \"{0}\". ", kanjiReading);
                    itemType = CsvItemType.Vocab;
                }

                string readings = ReadAcceptedReadings(row);

                long?sequenceNumber = ReadSequenceNumber(row);

                if (ReadingAutofill || MeaningAutofill)
                {
                    switch (itemType)
                    {
                    case CsvItemType.Kanji:
                        var kanji = kanjiDao.GetFirstMatchingKanji(kanjiReading);
                        if (kanji == null)
                        {
                            log.Append("Can't find kanji in database. Skipping.");
                            return(null);
                        }
                        entry.LoadFromKanji(kanji);
                        break;

                    case CsvItemType.Vocab:
                        var vocabs = string.IsNullOrEmpty(readings) ? vocabDao.GetMatchingVocab(kanjiReading)
                                        : vocabDao.GetVocabByReadings(kanjiReading, readings);
                        if (sequenceNumber.HasValue)
                        {
                            vocabs = vocabs.Where(v => v.Seq == sequenceNumber);
                        }
                        var vocab = vocabs.FirstOrDefault();
                        if (vocab == null)
                        {
                            log.Append("Can't find vocab in database. Skipping.");
                            return(null);
                        }
                        entry.LoadFromVocab(vocab);
                        entry.AssociatedVocab = kanjiReading;
                        entry.Readings        = default(string);
                        break;
                    }
                }

                // Find readings.
                if (!ReadingAutofill || !string.IsNullOrEmpty(readings))
                {
                    entry.Readings = readings;
                }
                if (itemType == CsvItemType.Kanji && string.IsNullOrEmpty(entry.Readings))
                {
                    log.Append("Empty readings. Skipping.");
                    return(null);
                }

                // Find meanings.
                string meanings = ReadAcceptedMeanings(row);
                if (!MeaningAutofill || !string.IsNullOrEmpty(meanings))
                {
                    entry.Meanings = meanings;
                }
                if (string.IsNullOrEmpty(entry.Meanings))
                {
                    log.Append("Empty meanings. Skipping.");
                    return(null);
                }

                // Find all optional info.
                entry.MeaningNote    = ReadMeaningNotes(row);
                entry.ReadingNote    = ReadReadingNotes(row);
                entry.Tags           = ReadTags(row);
                entry.CurrentGrade   = ReadStartLevel(row);
                entry.NextAnswerDate = ReadNextReviewDate(row);

                log.Append("OK.");
                return(entry);
            }
            catch (Exception ex)
            {
                log.AppendFormat("Unknown error: \"{0}\". Skipping.", ex.Message);
            }

            return(null);
        }