From ddc14574feedbf9389c670e721c33ee0f849c41c Mon Sep 17 00:00:00 2001
From: David Hoksza <david.hoksza@uni.lu>
Date: Wed, 17 Jan 2018 16:18:29 +0100
Subject: [PATCH] Added associated annotator class to miriam data including
 hibernate mapping + createMiriamData in element annotator. without unit tests

---
 .../services/annotators/ElementAnnotator.java | 30 +++++++++
 .../services/annotators/TairAnnotator.java    |  4 +-
 .../annotators/ElementAnnotatorTest.java      | 33 ++++++++++
 .../lcsb/mapviewer/model/map/MiriamData.java  | 61 ++++++++++++++++++-
 .../lcsb/mapviewer/model/map/MiriamType.java  |  2 +-
 persist/src/db/12.0.0/fix_db_20180117.sql     |  2 +
 6 files changed, 126 insertions(+), 6 deletions(-)
 create mode 100644 persist/src/db/12.0.0/fix_db_20180117.sql

diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java
index 84761857a5..e71c85ce6a 100644
--- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java
+++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotator.java
@@ -12,6 +12,7 @@ import lcsb.mapviewer.common.exception.InvalidArgumentException;
 import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerChemical;
 import lcsb.mapviewer.model.map.BioEntity;
 import lcsb.mapviewer.model.map.MiriamData;
+import lcsb.mapviewer.model.map.MiriamRelationType;
 import lcsb.mapviewer.model.map.MiriamType;
 import lcsb.mapviewer.model.map.reaction.Reaction;
 import lcsb.mapviewer.model.map.species.Chemical;
@@ -473,5 +474,34 @@ public abstract class ElementAnnotator extends CachableInterface {
 			logger.warn(prefix + "MCS in db different: \"" + element.getMechanicalConfidenceScore() + "\", \"" + value + "\"");
 		}
 	}
+	
+	private void setAnnotator(MiriamData md){
+		md.setAnnotator(this.getClass());		
+	}
+	
+	protected MiriamData createMiriamData(){
+		MiriamData md = new MiriamData();
+		setAnnotator(md);
+		return md;		
+	}
+	
+	protected MiriamData createMiriamData(MiriamData _md){
+		MiriamData md = new MiriamData(_md);
+		setAnnotator(md);
+		return md;		
+	}
+	
+	protected MiriamData createMiriamData(MiriamType mt, String resource){
+		MiriamData md = new MiriamData(mt, resource);
+		setAnnotator(md);
+		return md;		
+	}
+	
+	protected MiriamData createMiriamData(MiriamRelationType relationType, MiriamType mt, String resource){
+		MiriamData md = new MiriamData(relationType, mt, resource);
+		setAnnotator(md);
+		return md;		
+	}	
+	
 
 }
diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java
index 0388293b12..9dcac43bcd 100644
--- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java
+++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/annotators/TairAnnotator.java
@@ -58,7 +58,7 @@ public class TairAnnotator extends ElementAnnotator implements IExternalService
 		this.setCache(null);
 
 		try {
-			MiriamData md = tairToUniprot(new MiriamData(MiriamType.TAIR_LOCUS, "AT1G01030"));
+			MiriamData md = tairToUniprot(createMiriamData(MiriamType.TAIR_LOCUS, "AT1G01030"));
 
 			status.setStatus(ExternalServiceStatusType.OK);
 			if (md == null || !md.getResource().equalsIgnoreCase("Q9MAN1")) {
@@ -122,7 +122,7 @@ public class TairAnnotator extends ElementAnnotator implements IExternalService
 		Collection<MiriamData> result = new HashSet<MiriamData>();
 		Matcher m = tairToUniprot.matcher(pageContent);
 		if (m.find()) {
-			result.add(new MiriamData(MiriamType.UNIPROT, m.group(1)));
+			result.add(createMiriamData(MiriamType.UNIPROT, m.group(1)));
 		}		
 		return result;
 	}
diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotatorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotatorTest.java
index 1dac8112d6..f4c613ca90 100644
--- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotatorTest.java
+++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/annotators/ElementAnnotatorTest.java
@@ -2,6 +2,7 @@ package lcsb.mapviewer.annotation.services.annotators;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
 
 import org.junit.After;
 import org.junit.AfterClass;
@@ -12,6 +13,9 @@ import lcsb.mapviewer.annotation.AnnotationTestFunctions;
 import lcsb.mapviewer.annotation.cache.CachableInterface;
 import lcsb.mapviewer.annotation.cache.SourceNotAvailable;
 import lcsb.mapviewer.model.map.BioEntity;
+import lcsb.mapviewer.model.map.MiriamData;
+import lcsb.mapviewer.model.map.MiriamRelationType;
+import lcsb.mapviewer.model.map.MiriamType;
 import lcsb.mapviewer.model.map.reaction.Reaction;
 import lcsb.mapviewer.model.map.species.GenericProtein;
 import lcsb.mapviewer.model.map.species.Ion;
@@ -193,5 +197,34 @@ public class ElementAnnotatorTest extends AnnotationTestFunctions {
 
 		assertEquals(1, getWarnings().size());
 	}
+	
+	@Test
+	public void createMiriamData1() {
+		MiriamData md = annotator.createMiriamData();
+		assertEquals(md.getAnnotator(), annotator.getClass()); 
+	}
+	
+	@Test
+	public void createMiriamData2() {
+		MiriamData md = annotator.createMiriamData(MiriamType.UNIPROT, "XXX");
+		assertEquals(md.getAnnotator(), annotator.getClass()); 
+	}
+	
+	@Test
+	public void createMiriamData3() {
+		MiriamData md = annotator.createMiriamData(MiriamRelationType.BQ_BIOL_ENCODES, MiriamType.UNIPROT, "XXX");
+		assertEquals(md.getAnnotator(), annotator.getClass()); 
+	}
+	
+	@Test
+	public void createMiriamData4() {
+		MiriamData md1 = new MiriamData(MiriamType.UNIPROT, "XXX");
+		assertNull(md1.getAnnotator());
+		
+		MiriamData md2 = annotator.createMiriamData(md1);
+		assertEquals(md2.getAnnotator(), annotator.getClass());
+	}
+	
+	
 
 }
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/MiriamData.java b/model/src/main/java/lcsb/mapviewer/model/map/MiriamData.java
index f6f3ba7142..12f8c4b232 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/MiriamData.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/MiriamData.java
@@ -64,6 +64,13 @@ public class MiriamData implements Comparable<MiriamData>, Serializable {
 	 */
 	@Column(name = "resource")
 	private String						 resource					= "";
+	
+	/**
+	 * The annotator which created the miriam data or which 
+	 * should be associated with it.
+	 */
+	@Column(name = "annotator")
+	private Class<?> 					annotator 					= null;
 
 	/**
 	 * Default constructor.
@@ -82,8 +89,9 @@ public class MiriamData implements Comparable<MiriamData>, Serializable {
 		setRelationType(md.relationType);
 		setDataType(md.dataType);
 		setResource(md.resource);
+		setAnnotator(md.annotator);
 	}
-
+	
 	/**
 	 * Constructor that initialize the data by information from params.
 	 * 
@@ -93,8 +101,11 @@ public class MiriamData implements Comparable<MiriamData>, Serializable {
 	 *          type of the miriam data (see: {@link MiriamType})
 	 * @param resource
 	 *          {@link #resource}
+	 * @param annotator
+	 *          {@link #annotator}
+	 *          
 	 */
-	public MiriamData(MiriamRelationType relationType, MiriamType mt, String resource) {
+	public MiriamData(MiriamRelationType relationType, MiriamType mt, String resource, Class<?> annotator) {
 		if (mt == null) {
 			throw new InvalidArgumentException("MiriamType cannot be null");
 		}
@@ -104,8 +115,36 @@ public class MiriamData implements Comparable<MiriamData>, Serializable {
 		setRelationType(relationType);
 		setDataType(mt);
 		setResource(resource);
+		setAnnotator(annotator);
+	}
+
+	/**
+	 * Constructor that initialize the data by information from params.
+	 * 
+	 * @param relationType
+	 *          {@link #relationType}
+	 * @param mt
+	 *          type of the miriam data (see: {@link MiriamType})
+	 * @param resource
+	 *          {@link #resource}
+	 */
+	public MiriamData(MiriamRelationType relationType, MiriamType mt, String resource) {
+		this(relationType, mt, resource, null);
+	}
+	
+	/**
+	 * Constructor that initialize the data by information from params.
+	 * 
+	 * @param mt
+	 *          type of the miriam data (see: {@link MiriamType})
+	 * @param resource
+	 *          {@link #resource}
+	 */
+	public MiriamData(MiriamType mt, String resource, Class<?> annotator) {
+		this(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, mt, resource, annotator);
 	}
 
+	
 	/**
 	 * Constructor that initialize the data by information from params.
 	 * 
@@ -115,7 +154,7 @@ public class MiriamData implements Comparable<MiriamData>, Serializable {
 	 *          {@link #resource}
 	 */
 	public MiriamData(MiriamType mt, String resource) {
-		this(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, mt, resource);
+		this(mt, resource, null);
 	}
 
 	/**
@@ -250,4 +289,20 @@ public class MiriamData implements Comparable<MiriamData>, Serializable {
 
 		}
 	}
+	
+	/**
+	 * 	
+	 * @return {@link #annotator}
+	 */
+	public Class<?> getAnnotator() {
+		return annotator;
+	}
+
+	/**
+	 * 
+	 * @param {@link #annotator}
+	 */
+	public void setAnnotator(Class<?> annotator) {
+		this.annotator = annotator;
+	}
 }
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java b/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java
index 155ec768e4..338903c600 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/MiriamType.java
@@ -494,6 +494,7 @@ public enum MiriamType {
 	 * then annotation of this type is valid.
 	 */
 	private List<Class<? extends BioEntity>> requiredClass = new ArrayList<>();
+		
 
 	/**
 	 * Constructor that initialize enum object.
@@ -700,5 +701,4 @@ public enum MiriamType {
 		}
 		throw new InvalidArgumentException("Invalid miriam uri: " + miriamUri);
 	}
-
 }
diff --git a/persist/src/db/12.0.0/fix_db_20180117.sql b/persist/src/db/12.0.0/fix_db_20180117.sql
new file mode 100644
index 0000000000..af37bf7888
--- /dev/null
+++ b/persist/src/db/12.0.0/fix_db_20180117.sql
@@ -0,0 +1,2 @@
+-- column to store which annotator is responsible for given miriam data
+alter table miriam_data_table add column annotator varchar(256) default null;
-- 
GitLab