|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="AssociationExtensions.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022-2026 Starion Group S.A. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// </copyright> |
| 19 | +// ------------------------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +// ------------------------------------------------------------------------------------------------ |
| 22 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 23 | +// ------------------------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +namespace SysML2.NET.Serializer.Xmi.Extensions |
| 26 | +{ |
| 27 | + using System; |
| 28 | + using System.Collections.Generic; |
| 29 | + |
| 30 | + using Microsoft.Extensions.Logging; |
| 31 | + |
| 32 | + using SysML2.NET.Common; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Provides extensions methods for the <see cref="{SysML2.NET.Core.POCO.Kernel.Associations.{this.Name}}"/> to help resolve reference for properties |
| 36 | + /// </summary> |
| 37 | + public static class AssociationExtensions |
| 38 | + { |
| 39 | + /// <summary> |
| 40 | + /// Resolve and assign single reference value for a specific property |
| 41 | + /// </summary> |
| 42 | + /// <param name="poco">The <see cref="SysML2.NET.Core.POCO.Kernel.Associations.Association"/> that should have the value of a property to be set</param> |
| 43 | + /// <param name="propertyName">The name of the property</param> |
| 44 | + /// <param name="reference">The identifier of the <see cref="IData"/> value to set</param> |
| 45 | + /// <param name="xmiDataCache">The <see cref="IXmiDataCache"/></param> |
| 46 | + /// <param name="logger">The <see cref="ILogger" /> used to produce log statement</param> |
| 47 | + public static void ResolveAndAssignSingleValueReference(this SysML2.NET.Core.POCO.Kernel.Associations.Association poco, string propertyName, Guid reference, IXmiDataCache xmiDataCache, ILogger logger) |
| 48 | + { |
| 49 | + if (poco == null) |
| 50 | + { |
| 51 | + throw new ArgumentNullException(nameof(poco)); |
| 52 | + } |
| 53 | + |
| 54 | + if (xmiDataCache == null) |
| 55 | + { |
| 56 | + throw new ArgumentNullException(nameof(xmiDataCache)); |
| 57 | + } |
| 58 | + |
| 59 | + if (logger == null) |
| 60 | + { |
| 61 | + throw new ArgumentNullException(nameof(logger)); |
| 62 | + } |
| 63 | + |
| 64 | + switch (propertyName) |
| 65 | + { |
| 66 | + case "owningRelatedElement": |
| 67 | + { |
| 68 | + if (!xmiDataCache.TryGetData(reference, out var referencedData)) |
| 69 | + { |
| 70 | + logger.LogWarning("The reference to [{Reference}] for property [owningRelatedElement] on element type [Association] with id [{Id}] was not found in the cache, probably because its type is not supported.", |
| 71 | + reference, poco.Id); |
| 72 | + |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (referencedData is not SysML2.NET.Core.POCO.Root.Elements.IElement owningRelatedElementReference) |
| 77 | + { |
| 78 | + throw new InvalidOperationException($"The referenced element with the id [{reference}] is a {referencedData.GetType().Name}, expected type: an IElement"); |
| 79 | + } |
| 80 | + |
| 81 | + poco.OwningRelatedElement = owningRelatedElementReference; |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + case "owningRelationship": |
| 86 | + { |
| 87 | + if (!xmiDataCache.TryGetData(reference, out var referencedData)) |
| 88 | + { |
| 89 | + logger.LogWarning("The reference to [{Reference}] for property [owningRelationship] on element type [Association] with id [{Id}] was not found in the cache, probably because its type is not supported.", |
| 90 | + reference, poco.Id); |
| 91 | + |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (referencedData is not SysML2.NET.Core.POCO.Root.Elements.IRelationship owningRelationshipReference) |
| 96 | + { |
| 97 | + throw new InvalidOperationException($"The referenced element with the id [{reference}] is a {referencedData.GetType().Name}, expected type: an IRelationship"); |
| 98 | + } |
| 99 | + |
| 100 | + poco.OwningRelationship = owningRelationshipReference; |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + default: |
| 105 | + throw new ArgumentOutOfRangeException(nameof(propertyName), propertyName, "Unknown property name"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Resolve and assign multiple references value for a specific property |
| 111 | + /// </summary> |
| 112 | + /// <param name="poco">The <see cref="SysML2.NET.Core.POCO.Kernel.Associations.Association"/> that should have the value of a property to be set</param> |
| 113 | + /// <param name="propertyName">The name of the property</param> |
| 114 | + /// <param name="references">The collection of identifier values to set</param> |
| 115 | + /// <param name="xmiDataCache">The <see cref="IXmiDataCache"/></param> |
| 116 | + /// <param name="logger">The <see cref="ILogger" /> used to produce log statement</param> |
| 117 | + public static void ResolveAndAssignMultipleValueReferences(this SysML2.NET.Core.POCO.Kernel.Associations.Association poco, string propertyName, IReadOnlyCollection<Guid> references, IXmiDataCache xmiDataCache, ILogger logger) |
| 118 | + { |
| 119 | + if (poco == null) |
| 120 | + { |
| 121 | + throw new ArgumentNullException(nameof(poco)); |
| 122 | + } |
| 123 | + |
| 124 | + if (references == null) |
| 125 | + { |
| 126 | + throw new ArgumentNullException(nameof(references)); |
| 127 | + } |
| 128 | + |
| 129 | + if (xmiDataCache == null) |
| 130 | + { |
| 131 | + throw new ArgumentNullException(nameof(xmiDataCache)); |
| 132 | + } |
| 133 | + |
| 134 | + if (logger == null) |
| 135 | + { |
| 136 | + throw new ArgumentNullException(nameof(logger)); |
| 137 | + } |
| 138 | + |
| 139 | + switch (propertyName) |
| 140 | + { |
| 141 | + case "ownedRelatedElement": |
| 142 | + { |
| 143 | + foreach (var reference in references) |
| 144 | + { |
| 145 | + if (!xmiDataCache.TryGetData(reference, out var referencedData)) |
| 146 | + { |
| 147 | + logger.LogWarning("The reference to [{Reference}] for property [ownedRelatedElement] on element type [Association] with id [{Id}] was not found in the cache, probably because its type is not supported.", |
| 148 | + reference, poco.Id); |
| 149 | + |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + if (referencedData is not SysML2.NET.Core.POCO.Root.Elements.IElement ownedRelatedElementReference) |
| 154 | + { |
| 155 | + throw new InvalidOperationException($"The referenced element with the id [{reference}] is a {referencedData.GetType().Name}, expected type: an IElement"); |
| 156 | + } |
| 157 | + |
| 158 | + poco.OwnedRelatedElement.Add(ownedRelatedElementReference); |
| 159 | + } |
| 160 | + |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + case "ownedRelationship": |
| 165 | + { |
| 166 | + foreach (var reference in references) |
| 167 | + { |
| 168 | + if (!xmiDataCache.TryGetData(reference, out var referencedData)) |
| 169 | + { |
| 170 | + logger.LogWarning("The reference to [{Reference}] for property [ownedRelationship] on element type [Association] with id [{Id}] was not found in the cache, probably because its type is not supported.", |
| 171 | + reference, poco.Id); |
| 172 | + |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + if (referencedData is not SysML2.NET.Core.POCO.Root.Elements.IRelationship ownedRelationshipReference) |
| 177 | + { |
| 178 | + throw new InvalidOperationException($"The referenced element with the id [{reference}] is a {referencedData.GetType().Name}, expected type: an IRelationship"); |
| 179 | + } |
| 180 | + |
| 181 | + poco.OwnedRelationship.Add(ownedRelationshipReference); |
| 182 | + } |
| 183 | + |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + default: |
| 188 | + throw new ArgumentOutOfRangeException(nameof(propertyName), propertyName, "Unknown property name"); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// ------------------------------------------------------------------------------------------------ |
| 195 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 196 | +// ------------------------------------------------------------------------------------------------ |
0 commit comments