From 99ab3ac12c05befe90c894c3428259f2a7975a4a Mon Sep 17 00:00:00 2001 From: Joao Rua Date: Wed, 3 Apr 2024 18:54:55 +0100 Subject: [PATCH] now models can be created + validation + tests --- .../EngineeringModelsTableTestFixture.cs | 28 ++-- .../EngineeringModelValidatorTestFixture.cs | 79 ++++++++++ ...ineeringModelsTableViewModelTestFixture.cs | 59 +++++--- .../EngineeringModelsTable.razor | 103 +++++++++++-- .../EngineeringModelsTable.razor.cs | 32 ++-- .../EngineeringModel/ParticipantsTable.razor | 2 +- .../EngineeringModelValidator.cs | 74 ++++++++++ .../EngineeringModelsTableViewModel.cs | 138 +++++++++++++++++- .../IEngineeringModelsTableViewModel.cs | 70 ++++++++- 9 files changed, 514 insertions(+), 71 deletions(-) create mode 100644 COMETwebapp.Tests/Validators/EngineeringModel/EngineeringModelValidatorTestFixture.cs create mode 100644 COMETwebapp/Validators/EngineeringModel/EngineeringModelValidator.cs diff --git a/COMETwebapp.Tests/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableTestFixture.cs b/COMETwebapp.Tests/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableTestFixture.cs index a2c84dff..679ea7c1 100644 --- a/COMETwebapp.Tests/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableTestFixture.cs +++ b/COMETwebapp.Tests/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableTestFixture.cs @@ -121,29 +121,21 @@ public async Task VerifyDeleteEngineeringModel() } [Test] - public async Task VerifyAddingOrEditingEngineeringModel() + public async Task VerifyAddEngineeringModel() { var addEngineeringModelButton = this.renderer.FindComponents().First(x => x.Instance.Id == "addEngineeringModelButton"); await this.renderer.InvokeAsync(addEngineeringModelButton.Instance.Click.InvokeAsync); + var grid = this.renderer.FindComponent(); + Assert.That(grid.Instance.IsEditing(), Is.EqualTo(true)); - Assert.Multiple(() => - { - Assert.That(this.renderer.Instance.ShouldCreateThing, Is.EqualTo(true)); - Assert.That(this.viewModel.Object.Thing, Is.InstanceOf(typeof(EngineeringModelSetup))); - }); - - var editEngineeringModelButton = this.renderer.FindComponents().First(x => x.Instance.Id == "editEngineeringModelButton"); - await this.renderer.InvokeAsync(editEngineeringModelButton.Instance.Click.InvokeAsync); + var cancelEngineeringModelButton = this.renderer.FindComponents().First(x => x.Instance.Id == "cancelEngineeringModelButton"); + await this.renderer.InvokeAsync(cancelEngineeringModelButton.Instance.Click.InvokeAsync); + Assert.That(grid.Instance.IsEditing(), Is.EqualTo(false)); - Assert.Multiple(() => - { - Assert.That(this.renderer.Instance.ShouldCreateThing, Is.EqualTo(false)); - Assert.That(this.viewModel.Object.Thing, Is.InstanceOf(typeof(EngineeringModelSetup))); - }); - - var form = this.renderer.FindComponent(); - await this.renderer.InvokeAsync(form.Instance.EditModelSaving.InvokeAsync); - this.viewModel.Verify(x => x.CreateOrEditEngineeringModel(false), Times.Once); + await this.renderer.InvokeAsync(addEngineeringModelButton.Instance.Click.InvokeAsync); + var saveEngineeringModelButton = this.renderer.FindComponents().First(x => x.Instance.Id == "saveEngineeringModelButton"); + await this.renderer.InvokeAsync(saveEngineeringModelButton.Instance.Click.InvokeAsync); + this.viewModel.Verify(x => x.CreateEngineeringModel(), Times.Once); } [Test] diff --git a/COMETwebapp.Tests/Validators/EngineeringModel/EngineeringModelValidatorTestFixture.cs b/COMETwebapp.Tests/Validators/EngineeringModel/EngineeringModelValidatorTestFixture.cs new file mode 100644 index 00000000..df43906f --- /dev/null +++ b/COMETwebapp.Tests/Validators/EngineeringModel/EngineeringModelValidatorTestFixture.cs @@ -0,0 +1,79 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2023-2024 RHEA System S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Antoine Théate, João Rua +// +// This file is part of CDP4-COMET WEB Community Edition +// The CDP4-COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C. +// +// The CDP4-COMET WEB Community Edition is free software; you can redistribute it and/or +// modify it under the terms of the GNU Affero General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// The CDP4-COMET WEB Community Edition is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMETwebapp.Tests.Validators.EngineeringModel +{ + using CDP4Common.SiteDirectoryData; + using CDP4Common.Validation; + + using COMETwebapp.Validators.EngineeringModel; + + using NUnit.Framework; + + [TestFixture] + public class EngineeringModelValidatorTestFixture + { + private EngineeringModelValidator validator; + + [SetUp] + public void SetUp() + { + var validationService = new ValidationService(); + this.validator = new EngineeringModelValidator(validationService); + } + + [Test] + public void VerifyValidationScenarios() + { + var model = new EngineeringModelSetup(); + Assert.That(this.validator.Validate(model).IsValid, Is.EqualTo(false)); + + model = new EngineeringModelSetup() + { + Name = "name", + ShortName = "short" + }; + + Assert.That(this.validator.Validate(model).IsValid, Is.EqualTo(false)); + + model.SourceEngineeringModelSetupIid = Guid.NewGuid(); + Assert.That(this.validator.Validate(model).IsValid, Is.EqualTo(true)); + + // Verifies the inputs for when the site rdl is set + model.SourceEngineeringModelSetupIid = null; + model.RequiredRdl.Add(new ModelReferenceDataLibrary()); + Assert.That(this.validator.Validate(model).IsValid, Is.EqualTo(false)); + + model.ActiveDomain = [new DomainOfExpertise()]; + Assert.That(this.validator.Validate(model).IsValid, Is.EqualTo(true)); + + // Verifies the organization input validation + model.OrganizationalParticipant.Add(new OrganizationalParticipant()); + Assert.That(this.validator.Validate(model).IsValid, Is.EqualTo(false)); + + model.DefaultOrganizationalParticipant = model.OrganizationalParticipant.First(); + Assert.That(this.validator.Validate(model).IsValid, Is.EqualTo(true)); + } + } +} diff --git a/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModelTestFixture.cs b/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModelTestFixture.cs index 5c78ed29..65cd10b3 100644 --- a/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModelTestFixture.cs +++ b/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModelTestFixture.cs @@ -53,6 +53,7 @@ public class EngineeringModelsTableViewModelTestFixture private Mock> loggerMock; private CDPMessageBus messageBus; private EngineeringModelSetup engineeringModel; + private SiteDirectory siteDirectory; [SetUp] public void Setup() @@ -68,12 +69,15 @@ public void Setup() Name = "model 1", }; - var siteDirectory = new SiteDirectory() + this.siteDirectory = new SiteDirectory() { - ShortName = "siteDirectory" + ShortName = "siteDirectory", + SiteReferenceDataLibrary = { new SiteReferenceDataLibrary() }, + Domain = { new DomainOfExpertise() }, + Organization = { new Organization() } }; - siteDirectory.Model.Add(this.engineeringModel); + this.siteDirectory.Model.Add(this.engineeringModel); this.assembler = new Assembler(new Uri("http://localhost:5000/"), this.messageBus); var lazyModel = new Lazy(this.engineeringModel); @@ -83,9 +87,9 @@ public void Setup() var session = new Mock(); session.Setup(x => x.PermissionService).Returns(this.permissionService.Object); session.Setup(x => x.Assembler).Returns(this.assembler); - session.Setup(x => x.RetrieveSiteDirectory()).Returns(siteDirectory); + session.Setup(x => x.RetrieveSiteDirectory()).Returns(this.siteDirectory); this.sessionService.Setup(x => x.Session).Returns(session.Object); - this.sessionService.Setup(x => x.GetSiteDirectory()).Returns(siteDirectory); + this.sessionService.Setup(x => x.GetSiteDirectory()).Returns(this.siteDirectory); this.viewModel = new EngineeringModelsTableViewModel(this.sessionService.Object, this.messageBus, this.loggerMock.Object); } @@ -106,6 +110,12 @@ public void VerifyInitializeViewModel() { Assert.That(this.viewModel.Rows.Count, Is.EqualTo(1)); Assert.That(this.viewModel.Rows.Items.First().Thing, Is.EqualTo(this.engineeringModel)); + Assert.That(this.viewModel.DomainsOfExpertise, Is.EqualTo(this.siteDirectory.Domain)); + Assert.That(this.viewModel.EngineeringModels, Is.EqualTo(this.siteDirectory.Model)); + Assert.That(this.viewModel.Organizations, Is.EqualTo(this.siteDirectory.Organization)); + Assert.That(this.viewModel.SiteRdls, Is.EqualTo(this.siteDirectory.SiteReferenceDataLibrary)); + Assert.That(this.viewModel.ModelKinds, Is.Not.Empty); + Assert.That(this.viewModel.StudyPhases, Is.Not.Empty); }); } @@ -133,15 +143,10 @@ public void VerifySessionRefresh() this.messageBus.SendMessage(SessionStateKind.RefreshEnded); Assert.That(this.viewModel.Rows, Has.Count.EqualTo(1)); - var siteDirectory = new SiteDirectory() - { - ShortName = "newShortname" - }; - var engineeringModelTest = new EngineeringModelSetup() { Iid = Guid.NewGuid(), - Container = siteDirectory, + Container = this.siteDirectory, }; this.messageBus.SendObjectChangeEvent(engineeringModelTest, EventKind.Added); @@ -155,13 +160,13 @@ public void VerifySessionRefresh() Assert.That(this.viewModel.Rows, Has.Count.EqualTo(1)); - this.messageBus.SendObjectChangeEvent(siteDirectory, EventKind.Updated); + this.messageBus.SendObjectChangeEvent(this.siteDirectory, EventKind.Updated); this.messageBus.SendObjectChangeEvent(new PersonRole(), EventKind.Updated); this.messageBus.SendMessage(SessionStateKind.RefreshEnded); Assert.Multiple(() => { - Assert.That(this.viewModel.Rows.Items.First().ContainerName, Is.EqualTo(siteDirectory.ShortName)); + Assert.That(this.viewModel.Rows.Items.First().ContainerName, Is.EqualTo(this.siteDirectory.ShortName)); this.permissionService.Verify(x => x.CanWrite(engineeringModelTest.ClassKind, It.IsAny()), Times.AtLeast(this.viewModel.Rows.Count)); }); } @@ -199,15 +204,33 @@ public async Task VerifyRowOperations() } [Test] - public async Task VerifyModelCreateOrEdit() + public async Task VerifyModelCreation() { this.viewModel.InitializeViewModel(); - await this.viewModel.CreateOrEditEngineeringModel(false); - this.sessionService.Verify(x => x.UpdateThings(It.IsAny(), It.Is>(c => c.Count == 1)), Times.Once); + var testOrganization = this.siteDirectory.Organization.First(); + this.viewModel.SelectedOrganizations = [testOrganization]; + this.viewModel.SelectedModelAdminOrganization = testOrganization; + + this.viewModel.SelectedActiveDomains = [this.siteDirectory.Domain.First()]; + this.viewModel.SelectedSiteRdl = this.siteDirectory.SiteReferenceDataLibrary.First(); + this.viewModel.SelectedSourceModel = this.siteDirectory.Model.First(); + + this.viewModel.SetupEngineeringModelWithSelectedValues(); + await this.viewModel.CreateEngineeringModel(); - await this.viewModel.CreateOrEditEngineeringModel(true); - this.sessionService.Verify(x => x.UpdateThings(It.IsAny(), It.Is>(c => c.Count == 2)), Times.Once); + this.sessionService.Verify(x => x.UpdateThings(It.IsAny(), It.Is>(c => c.Count == 4)), Times.Once); + + this.viewModel.ResetSelectedValues(); + + Assert.Multiple(() => + { + Assert.That(this.viewModel.SelectedActiveDomains, Has.Count.EqualTo(0)); + Assert.That(this.viewModel.SelectedOrganizations, Has.Count.EqualTo(0)); + Assert.That(this.viewModel.SelectedModelAdminOrganization, Is.Null); + Assert.That(this.viewModel.SelectedSiteRdl, Is.Null); + Assert.That(this.viewModel.SelectedSourceModel, Is.Null); + }); } } } diff --git a/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor b/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor index 672a50e0..0f7890ca 100644 --- a/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor +++ b/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor @@ -15,6 +15,7 @@ Copyright (c) 2023-2024 RHEA System S.A. along with this program. If not, see http://www.gnu.org/licenses/. -------------------------------------------------------------------------------> @using COMETwebapp.ViewModels.Components.SiteDirectory.Rows +@using CDP4Common.SiteDirectoryData @inherits COMETwebapp.Components.Common.SelectedDataItemBase @@ -31,6 +32,7 @@ Copyright (c) 2023-2024 RHEA System S.A. CustomizeEditModel="this.CustomizeEditThing" EditMode="GridEditMode.PopupEditForm" EditModelSaving="@(() => this.OnEditThingSaving())" + EditFormButtonsVisible="false" PageSize="20" PagerNavigationMode="PagerNavigationMode.Auto" PageSizeSelectorVisible="true" @@ -50,11 +52,6 @@ Copyright (c) 2023-2024 RHEA System S.A. @{ var row = (EngineeringModelRowViewModel)context.DataItem; - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + Save + + + Cancel + +
diff --git a/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor.cs b/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor.cs index 3c621e45..97a0d7bc 100644 --- a/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor.cs +++ b/COMETwebapp/Components/SiteDirectory/EngineeringModel/EngineeringModelsTable.razor.cs @@ -50,6 +50,11 @@ public partial class EngineeringModelsTable : SelectedDataItemBase public bool IsModelSelected { get; private set; } + /// + /// Gets the condition to check if the source model was selected in creation form + /// + private bool IsSourceModelSelected => this.ViewModel.SelectedSourceModel is not null; + /// /// The selected component type /// @@ -88,7 +93,7 @@ protected override void OnInitialized() /// A protected override async Task OnEditThingSaving() { - await this.ViewModel.CreateOrEditEngineeringModel(this.ShouldCreateThing); + await this.ViewModel.CreateEngineeringModel(); } /// @@ -99,18 +104,9 @@ protected override void CustomizeEditThing(GridCustomizeEditModelEventArgs e) { base.CustomizeEditThing(e); - var dataItem = (EngineeringModelRowViewModel)e.DataItem; - this.ShouldCreateThing = e.IsNew; - - if (dataItem == null) - { - this.ViewModel.Thing = new EngineeringModelSetup(); - e.EditModel = this.ViewModel.Thing; - return; - } - - e.EditModel = dataItem; - this.ViewModel.Thing = dataItem.Thing.Clone(true); + this.ViewModel.Thing = new EngineeringModelSetup(); + this.ViewModel.ResetSelectedValues(); + e.EditModel = this.ViewModel.Thing; } /// @@ -132,5 +128,15 @@ private void OnDetailsItemClick(ToolbarItemClickEventArgs e) { this.SelectedComponent = this.mapOfComponentsAndNames.First(x => x.Value == e.ItemName).Key; } + + /// + /// Sets the selected values for the creation and submits the form + /// + /// A + private async Task SetSelectedValuesAndSubmit() + { + this.ViewModel.SetupEngineeringModelWithSelectedValues(); + await this.Grid.SaveChangesAsync(); + } } } diff --git a/COMETwebapp/Components/SiteDirectory/EngineeringModel/ParticipantsTable.razor b/COMETwebapp/Components/SiteDirectory/EngineeringModel/ParticipantsTable.razor index 41a50a4f..47667161 100644 --- a/COMETwebapp/Components/SiteDirectory/EngineeringModel/ParticipantsTable.razor +++ b/COMETwebapp/Components/SiteDirectory/EngineeringModel/ParticipantsTable.razor @@ -26,7 +26,7 @@ Copyright (c) 2023-2024 RHEA System S.A. ShowAllRows="true" SearchBoxNullText="Search for a participant..." PopupEditFormCssClass="pw-800" - PopupEditFormHeaderText="Participant (UNDER DEV)" + PopupEditFormHeaderText="Participant" CustomizeEditModel="this.CustomizeEditThing" EditMode="GridEditMode.PopupEditForm" EditModelSaving="@(() => this.OnEditThingSaving())" diff --git a/COMETwebapp/Validators/EngineeringModel/EngineeringModelValidator.cs b/COMETwebapp/Validators/EngineeringModel/EngineeringModelValidator.cs new file mode 100644 index 00000000..aeeb3c1e --- /dev/null +++ b/COMETwebapp/Validators/EngineeringModel/EngineeringModelValidator.cs @@ -0,0 +1,74 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2023-2024 RHEA System S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Antoine Théate, João Rua +// +// This file is part of CDP4-COMET WEB Community Edition +// The CDP4-COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C. +// +// The CDP4-COMET WEB Community Edition is free software; you can redistribute it and/or +// modify it under the terms of the GNU Affero General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// The CDP4-COMET WEB Community Edition is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMETwebapp.Validators.EngineeringModel +{ + using CDP4Common.SiteDirectoryData; + using CDP4Common.Validation; + + using COMET.Web.Common.Extensions; + + using FluentValidation; + + /// + /// A class to validate the + /// + public class EngineeringModelValidator : AbstractValidator + { + /// + /// Instantiates a new + /// + public EngineeringModelValidator(IValidationService validationService) : base() + { + this.RuleFor(x => x.Name).Validate(validationService, nameof(EngineeringModelSetup.Name)); + this.RuleFor(x => x.ShortName).Validate(validationService, nameof(EngineeringModelSetup.ShortName)); + this.RuleFor(x => x.Kind).Validate(validationService, nameof(EngineeringModelSetup.Kind)); + this.RuleFor(x => x.StudyPhase).Validate(validationService, nameof(EngineeringModelSetup.StudyPhase)); + + this.RuleFor(x => x.SourceEngineeringModelSetupIid) + .NotEmpty() + .WithMessage("The Source Model must be selected if no Site RDL is set") + .Validate(validationService, nameof(EngineeringModelSetup.SourceEngineeringModelSetupIid)) + .Unless(x => x.RequiredRdl.Count > 0); + + this.RuleFor(x => x.DefaultOrganizationalParticipant) + .NotNull() + .WithMessage("The Model Admin Organization must not be empty if any organization was selected") + .Validate(validationService, nameof(EngineeringModelSetup.DefaultOrganizationalParticipant)) + .Unless(x => x.OrganizationalParticipant.Count == 0); + + this.RuleFor(x => x.ActiveDomain) + .Must(x => x.Count > 0) + .WithMessage("One or more active domains must be selected") + .Validate(validationService, nameof(EngineeringModelSetup.ActiveDomain)) + .Unless(x => x.SourceEngineeringModelSetupIid != null); + + this.RuleFor(x => x.RequiredRdl) + .Must(x => x.Count > 0) + .WithMessage("The Site RDL must be selected if no Source Model is set") + .Validate(validationService, nameof(EngineeringModelSetup.RequiredRdl)) + .Unless(x => x.SourceEngineeringModelSetupIid != null); + } + } +} diff --git a/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModel.cs b/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModel.cs index 93a6bba3..99c89d0b 100644 --- a/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModel.cs +++ b/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/EngineeringModelsTableViewModel.cs @@ -31,10 +31,13 @@ namespace COMETwebapp.ViewModels.Components.SiteDirectory.EngineeringModels using COMET.Web.Common.Services.SessionManagement; + using COMETwebapp.ViewModels.Components.Common.BaseDataItemTable; using COMETwebapp.ViewModels.Components.Common.DeletableDataItemTable; using COMETwebapp.ViewModels.Components.ReferenceData.ParameterTypes; using COMETwebapp.ViewModels.Components.SiteDirectory.Rows; + using ReactiveUI; + /// /// View model used to manage /// @@ -53,24 +56,147 @@ public EngineeringModelsTableViewModel(ISessionService sessionService, ICDPMessa } /// - /// Creates or edits a + /// Gets a collection of the available engineering models + /// + public IEnumerable EngineeringModels { get; private set; } + + /// + /// Gets a collection of all the possible model kinds + /// + public IEnumerable ModelKinds { get; private set; } = Enum.GetValues(); + + /// + /// Gets a collection of all the possible study phase kinds + /// + public IEnumerable StudyPhases { get; private set; } = Enum.GetValues(); + + /// + /// Gets a collection of the available site reference data libraries + /// + public IEnumerable SiteRdls { get; private set; } + + /// + /// Gets a collection of the available domains of expertise + /// + public IEnumerable DomainsOfExpertise { get; private set; } + + /// + /// Gets a collection of the available organizations + /// + public IEnumerable Organizations { get; private set; } + + /// + /// Gets or sets the selected site reference data library + /// + public SiteReferenceDataLibrary SelectedSiteRdl { get; set; } + + /// + /// Gets or sets the collection of selected active domains + /// + public IEnumerable SelectedActiveDomains { get; set; } = Enumerable.Empty(); + + /// + /// Gets or sets the collection of selected organizations + /// + public IEnumerable SelectedOrganizations { get; set; } = Enumerable.Empty(); + + /// + /// Gets or sets the selected model admin organization + /// + public Organization SelectedModelAdminOrganization { get; set; } + + /// + /// Gets or sets the selected source + /// + public EngineeringModelSetup SelectedSourceModel { get; set; } + + /// + /// Initializes the + /// + public override void InitializeViewModel() + { + base.InitializeViewModel(); + + var siteDirectory = this.SessionService.GetSiteDirectory(); + this.EngineeringModels = siteDirectory.Model.OrderBy(x => x.Name); + this.SiteRdls = siteDirectory.SiteReferenceDataLibrary.OrderBy(x => x.Name); + this.DomainsOfExpertise = siteDirectory.Domain.OrderBy(x => x.Name); + this.Organizations = siteDirectory.Organization.OrderBy(x => x.Name); + } + + /// + /// Resets the selected values + /// + public void ResetSelectedValues() + { + this.SelectedActiveDomains = Enumerable.Empty(); + this.SelectedOrganizations = Enumerable.Empty(); + this.SelectedModelAdminOrganization = null; + this.SelectedSiteRdl = null; + this.SelectedSourceModel = null; + } + + /// + /// Updates the current thing with the selected properties + /// + public void SetupEngineeringModelWithSelectedValues() + { + this.Thing.ActiveDomain = this.SelectedActiveDomains?.ToList(); + this.Thing.SourceEngineeringModelSetupIid = this.SelectedSourceModel?.Iid; + this.Thing.OrganizationalParticipant.Clear(); + this.Thing.RequiredRdl.Clear(); + + if (this.SelectedOrganizations != null) + { + this.Thing.OrganizationalParticipant.AddRange(this.SelectedOrganizations.Select(org => new OrganizationalParticipant() + { + Organization = org + })); + + this.Thing.DefaultOrganizationalParticipant = this.Thing.OrganizationalParticipant.FirstOrDefault(x => x.Organization == this.SelectedModelAdminOrganization); + } + + if (this.SelectedSiteRdl != null) + { + this.Thing.RequiredRdl.Add(new ModelReferenceDataLibrary() + { + RequiredRdl = this.SelectedSiteRdl, + Name = $"{this.Thing.Name} Model RDL", + ShortName = $"{this.Thing.ShortName}MRDL" + }); + } + } + + /// + /// Creates a new /// - /// The value to check if a new should be created /// A - public async Task CreateOrEditEngineeringModel(bool shouldCreate) + public async Task CreateEngineeringModel() { + this.IsLoading = true; + var siteDirectoryClone = this.SessionService.GetSiteDirectory().Clone(false); var thingsToCreate = new List(); + this.Thing.EngineeringModelIid = Guid.NewGuid(); - if (shouldCreate) + if (this.Thing.OrganizationalParticipant.Count > 0) { - siteDirectoryClone.Model.Add(this.Thing); - thingsToCreate.Add(siteDirectoryClone); + thingsToCreate.AddRange(this.Thing.OrganizationalParticipant); } + if (this.Thing.RequiredRdl.Count > 0) + { + thingsToCreate.AddRange(this.Thing.RequiredRdl); + } + + siteDirectoryClone.Model.Add(this.Thing); + thingsToCreate.Add(siteDirectoryClone); thingsToCreate.Add(this.Thing); + await this.SessionService.UpdateThings(siteDirectoryClone, thingsToCreate); await this.SessionService.RefreshSession(); + + this.IsLoading = false; } } } diff --git a/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/IEngineeringModelsTableViewModel.cs b/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/IEngineeringModelsTableViewModel.cs index 5ae729f3..fbf14261 100644 --- a/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/IEngineeringModelsTableViewModel.cs +++ b/COMETwebapp/ViewModels/Components/SiteDirectory/EngineeringModels/IEngineeringModelsTableViewModel.cs @@ -35,10 +35,74 @@ namespace COMETwebapp.ViewModels.Components.SiteDirectory.EngineeringModels public interface IEngineeringModelsTableViewModel : IDeletableDataItemTableViewModel { /// - /// Creates or edits a + /// Gets a collection of the available engineering models + /// + IEnumerable EngineeringModels { get; } + + /// + /// Gets a collection of all the possible model kinds + /// + IEnumerable ModelKinds { get; } + + /// + /// Gets a collection of all the possible study phase kinds + /// + IEnumerable StudyPhases { get; } + + /// + /// Gets a collection of the available site reference data libraries + /// + IEnumerable SiteRdls { get; } + + /// + /// Gets a collection of the available domains of expertise + /// + IEnumerable DomainsOfExpertise { get; } + + /// + /// Gets a collection of the available organizations + /// + IEnumerable Organizations { get; } + + /// + /// Gets or sets the collection of selected organizations + /// + IEnumerable SelectedOrganizations { get; set; } + + /// + /// Gets or sets the selected model admin organization + /// + Organization SelectedModelAdminOrganization { get; set; } + + /// + /// Gets or sets the collection of selected active domains + /// + IEnumerable SelectedActiveDomains { get; set; } + + /// + /// Gets or sets the selected site reference data library + /// + SiteReferenceDataLibrary SelectedSiteRdl { get; set; } + + /// + /// Gets or sets the selected source + /// + EngineeringModelSetup SelectedSourceModel { get; set; } + + /// + /// Updates the current thing with the selected properties + /// + void SetupEngineeringModelWithSelectedValues(); + + /// + /// Creates a new /// - /// The value to check if a new should be created /// A - Task CreateOrEditEngineeringModel(bool shouldCreate); + Task CreateEngineeringModel(); + + /// + /// Resets the current values + /// + void ResetSelectedValues(); } }