Beispiel #1
0
        /// <summary>
        /// Loads properties from the given vocab entity.
        /// </summary>
        /// <param name="se">Target SRS entry.</param>
        /// <param name="v">Vocab to load.</param>
        public static void LoadFromVocab(this SrsEntry se, VocabEntity v)
        {
            // Compute the meaning string.
            string meaningString = string.Empty;

            foreach (VocabMeaning vm in v.Meanings)
            {
                meaningString += MultiValueFieldHelper.ReplaceSeparator(vm.Meaning)
                                 .Replace(" ;", MultiValueFieldHelper.ValueSeparator.ToString())
                                 + MultiValueFieldHelper.ValueSeparator;
            }
            meaningString = meaningString.Trim(
                new char[] { MultiValueFieldHelper.ValueSeparator });
            meaningString = MultiValueFieldHelper.Trim(meaningString);
            meaningString = MultiValueFieldHelper.Expand(meaningString);

            // Set values.
            se.Meanings        = meaningString;
            se.Readings        = v.KanaWriting;
            se.AssociatedVocab = v.KanjiWriting;

            if (string.IsNullOrEmpty(se.AssociatedVocab))
            {
                se.AssociatedVocab = v.KanaWriting;
            }
        }
        public VocabAudio(SrsEntry entry)
        {
            KanjiReading = entry.AssociatedVocab;
            KanaReading  = MultiValueFieldHelper.Trim(entry.Readings)
                           .Split(MultiValueFieldHelper.ValueSeparator)
                           .FirstOrDefault();

            if (string.IsNullOrWhiteSpace(KanaReading) || string.IsNullOrWhiteSpace(KanjiReading))
            {
                State = VocabAudioState.Unavailable;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Event callback designed to close the window when the edition is over.
        /// </summary>
        private void OnFinishedEditing(object sender, SrsEntryEditedEventArgs e)
        {
            Result = e.SrsEntry;

            if (e.SrsEntry != null)
            {
                e.SrsEntry.Meanings = MultiValueFieldHelper.Trim(e.SrsEntry.Meanings);
                e.SrsEntry.Readings = MultiValueFieldHelper.Trim(e.SrsEntry.Readings);
                e.SrsEntry.Tags     = MultiValueFieldHelper.Trim(e.SrsEntry.Tags);
            }

            IsSaved = e.IsSaved;
            DispatcherHelper.InvokeAsync(this.Close);
        }
        /// <summary>
        /// Updates the given SRS entry.
        /// </summary>
        /// <param name="entity">Entity to update.</param>
        /// <returns>True if the operation was sucessful. False otherwise.</returns>
        public bool Update(SrsEntry entity)
        {
            DaoConnection connection = null;
            bool          result     = false;

            try
            {
                connection = DaoConnection.Open(DaoConnectionEnum.SrsDatabase);

                // Create a parameter list and two string builders that will
                // be used to put the SQL request together.
                List <DaoParameter> parameters      = new List <DaoParameter>();
                StringBuilder       sqlQueryBuilder = new StringBuilder(
                    "UPDATE " + SqlHelper.Table_SrsEntry + " SET ");

                // NextAnswerDate
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_NextAnswerDate + "=");
                if (entity.NextAnswerDate == null)
                {
                    sqlQueryBuilder.Append("null");
                }
                else
                {
                    sqlQueryBuilder.Append("@NextAnswerDate");
                    parameters.Add(new DaoParameter(
                                       "@NextAnswerDate", entity.NextAnswerDate.Value.ToUniversalTime().Ticks));
                }
                sqlQueryBuilder.Append(",");

                // Meanings
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_Meanings + "=@Meanings,");
                parameters.Add(new DaoParameter("@Meanings",
                                                MultiValueFieldHelper.Trim(entity.Meanings ?? string.Empty)));

                // Readings
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_Readings + "=@Readings,");
                parameters.Add(new DaoParameter("@Readings",
                                                MultiValueFieldHelper.Trim(entity.Readings ?? string.Empty)));

                // CurrentGrade
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_CurrentGrade + "=@CurrentGrade,");
                parameters.Add(new DaoParameter("@CurrentGrade", entity.CurrentGrade));

                // FailureCount
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_FailureCount + "=@FailureCount,");
                parameters.Add(new DaoParameter("@FailureCount", entity.FailureCount));

                // SuccessCount
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_SuccessCount + "=@SuccessCount,");
                parameters.Add(new DaoParameter("@SuccessCount", entity.SuccessCount));

                // SuspensionDate
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_SuspensionDate + "=");
                if (entity.SuspensionDate == null)
                {
                    sqlQueryBuilder.Append("null");
                }
                else
                {
                    sqlQueryBuilder.Append("@SuspensionDate");
                    parameters.Add(new DaoParameter(
                                       "@SuspensionDate", entity.SuspensionDate.Value.ToUniversalTime().Ticks));
                }
                sqlQueryBuilder.Append(",");

                // AssociatedVocab
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_AssociatedVocab
                                       + "=@AssociatedVocab,");
                parameters.Add(new DaoParameter(
                                   "@AssociatedVocab", entity.AssociatedVocab));

                // AssociatedKanji
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_AssociatedKanji
                                       + "=@AssociatedKanji,");
                parameters.Add(new DaoParameter(
                                   "@AssociatedKanji", entity.AssociatedKanji));

                // MeaningNote
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_MeaningNote
                                       + "=@MeaningNote,");
                parameters.Add(new DaoParameter(
                                   "@MeaningNote", entity.MeaningNote));

                // ReadingNote
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_ReadingNote
                                       + "=@ReadingNote,");
                parameters.Add(new DaoParameter(
                                   "@ReadingNote", entity.ReadingNote));

                // ServerId
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_ServerId
                                       + "=@ServerId,");
                parameters.Add(new DaoParameter(
                                   "@ServerId", entity.ServerId));

                // IsDeleted
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_IsDeleted
                                       + "=@IsDeleted,");
                parameters.Add(new DaoParameter(
                                   "@IsDeleted", entity.IsDeleted));

                // LastUpdateDate
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_LastUpdateDate
                                       + "=@LastUpdateDate,");
                parameters.Add(new DaoParameter(
                                   "@LastUpdateDate", DateTime.UtcNow.Ticks));

                // Tags
                sqlQueryBuilder.Append(SqlHelper.Field_SrsEntry_Tags + "=@Tags");
                parameters.Add(new DaoParameter(
                                   "@Tags", MultiValueFieldHelper.Trim(entity.Tags)));

                // We are done with the string builders.

                // Bring the query pieces together.
                string finalQuery =
                    sqlQueryBuilder.ToString() + " WHERE "
                    + SqlHelper.Field_SrsEntry_Id + "=@Id";
                parameters.Add(new DaoParameter("@Id", entity.ID));

                // Execute the query.
                result = connection.ExecuteNonQuery(finalQuery, parameters.ToArray()) == 1;
            }
            catch (Exception ex)
            {
                LogHelper.GetLogger(this.GetType().Name).Error(
                    "An error occured during SRS item update.", ex);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }

            return(result);
        }
        /// <summary>
        /// Inserts the given entity in the database.
        /// Overrides the ID property of the given entity.
        /// </summary>
        /// <param name="entity">Entity to insert.</param>
        public void Add(SrsEntry entity)
        {
            DaoConnection connection = null;

            try
            {
                connection = DaoConnection.Open(DaoConnectionEnum.SrsDatabase);

                // Create a parameter list and two string builders that will
                // be used to put the SQL request together.
                List <DaoParameter> parameters    = new List <DaoParameter>();
                StringBuilder       sqlQueryStart = new StringBuilder(
                    "INSERT INTO " + SqlHelper.Table_SrsEntry + "(");
                StringBuilder sqlQueryEnd = new StringBuilder(
                    "VALUES(");

                // CreationDate
                if (entity.CreationDate != null)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_CreationDate + ",");
                    sqlQueryEnd.Append("@CreationDate,");
                    parameters.Add(new DaoParameter(
                                       "@CreationDate", entity.CreationDate.Value.ToUniversalTime().Ticks));
                }

                // NextAnswerDate
                if (entity.NextAnswerDate != null)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_NextAnswerDate + ",");
                    sqlQueryEnd.Append("@NextAnswerDate,");
                    parameters.Add(new DaoParameter(
                                       "@NextAnswerDate", entity.NextAnswerDate.Value.ToUniversalTime().Ticks));
                }

                // Meanings
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_Meanings + ",");
                sqlQueryEnd.Append("@Meanings,");
                parameters.Add(new DaoParameter("@Meanings",
                                                MultiValueFieldHelper.Trim(entity.Meanings ?? string.Empty)));

                // Readings
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_Readings + ",");
                sqlQueryEnd.Append("@Readings,");
                parameters.Add(new DaoParameter("@Readings",
                                                MultiValueFieldHelper.Trim(entity.Readings ?? string.Empty)));

                // CurrentGrade
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_CurrentGrade + ",");
                sqlQueryEnd.Append("@CurrentGrade,");
                parameters.Add(new DaoParameter("@CurrentGrade", entity.CurrentGrade));

                // FailureCount
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_FailureCount + ",");
                sqlQueryEnd.Append("@FailureCount,");
                parameters.Add(new DaoParameter("@FailureCount", entity.FailureCount));

                // SuccessCount
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_SuccessCount + ",");
                sqlQueryEnd.Append("@SuccessCount,");
                parameters.Add(new DaoParameter("@SuccessCount", entity.SuccessCount));

                // SuspensionDate
                if (entity.SuspensionDate.HasValue)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_SuspensionDate + ",");
                    sqlQueryEnd.Append("@SuspensionDate,");
                    parameters.Add(new DaoParameter("@SuspensionDate", entity.SuspensionDate.Value.ToUniversalTime().Ticks));
                }

                // AssociatedVocab
                if (!string.IsNullOrWhiteSpace(entity.AssociatedVocab))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_AssociatedVocab + ",");
                    sqlQueryEnd.Append("@AssociatedVocab,");
                    parameters.Add(new DaoParameter(
                                       "@AssociatedVocab", entity.AssociatedVocab));
                }

                // AssociatedKanji
                if (!string.IsNullOrWhiteSpace(entity.AssociatedKanji))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_AssociatedKanji + ",");
                    sqlQueryEnd.Append("@AssociatedKanji,");
                    parameters.Add(new DaoParameter(
                                       "@AssociatedKanji", entity.AssociatedKanji));
                }

                // MeaningNote
                if (!string.IsNullOrWhiteSpace(entity.MeaningNote))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_MeaningNote + ",");
                    sqlQueryEnd.Append("@MeaningNote,");
                    parameters.Add(new DaoParameter(
                                       "@MeaningNote", entity.MeaningNote));
                }

                // ReadingNote
                if (!string.IsNullOrWhiteSpace(entity.ReadingNote))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_ReadingNote + ",");
                    sqlQueryEnd.Append("@ReadingNote,");
                    parameters.Add(new DaoParameter(
                                       "@ReadingNote", entity.ReadingNote));
                }

                // Tags
                if (!string.IsNullOrWhiteSpace(entity.Tags))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_Tags + ",");
                    sqlQueryEnd.Append("@Tags,");
                    parameters.Add(new DaoParameter(
                                       "@Tags", MultiValueFieldHelper.Trim(entity.Tags)));
                }

                // LastUpdateDate
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_LastUpdateDate + ",");
                sqlQueryEnd.Append("@LastUpdateDate,");
                parameters.Add(new DaoParameter(
                                   "@LastUpdateDate", DateTime.UtcNow.Ticks));

                // ServerId
                if (entity.ServerId.HasValue)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_ServerId + ",");
                    sqlQueryEnd.Append("@ServerId,");
                    parameters.Add(new DaoParameter(
                                       "@ServerId", entity.ServerId));
                }

                // IsDeleted (because why not?)
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_IsDeleted + ",");
                sqlQueryEnd.Append("@IsDeleted,");
                parameters.Add(new DaoParameter("@IsDeleted", entity.IsDeleted));

                // We are done with the string builders.

                // Bring the query pieces together.
                string finalQuery =
                    sqlQueryStart.ToString().TrimEnd(new char[] { ',' }) + ") "
                    + sqlQueryEnd.ToString().TrimEnd(new char[] { ',' }) + ")";

                // Execute the query.
                if (connection.ExecuteNonQuery(finalQuery, parameters.ToArray()) == 1)
                {
                    // If the row was inserted, put the insert ID in the entity.
                    entity.ID = connection.GetLastInsertId();
                }
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
        }