From 1669d630207299f58398a52a82ec181965e2c4e2 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 31 Jul 2017 16:34:26 +0200
Subject: [PATCH] search by database id implemented

---
 .../services/impl/SearchService.java          | 43 +++++++++++-
 .../services/impl/SearchServiceTest.java      | 66 ++++++++++++++-----
 2 files changed, 92 insertions(+), 17 deletions(-)

diff --git a/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java b/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java
index d202b0c4c8..34186d37bb 100644
--- a/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java
+++ b/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java
@@ -28,10 +28,10 @@ import lcsb.mapviewer.model.map.model.Model;
 import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
 import lcsb.mapviewer.model.map.reaction.Reaction;
 import lcsb.mapviewer.model.map.reaction.ReactionNode;
-import lcsb.mapviewer.model.map.species.Element;
 import lcsb.mapviewer.model.map.species.Complex;
 import lcsb.mapviewer.model.map.species.Degraded;
 import lcsb.mapviewer.model.map.species.Drug;
+import lcsb.mapviewer.model.map.species.Element;
 import lcsb.mapviewer.model.map.species.Gene;
 import lcsb.mapviewer.model.map.species.Ion;
 import lcsb.mapviewer.model.map.species.Phenotype;
@@ -77,6 +77,12 @@ public class SearchService implements ISearchService {
 	 */
 	public static final String										REACTION_SEARCH_PREFIX		 = "reaction";
 
+	/**
+	 * Prefix used in search by name interface to limit results only to
+	 * {@link Element}.
+	 */
+	private static final String										ELEMENT_SEARCH_PREFIX			 = "element";
+
 	/**
 	 * Default class logger.
 	 */
@@ -240,6 +246,8 @@ public class SearchService implements ISearchService {
 			return getReactionById(model, query.replaceFirst(REACTION_SEARCH_PREFIX, "").toLowerCase());
 		} else if (query.startsWith(SPECIES_SEARCH_PREFIX)) {
 			result.add(fullAliasViewFactory.create(model.getElementByElementId(query.replaceFirst(SPECIES_SEARCH_PREFIX, ""))));
+		} else if (query.startsWith(ELEMENT_SEARCH_PREFIX)) {
+			return getElementById(model, query.replaceFirst(ELEMENT_SEARCH_PREFIX, "").toLowerCase());
 		} else {
 			Set<Element> aliases = model.getElements();
 
@@ -309,14 +317,45 @@ public class SearchService implements ISearchService {
 	private List<IHeavyView> getReactionById(Model model, String reactionId) {
 		Set<Reaction> reactions = model.getReactions();
 		for (Reaction reaction : reactions) {
-
 			if (searchIndexer.getQueryStringForIndex(reaction.getIdReaction().toLowerCase(), new ArrayList<>()).equals(reactionId)) {
 				return reactionToResultList(reaction);
 			}
+			if (Integer.toString(reaction.getId()).equals(reactionId)) {
+				return reactionToResultList(reaction);
+			}
 		}
 		return new ArrayList<>();
 	}
 
+	/**
+	 * Returns list with the element with a given id. If element with such id
+	 * doesn't exist then empty list is returned.
+	 * 
+	 * @param topModel
+	 *          where the search is performed
+	 * @param elementId
+	 *          id of the element
+	 * @return list that contains element with given id (or empty list if such
+	 *         element doesn't exist)
+	 */
+	private List<IHeavyView> getElementById(Model topModel, String elementId) {
+		List<IHeavyView> result = new ArrayList<>();
+
+		Set<Model> models = new HashSet<>();
+		models.add(topModel);
+		models.addAll(topModel.getSubmodels());
+		for (Model model : models) {
+			for (Element element : model.getElements()) {
+				if (searchIndexer.getQueryStringForIndex(element.getElementId().toLowerCase(), new ArrayList<>()).equals(elementId)) {
+					result.add(fullAliasViewFactory.create(element));
+				} else if (Integer.toString(element.getId()).equals(elementId)) {
+					result.add(fullAliasViewFactory.create(element));
+				}
+			}
+		}
+		return result;
+	}
+
 	/**
 	 * Transform {@link Reaction} into set of result entries.
 	 * 
diff --git a/service/src/test/java/lcsb/mapviewer/services/impl/SearchServiceTest.java b/service/src/test/java/lcsb/mapviewer/services/impl/SearchServiceTest.java
index c6d48d10ac..8dc37c8a7c 100644
--- a/service/src/test/java/lcsb/mapviewer/services/impl/SearchServiceTest.java
+++ b/service/src/test/java/lcsb/mapviewer/services/impl/SearchServiceTest.java
@@ -31,6 +31,7 @@ import lcsb.mapviewer.model.map.reaction.Reaction;
 import lcsb.mapviewer.model.map.species.Complex;
 import lcsb.mapviewer.model.map.species.Degraded;
 import lcsb.mapviewer.model.map.species.Drug;
+import lcsb.mapviewer.model.map.species.Element;
 import lcsb.mapviewer.model.map.species.Gene;
 import lcsb.mapviewer.model.map.species.GenericProtein;
 import lcsb.mapviewer.model.map.species.Ion;
@@ -50,7 +51,6 @@ import lcsb.mapviewer.services.utils.SearchIndexer;
 public class SearchServiceTest extends ServiceTestFunctions {
 	static Logger					logger										 = Logger.getLogger(SearchServiceTest.class);
 
-	private Model					model											 = null;
 	SearchIndexer					indexer										 = new SearchIndexer();
 
 	Map<Class<?>, String>	speciesSearchPrefix				 = new HashMap<Class<?>, String>();
@@ -75,9 +75,10 @@ public class SearchServiceTest extends ServiceTestFunctions {
 		speciesSearchReversePrefix.put(prefix, clazz);
 	}
 
-	protected void createFullModel() throws Exception {
-		model = getModelForFile("testFiles/searchModel.xml", false);
+	protected Model createFullModel() throws Exception {
+		Model model = getModelForFile("testFiles/searchModel.xml", false);
 		model.setProject(new Project("unknown project"));
+		return model;
 	}
 
 	@After
@@ -87,7 +88,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testSearchByName() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			long count = searchHistoryDao.getCount();
 			SearchElementResult result = searchService.searchByQuery(model, "reaction:re21", 50, null, "127.0.0.1");
 
@@ -109,7 +110,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testSearchByName2() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			SearchElementResult result = searchService.searchByQuery(model, "BAD:BCL-2", 50, null, "127.0.0.1");
 
 			assertNotNull(result);
@@ -126,7 +127,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testSearchById() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_HAS_VERSION, MiriamType.CAS, "123");
 			model.getElements().iterator().next().addMiriamData(md);
 			SearchElementResult global = searchService.searchByQuery(model, "CAS:123", 50, null, "127.0.0.1");
@@ -138,10 +139,25 @@ public class SearchServiceTest extends ServiceTestFunctions {
 		}
 	}
 
+	@Test
+	public void testSearchByElementId() throws Exception {
+		try {
+			Model model = createFullModel();
+			Element element =model.getElements().iterator().next(); 
+			element.setId(907);
+			SearchElementResult global = searchService.searchByQuery(model, "element:907", 50, null, "127.0.0.1");
+			assertNotNull(global);
+			assertEquals(1, global.size());
+		} catch (Exception e) {
+			e.printStackTrace();
+			throw e;
+		}
+	}
+
 	@Test
 	public void testSearchByName4() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			SearchElementResult result = searchService.searchByQuery(model, "BC", 50, true, "127.0.0.1");
 
 			assertNotNull(result);
@@ -166,7 +182,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testQueryName() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			SearchElementResult result = searchService.searchByQuery(model, indexer.getQueryStringForIndex("reaction:re21", new HashSet<>()), 50, null, "127.0.0.1");
 
 			assertNotNull(result);
@@ -185,7 +201,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testQueryReactionId() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			model.getReactions().iterator().next().setIdReaction("R_x1");
 			SearchElementResult result = searchService.searchByQuery(model, indexer.getQueryStringForIndex("reaction:r_x1", new HashSet<>()), 50, null, "127.0.0.1");
 
@@ -199,10 +215,30 @@ public class SearchServiceTest extends ServiceTestFunctions {
 		}
 	}
 
+	@Test
+	public void testQueryReactionId2() throws Exception {
+		try {
+			Model model = createFullModel();
+			Reaction reaction = model.getReactions().iterator().next();
+			reaction.setId(154);
+			reaction.setIdReaction("test-name");
+			SearchElementResult result = searchService.searchByQuery(model, indexer.getQueryStringForIndex("reaction:154", new HashSet<>()), 50, null, "127.0.0.1");
+
+			assertNotNull(result);
+			assertTrue(result.size() > 0);
+			FullReactionView resultReaction = (FullReactionView) result.get(0);
+			logger.debug(resultReaction.getName());
+			assertTrue(resultReaction.getName().contains("test-name"));
+		} catch (Exception e) {
+			e.printStackTrace();
+			throw e;
+		}
+	}
+
 	@Test
 	public void testQueryName2() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			model.getModelData().setId(-13);
 			SearchElementResult result = searchService
 					.searchByQuery(model, indexer.getQueryStringForIndex("BCL-2", speciesSearchReversePrefix.keySet()), 50, null, "127.0.0.1");
@@ -219,7 +255,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testQueryName3() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			SearchElementResult result = searchService.searchByQuery(model, "fdhgjkhdsfsdhfgfhsd", 50, null, "127.0.0.1");
 
 			assertNotNull(result);
@@ -233,7 +269,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testQueryName4() throws Exception {
 		try {
-			createFullModel();
+			Model model = createFullModel();
 			SearchElementResult result = searchService
 					.searchByQuery(model, indexer.getQueryStringForIndex("protein:BCL-2", speciesSearchReversePrefix.keySet()), 50, null, "127.0.0.1");
 			assertNotNull(result);
@@ -249,7 +285,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testSearchClosest() throws Exception {
 		try {
-			model = getModelForFile("testFiles/graph_path_example3.xml", true);
+			Model model = getModelForFile("testFiles/graph_path_example3.xml", true);
 			List<BioEntity> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
 			assertNotNull(elements);
 			assertEquals(5, elements.size());
@@ -278,7 +314,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testSearchClosest2() throws Exception {
 		try {
-			model = new ModelFullIndexed(null);
+			Model model = new ModelFullIndexed(null);
 			GenericProtein protein1 = new GenericProtein("s1");
 			protein1.setWidth(20);
 			protein1.setHeight(20);
@@ -317,7 +353,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
 	@Test
 	public void testSearchClosestWithEmptyModel() throws Exception {
 		try {
-			model = new ModelFullIndexed(null);
+			Model model = new ModelFullIndexed(null);
 			List<BioEntity> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
 			assertNotNull(elements);
 			assertEquals(0, elements.size());
-- 
GitLab