From af245bf063d1d250fb9af5e38bff5252601028c0 Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Wed, 20 Jun 2018 12:04:38 +0200
Subject: [PATCH] Storage: Improve error message for duplicated/missing IDs

* Print list of all affected elements
* Indicate if it ID was missing or duplicated

Issue-Ref: 3459
Issue-Url: https://af3-developer.fortiss.org/issues/3459

Signed-off-by: Simon Barner <barner@fortiss.org>
---
 .../kernel/internal/storage/eclipse/.ratings  |  2 +-
 .../storage/eclipse/ModelContext.java         | 32 ++++++++++++-------
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/.ratings
index 5484345d5..8708fd984 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/.ratings
@@ -1,5 +1,5 @@
 AutoUndoCommandStack.java 6aa645a9ed6e6547539c376fda97284928c4f9d4 GREEN
 EMFTransactionalCommand.java ba4b5bead9768b6ce6c955b9238cd96cb722533c GREEN
 EclipseResourceStorageService.java 1b9722e31a5ec33e4c3f7bb171fc2ce587729bf8 GREEN
-ModelContext.java 0510b23ced1fc381a43d5e45437ba6558b5d3301 YELLOW
+ModelContext.java 220944231c0e036f29d4911d2658c33b2427b09f YELLOW
 NonDirtyingEMFTransactionalCommand.java ec5f282603891096b09f2628155dd27e3a21c588 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java
index 0510b23ce..220944231 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/storage/eclipse/ModelContext.java
@@ -34,6 +34,7 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
 import org.eclipse.core.resources.IFile;
@@ -202,22 +203,22 @@ class ModelContext implements ITopLevelElement, CommandStackListener {
 	private boolean checkIDs() {
 		Set<Integer> ids = new HashSet<Integer>();
 
-		boolean hadMissing = false;
-		boolean hadDuplicates = false;
+		// Store String representation of element with duplicated IDs before fixing it
+		Map<String, Integer> objWithIdProblem = new HashMap<String, Integer>();
 		for(Iterator<EObject> i = getRootModelElement().eAllContents(); i.hasNext();) {
 			EObject eo = i.next();
 			if(eo instanceof IIdLabeled) {
 				final IIdLabeled element = (IIdLabeled)eo;
 				int id = element.getId();
 				if(id <= 0) {
-					hadMissing = true;
+					objWithIdProblem.put(element.toString(), null);
 				} else {
 					// Reset duplicate IDs. They will be regenerated together with missing IDs. This
 					// is a safe operation since at this stage, references are still based on Java
 					// references. Only in the persisted resource, references will be based on IDs,
 					// which is why special care must be taken to ensure uniqueness.
 					if(ids.contains(id)) {
-						hadDuplicates = true;
+						objWithIdProblem.put(element.toString(), element.getId());
 						runAsNonDirtyingCommand(() -> {
 							element.setId(0);
 						});
@@ -231,18 +232,27 @@ class ModelContext implements ITopLevelElement, CommandStackListener {
 
 		maxId = max(0, maxId);
 
-		if(hadMissing || hadDuplicates) {
+		if(!objWithIdProblem.isEmpty()) {
 			runAsNonDirtyingCommand(() -> {
 				maxId = generateMissingIDs(getRootModelElement(), maxId);
 			});
 
-			if(hadDuplicates) {
-				String msg = "Duplicate IDs have been removed from \"";
-				msg += resource.getURI().lastSegment() + "\". ";
-				msg +=
-						"Please report this incident since it could result in corrupted model files.";
-				error(ToolingKernelActivator.getDefault(), msg);
+			String msg = "The following missing/duplicate IDs have been fixed in \"";
+			msg += resource.getURI().lastSegment() + "\".\n";
+			msg += "Please report this incident since it indicates a programming error ";
+			msg += "that could result in corrupted model files.\n";
+			for(Entry<String, Integer> entry : objWithIdProblem.entrySet()) {
+				Integer id = entry.getValue();
+				if(id == null) {
+					msg += "  Missing ID added to ";
+				} else {
+					msg += "  Duplicate ID " + id + " disambiguated for ";
+				}
+				String name = entry.getKey();
+				msg += "element \"" + name + "\"\n";
 			}
+			error(ToolingKernelActivator.getDefault(), msg);
+
 			return true;
 		}
 
-- 
GitLab