From a79901365852e54bed6fbea4c0cb590dc434fd5d Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Tue, 11 Apr 2017 10:26:35 +0200 Subject: [PATCH] rest api for getting publication list --- .../api/project/ProjectController.java | 6 ++ .../api/project/ProjectRestImpl.java | 102 ++++++++++++++---- .../api/project/ProjectRestImplTest.java | 25 +++++ 3 files changed, 112 insertions(+), 21 deletions(-) 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 f27d48befc..74ddd74776 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 @@ -75,6 +75,12 @@ public class ProjectController extends BaseController { return projectController.getElementsByQuery(projectId, token, query, maxElements, perfectMatch); } + @RequestMapping(value = "/getPublications", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) + public Map<String, Object> getPublications(@RequestParam(value = "projectId") String projectId, @RequestParam(value = "token") String token, + @RequestParam(value = "start", defaultValue = "0") String start, @RequestParam(value = "length", defaultValue = "10") Integer length) throws QueryException, SecurityException { + return projectController.getPublications(projectId, token, start, length); + } + @RequestMapping(value = "/getProjectSource", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<byte[]> getOverlaySource(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId) throws SecurityException, QueryException { 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 506d37b9b3..c5bc026772 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 @@ -8,6 +8,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; @@ -20,6 +22,7 @@ import lcsb.mapviewer.common.exception.InvalidStateException; import lcsb.mapviewer.model.Project; import lcsb.mapviewer.model.cache.FileEntry; import lcsb.mapviewer.model.cache.UploadedFileEntry; +import lcsb.mapviewer.model.map.AnnotatedObject; import lcsb.mapviewer.model.map.MiriamData; import lcsb.mapviewer.model.map.MiriamType; import lcsb.mapviewer.model.map.OverviewImage; @@ -114,35 +117,48 @@ public class ProjectRestImpl { } result.setMap(new ModelMetaData(model)); - Set<MiriamData> publications = new HashSet<>(); - List<ModelData> models = new ArrayList<>(); - for (ModelSubmodelConnection connection : model.getSubmodels()) { - models.add(connection.getSubmodel()); - } - models.add(model); - for (ModelData modelData : models) { - for (Element element : modelData.getElements()) { - for (MiriamData md : element.getMiriamData()) { - if (md.getDataType().equals(MiriamType.PUBMED)) { - publications.add(md); + result.setPublicationCount(getPublications(model).size()); + + } + + return result; + } + + private SortedMap<MiriamData, List<AnnotatedObject>> getPublications(ModelData model) { + SortedMap<MiriamData, List<AnnotatedObject>> publications = new TreeMap<>(); + List<ModelData> models = new ArrayList<>(); + for (ModelSubmodelConnection connection : model.getSubmodels()) { + models.add(connection.getSubmodel()); + } + models.add(model); + for (ModelData modelData : models) { + for (Element element : modelData.getElements()) { + for (MiriamData md : element.getMiriamData()) { + if (md.getDataType().equals(MiriamType.PUBMED)) { + List<AnnotatedObject> list = publications.get(md); + if (list == null) { + list = new ArrayList<>(); + publications.put(md, list); } + list.add(element); } } - for (Reaction reaction : modelData.getReactions()) { - for (MiriamData md : reaction.getMiriamData()) { - if (md.getDataType().equals(MiriamType.PUBMED)) { - publications.add(md); + } + for (Reaction reaction : modelData.getReactions()) { + for (MiriamData md : reaction.getMiriamData()) { + if (md.getDataType().equals(MiriamType.PUBMED)) { + List<AnnotatedObject> list = publications.get(md); + if (list == null) { + list = new ArrayList<>(); + publications.put(md, list); } + list.add(reaction); } - } - } - - result.setPublicationCount(publications.size()); + } } - - return result; + return publications; } /** @@ -463,4 +479,48 @@ public class ProjectRestImpl { return entry; } + public Map<String, Object> getPublications(String projectId, String token, String startString, Integer length) throws SecurityException, QueryException { + AuthenticationToken authenticationToken = userService.getToken(token); + Model model = modelService.getLastModelByProjectId(projectId, authenticationToken); + if (model == null) { + throw new QueryException("Project with given id doesn't exist"); + } + + Integer start = Math.max(0, Integer.valueOf(startString)); + List<Map<String, Object>> resultList = new ArrayList<>(); + + SortedMap<MiriamData, List<AnnotatedObject>> publications = getPublications(model.getModelData()); + int index = 0; + Set<String> columns = new HashSet<>(); + columns.add("id"); + columns.add("type"); + columns.add("modelid"); + for (Map.Entry<MiriamData, List<AnnotatedObject>> entry : publications.entrySet()) { + if (index >= start && index < start + length) { + List<Object> elements = new ArrayList<>(); + for (AnnotatedObject object: entry.getValue()) { + if (object instanceof Element) { + elements.add(preparedElement((Element) object, columns)); + } else if (object instanceof Reaction) { + elements.add(preparedReaction((Reaction) object, columns)); + } + } + + Map<String, Object> row = new HashMap<>(); + row.put("elements", elements); + row.put("publication", annotationViewFactory.create(entry.getKey())); + resultList.add(row); + } + index++; + + } + + Map<String, Object> result = new HashMap<>(); + result.put("data", resultList); + result.put("totalSize", publications.size()); + result.put("start", start); + result.put("length", resultList.size()); + return result; + } + } diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java index 6155d0b7a0..c0de5c7b1c 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/project/ProjectRestImplTest.java @@ -112,4 +112,29 @@ public class ProjectRestImplTest extends RestTestFunctions { return _projectRestImpl; } + @Test + public void testPublications() throws Exception { + try { + ProjectRestImpl projectRest = createMockProjectRest("testFiles/model/sample.xml"); + Map<String, Object> result= projectRest.getPublications("sample", token.getId(),"0", 20); + assertEquals(1, result.get("totalSize")); + assertEquals(0, result.get("start")); + assertEquals(1, ((List<?>)result.get("data")).size()); + + result= projectRest.getPublications("sample", token.getId(),"1", 20); + assertEquals(1, result.get("totalSize")); + assertEquals(1, result.get("start")); + assertEquals(0, ((List<?>)result.get("data")).size()); + + result= projectRest.getPublications("sample", token.getId(),"0", 00); + assertEquals(1, result.get("totalSize")); + assertEquals(0, result.get("start")); + assertEquals(0, ((List<?>)result.get("data")).size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + } -- GitLab