From 33b5a143c83b888106c98eef56be24fb6df93ac0 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 10 May 2019 18:36:45 +0200
Subject: [PATCH] problem with not drawing images on map upload fixed

---
 .../bioEntity/element/ElementConverter.java   |   2 +-
 .../compartment/CompartmentConverter.java     |   9 +-
 .../element/species/SpeciesConverter.java     |  11 +-
 .../bioEntity/BioEntityConverterImplTest.java | 150 +++++++++++-------
 .../element/species/SpeciesConverterTest.java |  14 +-
 .../model/map/layout/graphics/Glyph.java      |   2 +-
 .../mapviewer/model/map/species/Element.java  |   4 +-
 .../model/map/species/ElementTest.java        |  12 ++
 8 files changed, 132 insertions(+), 72 deletions(-)

diff --git a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/ElementConverter.java b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/ElementConverter.java
index 9a6e85e6ec..973cf072fc 100644
--- a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/ElementConverter.java
+++ b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/ElementConverter.java
@@ -43,7 +43,7 @@ public abstract class ElementConverter<T extends Element> extends BioEntityConve
    * @throws DrawingException
    *           thrown when there is a problem with drawing
    */
-  private void drawGlyph(T bioEntity, Graphics2D graphics) throws DrawingException {
+  protected void drawGlyph(T bioEntity, Graphics2D graphics) throws DrawingException {
     try {
       Image img = ImageIO.read(new ByteArrayInputStream(bioEntity.getGlyph().getFile().getFileContent()));
       graphics.drawImage(img,
diff --git a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/compartment/CompartmentConverter.java b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/compartment/CompartmentConverter.java
index 2c9b32cc90..4d630b573c 100644
--- a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/compartment/CompartmentConverter.java
+++ b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/compartment/CompartmentConverter.java
@@ -179,7 +179,14 @@ public abstract class CompartmentConverter<T extends Compartment> extends Elemen
 
 	@Override
 	public void draw(T alias, Graphics2D graphics, ConverterParams params, List<ColorSchema> visualizedLayoutsColorSchemas) throws DrawingException {
-		drawImpl(alias, graphics, params);
+	  logger.debug("Draw: "+alias.getElementId());
+	    if (alias.getGlyph() != null) {
+	      logger.debug("as glyph");
+	      drawGlyph(alias, graphics);
+	    } else {
+          logger.debug("without glyph");
+	      drawImpl(alias, graphics, params);
+	    }
 
 		Color oldColor = graphics.getColor();
 		int count = 0;
diff --git a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverter.java b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverter.java
index 2aca0689bd..da4c75cc04 100644
--- a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverter.java
+++ b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverter.java
@@ -496,9 +496,16 @@ public abstract class SpeciesConverter<T extends Species> extends ElementConvert
   }
 
   @Override
-  public void draw(T species, Graphics2D graphics, ConverterParams params,
+  public final void draw(T species, Graphics2D graphics, ConverterParams params,
       List<ColorSchema> visualizedLayoutsColorSchemas) throws DrawingException {
-    drawImpl(species, graphics, params);
+    logger.debug("Draw: "+species.getElementId());
+    if (species.getGlyph() != null) {
+      logger.debug("as glyph");
+      drawGlyph(species, graphics);
+    } else {
+      logger.debug("without glyph");
+      drawImpl(species, graphics, params);
+    }
 
     Color oldColor = graphics.getColor();
     int count = 0;
diff --git a/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/BioEntityConverterImplTest.java b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/BioEntityConverterImplTest.java
index 68060ae783..4cd4aa512c 100644
--- a/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/BioEntityConverterImplTest.java
+++ b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/BioEntityConverterImplTest.java
@@ -1,13 +1,23 @@
 package lcsb.mapviewer.converter.graphics.bioEntity;
 
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.nullable;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
 import java.awt.Color;
+import java.awt.Desktop;
 import java.awt.Graphics2D;
+import java.awt.Image;
 import java.awt.geom.GeneralPath;
 import java.awt.geom.Point2D;
+import java.awt.image.BufferedImage;
+import java.awt.image.ImageObserver;
+import java.io.File;
+import java.nio.file.Files;
+
+import javax.imageio.ImageIO;
 
 import org.junit.After;
 import org.junit.AfterClass;
@@ -17,7 +27,10 @@ import org.mockito.Mockito;
 
 import lcsb.mapviewer.commands.ColorExtractor;
 import lcsb.mapviewer.converter.graphics.ConverterParams;
+import lcsb.mapviewer.converter.graphics.GraphicsTestFunctions;
+import lcsb.mapviewer.model.cache.UploadedFileEntry;
 import lcsb.mapviewer.model.graphics.PolylineData;
+import lcsb.mapviewer.model.map.layout.graphics.Glyph;
 import lcsb.mapviewer.model.map.modifier.Catalysis;
 import lcsb.mapviewer.model.map.reaction.Modifier;
 import lcsb.mapviewer.model.map.reaction.Product;
@@ -25,74 +38,103 @@ import lcsb.mapviewer.model.map.reaction.Reactant;
 import lcsb.mapviewer.model.map.reaction.Reaction;
 import lcsb.mapviewer.model.map.species.GenericProtein;
 
-public class BioEntityConverterImplTest {
+public class BioEntityConverterImplTest extends GraphicsTestFunctions{
+
+  ColorExtractor colorExtractor = new ColorExtractor(Color.RED, Color.GREEN, Color.BLUE);
+
+  @AfterClass
+  public static void tearDownAfterClass() throws Exception {
+  }
+
+  @Before
+  public void setUp() throws Exception {
+  }
+
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  @Test
+  public void testDrawReactionWithSemanticZoomingAndReactantOff() throws Exception {
+    try {
+      Graphics2D graphics = Mockito.mock(Graphics2D.class);
+      Reaction reaction = createReaction(1.0);
+      reaction.getReactants().get(0).getElement().setVisibilityLevel("11");
+
+      BioEntityConverterImpl rc = new BioEntityConverterImpl(reaction, false, colorExtractor);
+      rc.draw(reaction, graphics, new ConverterParams().nested(true).level(10));
+
+      verify(graphics, times(0)).draw(any(GeneralPath.class));
+
+    } catch (Exception e) {
+      throw e;
+    }
+  }
 
-	ColorExtractor colorExtractor = new ColorExtractor(Color.RED, Color.GREEN, Color.BLUE);
+  @Test
+  public void testDrawReactionWithSemanticZoomingAndProductOff() throws Exception {
+    try {
+      Graphics2D graphics = Mockito.mock(Graphics2D.class);
 
-	@AfterClass
-	public static void tearDownAfterClass() throws Exception {
-	}
+      Reaction reaction = createReaction(1.0);
+      reaction.getProducts().get(0).getElement().setVisibilityLevel("11");
 
-	@Before
-	public void setUp() throws Exception {
-	}
+      BioEntityConverterImpl rc = new BioEntityConverterImpl(reaction, false, colorExtractor);
+      rc.draw(reaction, graphics, new ConverterParams().nested(true).level(10));
 
-	@After
-	public void tearDown() throws Exception {
-	}
+      verify(graphics, times(0)).draw(any(GeneralPath.class));
 
-	@Test
-	public void testDrawReactionWithSemanticZoomingAndReactantOff() throws Exception {
-		try {
-			Graphics2D graphics = Mockito.mock(Graphics2D.class);
-			Reaction reaction = createReaction(1.0);
-			reaction.getReactants().get(0).getElement().setVisibilityLevel("11");
+    } catch (Exception e) {
+      throw e;
+    }
+  }
 
-			BioEntityConverterImpl rc = new BioEntityConverterImpl(reaction, false, colorExtractor);
-			rc.draw(reaction, graphics, new ConverterParams().nested(true).level(10));
+  @Test
+  public void testDrawAliasWithGlyph() throws Exception {
+    try {
+      BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
+      Graphics2D graphics = Mockito.spy(bi.createGraphics());
 
-			verify(graphics, times(0)).draw(any(GeneralPath.class));
+      GenericProtein alias = createProtein();
+      Glyph glyph = new Glyph();
+      UploadedFileEntry file = new UploadedFileEntry();
+      file.setOriginalFileName("test");
+      byte[] fileContent = Files.readAllBytes(new File("testFiles/glyph.png").toPath());
 
-		} catch (Exception e) {
-			throw e;
-		}
-	}
+      file.setFileContent(fileContent);
+      glyph.setFile(file);
+      alias.setGlyph(glyph);
+      BioEntityConverterImpl converter = new BioEntityConverterImpl(alias, false, colorExtractor);
+      converter.draw(alias, graphics, new ConverterParams());
 
-	@Test
-	public void testDrawReactionWithSemanticZoomingAndProductOff() throws Exception {
-		try {
-			Graphics2D graphics = Mockito.mock(Graphics2D.class);
 
-			Reaction reaction = createReaction(1.0);
-			reaction.getProducts().get(0).getElement().setVisibilityLevel("11");
 
-			BioEntityConverterImpl rc = new BioEntityConverterImpl(reaction, false, colorExtractor);
-			rc.draw(reaction, graphics, new ConverterParams().nested(true).level(10));
+      verify(graphics, times(1)).drawImage(any(Image.class), anyInt(), anyInt(), anyInt(), anyInt(), anyInt(), anyInt(),
+          anyInt(), anyInt(), nullable(ImageObserver.class));
 
-			verify(graphics, times(0)).draw(any(GeneralPath.class));
 
-		} catch (Exception e) {
-			throw e;
-		}
-	}
+    } catch (Exception e) {
+      throw e;
+    }
+  }
 
-	private Reaction createReaction(double lineWidth) {
-		Reaction result = new Reaction();
+  private Reaction createReaction(double lineWidth) {
+    Reaction result = new Reaction();
 
-		Modifier modifier = new Catalysis(new GenericProtein("s1"));
-		modifier.setLine(new PolylineData(new Point2D.Double(100, 20), new Point2D.Double(100, 80)));
-		modifier.getLine().setWidth(lineWidth);
+    Modifier modifier = new Catalysis(new GenericProtein("s1"));
+    modifier.setLine(new PolylineData(new Point2D.Double(100, 20), new Point2D.Double(100, 80)));
+    modifier.getLine().setWidth(lineWidth);
 
-		Reactant reactant = new Reactant(new GenericProtein("s2"));
-		reactant.setLine(new PolylineData(new Point2D.Double(90, 90), new Point2D.Double(10, 90)));
-		reactant.getLine().setWidth(lineWidth);
-		Product product = new Product(new GenericProtein("s3"));
-		product.setLine(new PolylineData(new Point2D.Double(200, 90), new Point2D.Double(110, 90)));
-		product.getLine().setWidth(lineWidth);
-		result.addModifier(modifier);
-		result.addProduct(product);
-		result.addReactant(reactant);
-		return result;
-	}
+    Reactant reactant = new Reactant(new GenericProtein("s2"));
+    reactant.setLine(new PolylineData(new Point2D.Double(90, 90), new Point2D.Double(10, 90)));
+    reactant.getLine().setWidth(lineWidth);
+    Product product = new Product(new GenericProtein("s3"));
+    product.setLine(new PolylineData(new Point2D.Double(200, 90), new Point2D.Double(110, 90)));
+    product.getLine().setWidth(lineWidth);
+    result.addModifier(modifier);
+    result.addProduct(product);
+    result.addReactant(reactant);
+    return result;
+  }
 
 }
diff --git a/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverterTest.java b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverterTest.java
index 50fabb870e..12c1aa6efc 100644
--- a/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverterTest.java
+++ b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/SpeciesConverterTest.java
@@ -33,6 +33,7 @@ import org.mockito.Mockito;
 
 import lcsb.mapviewer.commands.ColorExtractor;
 import lcsb.mapviewer.converter.graphics.ConverterParams;
+import lcsb.mapviewer.converter.graphics.GraphicsTestFunctions;
 import lcsb.mapviewer.converter.graphics.geometry.ArrowTransformation;
 import lcsb.mapviewer.model.cache.UploadedFileEntry;
 import lcsb.mapviewer.model.map.layout.ColorSchema;
@@ -49,7 +50,7 @@ import lcsb.mapviewer.model.map.species.field.RegulatoryRegion;
 import lcsb.mapviewer.model.map.species.field.Residue;
 import lcsb.mapviewer.model.map.species.field.TranscriptionSite;
 
-public class SpeciesConverterTest {
+public class SpeciesConverterTest extends GraphicsTestFunctions{
   Logger logger = Logger.getLogger(SpeciesConverterTest.class);
 
   ColorExtractor colorExtractor = new ColorExtractor(Color.RED, Color.GREEN, Color.BLUE);
@@ -405,15 +406,4 @@ public class SpeciesConverterTest {
     }
   }
 
-  private GenericProtein createProtein() {
-    GenericProtein protein = new GenericProtein("id");
-    protein.setName("NAME_OF_THE_ELEMENT");
-    protein.setX(10);
-    protein.setY(20);
-    protein.setWidth(100);
-    protein.setHeight(80);
-    protein.setColor(Color.WHITE);
-
-    return protein;
-  }
 }
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/layout/graphics/Glyph.java b/model/src/main/java/lcsb/mapviewer/model/map/layout/graphics/Glyph.java
index cbf530889c..d23576df24 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/layout/graphics/Glyph.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/layout/graphics/Glyph.java
@@ -73,7 +73,7 @@ public class Glyph implements Serializable {
    */
   public Glyph(Glyph original) {
     // we should reference to the same file
-    setFile(file);
+    setFile(original.getFile());
   }
 
   public UploadedFileEntry getFile() {
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java b/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java
index 6a4db28730..1d3ac88910 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/species/Element.java
@@ -302,7 +302,9 @@ public abstract class Element implements BioEntity, Serializable, SbmlArgument {
     this.formula = original.getFormula();
     setVisibilityLevel(original.getVisibilityLevel());
     setTransparencyLevel(original.getTransparencyLevel());
-    setGlyph(new Glyph(original.getGlyph()));
+    if (original.getGlyph() != null) {
+      setGlyph(new Glyph(original.getGlyph()));
+    }
   }
 
   /**
diff --git a/model/src/test/java/lcsb/mapviewer/model/map/species/ElementTest.java b/model/src/test/java/lcsb/mapviewer/model/map/species/ElementTest.java
index 29c629c7a0..0806862ac6 100644
--- a/model/src/test/java/lcsb/mapviewer/model/map/species/ElementTest.java
+++ b/model/src/test/java/lcsb/mapviewer/model/map/species/ElementTest.java
@@ -130,6 +130,18 @@ public class ElementTest extends ModelTestFunctions {
         }
     }
 
+    @Test
+    public void testCopyWithoutGlyph() {
+        try {
+            GenericProtein protein = new GenericProtein();
+            Element copy = new GenericProtein(protein);
+            assertNull(copy.getGlyph());
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw e;
+        }
+    }
+
 	@Test
 	public void testIncreaseBorder() {
 		try {
-- 
GitLab