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

get submodel connections moved to controller

parent 566a859b
package lcsb.mapviewer.modelutils.serializer.id;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lcsb.mapviewer.model.map.model.ModelData;
public class ModelDataAsModelIdSerializer extends JsonSerializer<ModelData> {
@Override
public void serialize(final ModelData model, final JsonGenerator gen, final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeNumberField("modelId", model.getId());
gen.writeEndObject();
}
}
\ No newline at end of file
package lcsb.mapviewer.api;
package lcsb.mapviewer.modelutils.serializer.model.map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
package lcsb.mapviewer.modelutils.serializer.model.map;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
public class MinifiedBioEntitySerializer extends JsonSerializer<BioEntity> {
@Override
public void serialize(final BioEntity object, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeNumberField("id", object.getId());
gen.writeNumberField("modelId", object.getModelData().getId());
if (object instanceof Element) {
gen.writeObjectField("type", ElementIdentifierType.ALIAS);
} else if (object instanceof Reaction) {
gen.writeObjectField("type", ElementIdentifierType.REACTION);
} else {
throw new InvalidStateException("Unknown type of result: " + object.getClass());
}
gen.writeEndObject();
}
}
\ No newline at end of file
......@@ -44,6 +44,7 @@ import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.modelutils.map.ElementUtils;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.persist.dao.map.species.ElementProperty;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IConfigurationService;
......
......@@ -7,6 +7,7 @@ import java.lang.reflect.InvocationTargetException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
......@@ -42,6 +43,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseController;
......@@ -54,11 +56,17 @@ import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.ProjectLogEntry;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.graphics.MapCanvasType;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.layout.ProjectBackground;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.serializer.id.ModelDataAsModelIdSerializer;
import lcsb.mapviewer.modelutils.serializer.model.map.MinifiedBioEntitySerializer;
import lcsb.mapviewer.persist.dao.map.species.ElementProperty;
import lcsb.mapviewer.services.ObjectExistsException;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
......@@ -667,11 +675,36 @@ public class ProjectController extends BaseController {
}
}
static class SubmapConnection {
@JsonSerialize(using = MinifiedBioEntitySerializer.class)
public BioEntity from;
@JsonSerialize(using = ModelDataAsModelIdSerializer.class)
public ModelData to;
public SubmapConnection(Element element) {
from = element;
to = element.getSubmodel().getSubmodel();
}
}
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)")
@GetMapping(value = "/{projectId}/submapConnections")
public List<Map<String, Object>> getSubmapConnections(@PathVariable(value = "projectId") String projectId)
public List<SubmapConnection> getSubmapConnections(@PathVariable(value = "projectId") String projectId)
throws QueryException {
return projectController.getSubmapConnections(projectId);
Project project = getProject(projectId);
Map<ElementProperty, List<? extends Object>> properties = new HashMap<>();
properties.put(ElementProperty.PROJECT, Arrays.asList(project));
properties.put(ElementProperty.SUBMAP_CONNECTION, Arrays.asList(Boolean.TRUE));
List<Element> elements = elementService.getElementsByFilter(properties, true);
List<SubmapConnection> result = new ArrayList<>();
for (Element element : elements) {
if (element.getSubmodel() != null) {
result.add(new SubmapConnection(element));
}
}
return result;
}
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId) "
......
......@@ -44,30 +44,5 @@ public class ProjectRestImpl extends BaseRestImpl {
return project;
}
public List<Map<String, Object>> getSubmapConnections(String projectId) throws QueryException {
Project project = getProjectByProjectId(projectId);
Map<ElementProperty, List<? extends Object>> properties = new HashMap<>();
properties.put(ElementProperty.PROJECT, Arrays.asList(project));
properties.put(ElementProperty.SUBMAP_CONNECTION, Arrays.asList(Boolean.TRUE));
List<Element> elements = elementService.getElementsByFilter(properties);
List<Map<String, Object>> result = new ArrayList<>();
for (Element element : elements) {
if (element.getSubmodel() != null) {
result.add(submodelConnectionToMap(element));
}
}
return result;
}
private Map<String, Object> submodelConnectionToMap(Element element) {
Map<String, Object> result = new TreeMap<>();
result.put("from", super.createMinifiedSearchResult(element));
Map<String, Object> to = new TreeMap<>();
to.put("modelId", element.getSubmodel().getSubmodel().getId());
result.put("to", to);
return result;
}
}
......@@ -21,10 +21,10 @@ 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.api.ElementIdentifierType;
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;
......
......@@ -19,6 +19,7 @@ import lcsb.mapviewer.api.*;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IUserService;
......
......@@ -11,7 +11,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ElementIdentifierType;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.geometry.PointTransformation;
import lcsb.mapviewer.model.Project;
......@@ -21,6 +20,7 @@ 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;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.persist.dao.map.ReactionDao;
import lcsb.mapviewer.persist.dao.map.species.ElementDao;
import lcsb.mapviewer.services.ObjectNotFoundException;
......
......@@ -12,10 +12,10 @@ import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.services.DrugSearchException;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ElementIdentifierType;
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;
......
......@@ -12,10 +12,10 @@ import lcsb.mapviewer.annotation.data.MiRNA;
import lcsb.mapviewer.annotation.services.MiRNASearchException;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ElementIdentifierType;
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;
......
......@@ -13,7 +13,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ElementIdentifierType;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.geometry.ColorParser;
import lcsb.mapviewer.model.Project;
......@@ -24,6 +23,7 @@ import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.overlay.*;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
......
......@@ -18,7 +18,7 @@ import lcsb.mapviewer.api.projects.overlays.OverlayRestImplTest;
AllMiRnaTests.class,
AllModelsTests.class,
OverlayRestImplTest.class,
ProjectRestImplTest.class })
ProjectControllerTest.class })
public class AllProjectTests {
}
package lcsb.mapviewer.api.projects;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.Gson;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.ProjectLogEntry;
import lcsb.mapviewer.model.ProjectLogEntryType;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.persist.dao.user.UserDao;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.IProjectService;
public class ProjectRestImplTest extends RestTestFunctions {
Logger logger = LogManager.getLogger();
@Autowired
ProjectRestImpl _projectRest;
ProjectRestImpl projectRest;
@Autowired
IModelService modelService;
@Autowired
IProjectService projectService;
@Autowired
ProjectDao projectDao;
@Autowired
UserDao userDao;
@Before
public void before() {
projectRest = _projectRest;
}
@Test(expected = ObjectNotFoundException.class)
public void testGetInvalidMetaData() throws Exception {
projectRest.getProject("unknown_model_id");
}
private void createMockProjectRest(String string) throws Exception {
Model model = null;
Project project = null;
model = super.getModelForFile(string, false);
project = new Project(model.getName());
project.addModel(model);
project.setOwner(userDao.getUserByLogin(ADMIN_BUILT_IN_LOGIN));
project.setProjectId(model.getName());
ProjectLogEntry entry = new ProjectLogEntry();
entry.setContent("content");
entry.setType(ProjectLogEntryType.OTHER);
entry.setSeverity("DEBUG");
project.addLogEntry(entry);
projectDao.add(project);
projectRest = Mockito.spy(_projectRest);
}
}
......@@ -2,6 +2,7 @@ package lcsb.mapviewer.services.impl;
import java.util.*;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -82,4 +83,16 @@ public class ElementService implements IElementService {
return elementAnnotations;
}
@Override
public List<Element> getElementsByFilter(Map<ElementProperty, List<? extends Object>> properties, boolean initializeLazy) {
List<Element> result = getElementsByFilter(properties);
if (initializeLazy) {
for (Element element : result) {
Hibernate.initialize(element.getSubmodel().getSubmodel());
Hibernate.initialize(element.getModel());
}
}
return result;
}
}
......@@ -18,6 +18,8 @@ public interface IElementService {
Element getElementById(String projectId, int mapId, int elementId) throws QueryException;
List<Element> getElementsByFilter(Map<ElementProperty, List<? extends Object>> properties);
List<Element> getElementsByFilter(Map<ElementProperty, List<? extends Object>> properties, boolean initializeLazy);
Map<MiriamType, Integer> getAnnotationStatistics(String projectId, String mapId) throws QueryException;
......
......@@ -35,7 +35,6 @@ import org.springframework.test.web.servlet.RequestBuilder;
import com.google.gson.JsonParser;
import lcsb.mapviewer.api.ElementIdentifierType;
import lcsb.mapviewer.api.projects.comments.CommentRestImpl;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.Project;
......@@ -45,6 +44,7 @@ import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.persist.dao.map.CommentDao;
import lcsb.mapviewer.services.interfaces.ICommentService;
import lcsb.mapviewer.services.interfaces.IUserService;
......
......@@ -369,6 +369,7 @@ abstract public class ControllerIntegrationTest {
ModelSubmodelConnection submodelConnection = new ModelSubmodelConnection(submap, SubmodelType.UNKNOWN);
map.addSubmodel(submodelConnection);
element.setSubmodel(new ElementSubmodelConnection(submap, SubmodelType.UNKNOWN));
project.addModel(map);
project.addModel(submap);
......
......@@ -31,7 +31,6 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParser;
import lcsb.mapviewer.api.ElementIdentifierType;
import lcsb.mapviewer.api.projects.models.bioEntities.elements.ElementsRestImpl;
import lcsb.mapviewer.api.projects.models.bioEntities.reactions.ReactionsRestImpl;
import lcsb.mapviewer.common.Configuration;
......@@ -47,6 +46,7 @@ import lcsb.mapviewer.model.overlay.DataOverlay;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.map.ElementUtils;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.services.interfaces.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
......
......@@ -510,15 +510,22 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
@Test
public void testGetSubmapConnectionsForProject() throws Exception {
createProjectInSeparateThread(TEST_PROJECT);
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
RequestBuilder request = get("/api/projects/{projectId}/submapConnections/", BUILT_IN_PROJECT).session(session);
RequestBuilder request = get("/api/projects/{projectId}/submapConnections/", TEST_PROJECT).session(session);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andDo(document("projects/project_maps/get_connections",
projectPathParameters(),
responseFields(fieldWithPath("[]").description("list of connections").type("array"))));
responseFields(
fieldWithPath("[]").description("list of connections").type(JsonFieldType.ARRAY),
fieldWithPath("[].from.id").description("anchor bioEntity id").type(JsonFieldType.NUMBER),
fieldWithPath("[].from.modelId").description("anchor map id").type(JsonFieldType.NUMBER),
fieldWithPath("[].from.type").description("anchor type").type(JsonFieldType.STRING),
fieldWithPath("[].to.modelId").description("destination map id").type(JsonFieldType.NUMBER))));
}
@Test
......
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