예제 #1
0
파일: TestForm.cs 프로젝트: radtek/Klokan
        private void removeItemButton_Click(object sender, EventArgs e)
        {
            var dialogResult = MessageBox.Show(Properties.Resources.PromptTextDatabaseUpdate, Properties.Resources.PromptCaptionDatabaseUpdate,
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dialogResult == DialogResult.No)
            {
                return;
            }

            if (dataView.SelectedRows.Count == 0)
            {
                MessageBox.Show(Properties.Resources.ErrorTextNoRowSelected, Properties.Resources.ErrorCaptionGeneral,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // multiselect is set to false for this data view
            var rowToRemove   = dataView.SelectedRows[0];
            int rowToRemoveID = (int)(rowToRemove.Cells[0].Value);

            dataView.Rows.Remove(rowToRemove);

            using (var testDB = new KlokanTestDBContext())
            {
                var scanToRemoveQuery = from scan in testDB.Scans
                                        where scan.ScanId == rowToRemoveID
                                        select scan;

                var scanToRemove = scanToRemoveQuery.FirstOrDefault();
                if (scanToRemove != default(KlokanTestDBScan))
                {
                    // lazy loading is used, so we need to load all the relations of scan if we want Remove() to remove those as well
                    var expectedAnswers = scanToRemove.ExpectedValues;
                    var computedValues  = scanToRemove.ComputedValues;

                    testDB.Scans.Remove(scanToRemove);

                    try
                    {
                        testDB.SaveChanges();

                        MessageBox.Show(Properties.Resources.InfoTextDatabaseUpdated, Properties.Resources.InfoCaptionDatabaseUpdated,
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex) when(ex is DbUpdateException || ex is DbUpdateConcurrencyException)
                    {
                        MessageBox.Show(Properties.Resources.ErrorTextDatabase, Properties.Resources.ErrorCaptionGeneral, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            ShowAverageCorrectness();
        }
예제 #2
0
        private void updateButton_Click(object sender, EventArgs e)
        {
            var dialogResult = MessageBox.Show(Properties.Resources.PromptTextDatabaseUpdate, Properties.Resources.PromptCaptionDatabaseUpdate,
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dialogResult == DialogResult.No)
            {
                return;
            }

            updateButton.Enabled = false;

            // prepare the DbSet of chosen expected answers
            List <KlokanTestDBExpectedAnswer> expectedAnswers = new List <KlokanTestDBExpectedAnswer>();

            expectedAnswers.AddRange(TableArrayHandling.AnswersToDbSet <KlokanTestDBExpectedAnswer>(expectedValuesStudentTable, 0, true));
            for (int i = 0; i < 3; i++)
            {
                expectedAnswers.AddRange(TableArrayHandling.AnswersToDbSet <KlokanTestDBExpectedAnswer>(expectedValuesAnswerTable, i, false));
            }

            if (addMode)
            {
                scanItem.Image = ImageHandling.GetImageBytes(scanFilePath, ImageFormat.Png);
            }

            scanItem.ExpectedValues = expectedAnswers;
            scanItem.Correctness    = -1;               // correctness will only have a valid value once the evaluation is run

            using (var testDB = new KlokanTestDBContext())
            {
                // when editing an item we first have to delete the old one
                if (!addMode)
                {
                    var oldScanItemQuery = from scan in testDB.Scans
                                           where scan.ScanId == scanItem.ScanId
                                           select scan;

                    var oldExpectedAnswers = from answer in testDB.ExpectedValues
                                             where answer.ScanId == scanItem.ScanId
                                             select answer;

                    // delete the old expected answers
                    foreach (var answer in oldExpectedAnswers)
                    {
                        testDB.ExpectedValues.Remove(answer);
                    }

                    // assign new expected answers
                    var oldScanItem = oldScanItemQuery.FirstOrDefault();
                    oldScanItem.ExpectedValues = scanItem.ExpectedValues;
                    oldScanItem.Correctness    = -1;
                }
                else
                {
                    KlokanTestDBScan newScanItem = new KlokanTestDBScan
                    {
                        ExpectedValues = scanItem.ExpectedValues,
                        ComputedValues = scanItem.ComputedValues,
                        Image          = scanItem.Image,
                        Correctness    = -1
                    };

                    testDB.Scans.Add(newScanItem);
                }

                try
                {
                    testDB.SaveChanges();

                    MessageBox.Show(Properties.Resources.InfoTextDatabaseUpdated, Properties.Resources.InfoCaptionDatabaseUpdated,
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex) when(ex is DbUpdateException || ex is DbUpdateConcurrencyException)
                {
                    MessageBox.Show(Properties.Resources.ErrorTextDatabase, Properties.Resources.ErrorCaptionGeneral, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            PopulateForm();
        }