Commit d6612a42 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

LogSortColumn is deserialized by spring

parent c700f419
package lcsb.mapviewer.api.projects;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
......@@ -24,8 +25,10 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -34,6 +37,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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;
......@@ -482,6 +486,51 @@ public class ProjectController extends BaseController {
}
}
public static enum LogSortColumn {
ID,
LEVEL,
TYPE,
OBJECT_IDENTIFIER,
OBJECT_CLASS,
MAP_NAME,
SOURCE,
CONTENT;
@JsonCreator
public static LogSortColumn fromValue(String value) {
switch (value) {
case ("id"):
return LogSortColumn.ID;
case ("level"):
return LogSortColumn.LEVEL;
case ("objectIdentifier"):
return LogSortColumn.OBJECT_IDENTIFIER;
case ("objectClass"):
return LogSortColumn.OBJECT_CLASS;
case ("mapName"):
return LogSortColumn.MAP_NAME;
case ("source"):
return LogSortColumn.SOURCE;
case ("content"):
return LogSortColumn.CONTENT;
default:
return null;
}
}
}
public class LogSortColumnConverter extends PropertyEditorSupport {
public void setAsText(final String text) throws IllegalArgumentException {
setValue(LogSortColumn.fromValue(text));
}
}
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(LogSortColumn.class, new LogSortColumnConverter());
}
@PreAuthorize("hasAuthority('IS_ADMIN')" +
" or hasAuthority('IS_CURATOR') and hasAuthority('READ_PROJECT:' + #projectId)")
......@@ -489,9 +538,9 @@ public class ProjectController extends BaseController {
public LogPage getLogs(
@PathVariable(value = "projectId") String projectId,
@RequestParam(value = "level", defaultValue = "*") String level,
@RequestParam(value = "start", defaultValue = "0") String start,
@RequestParam(value = "start", defaultValue = "0") Integer start,
@RequestParam(value = "length", defaultValue = "10") Integer length,
@RequestParam(value = "sortColumn", defaultValue = "id") String sortColumn,
@RequestParam(value = "sortColumn", defaultValue = "id") LogSortColumn sortColumn,
@RequestParam(value = "sortOrder", defaultValue = "asc") String sortOrder,
@RequestParam(value = "search", defaultValue = "") String search) throws QueryException {
return projectController.getLogs(projectId, level, start, length, sortColumn, sortOrder, search);
......
......@@ -16,6 +16,7 @@ import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.OperationNotAllowedException;
import lcsb.mapviewer.api.projects.ProjectController.LogEntry;
import lcsb.mapviewer.api.projects.ProjectController.LogPage;
import lcsb.mapviewer.api.projects.ProjectController.LogSortColumn;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.ProjectLogEntry;
......@@ -80,14 +81,12 @@ public class ProjectRestImpl extends BaseRestImpl {
return getProject(projectId);
}
public LogPage getLogs(String projectId, String level, String startString, Integer length,
String sortColumn, String sortOrder, String search) throws QueryException {
public LogPage getLogs(String projectId, String level, Integer start, Integer length,
LogSortColumn sortColumn, String sortOrder, String search) throws QueryException {
Project project = getProjectByProjectId(projectId);
LogSortColumn sortColumnEnum = getSortOrderColumn(sortColumn);
Comparator<LogEntry> comparator = getComparatorForColumn(sortColumnEnum, sortOrder);
Comparator<LogEntry> comparator = getComparatorForColumn(sortColumn, sortOrder);
Integer start = Math.max(0, Integer.valueOf(startString));
List<LogEntry> resultList = new ArrayList<>();
List<LogEntry> logEntries = getEntries(project, level);
......@@ -176,18 +175,6 @@ public class ProjectRestImpl extends BaseRestImpl {
}
}
private LogSortColumn getSortOrderColumn(String sortColumn) throws QueryException {
if (!sortColumn.isEmpty()) {
for (LogSortColumn type : LogSortColumn.values()) {
if (type.commonName.toLowerCase().equals(sortColumn.toLowerCase())) {
return type;
}
}
throw new QueryException("Unknown sortColumn: " + sortColumn);
}
return null;
}
public List<Map<String, Object>> getSubmapConnections(String projectId) throws QueryException {
Project project = getProjectByProjectId(projectId);
Map<ElementProperty, List<? extends Object>> properties = new HashMap<>();
......@@ -214,23 +201,6 @@ public class ProjectRestImpl extends BaseRestImpl {
return result;
}
private enum LogSortColumn {
ID("id"),
LEVEL("level"),
TYPE("type"),
OBJECT_IDENTIFIER("objectIdentifier"),
OBJECT_CLASS("objectClass"),
MAP_NAME("mapName"),
SOURCE("source"),
CONTENT("content");
private String commonName;
LogSortColumn(String commonName) {
this.commonName = commonName;
}
}
public List<ProjectBackground> getBackgrounds(String projectId) throws ObjectNotFoundException {
List<ProjectBackground> result = getProjectByProjectId(projectId).getProjectBackgrounds();
for (ProjectBackground projectBackground : result) {
......
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