diff --git a/COMET.Web.Common.Tests/Utilities/CherryPick/CherryPickRunnerTestFixture.cs b/COMET.Web.Common.Tests/Utilities/CherryPick/CherryPickRunnerTestFixture.cs new file mode 100644 index 00000000..1ea7499b --- /dev/null +++ b/COMET.Web.Common.Tests/Utilities/CherryPick/CherryPickRunnerTestFixture.cs @@ -0,0 +1,66 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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. +// +// The 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 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 COMET.Web.Common.Tests.Utilities.CherryPick; + +using NUnit.Framework; + +using CDP4Common.SiteDirectoryData; + +using Moq; + +[TestFixture] +public class CherryPickRunnerTestFixture +{ + private Common.Utilities.CherryPick.CherryPickRunner viewModel; + private Mock sessionService; + private Mock needCherryPickedData; + + [SetUp] + public void Setup() + { + this.sessionService = new Mock(); + this.needCherryPickedData = new Mock(); + this.viewModel = new Common.Utilities.CherryPick.CherryPickRunner(this.sessionService.Object); + } + + [Test] + public async Task VerifyProperties() + { + Assert.That(this.viewModel.IsCherryPicking, Is.False); + this.viewModel.InitializeProperties(new List { this.needCherryPickedData.Object }); + + this.sessionService.Setup(x => x.Session.RetrieveSiteDirectory()).Returns(new SiteDirectory()); + + var propertyInfo = typeof(Common.Utilities.CherryPick.CherryPickRunner).GetProperty("IsCherryPicking", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); + + propertyInfo?.SetValue(this.viewModel, true, null); + await this.viewModel.RunCherryPick(); + this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny>>()), Times.Never); + + propertyInfo?.SetValue(this.viewModel, false, null); + await this.viewModel.RunCherryPick(); + this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny>>()), Times.Once); + } +} diff --git a/COMET.Web.Common/Utilities/CherryPick/CherryPickRunner.cs b/COMET.Web.Common/Utilities/CherryPick/CherryPickRunner.cs new file mode 100644 index 00000000..c9bc990c --- /dev/null +++ b/COMET.Web.Common/Utilities/CherryPick/CherryPickRunner.cs @@ -0,0 +1,110 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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.Utilities.CherryPick +{ + using CDP4Common.SiteDirectoryData; + + using COMET.Web.Common.Services.SessionManagement; + + /// + /// Utility class that could run CherryPick query for + /// + public class CherryPickRunner : ICherryPickRunner + { + /// + /// Gets the collection of + /// + private readonly List NeedCherryPicked = new(); + + /// + /// Gets the + /// + protected readonly ISessionService SessionService; + + /// + /// Initializes a new + /// + /// The + public CherryPickRunner(ISessionService sessionService) + { + this.SessionService = sessionService; + } + + /// + /// Asserts that the cherrypick feature is on going + /// + public bool IsCherryPicking { get; private set; } + + /// + /// Initializes the internal properties + /// + /// A collection of + public void InitializeProperties(IEnumerable needCherryPicked) + { + this.NeedCherryPicked.Clear(); + this.NeedCherryPicked.AddRange(needCherryPicked); + } + + /// + /// Runs the cherrypick features based on data required from + /// + /// A + public async Task RunCherryPick() + { + if (this.IsCherryPicking) + { + return; + } + + this.IsCherryPicking = true; + var classKinds = this.NeedCherryPicked.SelectMany(x => x.ClassKindsOfInterest).Distinct(); + var categoriesName = this.NeedCherryPicked.SelectMany(x => x.CategoriesOfInterest).Distinct(); + + var categories = new List(); + + foreach (var referenceDataLibrary in this.SessionService.Session.RetrieveSiteDirectory().AvailableReferenceDataLibraries()) + { + categories.AddRange(referenceDataLibrary.DefinedCategory + .Where(x => categoriesName.Contains(x.Name)) + .ToList()); + } + + var availableEngineeringModelSetups = this.SessionService.GetParticipantModels().ToList(); + + var cherryPicks = availableEngineeringModelSetups.Select(engineeringModelSetup => this.SessionService.Session.CherryPick(engineeringModelSetup.EngineeringModelIid, engineeringModelSetup.IterationSetup.Single(x => x.FrozenOn == null).IterationIid, classKinds, categories.Select(x => x.Iid))) + .ToList(); + + var results = (await Task.WhenAll(cherryPicks)).Where(x => x.Any()).ToList(); + + foreach (var needCherryPickedData in this.NeedCherryPicked) + { + needCherryPickedData.ProcessCherryPickedData(results); + } + + this.IsCherryPicking = false; + } + } +} diff --git a/COMET.Web.Common/Utilities/CherryPick/ICherryPickRunner.cs b/COMET.Web.Common/Utilities/CherryPick/ICherryPickRunner.cs new file mode 100644 index 00000000..aa8c4e32 --- /dev/null +++ b/COMET.Web.Common/Utilities/CherryPick/ICherryPickRunner.cs @@ -0,0 +1,50 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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.Utilities.CherryPick +{ + /// + /// Utility class that could run CherryPick query for + /// + public interface ICherryPickRunner + { + /// + /// Asserts that the cherrypick feature is on going + /// + bool IsCherryPicking { get; } + + /// + /// Initializes the internal properties + /// + /// A collection of + void InitializeProperties(IEnumerable needCherryPicked); + + /// + /// Runs the cherrypick features based on data required from + /// + /// A + Task RunCherryPick(); + } +} diff --git a/COMET.Web.Common/Utilities/CherryPick/INeedCherryPickedData.cs b/COMET.Web.Common/Utilities/CherryPick/INeedCherryPickedData.cs new file mode 100644 index 00000000..44d012ef --- /dev/null +++ b/COMET.Web.Common/Utilities/CherryPick/INeedCherryPickedData.cs @@ -0,0 +1,54 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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.Utilities.CherryPick +{ + using CDP4Common.CommonData; + using CDP4Common.SiteDirectoryData; + + using Thing = CDP4Common.DTO.Thing; + + /// + /// Interface that define that an object needs data that comes from CherryPick feature + /// + public interface INeedCherryPickedData + { + /// + /// Gets the collection of name that this object is interested on + /// + public IReadOnlyCollection CategoriesOfInterest { get; } + + /// + /// Gets the collection of that this object is interested on + /// + public IReadOnlyCollection ClassKindsOfInterest { get; } + + /// + /// Process Cherry picked data + /// + /// A collection of collection of + public void ProcessCherryPickedData(IEnumerable> cherryPickedData); + } +}