Commit 9b2dd147 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

update background API endpoint implemented

parent 64c818db
......@@ -11,6 +11,8 @@ import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.*;
import org.hibernate.annotations.CascadeType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.graphics.MapCanvasType;
......@@ -373,6 +375,7 @@ public class Project implements Serializable {
addModel(topModel);
}
@JsonIgnore
public void setTopModel(Model topModel) {
setTopModel(topModel.getModelData());
}
......
......@@ -2,6 +2,8 @@ package lcsb.mapviewer.model.map.model;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.*;
import lcsb.mapviewer.model.map.compartment.Compartment;
......@@ -58,6 +60,7 @@ public interface Model {
* @param text
* new model width
*/
@JsonIgnore
void setWidth(String text);
/**
......@@ -65,6 +68,7 @@ public interface Model {
* @param width
* new {@link ModelData#width}
*/
@JsonIgnore
void setWidth(int width);
/**
......@@ -87,6 +91,7 @@ public interface Model {
* @param text
* new model height
*/
@JsonIgnore
void setHeight(String text);
/**
......@@ -94,6 +99,7 @@ public interface Model {
* @param height
* new {@link ModelData#height}
*/
@JsonIgnore
void setHeight(int height);
/**
......
......@@ -11,6 +11,8 @@ import org.apache.logging.log4j.Logger;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
......@@ -179,6 +181,7 @@ public class ModelData implements Serializable {
*/
@Transient
@XmlTransient
@JsonIgnore
private Model model;
/**
......@@ -450,6 +453,7 @@ public class ModelData implements Serializable {
* @see #model
*/
@XmlTransient
@JsonIgnore
public Model getModel() {
if (model == null) {
logger.warn("Model not set in model data.");
......@@ -462,6 +466,7 @@ public class ModelData implements Serializable {
* the model to set
* @see #model
*/
@JsonIgnore
public void setModel(Model model) {
this.model = model;
}
......@@ -530,6 +535,7 @@ public class ModelData implements Serializable {
* @param width
* new {@link #width}
*/
@JsonIgnore
public void setWidth(int width) {
setWidth(Double.valueOf(width));
}
......@@ -556,6 +562,7 @@ public class ModelData implements Serializable {
* @param height
* new {@link #height}
*/
@JsonIgnore
public void setHeight(int height) {
setHeight(Double.valueOf(height));
}
......
......@@ -5,6 +5,8 @@ import javax.persistence.*;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lcsb.mapviewer.common.exception.NotImplementedException;
/**
......@@ -108,6 +110,7 @@ public class ModelSubmodelConnection extends SubmodelConnection {
* the parent model to set
* @see #parentModel
*/
@JsonIgnore
public void setParentModel(Model model) {
setParentModel(model.getModelData());
}
......
......@@ -9,6 +9,8 @@ import org.apache.logging.log4j.Logger;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* This abstract class defines connection between models. It points to submodel
* and describe type of the connection.
......@@ -171,6 +173,7 @@ public abstract class SubmodelConnection implements Serializable {
* submodel to set.
* @see #submodel
*/
@JsonIgnore
public void setSubmodel(Model submodel) {
setSubmodel(submodel.getModelData());
}
......
......@@ -522,6 +522,26 @@ public abstract class BaseRestImpl {
}
}
protected Boolean parseBoolean(Object value, String fieldName) throws QueryException {
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value == null) {
return null;
} else if (value instanceof String) {
if (((String) value).equalsIgnoreCase("")) {
return null;
} else {
return Boolean.parseBoolean((String) value);
}
} else {
throw new QueryException("Invalid " + fieldName + " value: " + value);
}
}
protected Boolean parseBoolean(Object value) throws QueryException {
return parseBoolean(value, "boolean");
}
protected Integer parseInteger(Object value) throws QueryException {
return parseInteger(value, "integer");
}
......
......@@ -193,12 +193,11 @@ public class ProjectController extends BaseController {
@PatchMapping(value = "/{projectId}/backgrounds/{backgroundId}")
public ProjectBackground updateBackground(
@RequestBody String body,
@PathVariable(value = "overlayId") Integer overlayId,
@PathVariable(value = "backgroundId") String projectId)
@PathVariable(value = "backgroundId") Integer backgroundId,
@PathVariable(value = "projectId") String projectId)
throws QueryException, IOException {
Map<String, Object> node = parseBody(body);
Map<String, Object> data = getData(node, "overlay");
return projectController.updateOverlay(projectId, overlayId, data);
return projectController.updateBackground(projectId, backgroundId, node);
}
public ServletContext getContext() {
......
......@@ -788,8 +788,38 @@ public class ProjectRestImpl extends BaseRestImpl {
throw new ObjectNotFoundException("Background does not exist");
}
public ProjectBackground updateOverlay(String projectId, Integer overlayId, Map<String, Object> data) {
throw new NotImplementedException();
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 (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("orderIndex")) {
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);
}
}
......@@ -234,14 +234,6 @@ public class OverlayRestImpl extends BaseRestImpl {
}
}
private boolean parseBoolean(Object value) {
if (value instanceof Boolean) {
return (Boolean) value;
} else {
return "true".equalsIgnoreCase((String) value);
}
}
public Map<String, Object> removeOverlay(String projectId, Integer overlayId)
throws QueryException, IOException {
DataOverlay layout = dataOverlayService.getDataOverlayById(projectId, overlayId);
......
......@@ -29,6 +29,9 @@ import org.springframework.test.web.servlet.RequestBuilder;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.google.gson.JsonParser;
import lcsb.mapviewer.api.projects.ProjectRestImpl;
......@@ -45,6 +48,7 @@ import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.web.serialization.ProjectBackgroundUpdateMixIn;
@RunWith(SpringJUnit4ClassRunner.class)
public class ProjectControllerIntegrationTest extends ControllerIntegrationTest {
......@@ -1027,16 +1031,19 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
ProjectBackground background = new ProjectBackground("weird_title");
background.setId(project.getProjectBackgrounds().get(0).getId());
RequestBuilder request = get("/projects/{projectId}/backgrounds/{backgroundId}", TEST_PROJECT,
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(ProjectBackground.class, ProjectBackgroundUpdateMixIn.class);
RequestBuilder request = patch("/projects/{projectId}/backgrounds/{backgroundId}", TEST_PROJECT,
project.getProjectBackgrounds().get(0).getId())
.session(session)
.content(objectMapper.writeValueAsString(background));
.content(mapper.writeValueAsString(background));
String response = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andDo(document("projects/project_data/update_background",
projectPathParameters(),
responseFields()))
backgroundPathParameters(),
responseFields(getBackgroundFields())))
.andReturn().getResponse().getContentAsString();
ProjectBackground newBackground = objectMapper.readValue(response, new TypeReference<ProjectBackground>() {
......
package lcsb.mapviewer.web.serialization;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.layout.ProjectBackgroundImageLayer;
import lcsb.mapviewer.model.map.layout.ProjectBackgroundStatus;
public abstract class ProjectBackgroundUpdateMixIn {
@JsonIgnore
private Project project;
@JsonIgnore
private ProjectBackgroundStatus status;
@JsonIgnore
private double progress;
@JsonIgnore
public abstract Project getProject();
@JsonIgnore
public abstract ProjectBackgroundStatus getStatus();
@JsonIgnore
public abstract double getProgress();
@JsonIgnore
public abstract List<ProjectBackgroundImageLayer> getProjectBackgroundImageLayer();
}
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