Index: Ringtoets/Integration/src/Ringtoets.Integration.Forms/Commands/AssessmentSectionFromFileCommandHandler.cs =================================================================== diff -u -r2d0a1ea654af9fb4685e01c7d4883884c8a9249f -r50b203a8d5c805fc548fb6b92c70a847190da6a5 --- Ringtoets/Integration/src/Ringtoets.Integration.Forms/Commands/AssessmentSectionFromFileCommandHandler.cs (.../AssessmentSectionFromFileCommandHandler.cs) (revision 2d0a1ea654af9fb4685e01c7d4883884c8a9249f) +++ Ringtoets/Integration/src/Ringtoets.Integration.Forms/Commands/AssessmentSectionFromFileCommandHandler.cs (.../AssessmentSectionFromFileCommandHandler.cs) (revision 50b203a8d5c805fc548fb6b92c70a847190da6a5) @@ -23,13 +23,18 @@ using System.Collections.Generic; using System.Linq; using System.Windows.Forms; +using Core.Common.Gui; +using Core.Common.Gui.Forms.ViewHost; using Core.Common.IO.Exceptions; using log4net; using Ringtoets.Common.Data.AssessmentSection; +using Ringtoets.Common.Data.FailureMechanism; +using Ringtoets.Common.Forms.Helpers; using Ringtoets.Common.IO; using Ringtoets.Common.IO.Exceptions; using Ringtoets.Integration.Data; using Ringtoets.Integration.Forms.Properties; +using BaseResources = Core.Common.Base.Properties.Resources; namespace Ringtoets.Integration.Forms.Commands { @@ -39,55 +44,129 @@ public class AssessmentSectionFromFileCommandHandler { private static readonly ILog log = LogManager.GetLogger(typeof(AssessmentSectionFromFileCommandHandler)); + private readonly string shapeFileDirectory = RingtoetsSettingsHelper.GetCommonDocumentsRingtoetsShapeFileDirectory(); private readonly IWin32Window dialogParent; + private readonly IProjectOwner projectOwner; + private readonly IDocumentViewController viewController; private IEnumerable settings; private IEnumerable referenceLineMetas = new List(); /// /// Initializes a new instance of the class. /// /// The parent of the dialog. - public AssessmentSectionFromFileCommandHandler(IWin32Window dialogParent) + /// The class owning the application project. + /// The document view controller. + public AssessmentSectionFromFileCommandHandler(IWin32Window dialogParent, IProjectOwner projectOwner, IDocumentViewController viewController) { + if (dialogParent == null) + { + throw new ArgumentNullException("dialogParent"); + } + if (projectOwner == null) + { + throw new ArgumentNullException("projectOwner"); + } + if (viewController == null) + { + throw new ArgumentNullException("viewController"); + } this.dialogParent = dialogParent; + this.projectOwner = projectOwner; + this.viewController = viewController; } /// - /// Creates a new , based upon the in a dialog selected , which is derived from the shape file in . + /// Creates an /// - /// The path to the folder where a shape file should be read. - /// The newly created . - /// Thrown when: - /// - /// The shape file does not contain any polylines. - /// The shape file does not contain the required attributes. - /// The assessment section ids in the shape file are not unique or are missing. - /// - /// Thrown when: - /// - /// points to an invalid directory. - /// The path does not contain any shape files. - /// - public IAssessmentSection CreateAssessmentSectionFromFile(string folderpath) + public void CreateAssessmentSectionFromFile() { - ValidateAssessmentSectionSettings(); - ValidateReferenceLineMetas(folderpath); + if (!TryValidate()) + { + return; + } + var assessmentSection = GetAssessmentSectionFromDialog(); + if (assessmentSection == null || !(projectOwner.Project is RingtoetsProject)) + { + return; + } + + SetAssessmentSectionToProject((RingtoetsProject) projectOwner.Project, assessmentSection); + } + + #region Dialog + + private AssessmentSection GetAssessmentSectionFromDialog() + { using (var dialog = CreateReferenceLineMetaSelectionDialogWithItems()) { if (dialog.ShowDialog() != DialogResult.OK) { return null; } + var selectedItem = dialog.SelectedReferenceLineMeta; return selectedItem == null ? null : CreateAssessmentSection(selectedItem, dialog.SelectedNorm); } } - private IAssessmentSection CreateAssessmentSection(ReferenceLineMeta selectedItem, int? norm) + private ReferenceLineMetaSelectionDialog CreateReferenceLineMetaSelectionDialogWithItems() { - IAssessmentSection assessmentSection; + return new ReferenceLineMetaSelectionDialog(dialogParent, referenceLineMetas); + } + + #endregion + + #region Set AssessmentSection to Project + + private static void SetFailureMechanismsValueN(AssessmentSection assessmentSection, int n) + { + assessmentSection.GrassCoverErosionInwards.GeneralInput.N = n; + assessmentSection.HeightStructures.GeneralInput.N = n; + + } + + private void SetAssessmentSectionToProject(RingtoetsProject ringtoetsProject, AssessmentSection assessmentSection) + { + assessmentSection.Name = GetUniqueForAssessmentSectionName(ringtoetsProject.AssessmentSections, assessmentSection.Name); + ringtoetsProject.AssessmentSections.Add(assessmentSection); + ringtoetsProject.NotifyObservers(); + + viewController.OpenViewForData(assessmentSection); + } + + private static string GetUniqueForAssessmentSectionName(IEnumerable assessmentSections, string baseName) + { + return NamingHelper.GetUniqueName(assessmentSections, baseName, a => a.Name); + } + + #endregion + + #region Create AssessmentSection + + private static AssessmentSection CreateDikeAssessmentSection() + { + var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); + return assessmentSection; + } + + private static AssessmentSection CreateDikeAssessmentSection(AssessmentSectionSettings settings) + { + var assessmentSection = CreateDikeAssessmentSection(); + SetFailureMechanismsValueN(assessmentSection, settings.N); + return assessmentSection; + } + + private static AssessmentSection CreateDuneAssessmentSection() + { + return new AssessmentSection(AssessmentSectionComposition.Dune); + } + + private AssessmentSection CreateAssessmentSection(ReferenceLineMeta selectedItem, int? norm) + { + AssessmentSection assessmentSection; var settingOfSelectedAssessmentSection = settings.FirstOrDefault(s => s.AssessmentSectionId == selectedItem.AssessmentSectionId); if (settingOfSelectedAssessmentSection == null) { @@ -123,44 +202,35 @@ return assessmentSection; } - private static void SetFailureMechanismsValueN(AssessmentSection assessmentSection, int n) - { - assessmentSection.GrassCoverErosionInwards.GeneralInput.N = n; - assessmentSection.HeightStructures.GeneralInput.N = n; - } + #endregion - private ReferenceLineMetaSelectionDialog CreateReferenceLineMetaSelectionDialogWithItems() - { - return new ReferenceLineMetaSelectionDialog(dialogParent, referenceLineMetas); - } + #region Validators - #region Create AssessmentSection - - private static AssessmentSection CreateDikeAssessmentSection() + private bool TryValidate() { - var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); - return assessmentSection; - } + ValidateAssessmentSectionSettings(); - private static AssessmentSection CreateDikeAssessmentSection(AssessmentSectionSettings settings) - { - var assessmentSection = CreateDikeAssessmentSection(); - SetFailureMechanismsValueN(assessmentSection, settings.N); - return assessmentSection; + try + { + ValidateReferenceLineMetas(); + } + catch (CriticalFileValidationException exception) + { + MessageBox.Show(exception.Message, BaseResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + log.Warn(exception.Message, exception.InnerException); + return false; + } + catch (CriticalFileReadException exception) + { + log.Error(exception.Message, exception.InnerException); + return false; + } + return true; } - private static IAssessmentSection CreateDuneAssessmentSection() + private void ValidateReferenceLineMetas() { - return new AssessmentSection(AssessmentSectionComposition.Dune); - } - - #endregion - - #region Validators - - private void ValidateReferenceLineMetas(string folderpath) - { - var importer = new ReferenceLineMetaImporter(folderpath); + var importer = new ReferenceLineMetaImporter(shapeFileDirectory); referenceLineMetas = importer.GetReferenceLineMetas(); if (!referenceLineMetas.Any())