Beispiel #1
0
        /// <summary>
        /// Process the Data input of a Report creation dialog to create a new Report instance and proceeds to insert it in the database
        /// </summary>
        /// <param name="dialogViewModelInstance">The ViewModel of the dialog used to collect data</param>
        /// <returns>The new report instance</returns>
        private Report CreateReportFromCreationDialog(ViewModels.ReportCreationDialogViewModel dialogViewModelInstance)
        {
            Report output;

            // Creates new  report instance

            output = new Report
            {
                AuthorID               = dialogViewModelInstance.Author.ID,
                BatchID                = dialogViewModelInstance.SelectedBatch.ID,
                Category               = "TR",
                Description            = (dialogViewModelInstance.SelectedControlPlan != null) ? dialogViewModelInstance.SelectedControlPlan.Name : dialogViewModelInstance.Description,
                IsComplete             = false,
                Number                 = dialogViewModelInstance.Number,
                SpecificationVersionID = dialogViewModelInstance.SelectedVersion.ID,
                StartDate              = DateTime.Now.ToShortDateString()
            };

            // Test Record is created from parent task or from the selected requirements

            output.TestRecord = new TestRecord()
            {
                BatchID      = output.BatchID,
                RecordTypeID = 1
            };

            if (dialogViewModelInstance.IsCreatingFromTask)
            {
                foreach (Test tst in dialogViewModelInstance.TaskInstance.GenerateTests())
                {
                    output.TestRecord.Tests.Add(tst);
                }
            }
            else
            {
                foreach (Test tst in GenerateTestList(dialogViewModelInstance.SelectedRequirements))
                {
                    output.TestRecord.Tests.Add(tst);
                }
            }

            // Calculates total test duration

            output.TotalDuration = output.TestRecord.Tests.Sum(tst => tst.Duration);

            //Inserts new entry in the DB

            output.Create();

            // If the tested Batch does not have a "basic" report, add reference to current instance

            if (dialogViewModelInstance.SelectedBatch.BasicReportID == null)
            {
                using (LabDbEntities entities = _dbContextFactory.Create())
                {
                    entities.Batches
                    .First(btc => btc.ID == dialogViewModelInstance.SelectedBatch.ID)
                    .BasicReportID = output.ID;

                    entities.SaveChanges();
                }
            }

            // If using Task as template, the parent is updated with the child Report ID

            if (dialogViewModelInstance.CreationMode == ViewModels.ReportCreationDialogViewModel.CreationModes.ReportFromTask)
            {
                dialogViewModelInstance.TaskInstance.ReportID = output.ID;
                dialogViewModelInstance.TaskInstance.Update();
            }

            return(output);
        }