Skip to content

Commit

Permalink
[3333] Add JsonResourceMigrationVersionProcessor
Browse files Browse the repository at this point in the history
Bug: #3333
Signed-off-by: Michaël Charfadi <michael.charfadi@obeosoft.com>
  • Loading branch information
mcharfadi committed Apr 4, 2024
1 parent ac482a9 commit 33207fa
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ image:doc/screenshots/inside_outside_labels.png[Isinde outside label example, 70
- https://github.com/eclipse-sirius/sirius-web/issues/3279[#3279] [gantt] The expressions `TaskDescription#startTimeExpression` and `TaskDescription#endTimeExpression` can now both accept `String` values as a result if those values can be parsed by `java.timeInstant#parse`.
They still support returning an `java.time.Instant` object directly.
- https://github.com/eclipse-sirius/sirius-web/issues/3309[#3309] [diagram] Hide header border-bottom when the node is collapsed
- https://github.com/eclipse-sirius/sirius-web/issues/3333[#3333] [sirius-web] Now, store the last migration performed in a document


== v2024.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2023 Obeo.
* Copyright (c) 2019, 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -33,12 +33,15 @@
public class ResourceMetadataAdapter implements Adapter {
private String name;

private String lastMigrationPerformed = "";

private String migrationVersion = "";

private Notifier notifier;

public ResourceMetadataAdapter(String name) {
this.name = Objects.requireNonNull(name);
}

public String getName() {
return this.name;
}
Expand All @@ -47,6 +50,22 @@ public void setName(String name) {
this.name = name;
}

public String getLastMigrationPerformed() {
return lastMigrationPerformed;
}

public void setLastMigrationPerformed(String lastMigrationPerformed) {
this.lastMigrationPerformed = lastMigrationPerformed;
}

public String getMigrationVersion() {
return migrationVersion;
}

public void setMigrationVersion(String migrationVersion) {
this.migrationVersion = migrationVersion;
}

@Override
public void notifyChanged(Notification notification) {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.web.application.editingcontext.services.api.IDocumentToResourceService;
import org.eclipse.sirius.web.application.editingcontext.services.api.JsonResourceMigrationVersionProcessor;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,11 +42,15 @@ public class DocumentToResourceService implements IDocumentToResourceService {
@Override
public Optional<Resource> toResource(ResourceSet resourceSet, Document document) {
Optional<Resource> optionalResource = Optional.empty();
var jsonResourceProcessor = new JsonResourceMigrationVersionProcessor();

HashMap<Object, Object> options = new HashMap<>();
options.put(JsonResource.OPTION_JSON_RESSOURCE_PROCESSOR, jsonResourceProcessor);

var resource = new JSONResourceFactory().createResourceFromPath(document.getId().toString());
try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
resourceSet.getResources().add(resource);
resource.load(inputStream, null);
resource.load(inputStream, options);

resource.eAdapters().add(new ResourceMetadataAdapter(document.getName()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.web.application.UUIDParser;
import org.eclipse.sirius.web.application.editingcontext.services.api.IResourceToDocumentService;
import org.eclipse.sirius.web.application.editingcontext.services.api.JsonResourceMigrationVersionProcessor;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;

/**
Expand All @@ -42,11 +44,13 @@ public class ResourceToDocumentService implements IResourceToDocumentService {
@Override
public Optional<DocumentData> toDocument(Resource resource) {
var serializationListener = new JsonResourceSerializationListener();
var jsonResourceProcessor = new JsonResourceMigrationVersionProcessor();

HashMap<Object, Object> options = new HashMap<>();
options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
options.put(JsonResource.OPTION_SCHEMA_LOCATION, true);
options.put(JsonResource.OPTION_SERIALIZATION_LISTENER, serializationListener);
options.put(JsonResource.OPTION_JSON_RESSOURCE_PROCESSOR, jsonResourceProcessor);

Optional<DocumentData> optionalDocumentData = Optional.empty();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.editingcontext.services.api;

import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.emfjson.resource.JsonResource;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

/**
* Used to load and serialize document version.
*
* @author mcharfadi
*/
public class JsonResourceMigrationVersionProcessor implements JsonResource.IJsonResourceProcessor {
@Override
public void preDeserialization(JsonResource resource, JsonObject jsonObject) {
var optionalResourceMetadataAdapter = resource.eAdapters().stream()
.filter(ResourceMetadataAdapter.class::isInstance)
.map(ResourceMetadataAdapter.class::cast)
.findFirst();
var migrationObject = jsonObject.getAsJsonObject("migration");
if (migrationObject != null) {
var lastMigrationPerformed = migrationObject.get("lastMigrationPerformed");
var migrationVersion = migrationObject.get("migrationVersion");
if (lastMigrationPerformed != null && migrationVersion != null) {
optionalResourceMetadataAdapter.ifPresent(resourceMetadataAdapter -> resourceMetadataAdapter.setMigrationVersion(migrationVersion.getAsString()));
optionalResourceMetadataAdapter.ifPresent(resourceMetadataAdapter -> resourceMetadataAdapter.setLastMigrationPerformed(lastMigrationPerformed.getAsString()));
System.out.println(migrationVersion.getAsString());
System.out.println(lastMigrationPerformed.getAsString());
}
}
}

@Override
public void postSerialization(JsonResource resource, JsonObject jsonObject) {
var version = resource.eAdapters().stream()
.filter(ResourceMetadataAdapter.class::isInstance)
.map(ResourceMetadataAdapter.class::cast)
.findFirst();

version.ifPresent(resourceMetadataAdapter -> {
var rootMigration = new JsonObject();
var migrationVersionElement = new JsonPrimitive(resourceMetadataAdapter.getMigrationVersion());
rootMigration.add("version", migrationVersionElement);
var migrationLastPerformed = new JsonPrimitive(resourceMetadataAdapter.getLastMigrationPerformed());
rootMigration.add("lastMigrationPerformed", migrationLastPerformed);

jsonObject.add("migration", rootMigration);
});
}
}

0 comments on commit 33207fa

Please sign in to comment.