-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3333] Add JsonResourceMigrationVersionProcessor
Bug: #3333 Signed-off-by: Michaël Charfadi <michael.charfadi@obeosoft.com>
- Loading branch information
Showing
5 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...us/web/application/editingcontext/services/api/JsonResourceMigrationVersionProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |