diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectController.java b/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectController.java index b023eb744c9baa65f6459168484019e1f4024e4d..a0df1b6c6087d28a5a616f49463cc73c3120a8a3 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectController.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectController.java @@ -43,7 +43,7 @@ public class ProjectController extends BaseController { 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") Integer count) throws QueryException, SecurityException { + @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'"); @@ -60,4 +60,11 @@ public class ProjectController extends BaseController { return projectController.getClosestElementsByCoordinates(projectId, modelId, token, pointCoordinates, Integer.valueOf(count)); } + @RequestMapping(value = "/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); + } + } \ No newline at end of file diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectRestImpl.java index 4b207934092e19dfd3fc062477ad26577080bac6..69b291615c505c863c35e92f17d8530b33bb8bcb 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectRestImpl.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/project/ProjectRestImpl.java @@ -113,24 +113,29 @@ public class ProjectRestImpl { List<Object> elements = searchService.getClosestElements(submodel, coordinates, count); for (Object object : elements) { - 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()); - } + 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; }; public List<Map<String, Object>> getReactions(String projectId, String id, String columns, String token) throws UserAccessException, SecurityException { @@ -323,4 +328,19 @@ 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; + } + } diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java index b2ab1e506452e627c019e2864634f74c7fdb6cf2..c37e1cc8455d77c9b935210f098f7faf874f5aff 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/comment/CommentRestImplTest.java @@ -32,7 +32,7 @@ public class CommentRestImplTest extends RestTestFunctions { @Test public void testGetForNonExisting() throws Exception { try { - commentRestImpl.getCommentList(token.getId(), "unk", "","",""); + commentRestImpl.getCommentList(token.getId(), "unk", "","","",""); fail("Exception expected"); } catch (QueryException e2) { assertTrue(e2.getMessage().contains("Project with given id doesn't exist")); 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 05986fdeeb56ffe5b5767a7662ca62c61e30508c..f507431aebd46fa5af79a553a636d9630451d870 100644 --- a/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java +++ b/service/src/main/java/lcsb/mapviewer/services/impl/SearchService.java @@ -67,47 +67,47 @@ public class SearchService implements ISearchService { /** * Prefix used in search by name interface to limit results only to species. */ - public static final String SPECIES_SEARCH_PREFIX = "species"; + public static final String SPECIES_SEARCH_PREFIX = "species"; /** * Prefix used in search by name interface to limit results only to reactions. */ - public static final String REACTION_SEARCH_PREFIX = "reaction"; + public static final String REACTION_SEARCH_PREFIX = "reaction"; /** * Default class logger. */ @SuppressWarnings("unused") - private static Logger logger = Logger.getLogger(SearchService.class.getName()); + private static Logger logger = Logger.getLogger(SearchService.class.getName()); /** * Data access object for models. */ @Autowired - private ModelDao modelDao; + private ModelDao modelDao; /** * Service used for managing search history. */ @Autowired - private ISearchHistoryService searchHistoryService; + private ISearchHistoryService searchHistoryService; /** * Factory object used to create {@link FullAliasView} objects. */ @Autowired - private FullAliasViewFactory fullAliasViewFactory; + private FullAliasViewFactory fullAliasViewFactory; /** * Factory object used to create {@link FullReactionView} objects. */ @Autowired - private FullReactionViewFactory fullReactionViewFactory; + private FullReactionViewFactory fullReactionViewFactory; /** * Object used for indexing elements on the map. */ - private SearchIndexer searchIndexer = new SearchIndexer(); + private SearchIndexer searchIndexer = new SearchIndexer(); /** * This object maps class of elements on the map into short string prefixes @@ -775,4 +775,22 @@ public class SearchService implements ISearchService { } } + @Override + public List<Object> searchByQuery(Model model, String query, int limit, Boolean perfectMatch) { + SearchElementResult resultElement = searchByQuery(model, query, limit, perfectMatch, null); + List<Object> result = new ArrayList<>(); + for (IHeavyView view : resultElement.getElements()) { + if (view instanceof FullAliasView) { + FullAliasView elementView = (FullAliasView) view; + Element element = model.getSubmodelById(elementView.getModelId()).getElementByDbId(Integer.valueOf(elementView.getIdObject())); + result.add(element); + } else if (view instanceof FullReactionView) { + FullReactionView reactionView = (FullReactionView) view; + Reaction element = model.getSubmodelById(reactionView.getModelId()).getReactionByDbId(Integer.valueOf(reactionView.getIdObject())); + result.add(element); + } + } + return result; + } + } \ No newline at end of file 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 e58176d8223c635dd37b70cf4443949f775d3262..9b496ebca11e1d3631ca4ca7c8d4149eb5b27973 100644 --- a/service/src/main/java/lcsb/mapviewer/services/interfaces/ISearchService.java +++ b/service/src/main/java/lcsb/mapviewer/services/interfaces/ISearchService.java @@ -282,6 +282,8 @@ public interface ISearchService { */ SearchElementResult searchByQuery(Model model, String query, int limit, Boolean perfectMatch, String ipAddress); + List<Object> searchByQuery(Model model, String query, int limit, Boolean perfectMatch); + /** * Returns the closest elements to the coordinates on the model. *