diff --git a/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs index a0ff55e9..35d27aa0 100644 --- a/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs +++ b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs @@ -36,7 +36,6 @@ namespace COMET.Web.Common.Tests.Components.BookEditor using COMET.Web.Common.ViewModels.Components.BookEditor; using DevExpress.Blazor; - using DevExpress.Blazor.Popup.Internal; using DynamicData; @@ -47,6 +46,8 @@ namespace COMET.Web.Common.Tests.Components.BookEditor using NUnit.Framework; + using RichardSzalay.MockHttp; + using TestContext = Bunit.TestContext; [TestFixture] @@ -61,6 +62,8 @@ public class EditotPopupTestFixture private bool onCancelCalled; private bool onAcceptCalled; private Mock sessionService; + private MockHttpMessageHandler mockHttpMessageHandler; + private HttpClient httpClient; [SetUp] public void Setup() @@ -69,7 +72,13 @@ public void Setup() this.context.ConfigureDevExpressBlazor(); this.sessionService = new Mock(); this.context.Services.AddSingleton(this.sessionService.Object); - + this.mockHttpMessageHandler = new MockHttpMessageHandler(); + this.httpClient = this.mockHttpMessageHandler.ToHttpClient(); + this.httpClient.BaseAddress = new Uri("http://localhost/"); + this.context.Services.AddScoped(_ => this.httpClient); + var httpResponse = new HttpResponseMessage(); + httpResponse.Content = new StringContent("{\n \"ShowName\": true,\n \"ShowShortName\" : true \n}\n"); + this.mockHttpMessageHandler.When(HttpMethod.Get, "/_content/CDP4.WEB.Common/BookInputConfiguration.json").Respond(_ => httpResponse); this.book = new Book(); this.activeDomains = new List diff --git a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs index bd9de519..8d13aa7d 100644 --- a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs +++ b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs @@ -24,8 +24,6 @@ namespace COMET.Web.Common.Tests.Components.BookEditor { - using System.Text.Json; - using Bunit; using CDP4Common.ReportingData; @@ -44,8 +42,6 @@ namespace COMET.Web.Common.Tests.Components.BookEditor using NUnit.Framework; - using RichardSzalay.MockHttp; - using TestContext = Bunit.TestContext; [TestFixture] @@ -57,8 +53,6 @@ public class InputEditorTestFixture private List activeDomains; private List availableCategories; private Mock sessionService; - private MockHttpMessageHandler mockHttpMessageHandler; - private HttpClient httpClient; private const string BookName = "Book Example"; private const string BookShortName = "bookExample"; @@ -69,13 +63,6 @@ public void Setup() this.context.ConfigureDevExpressBlazor(); this.sessionService = new Mock(); this.context.Services.AddSingleton(this.sessionService.Object); - this.mockHttpMessageHandler = new MockHttpMessageHandler(); - this.httpClient = this.mockHttpMessageHandler.ToHttpClient(); - this.httpClient.BaseAddress = new Uri("http://localhost/"); - this.context.Services.AddScoped(_ => this.httpClient); - var httpResponse = new HttpResponseMessage(); - httpResponse.Content = new StringContent("{\n \"ShowName\": true,\n \"ShowShortName\" : true \n}\n"); - this.mockHttpMessageHandler.When(HttpMethod.Get, "/_content/CDP4.WEB.Common/BookInputConfiguration.json").Respond(_ => httpResponse); this.activeDomains = new List { @@ -100,6 +87,8 @@ public void Setup() parameters.Add(p => p.Item, this.book); parameters.Add(p => p.ActiveDomains, this.activeDomains); parameters.Add(p => p.AvailableCategories, this.availableCategories); + parameters.Add(p => p.ShowName, true); + parameters.Add(p => p.ShowShortName, true); }); } @@ -119,6 +108,8 @@ public void VerifyComponent() Assert.IsNotNull(shortNameTextbox); Assert.That(combobox.Instance.Value, Is.EqualTo(this.activeDomains.First())); Assert.That(categoryComboBox.Instance, Is.Not.Null); + Assert.IsTrue(this.component.Instance.ShowName); + Assert.IsTrue(this.component.Instance.ShowShortName); }); this.component.Render(); diff --git a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor index 4d3482fc..994f736e 100644 --- a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor +++ b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor @@ -24,7 +24,7 @@ - +
@foreach (var validationError in this.ViewModel.ValidationErrors.Items) { diff --git a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs index 46434852..34745ef2 100644 --- a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs +++ b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs @@ -24,13 +24,19 @@ // -------------------------------------------------------------------------------------------------------------------- namespace COMET.Web.Common.Components.BookEditor -{ - using CDP4Common.ReportingData; - +{ + using System.Text.Json; + + using CDP4Common.ReportingData; + + using COMET.Web.Common.Model; using COMET.Web.Common.Services; + using COMET.Web.Common.Utilities; using COMET.Web.Common.ViewModels.Components.BookEditor; using Microsoft.AspNetCore.Components; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; using INamedThing = CDP4Common.CommonData.INamedThing; using IOwnedThing = CDP4Common.EngineeringModelData.IOwnedThing; @@ -47,6 +53,41 @@ public partial class EditorPopup [Parameter] public IEditorPopupViewModel ViewModel { get; set; } + /// + /// Gets or sets the + /// + [Inject] + public HttpClient HttpClient { get; set; } + + /// + /// Gets or sets the + /// + [Inject] + public IOptions Options { get; set; } + + [Inject] + public ILogger Logger { get; set; } + + /// + /// Sets if the component should show the name field + /// + private bool showName; + + /// + /// The name of the ShowName property on the configuration file + /// + private const string showNameConfigurationProperty = "ShowName"; + + /// + /// Sets if the component should show the shorname field + /// + private bool showShortName; + + /// + /// The name of the ShowShortName property on the configuration file + /// + private const string showShortNameConfigurationProperty = "ShowShortName"; + /// /// Method invoked when the component is ready to start, having received its /// initial parameters from its parent in the render tree. @@ -59,6 +100,40 @@ protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); this.Disposables.Add(this.ViewModel.ValidationErrors.Connect().Subscribe(_ => this.InvokeAsync(this.StateHasChanged))); + + var jsonFile = this.Options.Value.JsonConfigurationFile ?? "BookInputConfiguration.json"; + + try + { + var configurations = await this.GetBookInputConfigurationAsync(jsonFile); + + if (configurations.TryGetValue(showNameConfigurationProperty, out var showNameValue)) + { + this.showName = showNameValue; + } + + if (configurations.TryGetValue(showShortNameConfigurationProperty, out var showShortNameValue)) + { + this.showShortName = showShortNameValue; + } + } + catch (Exception e) + { + this.Logger.LogError(e, "Error while getting the configuration file."); + } + } + + /// + /// Acquires the BookInput configurations + /// + /// The file name that contains the configurations + /// A KeyValuePair collection with each available configuration + private async Task> GetBookInputConfigurationAsync(string fileName) + { + var path = ContentPathBuilder.BuildPath(fileName); + var jsonContent = await this.HttpClient.GetStreamAsync(path); + var configurations = JsonSerializer.Deserialize>(jsonContent); + return configurations; } /// @@ -68,13 +143,13 @@ private async Task OnConfirmClick() { var validationErrors = new List(); - if (this.ViewModel.Item is INamedThing namedThing) + if (this.ViewModel.Item is INamedThing namedThing && this.showName) { var error = ValidationService.ValidateProperty(nameof(namedThing.Name), namedThing.Name); validationErrors.Add(error); } - if (this.ViewModel.Item is IShortNamedThing shortNamedThing) + if (this.ViewModel.Item is IShortNamedThing shortNamedThing && this.showShortName) { var error = ValidationService.ValidateProperty(nameof(shortNamedThing.ShortName), shortNamedThing.ShortName); validationErrors.Add(error); diff --git a/COMET.Web.Common/Components/BookEditor/InputEditor.razor b/COMET.Web.Common/Components/BookEditor/InputEditor.razor index 37eda25a..c5c67448 100644 --- a/COMET.Web.Common/Components/BookEditor/InputEditor.razor +++ b/COMET.Web.Common/Components/BookEditor/InputEditor.razor @@ -28,7 +28,7 @@ @typeparam TItem
- @if (this.Item is INamedThing namedThing && this.showName) + @if (this.Item is INamedThing namedThing && this.ShowName) {

Name:

@@ -36,7 +36,7 @@
} - @if (this.Item is IShortNamedThing shortNamedThing && this.showShortName) + @if (this.Item is IShortNamedThing shortNamedThing && this.ShowShortName) {

ShortName:

diff --git a/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs b/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs index 4021b281..5fb6dd0c 100644 --- a/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs +++ b/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs @@ -27,6 +27,7 @@ namespace COMET.Web.Common.Components.BookEditor { using System.Text.Json; + using CDP4Common.CommonData; using CDP4Common.EngineeringModelData; using CDP4Common.SiteDirectoryData; @@ -49,18 +50,6 @@ public partial class InputEditor [Inject] public ILogger> Logger { get; set; } - /// - /// Gets or sets the - /// - [Inject] - public HttpClient HttpClient { get; set; } - - /// - /// Gets or sets the - /// - [Inject] - public IOptions Options { get; set; } - /// /// Gets or sets the /// @@ -84,27 +73,19 @@ public partial class InputEditor ///
[Parameter] public IEnumerable AvailableCategories { get; set; } - - /// - /// Sets if the component should show the name field - /// - private bool showName; - - /// - /// The name of the ShowName property on the configuration file - /// - private const string showNameConfigurationProperty = "ShowName"; - + /// - /// Sets if the component should show the shorname field + /// Gets or sets if the InputEditor should display the Name field /// - private bool showShortName; - + [Parameter] + public bool ShowName { get; set; } + /// - /// The name of the ShowShortName property on the configuration file + /// Gets or sets if the InputEditor should display the ShortName field /// - private const string showShortNameConfigurationProperty = "ShowShortName"; - + [Parameter] + public bool ShowShortName { get; set; } + /// /// Method invoked when the component is ready to start, having received its /// initial parameters from its parent in the render tree. @@ -117,46 +98,30 @@ protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); - var jsonFile = this.Options.Value.JsonConfigurationFile ?? "BookInputConfiguration.json"; - try { - var configurations = await this.GetBookInputConfigurationAsync(jsonFile); - - if (configurations.TryGetValue(showNameConfigurationProperty, out var showNameValue)) + if (this.Item is IOwnedThing ownedThing) { - this.showName = showNameValue; + ownedThing.Owner = this.SessionService.Session.ActivePerson.DefaultDomain; } - if (configurations.TryGetValue(showShortNameConfigurationProperty, out var showShortNameValue)) + //Name and ShortName are required fields on the SDK - setting these to - as default, as per request on the ticket. + if (this.Item is INamedThing namedThing && !this.ShowName) { - this.showShortName = showShortNameValue; + namedThing.Name = "-"; } - if (this.Item is IOwnedThing ownedThing) + if (this.Item is IShortNamedThing shortNamedThing && !this.ShowShortName) { - ownedThing.Owner = this.SessionService.Session.ActivePerson.DefaultDomain; + shortNamedThing.ShortName = "-"; } } - catch (Exception e) + catch (Exception ex) { - this.Logger.LogError(e, "Error while getting the configuration file."); + this.Logger.LogError(ex, "Exception while setting default values of the InputEditor."); } } - /// - /// Acquires the BookInput configurations - /// - /// The file name that contains the configurations - /// A KeyValuePair collection with each available configuration - private async Task> GetBookInputConfigurationAsync(string fileName) - { - var path = ContentPathBuilder.BuildPath(fileName); - var jsonContent = await this.HttpClient.GetStreamAsync(path); - var configurations = JsonSerializer.Deserialize>(jsonContent); - return configurations; - } - /// /// Handler for when the selected categories changed /// diff --git a/COMET.Web.Common/wwwroot/BookInputConfiguration.json b/COMET.Web.Common/wwwroot/BookInputConfiguration.json index a0c6a76d..cd465b2a 100644 --- a/COMET.Web.Common/wwwroot/BookInputConfiguration.json +++ b/COMET.Web.Common/wwwroot/BookInputConfiguration.json @@ -1,4 +1,4 @@ { "ShowName": true, "ShowShortName" : true -} +} \ No newline at end of file