Commit 743f5a2e authored by Piotr Gawron's avatar Piotr Gawron
Browse files

update project moved to controller

parent 4f3b16c4
......@@ -3,6 +3,7 @@ package lcsb.mapviewer.api.projects;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.servlet.ServletContext;
......@@ -23,14 +24,21 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.graphics.MapCanvasType;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.layout.ProjectBackground;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IMeshService;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.interfaces.IUserService;
@RestController
......@@ -39,13 +47,18 @@ public class ProjectController extends BaseController {
private ServletContext context;
private ProjectRestImpl projectController;
private IProjectService projectService;
private IUserService userService;
private IMeshService meshService;
@Autowired
public ProjectController(ServletContext context, ProjectRestImpl projectController, IUserService userService) {
public ProjectController(ServletContext context, ProjectRestImpl projectController, IUserService userService, IProjectService projectService,
IMeshService meshService) {
this.context = context;
this.projectController = projectController;
this.userService = userService;
this.projectService = projectService;
this.meshService = meshService;
}
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)")
......@@ -55,14 +68,92 @@ public class ProjectController extends BaseController {
return projectController.getProject(projectId);
}
static class UpdateProjectData {
public ProjectData project;
}
static class ProjectData {
public String version;
public String projectId;
public String name;
public String notifyEmail;
public Optional<MiriamData> organism;
public Optional<MiriamData> disease;
public MapCanvasType mapCanvasType;
}
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'WRITE_PROJECT:' + #projectId)")
@PatchMapping(value = "/{projectId:.+}")
@PatchMapping(value = "/{projectId:.+}", consumes = MediaType.APPLICATION_JSON_VALUE)
public Project updateProject(
@RequestBody String body,
@RequestBody UpdateProjectData body,
@PathVariable(value = "projectId") String projectId) throws IOException, QueryException {
Map<String, Object> node = parseBody(body);
Map<String, Object> data = getData(node, "project");
return projectController.updateProject(projectId, data);
ProjectData data = body.project;
Project project = getProject(projectId);
if (data.version != null) {
if (data.version.length() > 20) {
throw new QueryException("version is too long (>20 characters)");
}
project.setVersion(data.version);
}
if (data.projectId != null) {
if (!project.getProjectId().equalsIgnoreCase(data.projectId)) {
throw new QueryException("You cannot modify projectId");
}
}
if (data.name != null) {
if (data.name.length() > 255) {
throw new QueryException("name is too long (>255 characters)");
}
project.setName(data.name);
}
if (data.notifyEmail != null) {
project.setNotifyEmail(data.notifyEmail);
}
if (data.organism.isPresent()) {
MiriamData organism = updateMiriamData(project.getOrganism(), data.organism.get());
project.setOrganism(organism);
}
if (data.disease.isPresent()) {
try {
MiriamData sourceData = updateMiriamData(null, data.disease.get());
if (meshService.isValidMeshId(sourceData)) {
MiriamData disease = updateMiriamData(project.getDisease(), data.disease.get());
project.setDisease(disease);
} else if (sourceData == null || sourceData.getResource().isEmpty()) {
project.setDisease(null);
} else {
throw new QueryException("invalid mesh identifier: " + data.disease.get());
}
} catch (AnnotatorException e) {
throw new QueryException("invalid miriamdData: " + data.disease.get(), e);
}
}
if (data.mapCanvasType != null) {
project.setMapCanvasType(data.mapCanvasType);
}
projectService.updateProject(project);
Project result = getProject(projectId);
System.out.println(result);
return result;
}
private MiriamData updateMiriamData(MiriamData original, MiriamData newData) {
if (newData == null) {
return null;
}
if (original == null) {
original = new MiriamData();
}
if (newData instanceof Map) {
original.setDataType(newData.getDataType());
original.setResource(newData.getResource());
return original;
} else {
throw new InvalidArgumentException("invalid miriamdData: " + newData);
}
}
@PreAuthorize("hasAuthority('IS_ADMIN') "
......@@ -121,7 +212,7 @@ public class ProjectController extends BaseController {
@PostFilter("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + filterObject['projectId'])")
@GetMapping(value = "/")
public List<Project> getProjects() {
return projectController.getProjects();
return projectService.getAllProjects(true);
}
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)")
......
......@@ -11,7 +11,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import org.apache.logging.log4j.LogManager;
......@@ -22,7 +21,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import lcsb.mapviewer.annotation.services.MeSHParser;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.OperationNotAllowedException;
import lcsb.mapviewer.common.comparator.StringComparator;
......@@ -147,93 +145,6 @@ public class ProjectRestImpl extends BaseRestImpl {
return result;
}
public List<Project> getProjects() {
return getProjectService().getAllProjects(true);
}
public Project updateProject(String projectId, Map<String, Object> data)
throws QueryException {
Project project = getProjectByProjectId(projectId);
Set<String> fields = data.keySet();
for (String fieldName : fields) {
Object value = data.get(fieldName);
String stringValue = null;
if (value instanceof String) {
stringValue = (String) value;
}
if (fieldName.equalsIgnoreCase("version")) {
if (value != null && ((String) value).length() > 20) {
throw new QueryException("version is too long (>20 characters)");
}
project.setVersion((String) value);
} else if (fieldName.equalsIgnoreCase("id")) {
int id = parseInteger(stringValue, "id");
if (id != project.getId()) {
throw new QueryException("Invalid id: " + stringValue);
}
} else if (fieldName.equalsIgnoreCase("projectId")) {
if (!project.getProjectId().equalsIgnoreCase(stringValue)) {
throw new QueryException("You cannot modify projectId");
}
} else if (fieldName.equalsIgnoreCase("name")) {
if (value != null && ((String) value).length() > 255) {
throw new QueryException("name is too long (>255 characters)");
}
project.setName((String) value);
} else if (fieldName.equalsIgnoreCase("notifyEmail")) {
project.setNotifyEmail(stringValue);
} else if (fieldName.equalsIgnoreCase("organism")) {
MiriamData organism = updateMiriamData(project.getOrganism(), value);
project.setOrganism(organism);
} else if (fieldName.equalsIgnoreCase("disease")) {
try {
MiriamData sourceData = updateMiriamData(null, value);
if (meshParser.isValidMeshId(sourceData)) {
MiriamData disease = updateMiriamData(project.getDisease(), value);
project.setDisease(disease);
} else if (sourceData == null || sourceData.getResource().isEmpty()) {
project.setDisease(null);
} else {
throw new QueryException("invalid mesh identifier: " + value);
}
} catch (AnnotatorException e) {
throw new QueryException("invalid miriamdData: " + value, e);
}
} else if (fieldName.equalsIgnoreCase("mapCanvasType")) {
MapCanvasType mapCanvasType;
try {
mapCanvasType = MapCanvasType.valueOf(stringValue);
} catch (Exception e) {
throw new QueryException("Invalid mapCanvasType value");
}
project.setMapCanvasType(mapCanvasType);
} else {
throw new QueryException("Unknown field: " + fieldName);
}
}
getProjectService().updateProject(project);
return getProject(projectId);
}
private MiriamData updateMiriamData(MiriamData organism, Object res) {
if (res == null || res.equals("")) {
return null;
}
if (organism == null) {
organism = new MiriamData();
}
if (res instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) res;
organism.setDataType(MiriamType.valueOf((String) map.get("type")));
organism.setResource((String) map.get("resource"));
return organism;
} else {
throw new InvalidArgumentException("invalid miriamdData: " + res);
}
}
public Project addProject(String projectId, MultiValueMap<String, Object> data, String path, User user)
throws QueryException, IOException, SecurityException {
Project project = getProjectService().getProjectByProjectId(projectId);
......
......@@ -72,22 +72,6 @@ public class ProjectRestImplTest extends RestTestFunctions {
projectRest.getProject("unknown_model_id");
}
@Test
public void testUpdateProject() throws Exception {
createMockProjectRest("testFiles/model/sample.xml");
Map<String, String> disease = new HashMap<>();
disease.put("type", MiriamType.MESH_2012.name());
disease.put("resource", "D010300");
Map<String, Object> data = new HashMap<>();
data.put("version", "1");
data.put("name", "test");
data.put("organism", null);
data.put("disease", disease);
data.put("projectId", "sample");
projectRest.updateProject("sample", data);
}
@Test
public void testComputePathForProject() throws Exception {
String projectId = "Some_id";
......
......@@ -26,4 +26,10 @@ public class MeshService implements IMeshService {
public MeSH getMesh(MiriamData meshID) throws AnnotatorException {
return meSHParser.getMeSH(meshID);
}
@Override
public boolean isValidMeshId(MiriamData meshId) throws AnnotatorException {
return meSHParser.isValidMeshId(meshId);
}
}
......@@ -7,4 +7,6 @@ import lcsb.mapviewer.model.map.MiriamData;
public interface IMeshService {
MeSH getMesh(MiriamData miriamData) throws AnnotatorException;
boolean isValidMeshId(MiriamData sourceData) throws AnnotatorException;
}
package lcsb.mapviewer.web;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.request.RequestDocumentation.*;
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.payload.PayloadDocumentation.subsectionWithPath;
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.result.MockMvcResultMatchers.status;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.restdocs.request.*;
import org.springframework.restdocs.request.ParameterDescriptor;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.restdocs.request.RequestParametersSnippet;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
......@@ -327,6 +343,7 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
+ "}}";
RequestBuilder request = patch("/api/projects/{projectId}/", TEST_PROJECT)
.contentType(MediaType.APPLICATION_JSON)
.content(content)
.session(session);
......@@ -376,7 +393,7 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
String content = "{\"project\":{\"version\":\"12345678901234567890123456\"}}";
RequestBuilder request = patch("/api/projects/" + TEST_PROJECT + "/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.contentType(MediaType.APPLICATION_JSON)
.content(content)
.session(session);
......@@ -391,7 +408,7 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
String content = "{\"project\":{\"version\":\"xxx\"}}";
RequestBuilder request = patch("/api/projects/*/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.contentType(MediaType.APPLICATION_JSON)
.content(content)
.session(session);
......@@ -792,7 +809,7 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
String content = "{\"project\":{\"name\":\"" + invalidName + "\"}}";
RequestBuilder request = patch("/api/projects/" + TEST_PROJECT + "/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.contentType(MediaType.APPLICATION_JSON)
.content(content)
.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