Commit 85f7b1fc authored by Piotr Gawron's avatar Piotr Gawron
Browse files

always use default serializer for DataOverlay

parent 2cde6a69
......@@ -51,17 +51,17 @@ public class OverlayController extends BaseController {
+ "or not @projectService.projectExists(#projectId)")
@PostFilter("hasAuthority('IS_ADMIN')" +
" or hasAuthority('IS_CURATOR') and hasAuthority('READ_PROJECT:' + #projectId)" +
" or hasAuthority('READ_PROJECT:' + #projectId) and (filterObject['creator'] == authentication.name or filterObject['publicOverlay'])")
" or hasAuthority('READ_PROJECT:' + #projectId) and (filterObject.creator.login == authentication.name or filterObject.isPublic())")
@GetMapping(value = "/")
public List<Map<String, Object>> getOverlayList(
public List<DataOverlay> getOverlayList(
@PathVariable(value = "projectId") String projectId,
@RequestParam(value = "creator", defaultValue = "") String creator,
@RequestParam(value = "publicOverlay", defaultValue = "false") boolean publicOverlay)
throws lcsb.mapviewer.services.ObjectNotFoundException {
return overlayRestImp.getOverlayList(projectId).stream()
.filter(overlay -> !publicOverlay || (Boolean) overlay.get("publicOverlay"))
.filter(overlay -> !publicOverlay || overlay.isPublic())
.filter(
overlay -> creator.isEmpty() || (overlay.get("creator") != null && overlay.get("creator").equals(creator)))
overlay -> creator.isEmpty() || overlay.getCreator() != null && overlay.getCreator().getLogin().equals(creator))
.collect(Collectors.toList());
}
......
package lcsb.mapviewer.api.projects.overlays;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
......@@ -21,7 +28,11 @@ import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.overlay.*;
import lcsb.mapviewer.model.overlay.DataOverlay;
import lcsb.mapviewer.model.overlay.DataOverlayEntry;
import lcsb.mapviewer.model.overlay.DataOverlayType;
import lcsb.mapviewer.model.overlay.GeneVariantDataOverlayEntry;
import lcsb.mapviewer.model.overlay.InvalidDataOverlayException;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
......@@ -50,45 +61,12 @@ public class OverlayRestImpl extends BaseRestImpl {
this.dataOverlayService = dataOverlayService;
}
public List<Map<String, Object>> getOverlayList(String projectId) throws ObjectNotFoundException {
public List<DataOverlay> getOverlayList(String projectId) throws ObjectNotFoundException {
Project project = getProjectService().getProjectByProjectId(projectId);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
List<DataOverlay> dataOverlays = dataOverlayService.getDataOverlaysByProject(project);
List<Map<String, Object>> result = new ArrayList<>();
result.addAll(overlaysToMap(dataOverlays));
return result;
}
private List<Map<String, Object>> overlaysToMap(List<DataOverlay> overlays) {
List<Map<String, Object>> result = new ArrayList<>();
for (DataOverlay overlay : overlays) {
result.add(overlayToMap(overlay));
}
return result;
}
public Map<String, Object> overlayToMap(DataOverlay overlay) {
Map<String, Object> result = new TreeMap<>();
result.put("idObject", overlay.getId());
result.put("name", overlay.getName());
result.put("order", overlay.getOrderIndex());
result.put("description", overlay.getDescription());
result.put("publicOverlay", overlay.isPublic());
result.put("type", overlay.getColorSchemaType());
result.put("genomeType", overlay.getGenomeType());
result.put("genomeVersion", overlay.getGenomeVersion());
result.put("googleLicenseConsent", overlay.isGoogleLicenseConsent());
if (overlay.getCreator() != null) {
result.put("creator", overlay.getCreator().getLogin());
}
return result;
return dataOverlayService.getDataOverlaysByProject(project, true);
}
public List<Map<String, Object>> getOverlayElements(String projectId, int overlayId, String columns)
......@@ -334,53 +312,54 @@ public class OverlayRestImpl extends BaseRestImpl {
String column = string.toLowerCase();
Object value = null;
switch (column) {
case "id":
case "idobject":
// casting to string is only to provide the same results as before refactoring
value = bioEntity.getId() + "";
break;
case "uniqueid":
value = overlayEntry.getId();
break;
case "modelid":
value = bioEntity.getModelData().getId();
break;
case "value":
value = overlayEntry.getValue();
break;
case "color":
if (overlayEntry.getColor() != null) {
value = colorParser.colorToMap(overlayEntry.getColor());
}
break;
case "description":
value = overlayEntry.getDescription();
break;
case "width":
if (bioEntity instanceof Reaction) {
value = overlayEntry.getLineWidth();
} else {
continue;
}
break;
case "type":
if (overlayEntry instanceof GeneVariantDataOverlayEntry) {
value = DataOverlayType.GENETIC_VARIANT;
} else {
value = DataOverlayType.GENERIC;
}
break;
case "genevariations":
if (overlayEntry instanceof GeneVariantDataOverlayEntry) {
value = ((GeneVariantDataOverlayEntry) overlayEntry).getGeneVariants();
Hibernate.initialize(((GeneVariantDataOverlayEntry) overlayEntry).getGeneVariants());
} else {
value = new Object[] {};
}
break;
default:
value = "Unknown column";
break;
case "id":
case "idobject":
// casting to string is only to provide the same results as before
// refactoring
value = bioEntity.getId() + "";
break;
case "uniqueid":
value = overlayEntry.getId();
break;
case "modelid":
value = bioEntity.getModelData().getId();
break;
case "value":
value = overlayEntry.getValue();
break;
case "color":
if (overlayEntry.getColor() != null) {
value = colorParser.colorToMap(overlayEntry.getColor());
}
break;
case "description":
value = overlayEntry.getDescription();
break;
case "width":
if (bioEntity instanceof Reaction) {
value = overlayEntry.getLineWidth();
} else {
continue;
}
break;
case "type":
if (overlayEntry instanceof GeneVariantDataOverlayEntry) {
value = DataOverlayType.GENETIC_VARIANT;
} else {
value = DataOverlayType.GENERIC;
}
break;
case "genevariations":
if (overlayEntry instanceof GeneVariantDataOverlayEntry) {
value = ((GeneVariantDataOverlayEntry) overlayEntry).getGeneVariants();
Hibernate.initialize(((GeneVariantDataOverlayEntry) overlayEntry).getGeneVariants());
} else {
value = new Object[] {};
}
break;
default:
value = "Unknown column";
break;
}
result.put(string, value);
}
......
......@@ -512,4 +512,15 @@ public class DataOverlayService implements IDataOverlayService {
return null;
}
@Override
public List<DataOverlay> getDataOverlaysByProject(Project project, boolean loadLazy) {
List<DataOverlay> result = getDataOverlaysByProject(project);
if (loadLazy) {
for (DataOverlay dataOverlay : result) {
Hibernate.initialize(dataOverlay.getCreator());
}
}
return result;
}
}
package lcsb.mapviewer.services.interfaces;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import lcsb.mapviewer.common.Pair;
......@@ -8,7 +11,10 @@ import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.overlay.*;
import lcsb.mapviewer.model.overlay.DataOverlay;
import lcsb.mapviewer.model.overlay.DataOverlayEntry;
import lcsb.mapviewer.model.overlay.DataOverlayType;
import lcsb.mapviewer.model.overlay.InvalidDataOverlayException;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
......@@ -29,11 +35,11 @@ public interface IDataOverlayService {
/**
*
* Input stream with coloring information data in the stream should correspond
* to a file that can be parsed by
* {@link lcsb.mapviewer.services.utils.ColorSchemaXlsxReader ColorSchemaReader}
* . This is a copy of original input stream, so we can read this input stream
* multiple times.
* Input stream with coloring information data in the stream should
* correspond to a file that can be parsed by
* {@link lcsb.mapviewer.services.utils.ColorSchemaXlsxReader
* ColorSchemaReader} . This is a copy of original input stream, so we can
* read this input stream multiple times.
*/
private ByteArrayOutputStream colorInputStreamCopy;
......@@ -143,6 +149,8 @@ public interface IDataOverlayService {
List<DataOverlay> getDataOverlaysByProject(Project project);
List<DataOverlay> getDataOverlaysByProject(Project project, boolean loadLazy);
void removeDataOverlay(DataOverlay dataOverlay);
void updateDataOverlay(DataOverlay dataOverlay);
......
package lcsb.mapviewer.web;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.restdocs.payload.*;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.restdocs.payload.ResponseFieldsSnippet;
import org.springframework.restdocs.request.RequestParametersSnippet;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import com.google.gson.Gson;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.JsonParser;
import lcsb.mapviewer.api.projects.overlays.OverlayRestImpl;
......@@ -205,16 +219,9 @@ public class OverlayControllerIntegrationTest extends ControllerIntegrationTest
DataOverlay userLayout = createOverlayInSeparateThread(TEST_PROJECT, user);
userLayout.setPublic(false);
callInSeparateThread(() -> {
try {
dataOverlayDao.update(layout);
dataOverlayDao.update(hiddenLayout);
dataOverlayDao.update(userLayout);
} catch (Exception e) {
e.printStackTrace();
}
return null;
});
dataOverlayService.updateDataOverlay(layout);
dataOverlayService.updateDataOverlay(hiddenLayout);
dataOverlayService.updateDataOverlay(userLayout);
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
......@@ -714,13 +721,16 @@ public class OverlayControllerIntegrationTest extends ControllerIntegrationTest
public void testAdminCanUpdateOverlay() throws Exception {
DataOverlay overlay = createOverlayInSeparateThread(user);
Map<String, Object> updateOverlay = overlayRestImpl.overlayToMap(overlay);
Map<String, Object> updateOverlay = objectMapper.readValue(objectMapper.writeValueAsString(overlay), new TypeReference<Map<String, Object>>() {
});
updateOverlay.remove("idObject");
updateOverlay.remove("defaultOverlay");
updateOverlay.remove("images");
updateOverlay.remove("genomeType");
updateOverlay.remove("genomeVersion");
updateOverlay.remove("inputDataAvailable");
String body = "{\"overlay\":" + new Gson().toJson(updateOverlay) + "}";
String body = "{\"overlay\":" + objectMapper.writeValueAsString(updateOverlay) + "}";
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
......
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