|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Obeo. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Obeo - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.syson.application.update; |
| 14 | + |
| 15 | +import java.time.Duration; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.LinkedHashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.UUID; |
| 25 | + |
| 26 | +import org.eclipse.emf.ecore.EObject; |
| 27 | +import org.eclipse.emf.ecore.EStructuralFeature.Setting; |
| 28 | +import org.eclipse.emf.ecore.resource.Resource; |
| 29 | +import org.eclipse.emf.ecore.resource.ResourceSet; |
| 30 | +import org.eclipse.emf.ecore.util.EcoreUtil.UnresolvedProxyCrossReferencer; |
| 31 | +import org.eclipse.sirius.components.collaborative.api.ChangeKind; |
| 32 | +import org.eclipse.sirius.components.core.api.IEditingContext; |
| 33 | +import org.eclipse.sirius.components.events.ICause; |
| 34 | +import org.eclipse.sirius.components.representations.Failure; |
| 35 | +import org.eclipse.sirius.components.representations.IStatus; |
| 36 | +import org.eclipse.sirius.components.representations.Message; |
| 37 | +import org.eclipse.sirius.components.representations.MessageLevel; |
| 38 | +import org.eclipse.sirius.components.representations.Success; |
| 39 | +import org.eclipse.sirius.web.application.UUIDParser; |
| 40 | +import org.eclipse.sirius.web.application.editingcontext.EditingContext; |
| 41 | +import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextDependencyLoader; |
| 42 | +import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextPersistenceFilter; |
| 43 | +import org.eclipse.sirius.web.application.library.dto.UpdateLibraryInput; |
| 44 | +import org.eclipse.sirius.web.application.library.services.LibraryMetadataAdapter; |
| 45 | +import org.eclipse.sirius.web.application.studio.services.library.api.IUpdateLibraryExecutor; |
| 46 | +import org.eclipse.sirius.web.domain.boundedcontexts.library.Library; |
| 47 | +import org.eclipse.sirius.web.domain.boundedcontexts.library.services.api.ILibrarySearchService; |
| 48 | +import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.SemanticData; |
| 49 | +import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.SemanticDataDependency; |
| 50 | +import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.services.api.ISemanticDataSearchService; |
| 51 | +import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.services.api.ISemanticDataUpdateService; |
| 52 | +import org.slf4j.Logger; |
| 53 | +import org.slf4j.LoggerFactory; |
| 54 | +import org.springframework.context.annotation.Primary; |
| 55 | +import org.springframework.data.jdbc.core.mapping.AggregateReference; |
| 56 | +import org.springframework.stereotype.Service; |
| 57 | + |
| 58 | +/** |
| 59 | + * Used to execute a library update. |
| 60 | + * |
| 61 | + * @author gdaniel |
| 62 | + */ |
| 63 | +@Service |
| 64 | +// This class should be removed once https://github.com/eclipse-sirius/sirius-web/issues/5400 is fixed. |
| 65 | +@Primary |
| 66 | +public class SysONUpdateLibraryExecutor implements IUpdateLibraryExecutor { |
| 67 | + |
| 68 | + public static final String REMOVED_PROXIES_PARAMETER_KEY = "removed_proxies_parameter_key"; |
| 69 | + |
| 70 | + private final Logger logger = LoggerFactory.getLogger(SysONUpdateLibraryExecutor.class); |
| 71 | + |
| 72 | + private final ILibrarySearchService librarySearchService; |
| 73 | + |
| 74 | + private final ISemanticDataSearchService semanticDataSearchService; |
| 75 | + |
| 76 | + private final ISemanticDataUpdateService semanticDataUpdateService; |
| 77 | + |
| 78 | + private final IEditingContextDependencyLoader editingContextDependencyLoader; |
| 79 | + |
| 80 | + private final List<IEditingContextPersistenceFilter> editingContextPersistenceFilters; |
| 81 | + |
| 82 | + public SysONUpdateLibraryExecutor(ILibrarySearchService librarySearchService, ISemanticDataSearchService semanticDataSearchService, ISemanticDataUpdateService semanticDataUpdateService, |
| 83 | + IEditingContextDependencyLoader editingContextDependencyLoader, List<IEditingContextPersistenceFilter> editingContextPersistenceFilters) { |
| 84 | + this.librarySearchService = Objects.requireNonNull(librarySearchService); |
| 85 | + this.semanticDataSearchService = Objects.requireNonNull(semanticDataSearchService); |
| 86 | + this.semanticDataUpdateService = Objects.requireNonNull(semanticDataUpdateService); |
| 87 | + this.editingContextDependencyLoader = Objects.requireNonNull(editingContextDependencyLoader); |
| 88 | + this.editingContextPersistenceFilters = Objects.requireNonNull(editingContextPersistenceFilters); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public IStatus updateLibrary(ICause cause, IEditingContext editingContext, UUID libraryId) { |
| 93 | + IStatus result = new Failure(List.of()); |
| 94 | + if (editingContext instanceof EditingContext siriusWebEditingContext) { |
| 95 | + Optional<Library> optionalNewLibrary = this.librarySearchService.findById(libraryId); |
| 96 | + if (optionalNewLibrary.isPresent()) { |
| 97 | + Library newLibrary = optionalNewLibrary.get(); |
| 98 | + |
| 99 | + // Find the version of the library already in the dependencies. |
| 100 | + Optional<Library> optionalOldLibrary = this.getLibraryDependencyWithDifferentVersion(siriusWebEditingContext, newLibrary); |
| 101 | + if (optionalOldLibrary.isPresent()) { |
| 102 | + Library oldLibrary = optionalOldLibrary.get(); |
| 103 | + List<Resource> oldLibraryResources = this.getAllResourcesFromLibraryRecursiveDependencies(siriusWebEditingContext, oldLibrary); |
| 104 | + |
| 105 | + for (Resource oldLibraryResource : oldLibraryResources) { |
| 106 | + oldLibraryResource.unload(); |
| 107 | + siriusWebEditingContext.getDomain().getResourceSet().getResources().remove(oldLibraryResource); |
| 108 | + } |
| 109 | + |
| 110 | + if (cause instanceof UpdateLibraryInput) { |
| 111 | + // Do not update the semantic data bounded context if we are not performing an actual library |
| 112 | + // update (this is for example the case when performing an impact analysis). |
| 113 | + this.addLibraryDependency(cause, siriusWebEditingContext, newLibrary); |
| 114 | + this.removeLibraryDependency(cause, siriusWebEditingContext, oldLibrary); |
| 115 | + } |
| 116 | + |
| 117 | + List<SemanticData> newDependencies = new ArrayList<>(); |
| 118 | + this.semanticDataSearchService.findById(newLibrary.getSemanticData().getId()) |
| 119 | + .ifPresent(newLibrarySemanticData -> { |
| 120 | + newDependencies.add(newLibrarySemanticData); |
| 121 | + newDependencies.addAll(this.semanticDataSearchService.findAllDependenciesRecursivelyById(newLibrarySemanticData.getId())); |
| 122 | + }); |
| 123 | + |
| 124 | + this.editingContextDependencyLoader.loadDependencies(siriusWebEditingContext, newDependencies); |
| 125 | + |
| 126 | + long start = System.nanoTime(); |
| 127 | + Map<EObject, Collection<Setting>> removedProxies = this.removeUnresolvedProxies(siriusWebEditingContext.getDomain().getResourceSet()); |
| 128 | + Duration timeToRemoveProxies = Duration.ofNanos(System.nanoTime() - start); |
| 129 | + this.logger.trace("Removed proxies in {}ms", timeToRemoveProxies.toMillis()); |
| 130 | + |
| 131 | + result = new Success(ChangeKind.SEMANTIC_CHANGE, Map.of(REMOVED_PROXIES_PARAMETER_KEY, removedProxies), |
| 132 | + List.of(new Message("Library " + oldLibrary.getName() + " updated to version " + newLibrary.getVersion(), MessageLevel.SUCCESS))); |
| 133 | + } else { |
| 134 | + result = new Failure(List.of(new Message("Cannot update Library " + newLibrary.getName() + ": the library is not a direct dependency", MessageLevel.ERROR))); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + private Optional<Library> getLibraryDependencyWithDifferentVersion(EditingContext editingContext, Library library) { |
| 142 | + return new UUIDParser().parse(editingContext.getId()) |
| 143 | + .flatMap(this.semanticDataSearchService::findById) |
| 144 | + .map(SemanticData::getDependencies) |
| 145 | + .orElse(List.of()) |
| 146 | + .stream() |
| 147 | + .map(SemanticDataDependency::dependencySemanticDataId) |
| 148 | + .map(this.librarySearchService::findBySemanticData) |
| 149 | + .filter(Optional::isPresent) |
| 150 | + .map(Optional::get) |
| 151 | + .filter(libraryDependency -> Objects.equals(libraryDependency.getNamespace(), library.getNamespace()) |
| 152 | + && Objects.equals(libraryDependency.getName(), library.getName()) |
| 153 | + && !Objects.equals(libraryDependency.getVersion(), library.getVersion())) |
| 154 | + .findFirst(); |
| 155 | + } |
| 156 | + |
| 157 | + private List<Resource> getAllResourcesFromLibraryRecursiveDependencies(EditingContext editingContext, Library library) { |
| 158 | + Set<Library> libraries = new LinkedHashSet<>(); |
| 159 | + libraries.add(library); |
| 160 | + this.semanticDataSearchService.findAllDependenciesRecursivelyById(library.getSemanticData().getId()).stream() |
| 161 | + .map(SemanticData::getId) |
| 162 | + .map(AggregateReference::<SemanticData, UUID> to) |
| 163 | + .map(this.librarySearchService::findBySemanticData) |
| 164 | + .filter(Optional::isPresent) |
| 165 | + .map(Optional::get) |
| 166 | + .forEach(libraries::add); |
| 167 | + List<Resource> libraryResources = new ArrayList<>(); |
| 168 | + for (Resource resource : editingContext.getDomain().getResourceSet().getResources()) { |
| 169 | + resource.eAdapters().stream() |
| 170 | + .filter(LibraryMetadataAdapter.class::isInstance) |
| 171 | + .map(LibraryMetadataAdapter.class::cast) |
| 172 | + .findFirst() |
| 173 | + .ifPresent(libraryMetadata -> { |
| 174 | + if (libraries.stream().anyMatch(libraryDependency -> Objects.equals(libraryMetadata.getNamespace(), libraryDependency.getNamespace()) |
| 175 | + && Objects.equals(libraryMetadata.getName(), libraryDependency.getName()) |
| 176 | + && Objects.equals(libraryMetadata.getVersion(), libraryDependency.getVersion()))) { |
| 177 | + libraryResources.add(resource); |
| 178 | + } |
| 179 | + }); |
| 180 | + } |
| 181 | + return libraryResources; |
| 182 | + } |
| 183 | + |
| 184 | + private void addLibraryDependency(ICause cause, EditingContext editingContext, Library library) { |
| 185 | + Optional<SemanticData> optionalSemanticData = new UUIDParser().parse(editingContext.getId()) |
| 186 | + .flatMap(this.semanticDataSearchService::findById); |
| 187 | + if (optionalSemanticData.isPresent()) { |
| 188 | + if (optionalSemanticData.get().getDependencies().stream().anyMatch(dependency -> dependency.dependencySemanticDataId().equals(library.getSemanticData()))) { |
| 189 | + this.logger.warn("Cannot add the dependency to library " + library.getNamespace() + ":" + library.getName() + ":" + library.getVersion() + ": the dependency already exists"); |
| 190 | + } else { |
| 191 | + this.semanticDataUpdateService.addDependencies(cause, AggregateReference.to(optionalSemanticData.get().getId()), List.of(library.getSemanticData())); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private void removeLibraryDependency(ICause cause, EditingContext editingContext, Library library) { |
| 197 | + new UUIDParser().parse(editingContext.getId()) |
| 198 | + .flatMap(this.semanticDataSearchService::findById) |
| 199 | + .ifPresent(semanticData -> { |
| 200 | + this.semanticDataUpdateService.removeDependencies(cause, AggregateReference.to(semanticData.getId()), List.of(library.getSemanticData())); |
| 201 | + }); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Removes proxies from non-library resources in the provided {@code resourceSet}. |
| 206 | + * |
| 207 | + * @param resourceSet |
| 208 | + * the resource set |
| 209 | + * @return a {@link Map} containing the unresolved proxies from non-library resources in the provided |
| 210 | + * {@code resourceSet} |
| 211 | + */ |
| 212 | + private Map<EObject, Collection<Setting>> removeUnresolvedProxies(ResourceSet resourceSet) { |
| 213 | + List<Resource> nonLibraryResources = resourceSet.getResources().stream() |
| 214 | + .filter(resource -> this.editingContextPersistenceFilters.stream().allMatch(editingContextPersistenceFilter -> editingContextPersistenceFilter.shouldPersist(resource))) |
| 215 | + .toList(); |
| 216 | + // Only look for proxies in Resources that are persisted in the project's semantic data |
| 217 | + Map<EObject, Collection<Setting>> unresolvedProxies = UnresolvedProxyCrossReferencer.find(nonLibraryResources); |
| 218 | + unresolvedProxies.forEach((proxyObject, settings) -> { |
| 219 | + for (Setting setting : settings) { |
| 220 | + if (!setting.getEStructuralFeature().isDerived()) { |
| 221 | + if (setting.getEStructuralFeature().isMany()) { |
| 222 | + List<?> value = (List<?>) setting.get(false); |
| 223 | + value.remove(proxyObject); |
| 224 | + } else { |
| 225 | + setting.unset(); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + }); |
| 230 | + return unresolvedProxies; |
| 231 | + |
| 232 | + } |
| 233 | +} |
| 234 | + |
0 commit comments