From e5bcb7f429bd786d20aadb5a57219aa6390592ac Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 20 Jun 2017 10:59:03 +0200
Subject: [PATCH] copying adds information about comaprtments properly

---
 .../lcsb/mapviewer/commands/CopyCommand.java  | 25 ++++---
 .../mapviewer/commands/CopyCommandTest.java   | 65 ++++++++++++++++++-
 2 files changed, 79 insertions(+), 11 deletions(-)

diff --git a/model-command/src/main/java/lcsb/mapviewer/commands/CopyCommand.java b/model-command/src/main/java/lcsb/mapviewer/commands/CopyCommand.java
index 3014886559..5ea871caf6 100644
--- a/model-command/src/main/java/lcsb/mapviewer/commands/CopyCommand.java
+++ b/model-command/src/main/java/lcsb/mapviewer/commands/CopyCommand.java
@@ -176,6 +176,7 @@ public class CopyCommand extends NewModelCommand {
 			if (alias instanceof Compartment) {
 				Compartment copy = ((Compartment) alias).copy();
 				copy.getElements().clear();
+				copy.setCompartment(null);
 				result.addElement(copy);
 			}
 
@@ -184,21 +185,25 @@ public class CopyCommand extends NewModelCommand {
 		for (Element alias : model.getElements()) {
 			if (alias instanceof Species) {
 				Species copy = ((Species) alias).copy();
-				Compartment ca = copy.getCompartment();
 				copy.setCompartment(null);
-
-				// the trick is that in addAlias also some connections are added, so
-				// lets clear info about parents
-
 				result.addElement(copy);
 
-				if (ca != null) {
-					ca = result.getElementByElementId(ca.getElementId());
-					copy.setCompartment(ca);
+				Compartment parentCompartment = alias.getCompartment();
+
+				if (parentCompartment != null) {
+					parentCompartment = result.getElementByElementId(parentCompartment.getElementId());
+					copy.setCompartment(parentCompartment);
 				}
+				
 			} else if (alias instanceof Compartment) {
-				// we already added compartment alias
-				continue;
+				
+				Compartment parentCompartment = alias.getCompartment();
+
+				if (parentCompartment != null) {
+					Compartment copy = result.getElementByElementId(alias.getElementId());
+					parentCompartment = result.getElementByElementId(parentCompartment.getElementId());
+					copy.setCompartment(parentCompartment);
+				}
 			} else {
 				throw new InvalidClassException("Don't know what to do with: " + alias.getClass());
 			}
diff --git a/model-command/src/test/java/lcsb/mapviewer/commands/CopyCommandTest.java b/model-command/src/test/java/lcsb/mapviewer/commands/CopyCommandTest.java
index 6b2c37080b..cc78f07b6e 100644
--- a/model-command/src/test/java/lcsb/mapviewer/commands/CopyCommandTest.java
+++ b/model-command/src/test/java/lcsb/mapviewer/commands/CopyCommandTest.java
@@ -1,8 +1,12 @@
 package lcsb.mapviewer.commands;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.awt.geom.Point2D;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
@@ -14,8 +18,11 @@ import org.junit.Test;
 import lcsb.mapviewer.common.exception.InvalidArgumentException;
 import lcsb.mapviewer.converter.ConverterParams;
 import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
+import lcsb.mapviewer.model.graphics.PolylineData;
 import lcsb.mapviewer.model.map.OverviewImage;
 import lcsb.mapviewer.model.map.OverviewModelLink;
+import lcsb.mapviewer.model.map.compartment.Compartment;
+import lcsb.mapviewer.model.map.compartment.SquareCompartment;
 import lcsb.mapviewer.model.map.layout.graphics.Layer;
 import lcsb.mapviewer.model.map.model.ElementSubmodelConnection;
 import lcsb.mapviewer.model.map.model.Model;
@@ -23,9 +30,12 @@ import lcsb.mapviewer.model.map.model.ModelComparator;
 import lcsb.mapviewer.model.map.model.ModelFullIndexed;
 import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
 import lcsb.mapviewer.model.map.model.SubmodelType;
+import lcsb.mapviewer.model.map.reaction.Product;
+import lcsb.mapviewer.model.map.reaction.Reactant;
 import lcsb.mapviewer.model.map.reaction.Reaction;
-import lcsb.mapviewer.model.map.species.Element;
+import lcsb.mapviewer.model.map.reaction.type.StateTransitionReaction;
 import lcsb.mapviewer.model.map.species.Complex;
+import lcsb.mapviewer.model.map.species.Element;
 import lcsb.mapviewer.model.map.species.GenericProtein;
 
 public class CopyCommandTest extends CommandTestFunctions {
@@ -311,4 +321,57 @@ public class CopyCommandTest extends CommandTestFunctions {
 		}
 	}
 
+	@Test
+	public void testCopyModelReaction() throws Exception {
+		try {
+			Model model = new ModelFullIndexed(null);
+
+			Compartment c1 = new SquareCompartment("c1");
+			Compartment c2 = new SquareCompartment("c2");
+			c1.setSemanticZoomLevelVisibility("2");
+			c2.setSemanticZoomLevelVisibility("3");
+
+			model.addElement(c1);
+			model.addElement(c2);
+
+			GenericProtein s1 = new GenericProtein("s1");
+			s1.setCompartment(c1);
+			model.addElement(s1);
+
+			GenericProtein s2 = new GenericProtein("s2");
+			s2.setCompartment(c2);
+			model.addElement(s2);
+
+			StateTransitionReaction reaction = new StateTransitionReaction();
+			Reactant reactant = new Reactant(s1);
+			reactant.setLine(new PolylineData(new Point2D.Double(0, 0), new Point2D.Double(10, 10)));
+			reaction.addReactant(reactant);
+			Product product = new Product(s2);
+			product.setLine(new PolylineData(new Point2D.Double(10, 0), new Point2D.Double(120, 10)));
+			reaction.addProduct(product);
+			reaction.setSemanticZoomLevelVisibility("4");
+
+			model.addReaction(reaction);
+
+			assertTrue(s1.equals(reaction.getReactants().get(0).getElement()));
+			assertTrue(s2.equals(reaction.getProducts().get(0).getElement()));
+
+			Model model2 = new CopyCommand(model).execute();
+			Reaction reaction2 = model2.getReactions().iterator().next();
+
+			assertTrue(s1.equals(reaction.getReactants().get(0).getElement()));
+			assertTrue(s2.equals(reaction.getProducts().get(0).getElement()));
+
+			assertFalse(s1.equals(reaction2.getReactants().get(0).getElement()));
+			assertFalse(s2.equals(reaction2.getProducts().get(0).getElement()));
+
+			assertNotNull(reaction2.getReactants().get(0).getElement().getCompartment());
+			assertNotNull(reaction2.getProducts().get(0).getElement().getCompartment());
+
+		} catch (Exception e) {
+			e.printStackTrace();
+			throw e;
+		}
+	}
+
 }
-- 
GitLab