diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectController.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectController.java index cbde568e64964e2f57e38af345e50d35b169643a..2af04a489c3fd13389c2fa6f2300a9564d4afceb 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectController.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectController.java @@ -41,33 +41,6 @@ public class ProjectController extends BaseController { return projectController.getSuggestedQueryList(projectId, token); } - @RequestMapping(value = "/project/getClosestElementsByCoordinates", method = { RequestMethod.GET, RequestMethod.POST }, - produces = { MediaType.APPLICATION_JSON_VALUE }) - public List<Map<String, Object>> getClosestElementsByCoordinates(@RequestParam(value = "projectId") String projectId, - @RequestParam(value = "modelId") String modelId, @RequestParam(value = "token") String token, @RequestParam(value = "coordinates") String coordinates, - @RequestParam(value = "count", defaultValue = "5") String count) throws QueryException, SecurityException { - String[] tmp = coordinates.split(","); - if (tmp.length != 2) { - throw new QueryException("Coordinates must be in the format: 'xxx.xx,yyy.yy'"); - } - Double x = null; - Double y = null; - try { - x = Double.valueOf(tmp[0]); - y = Double.valueOf(tmp[1]); - } catch (NumberFormatException e) { - throw new QueryException("Coordinates must be in the format: 'xxx.xx,yyy.yy'", e); - } - Point2D pointCoordinates = new Point2D.Double(x, y); - return projectController.getClosestElementsByCoordinates(projectId, modelId, token, pointCoordinates, Integer.valueOf(count)); - } - - @RequestMapping(value = "/project/getElementsByQuery", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) - public List<Map<String, Object>> getElementsByQuery(@RequestParam(value = "projectId") String projectId, @RequestParam(value = "token") String token, - @RequestParam(value = "query") String query, @RequestParam(value = "maxElements", defaultValue = "100") Integer maxElements, - @RequestParam(value = "perfectMatch", defaultValue = "false") String perfectMatch) throws QueryException, SecurityException { - return projectController.getElementsByQuery(projectId, token, query, maxElements, perfectMatch); - } @RequestMapping(value = "/project/getProjectSource", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<byte[]> getProjectSource(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId) diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java index 2be02b7293bcba2a83a393a082b3bf1950aa68a2..a92c21392ee8e3fd789c8d442f84619fa25ae0a2 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java @@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestParam; +import lcsb.mapviewer.api.BaseRestImpl; import lcsb.mapviewer.api.QueryException; import lcsb.mapviewer.api.projects.models.publications.PublicationsRestImpl; import lcsb.mapviewer.commands.ClearColorModelCommand; @@ -48,7 +49,6 @@ import lcsb.mapviewer.model.map.layout.ColorSchema; import lcsb.mapviewer.model.map.layout.InvalidColorSchemaException; import lcsb.mapviewer.model.map.layout.Layout; import lcsb.mapviewer.model.map.model.Model; -import lcsb.mapviewer.model.map.model.ModelData; import lcsb.mapviewer.model.map.reaction.Reaction; import lcsb.mapviewer.model.map.species.Element; import lcsb.mapviewer.model.user.User; @@ -68,7 +68,7 @@ import lcsb.mapviewer.services.view.AuthenticationToken; import lcsb.mapviewer.services.view.OverviewImageViewFactory; @Transactional(value = "txManager") -public class ProjectRestImpl { +public class ProjectRestImpl extends BaseRestImpl { /** * Constant defining size of the array returned by @@ -176,41 +176,6 @@ public class ProjectRestImpl { this.userService = userService; } - public List<Map<String, Object>> getClosestElementsByCoordinates(String projectId, String modelId, String token, Point2D coordinates, Integer count) - throws UserAccessException, SecurityException { - List<Map<String, Object>> resultMap = new ArrayList<>(); - - Model model = modelService.getLastModelByProjectId(projectId, userService.getToken(token)); - - Model submodel = model.getSubmodelById(modelId); - - List<Object> elements = searchService.getClosestElements(submodel, coordinates, count); - for (Object object : elements) { - Map<String, Object> result = createMinifiedSearchResult(object); - resultMap.add(result); - } - return resultMap; - } - - private Map<String, Object> createMinifiedSearchResult(Object object) { - Map<String, Object> result = new HashMap<>(); - if (object instanceof Element) { - result.put("type", ElementIdentifierType.ALIAS); - Element element = (Element) object; - result.put("id", element.getId()); - result.put("modelId", element.getModel().getId()); - } else if (object instanceof Reaction) { - result.put("type", ElementIdentifierType.REACTION); - Reaction element = (Reaction) object; - result.put("id", element.getId()); - result.put("modelId", element.getModel().getId()); - - } else { - throw new InvalidStateException("Unknown type of result: " + object.getClass()); - } - return result; - }; - /** * @return the modelService * @see #modelService @@ -245,22 +210,6 @@ public class ProjectRestImpl { this.searchService = searchService; } - public List<Map<String, Object>> getElementsByQuery(String projectId, String token, String query, Integer maxElements, String perfectMatch) - throws SecurityException { - List<Map<String, Object>> resultMap = new ArrayList<>(); - - Model model = modelService.getLastModelByProjectId(projectId, userService.getToken(token)); - - Integer limit = Integer.valueOf(maxElements); - boolean match = perfectMatch.equals("true"); - List<Object> elements = searchService.searchByQuery(model, query, limit, match); - for (Object object : elements) { - Map<String, Object> result = createMinifiedSearchResult(object); - resultMap.add(result); - } - return resultMap; - } - public String[] getSuggestedQueryList(String projectId, String token) throws SecurityException { Model model = modelService.getLastModelByProjectId(projectId, userService.getToken(token)); return searchService.getSuggestedQueryList(model); diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesController.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesController.java new file mode 100644 index 0000000000000000000000000000000000000000..9bef963ef950cd2cdd027c5c9614056510419947 --- /dev/null +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesController.java @@ -0,0 +1,66 @@ +package lcsb.mapviewer.api.projects.models.bioEntities; + +import java.awt.geom.Point2D; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import lcsb.mapviewer.api.BaseController; +import lcsb.mapviewer.api.QueryException; +import lcsb.mapviewer.services.SecurityException; + +@RestController +public class BioEntitiesController extends BaseController { + @Autowired + private BioEntitiesRestImpl projectController; + + @RequestMapping(value = "/projects/{projectId}/models/{modelId}/bioEntities:search", method = { RequestMethod.GET }, + produces = { MediaType.APPLICATION_JSON_VALUE }) + public List<Map<String, Object>> getClosestElementsByCoordinates(// + @PathVariable(value = "projectId") String projectId, // + @PathVariable(value = "modelId") String modelId, // + @RequestParam(value = "token") String token, // + @RequestParam(value = "coordinates", defaultValue = "") String coordinates, // + @RequestParam(value = "query", defaultValue = "") String query, // + @RequestParam(value = "count", defaultValue = "") String count, // + @RequestParam(value = "perfectMatch", defaultValue = "false") String perfectMatch// + ) throws QueryException, SecurityException { + if (!coordinates.trim().isEmpty()) { + if (modelId.equals("*")) { + throw new QueryException("modelId must be defined when searching by coordinates"); + } + if (count.trim().isEmpty()) { + count = "5"; + } + String[] tmp = coordinates.split(","); + if (tmp.length != 2) { + throw new QueryException("Coordinates must be in the format: 'xxx.xx,yyy.yy'"); + } + Double x = null; + Double y = null; + try { + x = Double.valueOf(tmp[0]); + y = Double.valueOf(tmp[1]); + } catch (NumberFormatException e) { + throw new QueryException("Coordinates must be in the format: 'xxx.xx,yyy.yy'", e); + } + Point2D pointCoordinates = new Point2D.Double(x, y); + return projectController.getClosestElementsByCoordinates(projectId, modelId, token, pointCoordinates, Integer.valueOf(count), perfectMatch); + } else if (!query.trim().isEmpty()) { + if (count.trim().isEmpty()) { + count = "100"; + } + return projectController.getElementsByQuery(projectId, token, modelId, query, Integer.valueOf(count), perfectMatch); + } else { + throw new QueryException("On of the parameters 'coordinates', 'query' is required"); + } + } + +} \ No newline at end of file diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesRestImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..12902400c5b291eda58cd84b29e347267aed7106 --- /dev/null +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesRestImpl.java @@ -0,0 +1,121 @@ +package lcsb.mapviewer.api.projects.models.bioEntities; + +import java.awt.geom.Point2D; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +import lcsb.mapviewer.api.BaseRestImpl; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.services.SecurityException; +import lcsb.mapviewer.services.UserAccessException; +import lcsb.mapviewer.services.interfaces.IModelService; +import lcsb.mapviewer.services.interfaces.ISearchService; +import lcsb.mapviewer.services.interfaces.IUserService; +import lcsb.mapviewer.services.view.AnnotationViewFactory; + +@Transactional(value = "txManager") +public class BioEntitiesRestImpl extends BaseRestImpl { + + Logger logger = Logger.getLogger(BioEntitiesRestImpl.class); + + @Autowired + private IUserService userService; + + @Autowired + private IModelService modelService; + + @Autowired + private ISearchService searchService; + + @Autowired + AnnotationViewFactory annotationViewFactory; + + /** + * @return the userService + * @see #userService + */ + public IUserService getUserService() { + return userService; + } + + /** + * @param userService + * the userService to set + * @see #userService + */ + public void setUserService(IUserService userService) { + this.userService = userService; + } + + public List<Map<String, Object>> getClosestElementsByCoordinates(String projectId, String modelId, String token, Point2D coordinates, Integer count, String perfectMatch) + throws UserAccessException, SecurityException { + List<Map<String, Object>> resultMap = new ArrayList<>(); + + Model model = modelService.getLastModelByProjectId(projectId, userService.getToken(token)); + + Model submodel = model.getSubmodelById(modelId); + + List<Object> elements = searchService.getClosestElements(submodel, coordinates, count, perfectMatch.equalsIgnoreCase("true")); + for (Object object : elements) { + Map<String, Object> result = createMinifiedSearchResult(object); + resultMap.add(result); + } + return resultMap; + } + + /** + * @return the modelService + * @see #modelService + */ + public IModelService getModelService() { + return modelService; + } + + /** + * @param modelService + * the modelService to set + * @see #modelService + */ + public void setModelService(IModelService modelService) { + this.modelService = modelService; + } + + /** + * @return the searchService + * @see #searchService + */ + public ISearchService getSearchService() { + return searchService; + } + + /** + * @param searchService + * the searchService to set + * @see #searchService + */ + public void setSearchService(ISearchService searchService) { + this.searchService = searchService; + } + + public List<Map<String, Object>> getElementsByQuery(String projectId, String token, String modelId, String query, Integer maxElements, String perfectMatch) + throws SecurityException { + List<Map<String, Object>> resultMap = new ArrayList<>(); + + Model model = modelService.getLastModelByProjectId(projectId, userService.getToken(token)); + + Integer limit = Integer.valueOf(maxElements); + boolean match = perfectMatch.equals("true"); + List<Object> elements = searchService.searchByQuery(model, query, limit, match); + for (Object object : elements) { + Map<String, Object> result = createMinifiedSearchResult(object); + resultMap.add(result); + } + return resultMap; + } + +} diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsController.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsController.java index 342e044466a3a04f092dbe0ee2805436f4ab22d2..0d2175c1d6d1a1e54d3b428cbb930ed8e5a2e8ba 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsController.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsController.java @@ -23,13 +23,13 @@ public class ReactionsController extends BaseController { produces = { MediaType.APPLICATION_JSON_VALUE }) public List<Map<String, Object>> getReactions(// @PathVariable(value = "projectId") String projectId, // - @PathVariable(value = "modelId") String modelId, + @PathVariable(value = "modelId") String modelId, // @RequestParam(value = "id", defaultValue = "") String id, // @RequestParam(value = "columns", defaultValue = "") String columns, // @RequestParam(value = "token") String token, // @RequestParam(value = "participantId", defaultValue = "") String participantId// ) throws SecurityException { - return reactionController.getReactions(projectId, id, columns, modelId, token, participantId); + return reactionController.getReactions(projectId, id, columns, modelId, token, participantId); } } \ No newline at end of file diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsRestImpl.java index 64bd74ad1fc524c919ba60c1d8e3885091ae7db6..c1305c14372b227c0cbbe080b7237a8ffc2152d3 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsRestImpl.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/reactions/ReactionsRestImpl.java @@ -13,6 +13,7 @@ import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import lcsb.mapviewer.api.BaseRestImpl; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.model.map.reaction.Modifier; import lcsb.mapviewer.model.map.reaction.Product; @@ -27,27 +28,18 @@ import lcsb.mapviewer.services.search.data.LightReactionView; import lcsb.mapviewer.services.view.AnnotationViewFactory; @Transactional(value = "txManager") -public class ReactionsRestImpl { +public class ReactionsRestImpl extends BaseRestImpl { - /** - * Constant defining size of the array returned by - * {@link PathIterator#currentSegment(double[])} method. More nformation can - * be found <a href= - * "http://docs.oracle.com/javase/7/docs/api/java/awt/geom/PathIterator.html#currentSegment(double[])" - * >here</a> - */ - private static final int PATH_ITERATOR_SEGMENT_SIZE = 6; - - Logger logger = Logger.getLogger(ReactionsRestImpl.class); + Logger logger = Logger.getLogger(ReactionsRestImpl.class); @Autowired - private IUserService userService; + private IUserService userService; @Autowired - private IModelService modelService; + private IModelService modelService; @Autowired - AnnotationViewFactory annotationViewFactory; + AnnotationViewFactory annotationViewFactory; /** * @return the userService @@ -85,9 +77,8 @@ public class ReactionsRestImpl { List<Map<String, Object>> result = new ArrayList<>(); - List<Model> models = new ArrayList<>(); - models.addAll(model.getSubmodels()); - models.add(model); + List<Model> models = getModels(projectId, modelId, token); + for (Model model2 : models) { for (Reaction reaction : model2.getReactions()) { if (ids.size() == 0 || ids.contains(reaction.getId())) { diff --git a/rest-api/src/main/resources/applicationContext-rest.xml b/rest-api/src/main/resources/applicationContext-rest.xml index 11c5a2f10eafade53ce584862507b0263650d549..349fb0bab5da3cf867212e48622466e4daf6ef4d 100644 --- a/rest-api/src/main/resources/applicationContext-rest.xml +++ b/rest-api/src/main/resources/applicationContext-rest.xml @@ -19,6 +19,7 @@ <bean id="OverlayRestImpl" class="lcsb.mapviewer.api.overlay.OverlayRestImpl"/> <bean id="ProjectRestImpl" class="lcsb.mapviewer.api.projects.ProjectRestImpl"/> + <bean id="BioEntitiesRestImpl" class="lcsb.mapviewer.api.projects.models.bioEntities.BioEntitiesRestImpl"/> <bean id="ElementsRestImpl" class="lcsb.mapviewer.api.projects.models.bioEntities.elements.ElementsRestImpl"/> <bean id="ReactionsRestImpl" class="lcsb.mapviewer.api.projects.models.bioEntities.reactions.ReactionsRestImpl"/> <bean id="PublicationsRestImpl" class="lcsb.mapviewer.api.projects.models.publications.PublicationsRestImpl"/> diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/ProjectRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/ProjectRestImplTest.java index baeb04af01f2a0166c827017080ba0dabac81943..4c04050f6e09b4517a9aa9422143177076cd78d9 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/projects/ProjectRestImplTest.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/ProjectRestImplTest.java @@ -1,16 +1,10 @@ package lcsb.mapviewer.api.projects; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; -import java.awt.geom.Point2D; -import java.util.List; -import java.util.Map; - import org.apache.log4j.Logger; import org.junit.After; import org.junit.AfterClass; @@ -23,8 +17,6 @@ import com.google.gson.Gson; import lcsb.mapviewer.api.QueryException; import lcsb.mapviewer.api.RestTestFunctions; -import lcsb.mapviewer.api.projects.ProjectMetaData; -import lcsb.mapviewer.api.projects.ProjectRestImpl; import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.converter.graphics.PdfImageGenerator; import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser; @@ -52,19 +44,6 @@ public class ProjectRestImplTest extends RestTestFunctions { public void tearDown() throws Exception { } - @Test - public void testGetClosestElementsByCoordinates() throws Exception { - try { - ProjectRestImpl projectRest = createMockProjectRest("testFiles/model/sample.xml"); - int count = 3; - List<Map<String, Object>> result = projectRest.getClosestElementsByCoordinates("sample", "0", token.getId(), new Point2D.Double(1, 2), count); - assertEquals(count, result.size()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - @Test public void testGetModelAsImage() throws Exception { try { @@ -160,5 +139,4 @@ public class ProjectRestImplTest extends RestTestFunctions { return _projectRestImpl; } - } diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/AllModelsTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/AllModelsTests.java index f80d0faa3754a96e59616e236fa641e91525acfa..f89ba7fa4d53d265c8f1b116b26d7832ed327c2c 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/AllModelsTests.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/AllModelsTests.java @@ -4,12 +4,13 @@ import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; +import lcsb.mapviewer.api.projects.models.bioEntities.AllBioeEntitiesTests; import lcsb.mapviewer.api.projects.models.bioEntities.elements.AllElementsTests; import lcsb.mapviewer.api.projects.models.publications.AllPublicationsTests; @RunWith(Suite.class) @SuiteClasses({ // - AllElementsTests.class, // + AllBioeEntitiesTests.class, // AllPublicationsTests.class// }) public class AllModelsTests { diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/AllBioeEntitiesTests.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/AllBioeEntitiesTests.java new file mode 100644 index 0000000000000000000000000000000000000000..48a870c00f8daae830fd7f788730562cda51e4e0 --- /dev/null +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/AllBioeEntitiesTests.java @@ -0,0 +1,16 @@ +package lcsb.mapviewer.api.projects.models.bioEntities; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +import lcsb.mapviewer.api.projects.models.bioEntities.elements.AllElementsTests; + +@RunWith(Suite.class) +@SuiteClasses({ // + AllElementsTests.class, // + BioEntitiesControllerTest.class // +}) +public class AllBioeEntitiesTests { + +} diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesControllerTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3121878299dc8e7791174960cb12677a40f09ddc --- /dev/null +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/BioEntitiesControllerTest.java @@ -0,0 +1,62 @@ +package lcsb.mapviewer.api.projects.models.bioEntities; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; + +import java.awt.geom.Point2D; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; + +import lcsb.mapviewer.api.RestTestFunctions; +import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.services.interfaces.IModelService; + +public class BioEntitiesControllerTest extends RestTestFunctions { + Logger logger = Logger.getLogger(BioEntitiesControllerTest.class); + + @Autowired + BioEntitiesRestImpl _bioEntitiesRestImpl; + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClosestElementsByCoordinates() throws Exception { + try { + BioEntitiesRestImpl projectRest = createMockProjectRest("testFiles/model/sample.xml"); + int count = 3; + List<Map<String, Object>> result = projectRest.getClosestElementsByCoordinates("sample", "0", token.getId(), new Point2D.Double(1, 2), count, "false"); + assertEquals(count, result.size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + private BioEntitiesRestImpl createMockProjectRest(String string) throws Exception { + Model model = super.getModelForFile(string, true); + IModelService mockModelService = Mockito.mock(IModelService.class); + Mockito.when(mockModelService.getLastModelByProjectId(anyString(), any())).thenReturn(model); + _bioEntitiesRestImpl.setModelService(mockModelService); + return _bioEntitiesRestImpl; + } + +} 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 5020536b56da2bd99e24463d210effedf3f9121d..947a3d32991a6e0c51c83535987c72923b18b4da 100644 --- a/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java +++ b/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java @@ -527,7 +527,7 @@ public class SearchService implements ISearchService { } @Override - public List<Object> getClosestElements(Model model, Point2D point, int numberOfElements) { + public List<Object> getClosestElements(Model model, Point2D point, int numberOfElements, boolean perfectHit) { List<Object> result = new ArrayList<>(); // probably this could be improved algorithmitically, right now all objects @@ -545,7 +545,11 @@ public class SearchService implements ISearchService { Collections.sort(tmpList); int size = Math.min(tmpList.size(), numberOfElements); for (int i = 0; i < size; i++) { - result.add(tmpList.get(i).getReference()); + if (!perfectHit) { + result.add(tmpList.get(i).getReference()); + } else if (tmpList.get(i).getDistance() < Configuration.EPSILON) { + result.add(tmpList.get(i).getReference()); + } } return result; } diff --git a/service/src/main/java/lcsb/mapviewer/services/interfaces/ISearchService.java b/service/src/main/java/lcsb/mapviewer/services/interfaces/ISearchService.java index 3801640516d876b6b3d94cf72a0f9a3566b7d6b0..c7f51a65f43b178ba00c4eed1258cdaf3938bc4f 100644 --- a/service/src/main/java/lcsb/mapviewer/services/interfaces/ISearchService.java +++ b/service/src/main/java/lcsb/mapviewer/services/interfaces/ISearchService.java @@ -295,7 +295,7 @@ public interface ISearchService { * how many closest elements should be returned * @return list of the closest elements */ - List<Object> getClosestElements(Model model, Point2D point, int numberOfElements); + List<Object> getClosestElements(Model model, Point2D point, int numberOfElements, boolean perfectHit); /** * Returns list of autocomplete strings for the partial query. 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 5e7afaf6357abab75d2343da1aa93c2734514194..571ef6105b32936e8d38fdcabce39d4d96cfe470 100644 --- a/service/src/test/java/lcsb/mapviewer/services/impl/SearchServiceTest.java +++ b/service/src/test/java/lcsb/mapviewer/services/impl/SearchServiceTest.java @@ -231,7 +231,7 @@ public class SearchServiceTest extends ServiceTestFunctions { public void testSearchClosest() throws Exception { try { model = getModelForFile("testFiles/graph_path_example3.xml", true); - List<Object> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5); + List<Object> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false); assertNotNull(elements); assertEquals(5, elements.size()); assertTrue(elements.get(0) instanceof Species); @@ -260,7 +260,7 @@ public class SearchServiceTest extends ServiceTestFunctions { public void testSearchClosestWithEmptyModel() throws Exception { try { model = new ModelFullIndexed(null); - List<Object> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5); + List<Object> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false); assertNotNull(elements); assertEquals(0, elements.size());