Skip to content

Commit

Permalink
[2159] Change Capella Migration due to Sirius uid support
Browse files Browse the repository at this point in the history
Bug: 2159
Change-Id: I09e4e74f2e7f416c4649d984c7f714205fa7f145
Signed-off-by: Philippe DUL <philippe.dul@thalesgroup.com>
  • Loading branch information
pdulth committed Aug 28, 2018
1 parent 36cccf6 commit d242d89
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 58 deletions.
Expand Up @@ -22,11 +22,12 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.XMLLoad;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl;
import org.eclipse.sirius.business.api.resource.ResourceDescriptor;
import org.eclipse.sirius.business.internal.migration.RepresentationsFileVersionSAXParser;
import org.eclipse.sirius.business.internal.resource.AirDResourceFactory;
import org.eclipse.sirius.business.internal.resource.AirDResourceImpl;
import org.eclipse.sirius.business.internal.resource.parser.RepresentationsFileXMIHelper;
import org.eclipse.sirius.viewpoint.ViewpointPackage;
Expand All @@ -41,8 +42,6 @@
@SuppressWarnings("restriction")
public class AirdMigrationRunnable extends ModelMigrationRunnable {

static final String FORMAT_UTF8 = "UTF-8"; //$NON-NLS-1$

/**
* @param file
*/
Expand Down Expand Up @@ -79,75 +78,63 @@ static boolean isAirdFile(URI uri) {
@Override
public XMLResource doCreateResource(URI uri, final MigrationContext context) {
if (isAirdFile(uri)) {
XMLResource result = new AirDResourceImpl(uri) {

@Override
protected XMIExtensionHelperImpl createXMLHelper() {
return createCapellaXMLHelper(this);
}

return (XMIResource) new AirDResourceFactory() {

@Override
protected XMLLoad createXMLLoad() {
return createCustomizedHandler(createXMLHelper(), context);
}

@Override
protected XMLLoad createXMLLoad(Map<?, ?> options)
{
return createXMLLoad();
protected XMIResource doCreateAirdResourceImpl(URI uri) {
return new AirDResourceImpl(uri) {

@Override
protected XMIExtensionHelperImpl createXMLHelper() {
return createCapellaXMLHelper(this);
}

@Override
protected XMLLoad createXMLLoad() {
return createCustomizedHandler(createXMLHelper(), context);
}

@Override
protected XMLLoad createXMLLoad(Map<?, ?> options) {
return createXMLLoad();
}

@Override
protected void init() {
super.init();

//We want the loaded version information, even if there is no Sirius migration needed. maybe we have some too
RepresentationsFileVersionSAXParser parser = new RepresentationsFileVersionSAXParser(getURI());
String loadedVersion = parser.getVersion(new NullProgressMonitor());
getDefaultLoadOptions().put("VERSION", loadedVersion);

}
};
}

// @Override
// protected XMLSave createXMLSave() {
// return createCustomizedSaveHandler(createXMLHelper(), context);
// }

@Override
protected void init() {
super.init();

org.eclipse.sirius.business.internal.migration.RepresentationsFileVersionSAXParser parser = new RepresentationsFileVersionSAXParser(
getURI());
String loadedVersion = parser.getVersion(new NullProgressMonitor());

getDefaultLoadOptions().put("VERSION", loadedVersion);

// like CapellamodellerResource and AirDResourceFactory, add some
// initial properties
getDefaultSaveOptions().put(XMLResource.OPTION_ENCODING, FORMAT_UTF8);
getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, FORMAT_UTF8);
getDefaultSaveOptions().put(XMLResource.OPTION_URI_HANDLER, new URIHandlerImpl.PlatformSchemeAware());

getDefaultSaveOptions().put(XMLResource.OPTION_USE_FILE_BUFFER, Boolean.TRUE);
getDefaultSaveOptions().put(XMLResource.OPTION_FLUSH_THRESHOLD, Integer.valueOf(0x01000000));

// Avoid any error with unknown features which can exist in aird
// files. Migration of aird will be processed after
getDefaultLoadOptions().put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);

AirDResourceImpl.addMigrationOptions(loadedVersion, getDefaultLoadOptions(), getDefaultSaveOptions());

if (!getEncoding().equals(FORMAT_UTF8)) {
setEncoding(FORMAT_UTF8);
}
}

};

return result;
}.createResource(uri);

}

return super.doCreateResource(uri, context);
}

@Override
@SuppressWarnings("restriction")
public XMIExtensionHelperImpl createCapellaXMLHelper(XMLResource resource) {
// Ideally, the XMLHelper used should be inherited from RepresentationsFileXMIHelper. But, as
// XMIExtensionHelperImpl and RepresentationsFileXMIHelper are not in the same inheritance branch we do
// inheritance by composition instantiating RepresentationsFileXMIHelper as delegate.
final RepresentationsFileXMIHelper delegateXMLHelper = new RepresentationsFileXMIHelper(resource);
XMIExtensionHelperImpl result = new XMIExtensionHelperImpl(resource) {

@Override
public String getID(EObject obj) {
//[525261] Add technical id on Sirius meta-model
//getId on RepresentationsFileXMIHelper has been added to avoid save of xmi:id when an uid exist.
return delegateXMLHelper.getID(obj);
}

@Override
public EClassifier getType(EFactory eFactory, String typeName) {
EClassifier type = null;
Expand Down
Expand Up @@ -37,8 +37,8 @@
import org.polarsys.capella.core.diagram.helpers.IRepresentationAnnotationConstants;
import org.polarsys.capella.core.model.handler.helpers.CapellaAdapterHelper;
import org.polarsys.capella.core.model.handler.helpers.RepresentationHelper;
import org.polarsys.capella.core.model.handler.helpers.SemanticResourcesScope;
import org.polarsys.capella.shared.id.handler.IdManager;
import org.polarsys.capella.shared.id.handler.ResourceSetScope;

/**
* This class migrates annotation from DRepresentation to DRepresentationDescriptor
Expand Down Expand Up @@ -113,6 +113,7 @@ public void postMigrationExecute(ExecutionManager executionManager, ResourceSet
}
}

@SuppressWarnings("deprecation")
private int migrate(DRepresentation diagram, DRepresentationDescriptor descriptor, ResourceSet resourceSet) {
int result = 0;

Expand Down Expand Up @@ -199,7 +200,7 @@ private int migrate(DRepresentation diagram, DRepresentationDescriptor descripto
if (annotation != null) {
DAnnotation newAnnotation = null;
for (String id : annotation.getDetails().values()) {
EObject object = IdManager.getInstance().getEObject(id, new ResourceSetScope(resourceSet));
EObject object = IdManager.getInstance().getEObject(id, new SemanticResourcesScope(resourceSet));
if (object != null) {
if (newAnnotation == null) {
newAnnotation = DAnnotationHelper.createAnnotation(IRepresentationAnnotationConstants.ContextualElements, descriptor);
Expand Down
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2018 THALES GLOBAL SERVICES.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.model.handler.helpers;

import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.polarsys.capella.core.model.handler.command.CapellaResourceHelper;
import org.polarsys.capella.shared.id.handler.IScope;

/**
* A scope browsing only semantic resources on the given resource set
*/
public class SemanticResourcesScope implements IScope {

private ResourceSet _set;

public SemanticResourcesScope(ResourceSet set) {
_set = set;
}

@Override
public List<Resource> getResources() {
return _set.getResources().stream().filter(x -> CapellaResourceHelper.isCapellaResource(x))
.collect(Collectors.toList());
}

}

0 comments on commit d242d89

Please sign in to comment.