-
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.
Bug: #3333 Signed-off-by: Michaël Charfadi <michael.charfadi@obeosoft.com>
- Loading branch information
Showing
10 changed files
with
729 additions
and
4 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
60 changes: 60 additions & 0 deletions
60
...s-components-emf/src/main/java/org/eclipse/sirius/components/emf/utils/MigrationData.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,60 @@ | ||
/******************************************************************************* | ||
* 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.components.emf.utils; | ||
|
||
/** | ||
* POJO for MigrationData. | ||
* | ||
* @author mcharfadi | ||
*/ | ||
public class MigrationData { | ||
|
||
public static final String JSON_OBJECT_ROOT = "migration"; | ||
private String lastMigrationPerformed; | ||
|
||
private String migrationVersion; | ||
|
||
public MigrationData() { | ||
this.lastMigrationPerformed = "none"; | ||
this.migrationVersion = "0"; | ||
} | ||
|
||
public MigrationData(String lastMigrationPerformed, String migrationVersion) { | ||
this.lastMigrationPerformed = lastMigrationPerformed; | ||
this.migrationVersion = migrationVersion; | ||
} | ||
|
||
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 String toString() { | ||
return "MigrationData{" + | ||
"lastMigrationPerformed='" + lastMigrationPerformed + '\'' + | ||
", migrationVersion='" + migrationVersion + '\'' + | ||
'}'; | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
...va/org/eclipse/sirius/web/application/editingcontext/migration/IMigrationParticipant.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,146 @@ | ||
/******************************************************************************* | ||
* 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.migration; | ||
|
||
import org.eclipse.emf.ecore.EClass; | ||
import org.eclipse.emf.ecore.EClassifier; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.EPackage; | ||
import org.eclipse.emf.ecore.EStructuralFeature; | ||
import org.eclipse.sirius.emfjson.resource.JsonResource; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
/** | ||
* Interface of MigrationParticipant. | ||
* | ||
* @author mcharfadi | ||
*/ | ||
public interface IMigrationParticipant { | ||
String getVersion(); | ||
|
||
/** | ||
* Called during the parsing of JSonResources (at loading time). It allows to | ||
* retrieve a renamed EStructuralFeature from its old name. For example, if a | ||
* EStructuralFeature 'aaa' has been renamed to 'bbb', then your MigrationParticipant | ||
* should return the 'bbb' EStructuralFeature when given the 'aaa' name. | ||
* | ||
* @param eClass | ||
* the given eClass. | ||
* @param oldFeatureName | ||
* the feature name before migration. | ||
* @return the new structural feature or null if not found. The attribute | ||
* corresponding to given old name into given eClass. | ||
*/ | ||
default EStructuralFeature getStructuralFeature(EClass eClass, String oldFeatureName) { | ||
return eClass.getEStructuralFeature(oldFeatureName); | ||
} | ||
|
||
/** | ||
* Called before a JsonResource is serialized. | ||
* | ||
* @param resource | ||
* The JsonResource that is serialized | ||
* @param jsonObject | ||
* The root jsonObject | ||
*/ | ||
default void preDeserialization(JsonResource resource, JsonObject jsonObject) { | ||
|
||
} | ||
|
||
/** | ||
* Called after a JsonResource is serialized. | ||
* | ||
* @param resource | ||
* The JsonResource that is serialized | ||
* @param jsonObject | ||
* The root jsonObject | ||
*/ | ||
default void postSerialization(JsonResource resource, JsonObject jsonObject) { | ||
|
||
} | ||
|
||
/** | ||
* Called during the parsing of JSonResources (at loading time). It allows to | ||
* retrieve and modify an eobject just before it's loaded into the resource, | ||
* after every feature were set. | ||
* | ||
* @param eobject | ||
* the given eobject. | ||
* @param jsonObject | ||
* the jsonObject used to set the features of the eobject. | ||
*/ | ||
default void getJSonObjectBeforeLoading(EObject eobject, JsonObject jsonObject) { | ||
|
||
} | ||
|
||
/** | ||
* Called during the parsing of XMIResources (at loading time). If a feature value has changed since a previous | ||
* version, use this method to return the correct expected value. The feature value do not have to be set here, | ||
* that will be done by JsonHelper.setValue(). | ||
* | ||
* @param object | ||
* the object containing the feature. | ||
* @param feature | ||
* the feature to set value. | ||
* @param value | ||
* the initial serialized value. | ||
* @return the new value, or null otherwise. | ||
*/ | ||
default Object getValue(EObject object, EStructuralFeature feature, Object value) { | ||
return null; | ||
} | ||
|
||
/** | ||
* Called during the parsing of JSonResources (at loading time). It allows to | ||
* retrieve a renamed EStructuralFeature from its old name. For example, if a | ||
* feature 'aaa' has been renamed to 'bbb', then your MigrationParticipant | ||
* should return the 'bbb' feature when given the 'aaa' name. | ||
* | ||
* @param eClass | ||
* the given eClass. | ||
* @param oldAttributeName | ||
* the feature name before migration. | ||
* @return the new structural feature or null if not found. The attribute | ||
* corresponding to given old name into given eClass. | ||
*/ | ||
default EStructuralFeature getElement(EClass eClass, String oldAttributeName) { | ||
return eClass.getEStructuralFeature(oldAttributeName); | ||
} | ||
|
||
/** | ||
* Called during the parsing of JSonResources (at loading time). If an | ||
* EClassifier name has changed, then you should return the correct one. | ||
* | ||
* @param ePackage | ||
* the package where looking for classifier. | ||
* @param typeName | ||
* the old classifier name before migration. | ||
* @return the new classifier corresponding to the old given name into given | ||
* ePackage or null if not found. | ||
*/ | ||
default EClassifier getType(EPackage ePackage, String typeName) { | ||
return ePackage.getEClassifier(typeName); | ||
} | ||
|
||
/** | ||
* Return the EPackage to use for the given namespace. | ||
* | ||
* @param namespace | ||
* the nsURI of the package we are looking for. | ||
* @return an EPackage if some new mapping exists, null otherwise. | ||
*/ | ||
default EPackage getPackage(String namespace) { | ||
return null; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...rg/eclipse/sirius/web/application/editingcontext/migration/MigrationExtendedMetaData.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,88 @@ | ||
/******************************************************************************* | ||
* 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.migration; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.eclipse.emf.ecore.EClass; | ||
import org.eclipse.emf.ecore.EClassifier; | ||
import org.eclipse.emf.ecore.EPackage; | ||
import org.eclipse.emf.ecore.EStructuralFeature; | ||
import org.eclipse.emf.ecore.util.BasicExtendedMetaData; | ||
import org.eclipse.sirius.components.emf.utils.MigrationData; | ||
|
||
/** | ||
* Specialized BasicExtendedMetaData. | ||
* | ||
* @author mcharfadi | ||
*/ | ||
public class MigrationExtendedMetaData extends BasicExtendedMetaData { | ||
private final MigrationData documentMigrationData; | ||
private final List<IMigrationParticipant> migrationParticipants; | ||
public MigrationExtendedMetaData(List<IMigrationParticipant> migrationParticipants, MigrationData documentMigrationData) { | ||
this.migrationParticipants = Objects.requireNonNull(migrationParticipants); | ||
this.documentMigrationData = Objects.requireNonNull(documentMigrationData); | ||
} | ||
private boolean isCandidateVersion(IMigrationParticipant migrationParticipant) { | ||
return migrationParticipant.getVersion().compareTo(this.documentMigrationData.getMigrationVersion()) > 0; | ||
} | ||
|
||
@Override | ||
public EStructuralFeature getElement(EClass eClass, String namespace, String name) { | ||
EStructuralFeature structuralFeature = eClass.getEStructuralFeature(name); | ||
var migrationParticipantsCandidate = migrationParticipants.stream() | ||
.filter(this::isCandidateVersion) | ||
.sorted(Comparator.comparing(IMigrationParticipant::getVersion)) | ||
.toList(); | ||
for (IMigrationParticipant migrationParticipant : migrationParticipantsCandidate) { | ||
EStructuralFeature newStructuralFeature = migrationParticipant.getElement(eClass, name); | ||
if (newStructuralFeature != null) { | ||
structuralFeature = newStructuralFeature; | ||
} | ||
} | ||
return structuralFeature; | ||
} | ||
@Override | ||
public EClassifier getType(EPackage ePackage, String typeName) { | ||
EClassifier eClassifier = ePackage.getEClassifier(typeName); | ||
var migrationParticipantsCandidate = migrationParticipants.stream() | ||
.filter(this::isCandidateVersion) | ||
.sorted(Comparator.comparing(IMigrationParticipant::getVersion)) | ||
.toList(); | ||
for (IMigrationParticipant migrationParticipant : migrationParticipantsCandidate) { | ||
EClassifier newEClassifier = migrationParticipant.getType(ePackage, typeName); | ||
if (newEClassifier != null) { | ||
eClassifier = newEClassifier; | ||
} | ||
} | ||
return eClassifier; | ||
} | ||
|
||
@Override | ||
public EPackage getPackage(String namespace) { | ||
EPackage ePackage = super.getPackage(namespace); | ||
var migrationParticipantsCandidate = migrationParticipants.stream() | ||
.filter(this::isCandidateVersion) | ||
.sorted(Comparator.comparing(IMigrationParticipant::getVersion)) | ||
.toList(); | ||
for (IMigrationParticipant migrationParticipant : migrationParticipantsCandidate) { | ||
EPackage newEPackage = migrationParticipant.getPackage(namespace); | ||
if (newEPackage != null) { | ||
ePackage = newEPackage; | ||
} | ||
} | ||
return ePackage; | ||
} | ||
} |
Oops, something went wrong.