Skip to content
Snippets Groups Projects
Commit 085cec84 authored by Dongyue Mou's avatar Dongyue Mou
Browse files

YEELOW for prepare unique ID methods

parent e0d57e17
No related branches found
No related tags found
No related merge requests found
......@@ -17,12 +17,8 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.utils;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
import org.fortiss.tooling.kernel.model.IIdLabeled;
import org.fortiss.tooling.kernel.service.ICommandStackService;
import org.fortiss.tooling.kernel.service.IPersistencyService;
/**
* Utils class implementing basic model related functionality for convenience.
......@@ -30,7 +26,7 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
* @author doebber
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating YELLOW Hash: 526C09C0C004F117BD47159BB812532F
* @ConQAT.Rating YELLOW Hash: 6F9E8BE0FD48157F5B91BF398E3A5FA8
*/
public class ModelUtils {
......@@ -48,42 +44,4 @@ public class ModelUtils {
public static void runAsCommand(EObject element, Runnable command) {
ICommandStackService.INSTANCE.runAsCommand(element, command);
}
/**
* Assigns unique IDs to the given target model element.
*
* @param element
* the model element, which should be prepared for insertion into
* the given persisted model.
* @param context
* a model element already contained in a persisted model.
* @throws NullPointerException
* if the context is not contained in a persisted model.
*/
public static void prepareUniqueID(EObject element, EObject context) {
if(element instanceof IIdLabeled) {
ITopLevelElement top = IPersistencyService.INSTANCE.getTopLevelElementFor(context);
if(top == null) {
// Since we do not catch the max id, the id can be directly set as max id + 1
EObject root = context;
while(root.eContainer() != null) {
root = root.eContainer();
}
// see also org.fortiss.tooling.kernel.internal.storage.eclipse.ModelContext:129
int id = 0;
for(TreeIterator<EObject> iter = root.eAllContents(); iter.hasNext();) {
EObject o = iter.next();
if(o instanceof IIdLabeled) {
id = Math.max(id, ((IIdLabeled)o).getId());
}
}
((IIdLabeled)element).setId(id + 1);
} else {
top.prepareIDs(element);
}
}
}
}
......@@ -21,8 +21,11 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
import org.fortiss.tooling.kernel.model.IIdLabeled;
import org.fortiss.tooling.kernel.service.IPersistencyService;
/**
* Utility methods for unique IDs of {@link IIdLabeled} model elements.
......@@ -33,7 +36,7 @@ import org.fortiss.tooling.kernel.model.IIdLabeled;
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: A5E21261E29DDDE24057C442595C269B
* @ConQAT.Rating YELLOW Hash: 15530F3C5556388E5D4B10DB5442F364
*/
public class UniqueIDUtils {
......@@ -161,4 +164,42 @@ public class UniqueIDUtils {
}
return currentMaxId;
}
/**
* Assigns unique IDs to the given target model element.
*
* @param element
* the model element, which should be prepared for insertion into
* the given persisted model.
* @param context
* a model element already contained in a persisted model.
* @throws NullPointerException
* if the context is not contained in a persisted model.
*/
public static void prepareUniqueID(EObject element, EObject context) {
if(element instanceof IIdLabeled) {
ITopLevelElement top = IPersistencyService.INSTANCE.getTopLevelElementFor(context);
if(top == null) {
// Since we do not catch the max id, the id can be directly set as max id + 1
EObject root = context;
while(root.eContainer() != null) {
root = root.eContainer();
}
// see also org.fortiss.tooling.kernel.internal.storage.eclipse.ModelContext:129
int id = 0;
for(TreeIterator<EObject> iter = root.eAllContents(); iter.hasNext();) {
EObject o = iter.next();
if(o instanceof IIdLabeled) {
id = Math.max(id, ((IIdLabeled)o).getId());
}
}
((IIdLabeled)element).setId(id + 1);
} else {
top.prepareIDs(element);
}
}
}
}
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