Commit 4a1890e9 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

update background moved to controller`

parent 743f5a2e
...@@ -3,6 +3,7 @@ package lcsb.mapviewer.api.projects; ...@@ -3,6 +3,7 @@ package lcsb.mapviewer.api.projects;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
...@@ -27,7 +28,6 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -27,7 +28,6 @@ import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException; import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseController; import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.model.Project; import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.cache.FileEntry; import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.graphics.MapCanvasType; import lcsb.mapviewer.model.graphics.MapCanvasType;
...@@ -288,16 +288,54 @@ public class ProjectController extends BaseController { ...@@ -288,16 +288,54 @@ public class ProjectController extends BaseController {
return projectController.removeBackground(projectId, backgroundId); return projectController.removeBackground(projectId, backgroundId);
} }
static class BackgroundDTO {
public String description;
public Integer id;
public String name;
public Integer order;
public Boolean defaultOverlay;
public String creator;
}
@PreAuthorize("hasAuthority('IS_ADMIN')" + @PreAuthorize("hasAuthority('IS_ADMIN')" +
" or hasAuthority('IS_CURATOR') and hasAuthority('WRITE_PROJECT:' + #projectId)") " or hasAuthority('IS_CURATOR') and hasAuthority('WRITE_PROJECT:' + #projectId)")
@PatchMapping(value = "/{projectId}/backgrounds/{backgroundId}") @PatchMapping(value = "/{projectId}/backgrounds/{backgroundId}")
public ProjectBackground updateBackground( public ProjectBackground updateBackground(
@RequestBody String body, @RequestBody BackgroundDTO data,
@PathVariable(value = "backgroundId") Integer backgroundId, @PathVariable(value = "backgroundId") Integer backgroundId,
@PathVariable(value = "projectId") String projectId) @PathVariable(value = "projectId") String projectId)
throws QueryException, IOException { throws QueryException, IOException {
Map<String, Object> node = parseBody(body); ProjectBackground background = getBackgroundById(projectId, backgroundId);
return projectController.updateBackground(projectId, backgroundId, node); if (data.description != null) {
background.setDescription(data.description);
}
if (data.id != null) {
if (!Objects.equals(data.id, backgroundId)) {
throw new QueryException("cannot change id");
}
}
if (data.name != null) {
if (data.name.trim().isEmpty()) {
throw new QueryException("name cannot be empty");
}
background.setName(data.name);
}
if (data.order != null) {
background.setOrderIndex(data.order);
}
if (data.defaultOverlay != null) {
background.setDefaultOverlay(data.defaultOverlay);
}
if (data.creator != null) {
User user = userService.getUserByLogin(data.creator);
if (user == null) {
throw new QueryException("overlay creator must be defined");
}
background.setCreator(user);
}
projectService.updateProject(background.getProject());
return getBackgroundById(projectId, backgroundId);
} }
public ServletContext getContext() { public ServletContext getContext() {
......
...@@ -606,38 +606,4 @@ public class ProjectRestImpl extends BaseRestImpl { ...@@ -606,38 +606,4 @@ public class ProjectRestImpl extends BaseRestImpl {
throw new ObjectNotFoundException("Background does not exist"); throw new ObjectNotFoundException("Background does not exist");
} }
public ProjectBackground updateBackground(String projectId, Integer overlayId, Map<String, Object> data)
throws QueryException {
ProjectBackground background = getBackgroundById(projectId, overlayId);
for (String key : data.keySet()) {
Object value = data.get(key);
if (key.equalsIgnoreCase("description")) {
background.setDescription((String) value);
} else if (key.equalsIgnoreCase("id")) {
if (!Objects.equals(value, overlayId)) {
throw new QueryException("cannot change id");
}
} else if (key.equalsIgnoreCase("name")) {
if (value == null || ((String) value).trim().isEmpty()) {
throw new QueryException("name cannot be empty");
}
background.setName((String) value);
} else if (key.equalsIgnoreCase("order")) {
background.setOrderIndex(parseInteger(value));
} else if (key.equalsIgnoreCase("defaultOverlay")) {
background.setDefaultOverlay(parseBoolean(value));
} else if (key.equalsIgnoreCase("creator")) {
if ("".equals(value)) {
throw new QueryException("overlay creator must be defined");
} else if (value != null) {
background.setCreator(getUserService().getUserByLogin((String) value));
}
} else {
throw new QueryException("Unknown parameter: " + key);
}
}
getProjectService().updateProject(background.getProject());
return getBackgroundById(projectId, overlayId);
}
} }
...@@ -1051,6 +1051,7 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest ...@@ -1051,6 +1051,7 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
RequestBuilder request = patch("/api/projects/{projectId}/backgrounds/{backgroundId}", TEST_PROJECT, RequestBuilder request = patch("/api/projects/{projectId}/backgrounds/{backgroundId}", TEST_PROJECT,
project.getProjectBackgrounds().get(0).getId()) project.getProjectBackgrounds().get(0).getId())
.contentType(MediaType.APPLICATION_JSON)
.session(session) .session(session)
.content(mapper.writeValueAsString(background)); .content(mapper.writeValueAsString(background));
......
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