From 64ea608385c8c3f2914fcaac45f42826ea7e424e Mon Sep 17 00:00:00 2001 From: antoineatrhea Date: Sat, 14 Oct 2023 21:39:19 +0200 Subject: [PATCH 1/2] Fix #477- ApplicationTemplate can be used as is --- .../Applications/ApplicationTemplate.razor | 29 +- .../Applications/ApplicationTemplate.razor.cs | 65 +- .../GenericApplicationTemplate.razor | 24 + .../GenericApplicationTemplate.razor.cs | 76 ++ .../SingleThingApplicationTemplate.razor | 2 +- .../Extensions/ServiceCollectionExtensions.cs | 2 + .../Extensions/ThingExtensions.cs | 1118 ++++++++--------- 7 files changed, 703 insertions(+), 613 deletions(-) create mode 100644 COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor create mode 100644 COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor.cs diff --git a/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor b/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor index 72ed67af..b87b8b37 100644 --- a/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor +++ b/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor @@ -1,24 +1,23 @@  @namespace COMET.Web.Common.Components.Applications -@typeparam TViewModel where TViewModel: class, COMET.Web.Common.ViewModels.Components.Applications.IApplicationTemplateViewModel -@inherits DisposableComponent +@inherits GenericApplicationTemplate \ No newline at end of file diff --git a/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor.cs b/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor.cs index 1591f5f7..e31ae072 100644 --- a/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor.cs +++ b/COMET.Web.Common/Components/Applications/ApplicationTemplate.razor.cs @@ -25,54 +25,43 @@ namespace COMET.Web.Common.Components.Applications { - using System.ComponentModel; + using COMET.Web.Common.Extensions; - using COMET.Web.Common.Services.ConfigurationService; - using COMET.Web.Common.Utilities; - using COMET.Web.Common.ViewModels.Components.Applications; - - using Microsoft.AspNetCore.Components; + using Microsoft.AspNetCore.WebUtilities; /// - /// Shared component that will englobe all applications - /// - /// Any - public partial class ApplicationTemplate - { - /// - /// Body of the application - /// - [Parameter] - public RenderFragment Body { get; set; } - - /// - /// Gets or Sets the - /// - [Inject] - public TViewModel ViewModel { get; set; } - - /// - /// The - /// - [Inject] - public NavigationManager NavigationManager { get; set; } + /// Shared component that will englobe any applications that on need to be connected to a 10-25 datasource + /// + public partial class ApplicationTemplate + { + /// + /// Method invoked when the component is ready to start, having received its + /// initial parameters from its parent in the render tree. + /// + protected override void OnInitialized() + { + base.OnInitialized(); - /// - /// The - /// - [Inject] - public IConfigurationService ConfigurationService { get; set; } + this.SetCorrectUrl(); + } /// - /// Set URL parameters based on the current context + /// Sets the correct url based on the context /// - /// A of URL parameters - protected virtual void SetUrlParameters(Dictionary currentOptions) + internal void SetCorrectUrl() { - if (string.IsNullOrEmpty(this.ConfigurationService.ServerConfiguration.ServerAddress)) + var urlPage = this.NavigationManager.Uri.Replace(this.NavigationManager.BaseUri, string.Empty).Split('?')[0]; + var currentOptions = this.NavigationManager.Uri.GetParametersFromUrl(); + this.SetUrlParameters(currentOptions); + var targetOptions = new Dictionary(); + + foreach (var currentOption in currentOptions.Where(x => !string.IsNullOrEmpty(x.Value))) { - currentOptions[QueryKeys.ServerKey] = this.ViewModel.SessionService.Session.DataSourceUri; + targetOptions[currentOption.Key] = currentOption.Value; } + + var targetUrl = QueryHelpers.AddQueryString(urlPage, targetOptions); + this.NavigationManager.NavigateTo(targetUrl); } } } diff --git a/COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor b/COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor new file mode 100644 index 00000000..72ed67af --- /dev/null +++ b/COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor @@ -0,0 +1,24 @@ + +@namespace COMET.Web.Common.Components.Applications +@typeparam TViewModel where TViewModel: class, COMET.Web.Common.ViewModels.Components.Applications.IApplicationTemplateViewModel +@inherits DisposableComponent diff --git a/COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor.cs b/COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor.cs new file mode 100644 index 00000000..7adcfdb1 --- /dev/null +++ b/COMET.Web.Common/Components/Applications/GenericApplicationTemplate.razor.cs @@ -0,0 +1,76 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2023 RHEA System S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine +// +// This file is part of COMET WEB Community Edition +// The COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 +// Annex A and Annex C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMET.Web.Common.Components.Applications +{ + using COMET.Web.Common.Services.ConfigurationService; + using COMET.Web.Common.Utilities; + using COMET.Web.Common.ViewModels.Components.Applications; + + using Microsoft.AspNetCore.Components; + + /// + /// Shared component that will englobe all applications + /// + /// Any + public abstract partial class GenericApplicationTemplate + { + /// + /// Body of the application + /// + [Parameter] + public RenderFragment Body { get; set; } + + /// + /// Gets or Sets the + /// + [Inject] + public TViewModel ViewModel { get; set; } + + /// + /// The + /// + [Inject] + public NavigationManager NavigationManager { get; set; } + + /// + /// The + /// + [Inject] + public IConfigurationService ConfigurationService { get; set; } + + /// + /// Set URL parameters based on the current context + /// + /// A of URL parameters + protected virtual void SetUrlParameters(Dictionary currentOptions) + { + if (string.IsNullOrEmpty(this.ConfigurationService.ServerConfiguration.ServerAddress)) + { + currentOptions[QueryKeys.ServerKey] = this.ViewModel.SessionService.Session.DataSourceUri; + } + } + } +} diff --git a/COMET.Web.Common/Components/Applications/SingleThingApplicationTemplate.razor b/COMET.Web.Common/Components/Applications/SingleThingApplicationTemplate.razor index 702e22d1..ce15cd1c 100644 --- a/COMET.Web.Common/Components/Applications/SingleThingApplicationTemplate.razor +++ b/COMET.Web.Common/Components/Applications/SingleThingApplicationTemplate.razor @@ -22,4 +22,4 @@ @namespace COMET.Web.Common.Components.Applications @typeparam TThing where TThing: CDP4Common.CommonData.Thing @typeparam TViewModel where TViewModel: class, COMET.Web.Common.ViewModels.Components.Applications.ISingleThingApplicationTemplateViewModel -@inherits ApplicationTemplate \ No newline at end of file +@inherits GenericApplicationTemplate \ No newline at end of file diff --git a/COMET.Web.Common/Extensions/ServiceCollectionExtensions.cs b/COMET.Web.Common/Extensions/ServiceCollectionExtensions.cs index c6272e65..4880425a 100644 --- a/COMET.Web.Common/Extensions/ServiceCollectionExtensions.cs +++ b/COMET.Web.Common/Extensions/ServiceCollectionExtensions.cs @@ -100,6 +100,8 @@ private static void RegisterCommonViewModels(this IServiceCollection serviceProv serviceProvider.AddScoped(); serviceProvider.AddTransient(); serviceProvider.AddTransient(); + serviceProvider.AddTransient(); + serviceProvider.AddTransient(); serviceProvider.AddTransient(); } } diff --git a/COMET.Web.Common/Extensions/ThingExtensions.cs b/COMET.Web.Common/Extensions/ThingExtensions.cs index 32fd5604..305a7983 100644 --- a/COMET.Web.Common/Extensions/ThingExtensions.cs +++ b/COMET.Web.Common/Extensions/ThingExtensions.cs @@ -1,563 +1,563 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) 2023 RHEA System S.A. -// -// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, Nabil Abbar -// -// This file is part of COMET WEB Community Edition -// The COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 -// Annex A and Annex C. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace COMET.Web.Common.Extensions -{ - using CDP4Common.CommonData; - using CDP4Common.EngineeringModelData; - using CDP4Common.Helpers; - using CDP4Common.SiteDirectoryData; - using CDP4Common.Types; - - /// - /// Extension class for - /// - public static class ThingExtensions - { - /// - /// Gets the name of the - /// - /// The - /// The name of the - public static string GetName(this Iteration iteration) - { - var engineeringSetup = (EngineeringModelSetup)iteration.IterationSetup.Container; - return $"{engineeringSetup.Name} - Iteration {iteration.IterationSetup.IterationNumber}"; - } - - /// - /// Queries the name of the of an - /// - /// The current - /// The name of the - public static string QueryModelName(this Iteration iteration) - { - return (iteration?.IterationSetup.Container as EngineeringModelSetup)?.Name; - } - - /// - /// Queries all that are used inside an - /// - /// The - /// A collection of - public static IEnumerable QueryUsedElementDefinitions(this Iteration iteration) - { - if (iteration is null) - { - throw new ArgumentNullException(nameof(iteration)); - } - - var elementBase = iteration.QueryNestedElements(); - var elementDefinitions = new List(); - - foreach (var nestedElement in elementBase) - { - switch (nestedElement.GetElementBase()) - { - case ElementDefinition elementDefinition: - elementDefinitions.Add(elementDefinition); - break; - case ElementUsage elementUsage: - elementDefinitions.Add(elementUsage.ElementDefinition); - break; - } - } - - return elementDefinitions.DistinctBy(x => x.Iid); - } - - /// - /// Queries all of the given - /// - /// The - /// A collection of - public static IEnumerable QueryNestedElements(this Iteration iteration) - { - var nestedElementTreeGenerator = new NestedElementTreeGenerator(); - var nestedElements = new List(); - - if (iteration.TopElement != null) - { - nestedElements.AddRange(iteration.Option.SelectMany(o => nestedElementTreeGenerator.Generate(o))); - } - - return nestedElements; - } - - /// - /// Queries all of the given based on - /// - /// The - /// The - /// A collection of - public static IEnumerable QueryNestedElements(this Iteration iteration, Option option) - { - var nestedElementTreeGenerator = new NestedElementTreeGenerator(); - var nestedElements = new List(); - - if (iteration.TopElement != null) - { - nestedElements.AddRange(nestedElementTreeGenerator.Generate(option)); - } - - return nestedElements; - } - - /// - /// Queries used inside an - /// - /// The - /// The collection of used - public static IEnumerable QueryUsedParameterTypes(this Iteration iteration) - { - return iteration.Element.SelectMany(x => x.Parameter).Select(x => x.ParameterType).DistinctBy(x => x.Iid); - } - - /// - /// Queries all of the given iteration - /// - /// The - /// A collection of - public static IEnumerable QueryParameterValueSetBase(this Iteration iteration) - { - var valueSets = new List(); - - if (iteration.TopElement != null) - { - valueSets.AddRange(iteration.TopElement.Parameter.SelectMany(x => x.ValueSet)); - } - - foreach (var elementUsage in iteration.Element.SelectMany(elementDefinition => elementDefinition.ContainedElement)) - { - if (!elementUsage.ParameterOverride.Any()) - { - valueSets.AddRange(elementUsage.ElementDefinition.Parameter.SelectMany(x => x.ValueSet)); - } - else - { - valueSets.AddRange(elementUsage.ParameterOverride.SelectMany(x => x.ValueSet)); - - valueSets.AddRange(elementUsage.ElementDefinition.Parameter.Where(x => elementUsage.ParameterOverride.All(o => o.Parameter.Iid != x.Iid)) - .SelectMany(x => x.ValueSet)); - } - } - - return valueSets.DistinctBy(x => x.Iid); - } - - /// - /// Queries all that belongs to a given - /// - /// The to get the s - /// The - /// A collection of - public static IEnumerable QueryNestedParameters(this Iteration iteration, Option option) - { - var generator = new NestedElementTreeGenerator(); - return iteration.TopElement == null ? Enumerable.Empty() : generator.GetNestedParameters(option); - } - +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2023 RHEA System S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, Nabil Abbar +// +// This file is part of COMET WEB Community Edition +// The COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 +// Annex A and Annex C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMET.Web.Common.Extensions +{ + using CDP4Common.CommonData; + using CDP4Common.EngineeringModelData; + using CDP4Common.Helpers; + using CDP4Common.SiteDirectoryData; + using CDP4Common.Types; + + /// + /// Extension class for + /// + public static class ThingExtensions + { + /// + /// Gets the name of the + /// + /// The + /// The name of the + public static string GetName(this Iteration iteration) + { + var engineeringSetup = (EngineeringModelSetup)iteration.IterationSetup.Container; + return $"{engineeringSetup.Name} - Iteration {iteration.IterationSetup.IterationNumber}"; + } + + /// + /// Queries the name of the of an + /// + /// The current + /// The name of the + public static string QueryModelName(this Iteration iteration) + { + return (iteration?.IterationSetup.Container as EngineeringModelSetup)?.Name; + } + + /// + /// Queries all that are used inside an + /// + /// The + /// A collection of + public static IEnumerable QueryUsedElementDefinitions(this Iteration iteration) + { + if (iteration is null) + { + throw new ArgumentNullException(nameof(iteration)); + } + + var elementBase = iteration.QueryNestedElements(); + var elementDefinitions = new List(); + + foreach (var nestedElement in elementBase) + { + switch (nestedElement.GetElementBase()) + { + case ElementDefinition elementDefinition: + elementDefinitions.Add(elementDefinition); + break; + case ElementUsage elementUsage: + elementDefinitions.Add(elementUsage.ElementDefinition); + break; + } + } + + return elementDefinitions.DistinctBy(x => x.Iid); + } + + /// + /// Queries all of the given + /// + /// The + /// A collection of + public static IEnumerable QueryNestedElements(this Iteration iteration) + { + var nestedElementTreeGenerator = new NestedElementTreeGenerator(); + var nestedElements = new List(); + + if (iteration.TopElement != null) + { + nestedElements.AddRange(iteration.Option.SelectMany(o => nestedElementTreeGenerator.Generate(o))); + } + + return nestedElements; + } + + /// + /// Queries all of the given based on + /// + /// The + /// The + /// A collection of + public static IEnumerable QueryNestedElements(this Iteration iteration, Option option) + { + var nestedElementTreeGenerator = new NestedElementTreeGenerator(); + var nestedElements = new List(); + + if (iteration.TopElement != null) + { + nestedElements.AddRange(nestedElementTreeGenerator.Generate(option)); + } + + return nestedElements; + } + + /// + /// Queries used inside an + /// + /// The + /// The collection of used + public static IEnumerable QueryUsedParameterTypes(this Iteration iteration) + { + return iteration.Element.SelectMany(x => x.Parameter).Select(x => x.ParameterType).DistinctBy(x => x.Iid); + } + + /// + /// Queries all of the given iteration + /// + /// The + /// A collection of + public static IEnumerable QueryParameterValueSetBase(this Iteration iteration) + { + var valueSets = new List(); + + if (iteration.TopElement != null) + { + valueSets.AddRange(iteration.TopElement.Parameter.SelectMany(x => x.ValueSet)); + } + + foreach (var elementUsage in iteration.Element.SelectMany(elementDefinition => elementDefinition.ContainedElement)) + { + if (!elementUsage.ParameterOverride.Any()) + { + valueSets.AddRange(elementUsage.ElementDefinition.Parameter.SelectMany(x => x.ValueSet)); + } + else + { + valueSets.AddRange(elementUsage.ParameterOverride.SelectMany(x => x.ValueSet)); + + valueSets.AddRange(elementUsage.ElementDefinition.Parameter.Where(x => elementUsage.ParameterOverride.All(o => o.Parameter.Iid != x.Iid)) + .SelectMany(x => x.ValueSet)); + } + } + + return valueSets.DistinctBy(x => x.Iid); + } + + /// + /// Queries all that belongs to a given + /// + /// The to get the s + /// The + /// A collection of + public static IEnumerable QueryNestedParameters(this Iteration iteration, Option option) + { + var generator = new NestedElementTreeGenerator(); + return iteration.TopElement == null ? Enumerable.Empty() : generator.GetNestedParameters(option); + } + /// /// Queries all the contained in an iteration /// /// the iteration - /// A collection of - public static IEnumerable QueryParameterAndOverrideBases(this Iteration iteration) - { - var parameters = new List(); - - var elements = iteration.QueryElementsBase(); - - foreach (var element in elements) - { - parameters.AddRange(element.QueryParameterAndOverrideBases()); - } - - return parameters; - } - - /// - /// Queries all that belongs to a given - /// - /// The to get the s - /// The - /// A collection of - public static IEnumerable QueryParameterAndOverrideBases(this Iteration iteration, Option option) - { - var elements = iteration.QueryNestedElements(option); - var parameters = new List(); - - foreach (var nestedElement in elements.Select(x => x.GetElementBase())) - { - parameters.AddRange(nestedElement.QueryParameterAndOverrideBases()); - } - - return parameters.DistinctBy(x => x.Iid); - } - - /// - /// Queries all that belongs to a given owned by a - /// - /// - /// The to get the s - /// The - /// The - /// A collection of - public static IEnumerable QueryParameterAndOverrideBases(this Iteration iteration, Option option, DomainOfExpertise domain) - { - var elements = iteration.QueryNestedElements(option); - var parameters = new List(); - - foreach (var nestedElement in elements.Select(x => x.GetElementBase())) - { - parameters.AddRange(nestedElement.QueryParameterAndOverrideBases(domain)); - } - - return parameters.DistinctBy(x => x.Iid); - } - - /// - /// Queries all contains in an - /// - /// The - /// A collection of - /// - /// If the is an , it will retrieve all - /// - /// and of its - /// - public static IEnumerable QueryParameterAndOverrideBases(this ElementBase elementBase) - { - switch (elementBase) - { - case ElementDefinition elementDefinition: - return elementDefinition.Parameter; - case ElementUsage usage: - var parameterAndOverrideBases = new List(usage.ParameterOverride); - parameterAndOverrideBases.AddRange(usage.ElementDefinition.QueryParameterAndOverrideBases()); - return parameterAndOverrideBases; - default: - return Enumerable.Empty(); - } - } - - /// - /// Queries all contains in an owned by a - /// - /// - /// The - /// The owner - /// A collection of - /// - /// If the is an , it will retrieve all - /// - /// and of its - /// - public static IEnumerable QueryParameterAndOverrideBases(this ElementBase elementBase, DomainOfExpertise domain) - { - switch (elementBase) - { - case ElementDefinition elementDefinition: - return elementDefinition.Parameter.Where(x => x.Owner.Iid == domain.Iid); - case ElementUsage usage: - var parameterAndOverrideBases = new List(usage.ParameterOverride.Where(x => x.Owner.Iid == domain.Iid)); - parameterAndOverrideBases.AddRange(usage.ElementDefinition.QueryParameterAndOverrideBases(domain)); - return parameterAndOverrideBases; - default: - return Enumerable.Empty(); - } - } - - /// - /// Queries all the unreferenced in an - /// An unreferenced element is an element with no associated ElementUsage - /// - /// The - /// All unreferenced - public static IEnumerable QueryUnreferencedElements(this Iteration iteration) - { - var elementUsages = iteration.Element.SelectMany(x => x.ContainedElement).ToList(); - - var associatedElementDefinitions = elementUsages.Select(x => x.ElementDefinition); - - var unreferencedElementDefinitions = iteration.Element.ToList(); - unreferencedElementDefinitions.RemoveAll(x => associatedElementDefinitions.Any(e => e.Iid == x.Iid)); - unreferencedElementDefinitions.RemoveAll(x => x.Iid == iteration.TopElement.Iid); - - return unreferencedElementDefinitions; - } - - /// - /// Queries unused in an - /// An unused element is an element not used in an option - /// - /// The - /// All unused - public static IEnumerable QueryUnusedElementDefinitions(this Iteration iteration) - { - var nestedElements = iteration.QueryNestedElements().ToList(); - - var associatedElements = nestedElements.SelectMany(x => x.ElementUsage.Select(e => e.ElementDefinition)) - .DistinctBy(x => x.Iid).ToList(); - - var unusedElementDefinitions = iteration.Element.ToList(); - unusedElementDefinitions.RemoveAll(e => associatedElements.Any(x => x.Iid == e.Iid)); - unusedElementDefinitions.RemoveAll(x => x.Iid == iteration.TopElement?.Iid); - return unusedElementDefinitions; - } - - /// - /// Gets the from this iteration - /// - /// the iteration used for retrieving the elements - /// an - /// if the iteration is null - public static IEnumerable QueryElementsBase(this Iteration iteration) - { - if (iteration is null) - { - throw new ArgumentNullException(nameof(iteration)); - } - - var elements = new List(); - - if (iteration.TopElement is not null) - { - elements.Add(iteration.TopElement); - } - - iteration.Element.ForEach(e => elements.AddRange(e.ContainedElement)); - - return elements; - } - - /// - /// Queries all owned by a given - /// contained into an - /// - /// The - /// The - /// A collection of - public static IEnumerable QueryOwnedParameterSubscriptions(this Iteration iteration, DomainOfExpertise domain) - { - var subscriptions = new List(); - - if (iteration.TopElement != null) - { - subscriptions.AddRange(iteration.TopElement.QueryOwnedParameterSubscriptions(domain)); - } - - subscriptions.AddRange(iteration.Element.SelectMany(x => x.ContainedElement).SelectMany(x => x.QueryOwnedParameterSubscriptions(domain))); - return subscriptions.DistinctBy(x => x.Iid).OrderBy(p => p.ParameterType.Name); - } - - /// - /// Queries all owned by a given - /// contained into an - /// - /// The - /// The - /// A collection of - public static IEnumerable QueryOwnedParameterSubscriptions(this ElementBase element, DomainOfExpertise domain) - { - var subscriptions = new List(); - - switch (element) - { - case ElementDefinition elementDefinition: - subscriptions.AddRange(elementDefinition.Parameter.QueryOwnedParameterSubscriptions(domain)); - break; - case ElementUsage elementUsage when !elementUsage.ParameterOverride.Any(): - return elementUsage.ElementDefinition.QueryOwnedParameterSubscriptions(domain); - case ElementUsage elementUsage: - var notOverridenParameters = elementUsage.ElementDefinition.Parameter.Where(x => elementUsage.ParameterOverride.All(p => p.Parameter.Iid != x.Iid)); - - subscriptions.AddRange(elementUsage.ParameterOverride.QueryOwnedParameterSubscriptions(domain)); - - subscriptions.AddRange(notOverridenParameters.QueryOwnedParameterSubscriptions(domain)); - break; - } - - return subscriptions; - } - - /// - /// Queries all owned by a given - /// contained into a collection of - /// - /// The collection of - /// The - /// A collection of - public static IEnumerable QueryOwnedParameterSubscriptions(this IEnumerable parameterOrOverrideBases, - DomainOfExpertise domain) - { - return parameterOrOverrideBases.Where(x => x.Owner.Iid != domain.Iid) - .SelectMany(x => x.ParameterSubscription.Where(p => p.Owner.Iid == domain.Iid)); - } - - /// - /// Queries owned contained into an - /// that contains of other - /// - /// The - /// The - /// A collection of - public static IEnumerable QuerySubscribedParameterByOthers(this Iteration iteration, DomainOfExpertise domain) - { - var subscriptions = new List(); - - if (iteration.TopElement != null) - { - subscriptions.AddRange(iteration.TopElement.QuerySubscribedParameterByOthers(domain)); - } - - subscriptions.AddRange(iteration.Element.SelectMany(x => x.ContainedElement).SelectMany(x => x.QuerySubscribedParameterByOthers(domain))); - return subscriptions.DistinctBy(x => x.Iid).OrderBy(p => p.ParameterType.Name); - } - - /// - /// Queries owned contained into an - /// that contains of other - /// - /// The - /// The - /// A collection of - public static IEnumerable QuerySubscribedParameterByOthers(this ElementBase element, DomainOfExpertise domain) - { - var subscriptions = new List(); - - switch (element) - { - case ElementDefinition elementDefinition: - subscriptions.AddRange(elementDefinition.Parameter.QuerySubscribedParameterByOthers(domain)); - break; - case ElementUsage elementUsage when !elementUsage.ParameterOverride.Any(): - return elementUsage.ElementDefinition.QuerySubscribedParameterByOthers(domain); - case ElementUsage elementUsage: - var notOverridenParameters = elementUsage.ElementDefinition.Parameter.Where(x => elementUsage.ParameterOverride.All(p => p.Parameter.Iid != x.Iid)); - subscriptions.AddRange(elementUsage.ParameterOverride.QuerySubscribedParameterByOthers(domain)); - subscriptions.AddRange(notOverridenParameters.QuerySubscribedParameterByOthers(domain)); - break; - } - - return subscriptions; - } - - /// - /// Queries all owned contained into a collection of - /// - /// that contains of other - /// - /// The collection of - /// The - /// A collection of - public static IEnumerable QuerySubscribedParameterByOthers(this IEnumerable parameterOrOverrideBases, - DomainOfExpertise domain) - { - return parameterOrOverrideBases.Where(x => x.Owner.Iid == domain.Iid - && x.ParameterSubscription.Any(p => p.Owner.Iid != domain.Iid)); - } - - /// - /// Query the evolution of contained into a - /// - /// - /// The - /// - /// All changes for each , - /// collected inside a where the key is the revision number - /// - public static Dictionary>> QueryParameterSubscriptionValueSetEvolution(this ParameterSubscription parameterSubscription) - { - var changes = new Dictionary>>(); - - foreach (var parameterSubscriptionValueSet in parameterSubscription.ValueSet) - { - changes[parameterSubscriptionValueSet.Iid] = parameterSubscriptionValueSet.QueryParameterSubscriptionValueSetEvolution(); - } - - return changes; - } - - /// - /// Queries the evolution of the of the Computed Value of the - /// - /// - /// The - /// All changes, collected inside a where the key is the revision number - public static Dictionary> QueryParameterSubscriptionValueSetEvolution(this ParameterSubscriptionValueSet valueSet) - { - var changes = new Dictionary>(); - var subscribedParameterValueSet = valueSet.SubscribedValueSet; - - if (!subscribedParameterValueSet.Revisions.Any() || !subscribedParameterValueSet.Revisions.Keys.Any(x => x >= valueSet.RevisionNumber)) - { - changes.Add(subscribedParameterValueSet.RevisionNumber, subscribedParameterValueSet.Published); - return changes; - } - - var currentRevisionNumber = subscribedParameterValueSet.Revisions.Keys.Where(x => x >= valueSet.RevisionNumber).Min(); - var currentValueSet = ((ParameterValueSetBase)subscribedParameterValueSet.Revisions[currentRevisionNumber]).Published; - - changes.Add(currentRevisionNumber, currentValueSet); - - foreach (var revisionNumber in subscribedParameterValueSet.Revisions.Keys.Where(x => x > currentRevisionNumber).ToList()) - { - var publishedValueAtRevisionNumber = ((ParameterValueSetBase)subscribedParameterValueSet.Revisions[revisionNumber]).Published; - - if (!publishedValueAtRevisionNumber.ContainsSameValues(currentValueSet)) - { - currentRevisionNumber = revisionNumber; - currentValueSet = publishedValueAtRevisionNumber; - changes.Add(currentRevisionNumber, currentValueSet); - } - } - - if (currentRevisionNumber != subscribedParameterValueSet.RevisionNumber && !subscribedParameterValueSet.Published.ContainsSameValues(currentValueSet)) - { - changes.Add(subscribedParameterValueSet.RevisionNumber, subscribedParameterValueSet.Published); - } - - return changes; - } - - /// - /// Queries the name of a - /// - /// The - /// The name - public static string QueryName(this IParameterTypeAssignment parameterTypeAssignment) - { - var name = parameterTypeAssignment.ParameterType.Name; - - if (parameterTypeAssignment.MeasurementScale != null) - { - name += $" [{parameterTypeAssignment.MeasurementScale.ShortName}]"; - } - - return name; - } - } -} + /// A collection of + public static IEnumerable QueryParameterAndOverrideBases(this Iteration iteration) + { + var parameters = new List(); + + var elements = iteration.QueryElementsBase(); + + foreach (var element in elements) + { + parameters.AddRange(element.QueryParameterAndOverrideBases()); + } + + return parameters; + } + + /// + /// Queries all that belongs to a given + /// + /// The to get the s + /// The + /// A collection of + public static IEnumerable QueryParameterAndOverrideBases(this Iteration iteration, Option option) + { + var elements = iteration.QueryNestedElements(option); + var parameters = new List(); + + foreach (var nestedElement in elements.Select(x => x.GetElementBase())) + { + parameters.AddRange(nestedElement.QueryParameterAndOverrideBases()); + } + + return parameters.DistinctBy(x => x.Iid); + } + + /// + /// Queries all that belongs to a given owned by a + /// + /// + /// The to get the s + /// The + /// The + /// A collection of + public static IEnumerable QueryParameterAndOverrideBases(this Iteration iteration, Option option, DomainOfExpertise domain) + { + var elements = iteration.QueryNestedElements(option); + var parameters = new List(); + + foreach (var nestedElement in elements.Select(x => x.GetElementBase())) + { + parameters.AddRange(nestedElement.QueryParameterAndOverrideBases(domain)); + } + + return parameters.DistinctBy(x => x.Iid); + } + + /// + /// Queries all contains in an + /// + /// The + /// A collection of + /// + /// If the is an , it will retrieve all + /// + /// and of its + /// + public static IEnumerable QueryParameterAndOverrideBases(this ElementBase elementBase) + { + switch (elementBase) + { + case ElementDefinition elementDefinition: + return elementDefinition.Parameter; + case ElementUsage usage: + var parameterAndOverrideBases = new List(usage.ParameterOverride); + parameterAndOverrideBases.AddRange(usage.ElementDefinition.QueryParameterAndOverrideBases()); + return parameterAndOverrideBases; + default: + return Enumerable.Empty(); + } + } + + /// + /// Queries all contains in an owned by a + /// + /// + /// The + /// The owner + /// A collection of + /// + /// If the is an , it will retrieve all + /// + /// and of its + /// + public static IEnumerable QueryParameterAndOverrideBases(this ElementBase elementBase, DomainOfExpertise domain) + { + switch (elementBase) + { + case ElementDefinition elementDefinition: + return elementDefinition.Parameter.Where(x => x.Owner.Iid == domain.Iid); + case ElementUsage usage: + var parameterAndOverrideBases = new List(usage.ParameterOverride.Where(x => x.Owner.Iid == domain.Iid)); + parameterAndOverrideBases.AddRange(usage.ElementDefinition.QueryParameterAndOverrideBases(domain)); + return parameterAndOverrideBases; + default: + return Enumerable.Empty(); + } + } + + /// + /// Queries all the unreferenced in an + /// An unreferenced element is an element with no associated ElementUsage + /// + /// The + /// All unreferenced + public static IEnumerable QueryUnreferencedElements(this Iteration iteration) + { + var elementUsages = iteration.Element.SelectMany(x => x.ContainedElement).ToList(); + + var associatedElementDefinitions = elementUsages.Select(x => x.ElementDefinition); + + var unreferencedElementDefinitions = iteration.Element.ToList(); + unreferencedElementDefinitions.RemoveAll(x => associatedElementDefinitions.Any(e => e.Iid == x.Iid)); + unreferencedElementDefinitions.RemoveAll(x => x.Iid == iteration.TopElement.Iid); + + return unreferencedElementDefinitions; + } + + /// + /// Queries unused in an + /// An unused element is an element not used in an option + /// + /// The + /// All unused + public static IEnumerable QueryUnusedElementDefinitions(this Iteration iteration) + { + var nestedElements = iteration.QueryNestedElements().ToList(); + + var associatedElements = nestedElements.SelectMany(x => x.ElementUsage.Select(e => e.ElementDefinition)) + .DistinctBy(x => x.Iid).ToList(); + + var unusedElementDefinitions = iteration.Element.ToList(); + unusedElementDefinitions.RemoveAll(e => associatedElements.Any(x => x.Iid == e.Iid)); + unusedElementDefinitions.RemoveAll(x => x.Iid == iteration.TopElement?.Iid); + return unusedElementDefinitions; + } + + /// + /// Gets the from this iteration + /// + /// the iteration used for retrieving the elements + /// an + /// if the iteration is null + public static IEnumerable QueryElementsBase(this Iteration iteration) + { + if (iteration is null) + { + throw new ArgumentNullException(nameof(iteration)); + } + + var elements = new List(); + + if (iteration.TopElement is not null) + { + elements.Add(iteration.TopElement); + } + + iteration.Element.ForEach(e => elements.AddRange(e.ContainedElement)); + + return elements; + } + + /// + /// Queries all owned by a given + /// contained into an + /// + /// The + /// The + /// A collection of + public static IEnumerable QueryOwnedParameterSubscriptions(this Iteration iteration, DomainOfExpertise domain) + { + var subscriptions = new List(); + + if (iteration.TopElement != null) + { + subscriptions.AddRange(iteration.TopElement.QueryOwnedParameterSubscriptions(domain)); + } + + subscriptions.AddRange(iteration.Element.SelectMany(x => x.ContainedElement).SelectMany(x => x.QueryOwnedParameterSubscriptions(domain))); + return subscriptions.DistinctBy(x => x.Iid).OrderBy(p => p.ParameterType.Name); + } + + /// + /// Queries all owned by a given + /// contained into an + /// + /// The + /// The + /// A collection of + public static IEnumerable QueryOwnedParameterSubscriptions(this ElementBase element, DomainOfExpertise domain) + { + var subscriptions = new List(); + + switch (element) + { + case ElementDefinition elementDefinition: + subscriptions.AddRange(elementDefinition.Parameter.QueryOwnedParameterSubscriptions(domain)); + break; + case ElementUsage elementUsage when !elementUsage.ParameterOverride.Any(): + return elementUsage.ElementDefinition.QueryOwnedParameterSubscriptions(domain); + case ElementUsage elementUsage: + var notOverridenParameters = elementUsage.ElementDefinition.Parameter.Where(x => elementUsage.ParameterOverride.All(p => p.Parameter.Iid != x.Iid)); + + subscriptions.AddRange(elementUsage.ParameterOverride.QueryOwnedParameterSubscriptions(domain)); + + subscriptions.AddRange(notOverridenParameters.QueryOwnedParameterSubscriptions(domain)); + break; + } + + return subscriptions; + } + + /// + /// Queries all owned by a given + /// contained into a collection of + /// + /// The collection of + /// The + /// A collection of + public static IEnumerable QueryOwnedParameterSubscriptions(this IEnumerable parameterOrOverrideBases, + DomainOfExpertise domain) + { + return parameterOrOverrideBases.Where(x => x.Owner.Iid != domain.Iid) + .SelectMany(x => x.ParameterSubscription.Where(p => p.Owner.Iid == domain.Iid)); + } + + /// + /// Queries owned contained into an + /// that contains of other + /// + /// The + /// The + /// A collection of + public static IEnumerable QuerySubscribedParameterByOthers(this Iteration iteration, DomainOfExpertise domain) + { + var subscriptions = new List(); + + if (iteration.TopElement != null) + { + subscriptions.AddRange(iteration.TopElement.QuerySubscribedParameterByOthers(domain)); + } + + subscriptions.AddRange(iteration.Element.SelectMany(x => x.ContainedElement).SelectMany(x => x.QuerySubscribedParameterByOthers(domain))); + return subscriptions.DistinctBy(x => x.Iid).OrderBy(p => p.ParameterType.Name); + } + + /// + /// Queries owned contained into an + /// that contains of other + /// + /// The + /// The + /// A collection of + public static IEnumerable QuerySubscribedParameterByOthers(this ElementBase element, DomainOfExpertise domain) + { + var subscriptions = new List(); + + switch (element) + { + case ElementDefinition elementDefinition: + subscriptions.AddRange(elementDefinition.Parameter.QuerySubscribedParameterByOthers(domain)); + break; + case ElementUsage elementUsage when !elementUsage.ParameterOverride.Any(): + return elementUsage.ElementDefinition.QuerySubscribedParameterByOthers(domain); + case ElementUsage elementUsage: + var notOverridenParameters = elementUsage.ElementDefinition.Parameter.Where(x => elementUsage.ParameterOverride.All(p => p.Parameter.Iid != x.Iid)); + subscriptions.AddRange(elementUsage.ParameterOverride.QuerySubscribedParameterByOthers(domain)); + subscriptions.AddRange(notOverridenParameters.QuerySubscribedParameterByOthers(domain)); + break; + } + + return subscriptions; + } + + /// + /// Queries all owned contained into a collection of + /// + /// that contains of other + /// + /// The collection of + /// The + /// A collection of + public static IEnumerable QuerySubscribedParameterByOthers(this IEnumerable parameterOrOverrideBases, + DomainOfExpertise domain) + { + return parameterOrOverrideBases.Where(x => x.Owner.Iid == domain.Iid + && x.ParameterSubscription.Any(p => p.Owner.Iid != domain.Iid)); + } + + /// + /// Query the evolution of contained into a + /// + /// + /// The + /// + /// All changes for each , + /// collected inside a where the key is the revision number + /// + public static Dictionary>> QueryParameterSubscriptionValueSetEvolution(this ParameterSubscription parameterSubscription) + { + var changes = new Dictionary>>(); + + foreach (var parameterSubscriptionValueSet in parameterSubscription.ValueSet) + { + changes[parameterSubscriptionValueSet.Iid] = parameterSubscriptionValueSet.QueryParameterSubscriptionValueSetEvolution(); + } + + return changes; + } + + /// + /// Queries the evolution of the of the Computed Value of the + /// + /// + /// The + /// All changes, collected inside a where the key is the revision number + public static Dictionary> QueryParameterSubscriptionValueSetEvolution(this ParameterSubscriptionValueSet valueSet) + { + var changes = new Dictionary>(); + var subscribedParameterValueSet = valueSet.SubscribedValueSet; + + if (!subscribedParameterValueSet.Revisions.Any() || !subscribedParameterValueSet.Revisions.Keys.Any(x => x >= valueSet.RevisionNumber)) + { + changes.Add(subscribedParameterValueSet.RevisionNumber, subscribedParameterValueSet.Published); + return changes; + } + + var currentRevisionNumber = subscribedParameterValueSet.Revisions.Keys.Where(x => x >= valueSet.RevisionNumber).Min(); + var currentValueSet = ((ParameterValueSetBase)subscribedParameterValueSet.Revisions[currentRevisionNumber]).Published; + + changes.Add(currentRevisionNumber, currentValueSet); + + foreach (var revisionNumber in subscribedParameterValueSet.Revisions.Keys.Where(x => x > currentRevisionNumber).ToList()) + { + var publishedValueAtRevisionNumber = ((ParameterValueSetBase)subscribedParameterValueSet.Revisions[revisionNumber]).Published; + + if (!publishedValueAtRevisionNumber.ContainsSameValues(currentValueSet)) + { + currentRevisionNumber = revisionNumber; + currentValueSet = publishedValueAtRevisionNumber; + changes.Add(currentRevisionNumber, currentValueSet); + } + } + + if (currentRevisionNumber != subscribedParameterValueSet.RevisionNumber && !subscribedParameterValueSet.Published.ContainsSameValues(currentValueSet)) + { + changes.Add(subscribedParameterValueSet.RevisionNumber, subscribedParameterValueSet.Published); + } + + return changes; + } + + /// + /// Queries the name of a + /// + /// The + /// The name + public static string QueryName(this IParameterTypeAssignment parameterTypeAssignment) + { + var name = parameterTypeAssignment.ParameterType.Name; + + if (parameterTypeAssignment.MeasurementScale != null) + { + name += $" [{parameterTypeAssignment.MeasurementScale.ShortName}]"; + } + + return name; + } + } +} From 6ab52bde506efbf86a59b25970697d79c52933bc Mon Sep 17 00:00:00 2001 From: antoineatrhea Date: Sat, 14 Oct 2023 21:52:20 +0200 Subject: [PATCH 2/2] Unit tests --- .../ApplicationTemplateTestFixture.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 COMET.Web.Common.Tests/Components/Applications/ApplicationTemplateTestFixture.cs diff --git a/COMET.Web.Common.Tests/Components/Applications/ApplicationTemplateTestFixture.cs b/COMET.Web.Common.Tests/Components/Applications/ApplicationTemplateTestFixture.cs new file mode 100644 index 00000000..270e8678 --- /dev/null +++ b/COMET.Web.Common.Tests/Components/Applications/ApplicationTemplateTestFixture.cs @@ -0,0 +1,90 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2023 RHEA System S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine +// +// This file is part of COMET WEB Community Edition +// The COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 +// Annex A and Annex C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMET.Web.Common.Tests.Components.Applications +{ + using CDP4Dal; + using CDP4Dal.DAL; + + using COMET.Web.Common.Components.Applications; + using COMET.Web.Common.Model.Configuration; + using COMET.Web.Common.Services.ConfigurationService; + using COMET.Web.Common.Services.SessionManagement; + using COMET.Web.Common.Utilities; + using COMET.Web.Common.ViewModels.Components.Applications; + + using Microsoft.AspNetCore.Components; + using Microsoft.Extensions.DependencyInjection; + + using Moq; + + using NUnit.Framework; + + using TestContext = Bunit.TestContext; + + [TestFixture] + public class ApplicationTemplateTestFixture + { + private TestContext context; + private Mock viewModel; + private Mock sessionService; + private Mock configurationService; + private ServerConfiguration configuration; + + [SetUp] + public void Setup() + { + this.context = new TestContext(); + this.viewModel = new Mock(); + this.sessionService = new Mock(); + this.configurationService = new Mock(); + this.configuration = new ServerConfiguration(); + this.configurationService.Setup(x => x.ServerConfiguration).Returns(this.configuration); + this.viewModel.Setup(x => x.SessionService).Returns(this.sessionService.Object); + var session = new Mock(); + session.Setup(x => x.DataSourceUri).Returns("abc"); + this.sessionService.Setup(x => x.Session).Returns(session.Object); + this.context.Services.AddSingleton(this.configurationService.Object); + this.context.Services.AddSingleton(this.viewModel.Object); + } + + [Test] + public void VerifyApplicationTemplateWithoutDefinedAddress() + { + this.context.RenderComponent(); + var navigationManager = this.context.Services.GetService(); + Assert.That(navigationManager.Uri, Does.Contain($"{QueryKeys.ServerKey}=abc")); + } + + [Test] + public void VerifyApplicationTemplateWithDefinedAddress() + { + this.configuration.ServerAddress = "abc"; + this.context.RenderComponent(); + var navigationManager = this.context.Services.GetService(); + Assert.That(navigationManager.Uri, Does.Not.Contain($"{QueryKeys.ServerKey}=abc")); + } + } +}