Skip to content
Snippets Groups Projects
Commit 8e3113a2 authored by Simon Barner's avatar Simon Barner
Browse files

- Add RemoveUnknownFeatureMigrationProviderBase, a base class for {@link...

- Add RemoveUnknownFeatureMigrationProviderBase, a base class for {@link IMigrationProvider}s to selectively remove unknown features from a model.
parent e6a906da
No related branches found
No related tags found
No related merge requests found
......@@ -19,10 +19,19 @@ Export-Package: org.fortiss.tooling.kernel;uses:="org.eclipse.core.runtime,org.o
org.fortiss.tooling.kernel.extension.base,
org.fortiss.tooling.kernel.extension.data,
org.fortiss.tooling.kernel.extension.exception,
org.fortiss.tooling.kernel.model;uses:=org.eclipse.emf.ecore,
org.fortiss.tooling.kernel.model.impl;uses:=org.fortiss.tooling.kernel.model,
org.fortiss.tooling.kernel.model.util;uses:="org.eclipse.emf.ecore, org.fortiss.tooling.kernel.model, org.eclipse.emf.common.notify.impl, org.eclipse.emf.common.notify",
org.fortiss.tooling.kernel.service;uses:="org.eclipse.core.runtime, org.eclipse.emf.ecore, org.fortiss.tooling.kernel.interfaces, org.conqat.lib.commons.collections",
org.fortiss.tooling.kernel.migration,
org.fortiss.tooling.kernel.model;uses:="org.eclipse.emf.ecore",
org.fortiss.tooling.kernel.model.impl;uses:="org.fortiss.tooling.kernel.model",
org.fortiss.tooling.kernel.model.util;
uses:="org.eclipse.emf.ecore,
org.fortiss.tooling.kernel.model,
org.eclipse.emf.common.notify.impl,
org.eclipse.emf.common.notify",
org.fortiss.tooling.kernel.service;
uses:="org.eclipse.core.runtime,
org.eclipse.emf.ecore,
org.fortiss.tooling.kernel.interfaces,
org.conqat.lib.commons.collections",
org.fortiss.tooling.kernel.service.base,
org.fortiss.tooling.kernel.service.listener,
org.fortiss.tooling.kernel.utils;uses:="org.eclipse.emf.ecore,org.eclipse.core.resources"
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.migration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.xml.type.AnyType;
import org.fortiss.tooling.kernel.extension.IMigrationProvider;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
/**
* Base class for {@link IMigrationProvider}s to selectively remove unknown features from a model.
*
* @author barner
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 6F0F43B9A185DF6A90E4C76169E4B7AB
*/
public abstract class RemoveUnknownFeatureMigrationProviderBase implements IMigrationProvider {
/** Type of container previously owning the known unknown feature. */
protected Class<?> containerType;
/**
* {@link EClass} representing how {@code anyType} references the unknown feature
* (e.g., {@link org.eclipse.emf.ecore.EcorePackage.Literals#EREFERENCE} or
* {@link org.eclipse.emf.ecore.EcorePackage.Literals#EATTRIBUTE}).
*/
private EClass fieldType;
/** Name of field of {@code anyType} that is used to reference the unknown feature. */
private String fieldName;
/** Class name of unknown feature to be removed. */
private String className;
/** Constructs a new {@link RemoveUnknownFeatureMigrationProviderBase}. */
protected RemoveUnknownFeatureMigrationProviderBase(Class<?> containerType, EClass fieldType,
final String fieldName, final String className) {
this.containerType = containerType;
this.fieldType = fieldType;
this.fieldName = fieldName;
this.className = className;
}
/** {@inheritDoc} */
@Override
public boolean needMigration(ITopLevelElement modelElement,
Map<EObject, AnyType> unknownFeatures) {
return doInternal(unknownFeatures, false);
}
/** {@inheritDoc} */
@Override
public void migrate(ITopLevelElement modelElement, Map<EObject, AnyType> unknownFeatures) {
doInternal(unknownFeatures, true);
}
/**
* Helper method to implement {@link #needMigration(ITopLevelElement, Map)} and
* {@link #migrate(ITopLevelElement, Map)}.
*
* @param unknownFeatures
* Set of unknown features to be investigated / manipulated
*
* @param migrate
* If {@code true}, the respective unknown features are removed. Otherwise, the
* return value indicates, if the migration is required.
*
* @return {@code true} iff there are unknown features that should be / have been removed.
*/
private boolean doInternal(Map<EObject, AnyType> unknownFeatures, boolean migrate) {
boolean rval = false;
for(EObject object : unknownFeatures.keySet()) {
if(containerType.isAssignableFrom(object.eClass().getInstanceClass())) {
AnyType anyType = unknownFeatures.get(object);
FeatureMap featureMap = anyType.getMixed();
List<FeatureMap.Entry> toRemove = new ArrayList<FeatureMap.Entry>();
for(FeatureMap.Entry feature : featureMap) {
EStructuralFeature eStructuralFeature = feature.getEStructuralFeature();
if(eStructuralFeature.eClass().equals(fieldType) &&
eStructuralFeature.getName().equals(fieldName) &&
getUnknownEClassName(feature).equals(className)) {
if(migrate) {
toRemove.add(feature);
rval = true;
} else {
return true;
}
}
}
featureMap.removeAll(toRemove);
}
}
return rval;
}
/**
* Returns the name of an unknown {@link EClass} (i.e., that is not part of the meta-model (any
* more)) represented by a given {@link org.eclipse.emf.ecore.util.FeatureMap.Entry}.
*
* In case the {@link EClass} is not represented by {@link AnyType} (e.g., because the
* {@link EClass} is (still) part of the meta-model), {@code null} is returned.
*/
private String getUnknownEClassName(FeatureMap.Entry feature) {
if(feature.getValue() instanceof AnyType) {
AnyType type = (AnyType)feature.getValue();
return type.eClass().getName();
}
return null;
}
}
<!--
$Id$
@version $Rev$
@ConQAT.Rating YELLOW Hash: 04C186527050C746EDC0216E603ABA60
-->
<body>
<code>IMigrationProvider</code>s and <code>IMigrationProvider</code> base classes provided by the kernel.
<p>
</body>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment