diff --git a/COMET.Web.Common/Extensions/SessionServiceExtensions.cs b/COMET.Web.Common/Extensions/SessionServiceExtensions.cs new file mode 100644 index 00000000..b08eedd1 --- /dev/null +++ b/COMET.Web.Common/Extensions/SessionServiceExtensions.cs @@ -0,0 +1,108 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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.Extensions +{ + using CDP4Common.EngineeringModelData; + using CDP4Common.SiteDirectoryData; + + using COMET.Web.Common.Services.SessionManagement; + + using FluentResults; + + /// + /// Static class with extension methods for the + /// + public static class SessionServiceExtensions + { + /// + /// Marks a top element for a given iteration + /// + /// The in which the iteration will be updated + /// The to be updated + /// The to be marked as top element + /// A + /// Throws an + public static async Task MarkTopElement(this ISessionService sessionService, Iteration iteration, ElementDefinition element) + { + if (iteration == null) + { + throw new ArgumentNullException(nameof(iteration)); + } + + var iterationClone = iteration.Clone(false); + iterationClone.TopElement = element; + + return await sessionService.UpdateThing(iteration.Container, iterationClone); + } + + /// + /// Adds a new parameter for a given element definition + /// + /// The in which data will be updated + /// The in which a new parameter will be added + /// The of the new parameter + /// The of the new parameter + /// The of the new parameter + /// The of the owner + /// A + /// Throws an + public static async Task AddParameter(this ISessionService sessionService, ElementDefinition elementDefinition, ParameterGroup group, ParameterType parameterType, MeasurementScale measurementScale, DomainOfExpertise owner) + { + if (elementDefinition == null) + { + throw new ArgumentNullException(nameof(elementDefinition), "The container ElementDefinition may not be null"); + } + + if (parameterType == null) + { + throw new ArgumentNullException(nameof(parameterType), "The ParameterType may not be null"); + } + + if (owner == null) + { + throw new ArgumentNullException(nameof(owner), "The owner DomainOfExpertise may not be null"); + } + + if (sessionService.Session == null) + { + throw new ArgumentNullException(nameof(sessionService.Session), "The session may not be null"); + } + + var parameter = new Parameter(Guid.NewGuid(), null, null) + { + Owner = owner, + ParameterType = parameterType, + Scale = measurementScale, + Group = group, + ValueSet = { new ParameterValueSet() } + }; + + var clone = elementDefinition.Clone(false); + clone.Parameter.Add(parameter); + + return await sessionService.CreateThing(clone, parameter); + } + } +}