Skip to content
Snippets Groups Projects
Commit a03e6e8b authored by Florian Hölzl's avatar Florian Hölzl
Browse files

extracted unique ID preparation into utility class

refs 450
parent 519e295e
No related branches found
No related tags found
No related merge requests found
......@@ -25,11 +25,9 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.EventObject;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
......@@ -47,6 +45,7 @@ import org.fortiss.tooling.kernel.ToolingKernelActivator;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
import org.fortiss.tooling.kernel.model.IIdLabeled;
import org.fortiss.tooling.kernel.service.IPersistencyService;
import org.fortiss.tooling.kernel.utils.UniqueIDUtils;
/**
* The model context provides additional commands and hooks for a model.
......@@ -55,7 +54,7 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
* @author hummel
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: DC486D7885D3E9603B1672F7B269ABC1
* @ConQAT.Rating YELLOW Hash: 2A3C09281619DEC4EBEDBA048E8E4046
*/
class ModelContext implements ITopLevelElement, CommandStackListener {
......@@ -139,73 +138,17 @@ class ModelContext implements ITopLevelElement, CommandStackListener {
runAsCommand(new Runnable() {
@Override
public void run() {
generateMissingIDs();
maxId = UniqueIDUtils.generateMissingIDs(
getRootModelElement(), maxId);
}
});
}
}
/** Generates missing IDs. Must be called within transaction. */
private void generateMissingIDs() {
for (Iterator<EObject> i = getRootModelElement().eAllContents(); i
.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
if (((IIdLabeled) eo).getId() <= 0) {
((IIdLabeled) eo).setId((++maxId));
}
}
}
}
/** {@inheritDoc} */
@Override
public void prepareIDs(EObject other) {
if (other == null) {
return;
}
boolean needSmart = false;
if (other instanceof IIdLabeled) {
if (((IIdLabeled) other).getId() <= 0) {
((IIdLabeled) other).setId(++maxId);
} else {
needSmart = true;
}
}
for (Iterator<EObject> i = other.eAllContents(); i.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
if (((IIdLabeled) eo).getId() <= 0) {
((IIdLabeled) eo).setId(++maxId);
} else {
needSmart = true;
}
}
}
if (needSmart) {
Set<Integer> usedIDs = new HashSet<Integer>();
for (Iterator<EObject> i = getRootModelElement().eAllContents(); i
.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
usedIDs.add(((IIdLabeled) eo).getId());
}
}
for (Iterator<EObject> i = other.eAllContents(); i.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
if (!usedIDs.add(((IIdLabeled) eo).getId())) {
int newId = ++maxId;
((IIdLabeled) eo).setId(newId);
usedIDs.add(newId);
}
}
}
}
maxId = UniqueIDUtils.prepareIDs(other, getRootModelElement(), maxId);
}
/** Destroys this context and closes all dependent editors. */
......
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 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.utils;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.tooling.kernel.model.IIdLabeled;
/**
* Utility methods for unique IDs of {@link IIdLabeled} model elements.
* IMPORTANT: Use this methods with care! It usually the task of the persistency
* layer to assign consistent IDs. However, test models and programmatic models
* may need to use this capability without the persistency mechanism in place.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 8A4299022B07F32CEED5A061DC03FBD2
*/
public class UniqueIDUtils {
/**
* Generates missing IDs of the given model starting from the given current
* maximum ID.
*
* @param existingModel
* the model to be updated
* @param currentMaxId
* the current maximum ID
*/
public static int generateMissingIDs(EObject existingModel, int currentMaxId) {
for (Iterator<EObject> i = existingModel.eAllContents(); i.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
if (((IIdLabeled) eo).getId() <= 0) {
((IIdLabeled) eo).setId((++currentMaxId));
}
}
}
return currentMaxId;
}
/**
* Assigns unique IDs to the given object and its content.
*
* @param object
* the model element to be prepared
* @param existingModel
* the existing model to be considered for used IDs if object
* already contains IDs > 0
* @param currentMaxId
* the current maximum ID to be started
* @return the new current maximum ID
*/
public static int prepareIDs(EObject object, EObject existingModel,
int currentMaxId) {
if (object == null) {
return currentMaxId;
}
boolean needSmart = false;
if (object instanceof IIdLabeled) {
if (((IIdLabeled) object).getId() <= 0) {
((IIdLabeled) object).setId(++currentMaxId);
} else {
needSmart = true;
}
}
for (Iterator<EObject> i = object.eAllContents(); i.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
if (((IIdLabeled) eo).getId() <= 0) {
((IIdLabeled) eo).setId(++currentMaxId);
} else {
needSmart = true;
}
}
}
if (needSmart) {
Set<Integer> usedIDs = new HashSet<Integer>();
for (Iterator<EObject> i = existingModel.eAllContents(); i
.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
usedIDs.add(((IIdLabeled) eo).getId());
}
}
for (Iterator<EObject> i = object.eAllContents(); i.hasNext();) {
EObject eo = i.next();
if (eo instanceof IIdLabeled) {
if (!usedIDs.add(((IIdLabeled) eo).getId())) {
int newId = ++currentMaxId;
((IIdLabeled) eo).setId(newId);
usedIDs.add(newId);
}
}
}
}
return currentMaxId;
}
}
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