From 6d4207edbae0f21ce68fe5037eab651f6e65127a Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Wed, 20 Jun 2018 11:31:27 +0200
Subject: [PATCH] Storage: Avoid duplicated save in case of duplicated/missing
 IDs

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         | 43 ++++++++++++-------
 2 files changed, 28 insertions(+), 17 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 79965691f..5484345d5 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 85ede185d4b232221a96d0bf37a4c676b105dc00 GREEN
+ModelContext.java 0510b23ced1fc381a43d5e45437ba6558b5d3301 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 85ede185d..0510b23ce 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
@@ -151,7 +151,14 @@ class ModelContext implements ITopLevelElement, CommandStackListener {
 			}
 		});
 
-		checkIDs();
+		if(checkIDs()) {
+			try {
+				doSave(new NullProgressMonitor());
+			} catch(IOException | CoreException e) {
+				error(ToolingKernelActivator.getDefault(),
+						"Error saving model file when fixing missing/duplicate IDs.", e);
+			}
+		}
 	}
 
 	/** {@inheritDoc} */
@@ -182,11 +189,17 @@ class ModelContext implements ITopLevelElement, CommandStackListener {
 	}
 
 	/**
-	 * Checks whether all IDs are present and unique and updates {@link #maxId}. In case duplicate
-	 * IDs have been detected (which could only be caused by a bug in AF3), these are removed and an
-	 * error is logged.
+	 * Checks whether all IDs are present and unique and updates {@link #maxId}.
+	 * <ul>
+	 * <li>Duplicate IDs indicate a programming error. They are removed and an error is logged</li>
+	 * <li>Missing IDs are silently added.</li>
+	 * </ul>
+	 * 
+	 * @return {@code true} iff the model was modified because of missing/duplicated IDs (and hence
+	 *         must
+	 *         be saved).
 	 */
-	private void checkIDs() {
+	private boolean checkIDs() {
 		Set<Integer> ids = new HashSet<Integer>();
 
 		boolean hadMissing = false;
@@ -222,20 +235,18 @@ class ModelContext implements ITopLevelElement, CommandStackListener {
 			runAsNonDirtyingCommand(() -> {
 				maxId = generateMissingIDs(getRootModelElement(), maxId);
 			});
-			try {
-				doSave(new NullProgressMonitor());
-			} catch(IOException | CoreException e) {
-				error(ToolingKernelActivator.getDefault(),
-						"Error saving model file when fixing missing/duplicate IDs.", e);
+
+			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);
 			}
+			return true;
 		}
 
-		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);
-		}
+		return false;
 	}
 
 	/** {@inheritDoc} */
-- 
GitLab