|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="AnnotatingElementExtensions.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022-2025 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.Dal |
| 26 | +{ |
| 27 | + using System; |
| 28 | + using System.Collections.Concurrent; |
| 29 | + using System.Collections.Generic; |
| 30 | + using System.Linq; |
| 31 | + |
| 32 | + using Core.POCO.Root.Annotations; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// A static class that provides extension methods for the <see cref="AnnotatingElement"/> class |
| 36 | + /// </summary> |
| 37 | + public static class AnnotatingElementExtensions |
| 38 | + { |
| 39 | + /// <summary> |
| 40 | + /// Updates the value properties of the <see cref="AnnotatingElement"/> by setting the value equal to that of the dto |
| 41 | + /// Removes deleted objects from the reference properties and returns the unique identifiers |
| 42 | + /// of the objects that have been removed from contained properties |
| 43 | + /// </summary> |
| 44 | + /// <param name="poco"> |
| 45 | + /// The <see cref="AnnotatingElement"/> that is to be updated |
| 46 | + /// </param> |
| 47 | + /// <param name="dto"> |
| 48 | + /// The DTO that is used to update the <see cref="AnnotatingElement"/> with |
| 49 | + /// </param> |
| 50 | + /// <returns> |
| 51 | + /// The unique identifiers of the objects that have been removed from contained properties |
| 52 | + /// </returns> |
| 53 | + /// <exception cref="ArgumentNullException"> |
| 54 | + /// Thrown when the <paramref name="poco"/> or <paramref name="dto"/> is null |
| 55 | + /// </exception> |
| 56 | + public static IEnumerable<Guid> UpdateValueAndRemoveDeletedReferenceProperties(this Core.POCO.Root.Annotations.AnnotatingElement poco, Core.DTO.Root.Annotations.AnnotatingElement dto) |
| 57 | + { |
| 58 | + if (poco == null) |
| 59 | + { |
| 60 | + throw new ArgumentNullException(nameof(poco), $"the {nameof(poco)} may not be null"); |
| 61 | + } |
| 62 | + |
| 63 | + if (dto == null) |
| 64 | + { |
| 65 | + throw new ArgumentNullException(nameof(dto), $"the {nameof(dto)} may not be null"); |
| 66 | + } |
| 67 | + |
| 68 | + var identifiersOfObjectsToDelete = new List<Guid>(); |
| 69 | + |
| 70 | + poco.AliasIds = dto.AliasIds; |
| 71 | + |
| 72 | + poco.DeclaredName = dto.DeclaredName; |
| 73 | + |
| 74 | + poco.DeclaredShortName = dto.DeclaredShortName; |
| 75 | + |
| 76 | + poco.ElementId = dto.ElementId; |
| 77 | + |
| 78 | + poco.IsImpliedIncluded = dto.IsImpliedIncluded; |
| 79 | + |
| 80 | + var ownedRelationshipToDelete = poco.OwnedRelationship.Select(x => x.Id).Except(dto.OwnedRelationship); |
| 81 | + |
| 82 | + foreach (var identifier in ownedRelationshipToDelete) |
| 83 | + { |
| 84 | + poco.OwnedRelationship.Remove(poco.OwnedRelationship.Single(x => x.Id == identifier)); |
| 85 | + } |
| 86 | + |
| 87 | + identifiersOfObjectsToDelete.AddRange(ownedRelationshipToDelete); |
| 88 | + |
| 89 | + |
| 90 | + return identifiersOfObjectsToDelete; |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Updates the Reference properties of the <see cref="AnnotatingElement"/> using the data (identifiers) encapsulated in the DTO |
| 95 | + /// and the provided cache to find the referenced object. |
| 96 | + /// </summary> |
| 97 | + /// <param name="poco"> |
| 98 | + /// The <see cref="AnnotatingElement"/> that is to be updated |
| 99 | + /// </param> |
| 100 | + /// <param name="dto"> |
| 101 | + /// The DTO that is used to update the <see cref="AnnotatingElement"/> with |
| 102 | + /// </param> |
| 103 | + /// <param name="cache"> |
| 104 | + /// The <see cref="ConcurrentDictionary{Guid, Lazy{Core.POCO.Root.Elements.IElement}}"/> that contains the |
| 105 | + /// <see cref="Core.POCO.Root.Elements.IElement"/>s that are know and cached. |
| 106 | + /// </param> |
| 107 | + /// <exception cref="ArgumentNullException"></exception> |
| 108 | + public static void UpdateReferenceProperties(this Core.POCO.Root.Annotations.AnnotatingElement poco, Core.DTO.Root.Annotations.AnnotatingElement dto, ConcurrentDictionary<Guid, Lazy<Core.POCO.Root.Elements.IElement>> cache) |
| 109 | + { |
| 110 | + if (poco == null) |
| 111 | + { |
| 112 | + throw new ArgumentNullException(nameof(poco), $"the {nameof(poco)} may not be null"); |
| 113 | + } |
| 114 | + |
| 115 | + if (dto == null) |
| 116 | + { |
| 117 | + throw new ArgumentNullException(nameof(dto), $"the {nameof(dto)} may not be null"); |
| 118 | + } |
| 119 | + |
| 120 | + if (cache == null) |
| 121 | + { |
| 122 | + throw new ArgumentNullException(nameof(cache), $"the {nameof(cache)} may not be null"); |
| 123 | + } |
| 124 | + |
| 125 | + Lazy<Core.POCO.Root.Elements.IElement> lazyPoco; |
| 126 | + |
| 127 | + var ownedRelationshipToAdd = dto.OwnedRelationship.Except(poco.OwnedRelationship.Select(x => x.Id)); |
| 128 | + |
| 129 | + foreach (var identifier in ownedRelationshipToAdd) |
| 130 | + { |
| 131 | + if (cache.TryGetValue(identifier, out lazyPoco)) |
| 132 | + { |
| 133 | + poco.OwnedRelationship.Add((Core.POCO.Root.Elements.IRelationship)lazyPoco.Value); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (dto.OwningRelationship.HasValue && cache.TryGetValue(dto.OwningRelationship.Value, out lazyPoco)) |
| 138 | + { |
| 139 | + poco.OwningRelationship = (Core.POCO.Root.Elements.IRelationship)lazyPoco.Value; |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + poco.OwningRelationship = null; |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Creates a <see cref="Core.DTO.Root.Annotations.AnnotatingElement"/> based on the provided POCO |
| 150 | + /// </summary> |
| 151 | + /// <param name="poco"> |
| 152 | + /// The subject <see cref="Core.POCO.Root.Annotations.AnnotatingElement"/> from which a DTO is to be created |
| 153 | + /// </param> |
| 154 | + /// <returns> |
| 155 | + /// An instance of <see cref="Core.POCO.Root.Annotations.AnnotatingElement"/> |
| 156 | + /// </returns> |
| 157 | + public static Core.DTO.Root.Annotations.AnnotatingElement ToDto(this Core.POCO.Root.Annotations.AnnotatingElement poco) |
| 158 | + { |
| 159 | + var dto = new Core.DTO.Root.Annotations.AnnotatingElement(); |
| 160 | + |
| 161 | + dto.Id = poco.Id; |
| 162 | + dto.AliasIds = poco.AliasIds; |
| 163 | + dto.DeclaredName = poco.DeclaredName; |
| 164 | + dto.DeclaredShortName = poco.DeclaredShortName; |
| 165 | + dto.ElementId = poco.ElementId; |
| 166 | + dto.IsImpliedIncluded = poco.IsImpliedIncluded; |
| 167 | + dto.OwnedRelationship = poco.OwnedRelationship.Select(x => x.Id).ToList(); |
| 168 | + dto.OwningRelationship = poco.OwningRelationship?.Id; |
| 169 | + |
| 170 | + return dto; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// ------------------------------------------------------------------------------------------------ |
| 176 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 177 | +// ------------------------------------------------------------------------------------------------ |
0 commit comments