Skip to content
Snippets Groups Projects
Commit e6e46455 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

new api call for list of publications

parent 77683f5c
No related branches found
No related tags found
1 merge request!11Resolve "Rest API should follow google guidlines"
This commit is part of merge request !11. Comments created here will be created in the context of that merge request.
Showing
with 378 additions and 147 deletions
......@@ -69,13 +69,6 @@ public class ProjectController extends BaseController {
return projectController.getElementsByQuery(projectId, token, query, maxElements, perfectMatch);
}
@RequestMapping(value = "/project/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 = "/project/getProjectSource", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<byte[]> getProjectSource(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId)
throws SecurityException, QueryException {
......
......@@ -14,8 +14,6 @@ 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.io.IOUtils;
import org.apache.log4j.Logger;
......@@ -23,8 +21,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.api.projects.models.publications.PublicationsRestImpl;
import lcsb.mapviewer.commands.ClearColorModelCommand;
import lcsb.mapviewer.commands.ColorExtractor;
import lcsb.mapviewer.commands.ColorModelCommand;
......@@ -42,10 +40,7 @@ import lcsb.mapviewer.converter.model.sbgnml.SbgnmlXmlConverter;
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.InconsistentModelException;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.OverviewImage;
import lcsb.mapviewer.model.map.OverviewImageLink;
import lcsb.mapviewer.model.map.OverviewLink;
......@@ -54,7 +49,6 @@ 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.model.ModelSubmodelConnection;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.user.User;
......@@ -97,13 +91,13 @@ public class ProjectRestImpl {
private IModelService modelService;
@Autowired
private ISearchService searchService;
private PublicationsRestImpl publicationsRestImpl;
@Autowired
AnnotationViewFactory annotationViewFactory;
private ISearchService searchService;
@Autowired
private PubmedParser pubmedParser;
AnnotationViewFactory annotationViewFactory;
@Autowired
private ILayoutService layoutService;
......@@ -127,7 +121,7 @@ public class ProjectRestImpl {
private ProjectMetaData createData(Project project, AuthenticationToken token) {
ProjectMetaData result = new ProjectMetaData();
ModelData model = modelService.getLastModelByProjectId(project.getProjectId(), token).getModelData();
Model model = modelService.getLastModelByProjectId(project.getProjectId(), token);
result.setName(project.getName());
result.setProjectId(project.getProjectId());
......@@ -155,50 +149,16 @@ public class ProjectRestImpl {
}
result.setMap(new ModelMetaData(model));
result.setPublicationCount(getPublications(model).size());
List<Model> models = new ArrayList<>();
models.add(model);
models.addAll(model.getSubmodels());
result.setPublicationCount(publicationsRestImpl.getPublications(models).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)) {
List<AnnotatedObject> list = publications.get(md);
if (list == null) {
list = new ArrayList<>();
publications.put(md, list);
}
list.add(reaction);
}
}
}
}
return publications;
}
/**
* @return the userService
* @see #userService
......@@ -335,55 +295,6 @@ 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()) {
elements.add(createMinifiedSearchResult(object));
}
Map<String, Object> row = new HashMap<>();
row.put("elements", elements);
if (entry.getKey().getDataType().equals(MiriamType.PUBMED)) {
try {
row.put("publication", pubmedParser.getPubmedArticleById(Integer.valueOf(entry.getKey().getResource())));
} catch (Exception e) {
logger.error(e, e);
row.put("publication", annotationViewFactory.create(entry.getKey()));
}
} else {
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;
}
public FileEntry getModelAsImage(String token, String projectId, String modelId, String handlerClass, String backgroundOverlayId, String overlayIds,
String zoomLevel, String polygonString) throws SecurityException, QueryException, IOException, InvalidColorSchemaException, CommandExecutionException {
AuthenticationToken authenticationToken = userService.getToken(token);
......@@ -557,21 +468,4 @@ public class ProjectRestImpl {
}
/**
* @return the pubmedParser
* @see #pubmedParser
*/
public PubmedParser getPubmedParser() {
return pubmedParser;
}
/**
* @param pubmedParser
* the pubmedParser to set
* @see #pubmedParser
*/
public void setPubmedParser(PubmedParser pubmedParser) {
this.pubmedParser = pubmedParser;
}
}
package lcsb.mapviewer.api.projects.models.publications;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.commands.CommandExecutionException;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.InvalidColorSchemaException;
import lcsb.mapviewer.services.SecurityException;
@RestController
public class PublicationsController extends BaseController {
@Autowired
private PublicationsRestImpl projectController;
@RequestMapping(value = "/projects/{projectId}/models/{modelId}/publications/", method = { RequestMethod.GET },
produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> getPublications(//
@PathVariable(value = "projectId") String projectId, //
@PathVariable(value = "modelId") String modelId, //
@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, modelId, token, start, length);
}
}
\ No newline at end of file
package lcsb.mapviewer.api.projects.models.publications;
import java.util.ArrayList;
import java.util.HashMap;
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.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.services.SecurityException;
import lcsb.mapviewer.services.interfaces.ILayoutService;
import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.ISearchService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.search.data.ElementIdentifier.ElementIdentifierType;
import lcsb.mapviewer.services.view.AnnotationViewFactory;
import lcsb.mapviewer.services.view.AuthenticationToken;
import lcsb.mapviewer.services.view.OverviewImageViewFactory;
@Transactional(value = "txManager")
public class PublicationsRestImpl extends BaseRestImpl{
Logger logger = Logger.getLogger(PublicationsRestImpl.class);
@Autowired
private IUserService userService;
@Autowired
private IModelService modelService;
@Autowired
private ISearchService searchService;
@Autowired
AnnotationViewFactory annotationViewFactory;
@Autowired
private PubmedParser pubmedParser;
@Autowired
private OverviewImageViewFactory factory;
public SortedMap<MiriamData, List<AnnotatedObject>> getPublications(List<Model> models) {
SortedMap<MiriamData, List<AnnotatedObject>> publications = new TreeMap<>();
for (Model 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)) {
List<AnnotatedObject> list = publications.get(md);
if (list == null) {
list = new ArrayList<>();
publications.put(md, list);
}
list.add(reaction);
}
}
}
}
return publications;
}
/**
* @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;
}
/**
* @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;
}
/**
* @return the factory
* @see #factory
*/
public OverviewImageViewFactory getFactory() {
return factory;
}
/**
* @param factory
* the factory to set
* @see #factory
*/
public void setFactory(OverviewImageViewFactory factory) {
this.factory = factory;
}
public Map<String, Object> getPublications(String projectId, String modelId, String token, String startString, Integer length) throws SecurityException, QueryException {
List<Model> models = getModels(projectId, modelId, token);
Integer start = Math.max(0, Integer.valueOf(startString));
List<Map<String, Object>> resultList = new ArrayList<>();
SortedMap<MiriamData, List<AnnotatedObject>> publications = getPublications(models);
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()) {
elements.add(createMinifiedSearchResult(object));
}
Map<String, Object> row = new HashMap<>();
row.put("elements", elements);
if (entry.getKey().getDataType().equals(MiriamType.PUBMED)) {
try {
row.put("publication", pubmedParser.getPubmedArticleById(Integer.valueOf(entry.getKey().getResource())));
} catch (Exception e) {
logger.error(e, e);
row.put("publication", annotationViewFactory.create(entry.getKey()));
}
} else {
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;
}
/**
* @return the pubmedParser
* @see #pubmedParser
*/
public PubmedParser getPubmedParser() {
return pubmedParser;
}
/**
* @param pubmedParser
* the pubmedParser to set
* @see #pubmedParser
*/
public void setPubmedParser(PubmedParser pubmedParser) {
this.pubmedParser = pubmedParser;
}
}
......@@ -21,6 +21,7 @@
<bean id="ProjectRestImpl" class="lcsb.mapviewer.api.projects.ProjectRestImpl"/>
<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"/>
<bean id="ReferenceGenomeRestImpl" class="lcsb.mapviewer.api.genomics.ReferenceGenomeRestImpl"/>
<bean id="UserRestImpl" class="lcsb.mapviewer.api.controller.UserRestImpl"/>
......
......@@ -4,10 +4,10 @@ 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;
import lcsb.mapviewer.api.projects.models.AllModelsTests;
@RunWith(Suite.class)
@SuiteClasses({ AllElementsTests.class, //
@SuiteClasses({ AllModelsTests.class, //
ModelMetaDataTest.class, //
ProjectRestImplTest.class })
public class AllProjectTests {
......
......@@ -160,28 +160,5 @@ 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;
}
}
}
package lcsb.mapviewer.api.projects.models;
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;
import lcsb.mapviewer.api.projects.models.publications.AllPublicationsTests;
@RunWith(Suite.class)
@SuiteClasses({ //
AllElementsTests.class, //
AllPublicationsTests.class//
})
public class AllModelsTests {
}
package lcsb.mapviewer.api.projects.models.publications;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ PublicationsRestImplTest.class })
public class AllPublicationsTests {
}
package lcsb.mapviewer.api.projects.models.publications;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
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 PublicationsRestImplTest extends RestTestFunctions {
Logger logger = Logger.getLogger(PublicationsRestImplTest.class);
@Autowired
PublicationsRestImpl _projectRestImpl;
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPublications() throws Exception {
try {
PublicationsRestImpl 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;
}
}
private PublicationsRestImpl 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);
_projectRestImpl.setModelService(mockModelService);
return _projectRestImpl;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment