Commit 477b10db authored by Piotr Gawron's avatar Piotr Gawron
Browse files

search by target moved to controller

parent fcc85ab2
......@@ -24,9 +24,12 @@ import lcsb.mapviewer.annotation.services.dapi.ChemicalSearchException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.modelutils.serializer.CustomExceptFilter;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IElementService;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.search.DbSearchCriteria;
import lcsb.mapviewer.services.search.chemical.IChemicalService;
......@@ -40,12 +43,15 @@ public class ChemicalController extends BaseController {
private ChemicalRestImpl chemicalController;
private IProjectService projectService;
private IChemicalService chemicalService;
private IElementService elementService;
@Autowired
public ChemicalController(ChemicalRestImpl chemicalController, IProjectService projectService, IChemicalService chemicalService) {
public ChemicalController(ChemicalRestImpl chemicalController, IProjectService projectService, IChemicalService chemicalService,
IElementService elementService) {
this.chemicalController = chemicalController;
this.projectService = projectService;
this.chemicalService = chemicalService;
this.elementService = elementService;
}
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)")
......@@ -55,15 +61,16 @@ public class ChemicalController extends BaseController {
@RequestParam(value = "columns", defaultValue = "") String columns,
@RequestParam(value = "query", defaultValue = "") String query,
@RequestParam(value = "target", defaultValue = "") String target) throws QueryException {
Project project = projectService.getProjectByProjectId(projectId);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
List<Chemical> data;
if (!query.equals("")) {
Project project = projectService.getProjectByProjectId(projectId);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
MiriamData organism = project.getOrganism();
if (organism == null) {
......@@ -75,7 +82,21 @@ public class ChemicalController extends BaseController {
} else if (target.contains(":")) {
String targetType = target.split(":", -1)[0];
String targetId = target.split(":", -1)[1];
data = chemicalController.getChemicalsByTarget(projectId, targetType, targetId, columns);
Integer dbId = Integer.valueOf(targetId);
List<Element> targets = new ArrayList<>();
if (targetType.equals(ElementIdentifierType.ALIAS.getJsName())) {
targets.add(elementService.getElementById(project.getProjectId(), dbId));
} else {
throw new QueryException("Targeting for the type not implemented");
}
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
data = chemicalService.getForTargets(targets,
new DbSearchCriteria().project(project).organisms(organism).disease(project.getDisease()));
} else {
data = new ArrayList<>();
}
......
......@@ -17,18 +17,13 @@ import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.annotation.data.MeSH;
import lcsb.mapviewer.annotation.services.MeSHParser;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.annotation.services.dapi.ChemicalSearchException;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IElementService;
import lcsb.mapviewer.services.search.DbSearchCriteria;
import lcsb.mapviewer.services.search.chemical.IChemicalService;
@Transactional
......@@ -52,107 +47,6 @@ public class ChemicalRestImpl extends BaseRestImpl {
this.meSHParser = meSHParser;
this.elementService = elementService;
}
protected Map<String, Object> prepareChemical(Chemical chemical, Set<String> columnsSet, Project project) {
Map<String, Object> result = new TreeMap<>();
String description = "Mesh term not available";
List<String> synonyms = new ArrayList<>();
try {
MeSH mesh = meSHParser.getMeSH(chemical.getChemicalId());
if (mesh != null) {
description = mesh.getDescription();
synonyms = mesh.getSynonyms();
} else {
logger.warn("Mesh used by chemical is invalid: " + chemical.getChemicalId());
}
} catch (AnnotatorException e) {
logger.error("Problem with accessing mesh database", e);
}
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value = null;
switch (column) {
case "id":
case "idobject":
value = chemical.getChemicalId();
break;
case "name":
value = chemical.getChemicalName();
break;
case "references":
List<MiriamData> references = new ArrayList<>();
references.add(chemical.getChemicalId());
if (chemical.getCasID() != null) {
references.add(chemical.getCasID());
}
value = references;
break;
case "directevidencereferences":
value = chemical.getDirectEvidencePublication();
break;
case "description":
value = description;
break;
case "directevidence":
if (chemical.getDirectEvidence() != null) {
value = chemical.getDirectEvidence().getValue();
}
break;
case "synonyms":
value = synonyms;
break;
case "targets":
value = prepareTargets(chemical.getInferenceNetwork(), project);
break;
default:
value = "Unknown column";
break;
}
result.put(string, value);
}
return result;
}
protected Set<String> createChemicalColumnSet(String columns) {
Set<String> columnsSet = new LinkedHashSet<>();
if (columns.equals("")) {
columnsSet.addAll(getAvailableChemicalColumns());
} else {
columnsSet.addAll(Arrays.asList(columns.split(",")));
}
return columnsSet;
}
public List<Chemical> getChemicalsByTarget(String projectId, String targetType,
String targetId, String columns) throws QueryException {
Project project = getProjectService().getProjectByProjectId(projectId);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
if (project.getDisease() == null) {
throw new QueryException("Project doesn't have disease associated to it");
}
Integer dbId = Integer.valueOf(targetId);
List<Element> targets = new ArrayList<>();
if (targetType.equals(ElementIdentifierType.ALIAS.getJsName())) {
targets.add(elementService.getElementById(project.getProjectId(), dbId));
} else {
throw new QueryException("Targeting for the type not implemented");
}
MiriamData organism = project.getOrganism();
if (organism == null) {
organism = TaxonomyBackend.HUMAN_TAXONOMY;
}
return chemicalService.getForTargets(targets,
new DbSearchCriteria().project(project).organisms(organism).disease(project.getDisease()));
}
public List<String> getSuggestedQueryList(String projectId) throws ChemicalSearchException, ObjectNotFoundException {
Project project = getProjectService().getProjectByProjectId(projectId);
if (project == null) {
......@@ -161,16 +55,4 @@ public class ChemicalRestImpl extends BaseRestImpl {
return chemicalService.getSuggestedQueryList(project, project.getDisease());
}
public List<String> getAvailableChemicalColumns() {
List<String> result = new ArrayList<>();
result.add("name");
result.add("references");
result.add("description");
result.add("synonyms");
result.add("id");
result.add("directEvidenceReferences");
result.add("directEvidence");
result.add("targets");
return result;
}
}
......@@ -4,7 +4,6 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.api.projects.chemicals.AllChemicalTests;
import lcsb.mapviewer.api.projects.comments.AllCommentTests;
import lcsb.mapviewer.api.projects.drugs.AllDrugTests;
import lcsb.mapviewer.api.projects.mirnas.AllMiRnaTests;
......@@ -12,7 +11,7 @@ import lcsb.mapviewer.api.projects.models.AllModelsTests;
import lcsb.mapviewer.api.projects.overlays.OverlayRestImplTest;
@RunWith(Suite.class)
@SuiteClasses({ AllChemicalTests.class,
@SuiteClasses({
AllCommentTests.class,
AllDrugTests.class,
AllMiRnaTests.class,
......
package lcsb.mapviewer.api.projects.chemicals;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ ChemicalRestImplTest.class })
public class AllChemicalTests {
}
package lcsb.mapviewer.api.projects.chemicals;
import static org.junit.Assert.assertNotNull;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
public class ChemicalRestImplTest extends RestTestFunctions {
Logger logger = LogManager.getLogger();
@Autowired
private ChemicalRestImpl _drugRestImpl;
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPrepareChemical() throws Exception {
Chemical chemical = new Chemical();
chemical.setChemicalId(new MiriamData(MiriamType.MESH_2012, "D010300"));
Map<String, Object> result = _drugRestImpl.prepareChemical(chemical, _drugRestImpl.createChemicalColumnSet(""),
new Project());
assertNotNull(result);
}
@Test
public void testPrepareChemicalWithProblematicMesh() throws Exception {
Chemical chemical = new Chemical();
chemical.setChemicalId(new MiriamData(MiriamType.MESH_2012, "invalidID"));
Map<String, Object> result = _drugRestImpl.prepareChemical(chemical, _drugRestImpl.createChemicalColumnSet(""),
new Project());
assertNotNull(result);
}
}
......@@ -85,16 +85,7 @@ public class ChemicalService extends DbSearchService implements IChemicalService
logger.warn("More than one chemical found by name: " + name);
}
Chemical chemical = result.get(0);
if (searchCriteria.getProject() != null) {
List<ProjectTarget> projectTargets = new ArrayList<>();
for (Target target : chemical.getTargets()) {
projectTargets.add(super.createTargetWithProjectData(target, searchCriteria.getProject()));
}
projectTargets.sort(ProjectTarget.COMPARATOR);
chemical.removeTargets(chemical.getTargets());
chemical.addTargets(projectTargets);
}
addProjectTargets(searchCriteria.getProject(), chemical);
return chemical;
}
} catch (ChemicalSearchException e) {
......@@ -103,6 +94,19 @@ public class ChemicalService extends DbSearchService implements IChemicalService
}
}
private void addProjectTargets(Project project, Chemical chemical) {
if (project != null) {
List<ProjectTarget> projectTargets = new ArrayList<>();
for (Target target : chemical.getTargets()) {
projectTargets.add(super.createTargetWithProjectData(target, project));
}
projectTargets.sort(ProjectTarget.COMPARATOR);
chemical.removeTargets(chemical.getTargets());
chemical.addTargets(projectTargets);
}
}
@Override
public List<Chemical> getForTargets(Collection<Element> targets, DbSearchCriteria searchCriteria) {
List<Chemical> chemicalList = new ArrayList<>();
......@@ -135,6 +139,9 @@ public class ChemicalService extends DbSearchService implements IChemicalService
}
Collections.sort(chemicalList, new Chemical.NameComparator());
for (Chemical chemical : chemicalList) {
addProjectTargets(searchCriteria.getProject(), chemical);
}
return chemicalList;
}
......
package lcsb.mapviewer.web;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.awt.geom.Point2D;
import java.io.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Supplier;
import javax.annotation.PostConstruct;
......@@ -21,7 +32,9 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.junit.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.restdocs.JUnitRestDocumentation;
......@@ -39,21 +52,46 @@ import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import lcsb.mapviewer.annotation.data.serializer.ChemicalSerializer;
import lcsb.mapviewer.api.projects.chemicals.ChemicalRestImpl;
import lcsb.mapviewer.common.*;
import lcsb.mapviewer.common.MinervaLoggerAppender;
import lcsb.mapviewer.common.TextFileUtils;
import lcsb.mapviewer.common.UnitTestFailedWatcher;
import lcsb.mapviewer.converter.ColorSchemaReader;
import lcsb.mapviewer.converter.zip.ZipEntryFileFactory;
import lcsb.mapviewer.model.*;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.ProjectLogEntry;
import lcsb.mapviewer.model.ProjectLogEntryType;
import lcsb.mapviewer.model.ProjectStatus;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.graphics.*;
import lcsb.mapviewer.model.map.*;
import lcsb.mapviewer.model.map.kinetics.*;
import lcsb.mapviewer.model.map.layout.*;
import lcsb.mapviewer.model.graphics.HorizontalAlign;
import lcsb.mapviewer.model.graphics.PolylineData;
import lcsb.mapviewer.model.graphics.VerticalAlign;
import lcsb.mapviewer.model.map.Comment;
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.OverviewModelLink;
import lcsb.mapviewer.model.map.OverviewSearchLink;
import lcsb.mapviewer.model.map.kinetics.SbmlFunction;
import lcsb.mapviewer.model.map.kinetics.SbmlParameter;
import lcsb.mapviewer.model.map.kinetics.SbmlUnit;
import lcsb.mapviewer.model.map.layout.ProjectBackground;
import lcsb.mapviewer.model.map.layout.ProjectBackgroundImageLayer;
import lcsb.mapviewer.model.map.layout.ProjectBackgroundStatus;
import lcsb.mapviewer.model.map.layout.graphics.Glyph;
import lcsb.mapviewer.model.map.model.*;
import lcsb.mapviewer.model.map.reaction.*;
import lcsb.mapviewer.model.map.model.ElementSubmodelConnection;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
import lcsb.mapviewer.model.map.model.SubmodelType;
import lcsb.mapviewer.model.map.reaction.Product;
import lcsb.mapviewer.model.map.reaction.Reactant;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.reaction.type.TransportReaction;
import lcsb.mapviewer.model.map.species.*;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.map.species.SimpleMolecule;
import lcsb.mapviewer.model.overlay.DataOverlay;
import lcsb.mapviewer.model.overlay.DataOverlayType;
import lcsb.mapviewer.model.security.PrivilegeType;
......@@ -62,7 +100,9 @@ import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.DbUtils;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
import lcsb.mapviewer.persist.dao.map.*;
import lcsb.mapviewer.persist.dao.map.CommentDao;
import lcsb.mapviewer.persist.dao.map.DataOverlayDao;
import lcsb.mapviewer.persist.dao.map.ModelDao;
import lcsb.mapviewer.persist.dao.user.ResetPasswordTokenDao;
import lcsb.mapviewer.persist.dao.user.UserDao;
import lcsb.mapviewer.services.interfaces.IProjectService;
......@@ -168,12 +208,12 @@ abstract public class ControllerIntegrationTest {
}
/**
* This method can be used to work around the fact that MockMvc cannot retrieve
* cookies from Spring Security. The Reason for that is that MockMvc calls the
* controller directly and (partially?) bypasses Spring.
* This method can be used to work around the fact that MockMvc cannot
* retrieve cookies from Spring Security. The Reason for that is that MockMvc
* calls the controller directly and (partially?) bypasses Spring.
*
* This method creates a mocked session that can be used in a request as opposed
* to the token.
* This method creates a mocked session that can be used in a request as
* opposed to the token.
*
* FIXME: Find a better solution, that does not violate the spirit of
* integration tests.
......@@ -306,7 +346,7 @@ abstract public class ControllerIntegrationTest {
}
ModelData map = new ModelData();
OverviewModelLink link1 =new OverviewModelLink();
OverviewModelLink link1 = new OverviewModelLink();
link1.setLinkedModel(map);
link1.setPolygon("0,0 10,10 10,0");
link1.setxCoord(10);
......@@ -314,12 +354,12 @@ abstract public class ControllerIntegrationTest {
link1.setZoomLevel(2);
image.addLink(link1);
OverviewImageLink link2 =new OverviewImageLink();
OverviewImageLink link2 = new OverviewImageLink();
link2.setLinkedOverviewImage(image);
link2.setPolygon("0,0 10,10 10,0");
image.addLink(link2);
OverviewSearchLink link3 =new OverviewSearchLink();
OverviewSearchLink link3 = new OverviewSearchLink();
link3.setQuery("a1");
link3.setPolygon("0,0 10,10 10,0");
image.addLink(link3);
......@@ -373,7 +413,7 @@ abstract public class ControllerIntegrationTest {
project.addModel(map);
project.addModel(submap);
ProjectBackground background = new ProjectBackground("Normal");
int id = 0;
for (ModelData model : project.getModels()) {
......@@ -386,7 +426,7 @@ abstract public class ControllerIntegrationTest {
background.setStatus(ProjectBackgroundStatus.OK);
background.setCreator(userService.getUserByLogin(BUILT_IN_TEST_ADMIN_LOGIN));
project.addProjectBackground(background);
projectDao.add(project);
return project;
}
......@@ -688,7 +728,7 @@ abstract public class ControllerIntegrationTest {
return requestParameters(
parameterWithName("columns")
.description("set of columns (all by default). Available options: "
+ StringUtils.join(chemicalRestImpl.getAvailableChemicalColumns(), ", "))
+ StringUtils.join(ChemicalSerializer.availableColumns(), ", "))
.optional(),
parameterWithName("query")
.description("name of chemical that we are searching for")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment