Commit 2209dcdb authored by Piotr Gawron's avatar Piotr Gawron
Browse files

get logs moved to controller

parent d6612a42
......@@ -6,6 +6,8 @@ import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -43,6 +45,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.converter.Converter;
import lcsb.mapviewer.converter.zip.ZipEntryFile;
......@@ -543,7 +546,96 @@ public class ProjectController extends BaseController {
@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);
Project project = getProject(projectId);
Comparator<LogEntry> comparator = getComparatorForColumn(sortColumn, sortOrder);
List<LogEntry> resultList = new ArrayList<>();
List<LogEntry> logEntries = getEntries(project, level);
List<LogEntry> filteredList = new ArrayList<>();
search = search.toLowerCase();
for (LogEntry entry : logEntries) {
if (isSearchResult(entry, search)) {
filteredList.add(entry);
}
}
if (comparator != null) {
filteredList.sort(comparator);
}
int index = 0;
for (LogEntry entry : filteredList) {
if (index >= start && index < start + length) {
resultList.add(entry);
}
index++;
}
LogPage result = new LogPage();
result.data = resultList;
result.totalSize = logEntries.size();
result.filteredSize = filteredList.size();
result.start = start;
result.length = resultList.size();
return result;
}
private boolean isSearchResult(LogEntry entry, String search) {
if (search == null || search.isEmpty()) {
return true;
}
if (entry.content.toLowerCase().contains(search) || entry.id.toString().contains(search)) {
return true;
}
return false;
}
private List<LogEntry> getEntries(Project project, String level) {
List<LogEntry> result = new ArrayList<>();
for (ProjectLogEntry s : project.getLogEntries()) {
if (level == null || level.equals("*") || level.equalsIgnoreCase(s.getSeverity())) {
result.add(new LogEntry(s));
}
}
return result;
}
private Comparator<LogEntry> getComparatorForColumn(LogSortColumn sortColumnEnum, String sortOrder)
throws QueryException {
final int orderFactor;
if (sortOrder.toLowerCase().equals("desc")) {
orderFactor = -1;
} else {
orderFactor = 1;
}
StringComparator stringComparator = new StringComparator();
if (sortColumnEnum == null) {
return null;
} else if (sortColumnEnum.equals(LogSortColumn.ID)) {
return (o1, o2) -> o1.id.compareTo(o2.id) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.CONTENT)) {
return (o1, o2) -> o1.content.compareTo(o2.content) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.LEVEL)) {
return (o1, o2) -> o1.level.compareTo(o2.level) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.MAP_NAME)) {
return (o1, o2) -> stringComparator.compare(o1.mapName, o2.mapName) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.OBJECT_CLASS)) {
return (o1, o2) -> stringComparator.compare(o1.objectClass, o2.objectClass) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.OBJECT_IDENTIFIER)) {
return (o1, o2) -> stringComparator.compare(o1.objectIdentifier, o2.objectIdentifier) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.SOURCE)) {
return (o1, o2) -> stringComparator.compare(o1.source, o2.source) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.TYPE)) {
return (o1, o2) -> stringComparator.compare(o1.type, o2.type) * orderFactor;
} else {
throw new QueryException("Sort order not implemented for: " + sortColumnEnum);
}
}
@PreAuthorize("hasAnyAuthority('IS_ADMIN', 'READ_PROJECT:' + #projectId)")
......
......@@ -2,7 +2,6 @@ package lcsb.mapviewer.api.projects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -14,12 +13,7 @@ import org.springframework.transaction.annotation.Transactional;
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;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.layout.ProjectBackground;
import lcsb.mapviewer.model.map.species.Element;
......@@ -81,100 +75,6 @@ public class ProjectRestImpl extends BaseRestImpl {
return getProject(projectId);
}
public LogPage getLogs(String projectId, String level, Integer start, Integer length,
LogSortColumn sortColumn, String sortOrder, String search) throws QueryException {
Project project = getProjectByProjectId(projectId);
Comparator<LogEntry> comparator = getComparatorForColumn(sortColumn, sortOrder);
List<LogEntry> resultList = new ArrayList<>();
List<LogEntry> logEntries = getEntries(project, level);
List<LogEntry> filteredList = new ArrayList<>();
search = search.toLowerCase();
for (LogEntry entry : logEntries) {
if (isSearchResult(entry, search)) {
filteredList.add(entry);
}
}
if (comparator != null) {
filteredList.sort(comparator);
}
int index = 0;
for (LogEntry entry : filteredList) {
if (index >= start && index < start + length) {
resultList.add(entry);
}
index++;
}
LogPage result = new LogPage();
result.data = resultList;
result.totalSize = logEntries.size();
result.filteredSize = filteredList.size();
result.start = start;
result.length = resultList.size();
return result;
}
private boolean isSearchResult(LogEntry entry, String search) {
if (search == null || search.isEmpty()) {
return true;
}
if (entry.content.toLowerCase().contains(search) || entry.id.toString().contains(search)) {
return true;
}
return false;
}
private List<LogEntry> getEntries(Project project, String level) {
List<LogEntry> result = new ArrayList<>();
for (ProjectLogEntry s : project.getLogEntries()) {
if (level == null || level.equals("*") || level.equalsIgnoreCase(s.getSeverity())) {
result.add(new LogEntry(s));
}
}
return result;
}
private Comparator<LogEntry> getComparatorForColumn(LogSortColumn sortColumnEnum, String sortOrder)
throws QueryException {
final int orderFactor;
if (sortOrder.toLowerCase().equals("desc")) {
orderFactor = -1;
} else {
orderFactor = 1;
}
StringComparator stringComparator = new StringComparator();
if (sortColumnEnum == null) {
return null;
} else if (sortColumnEnum.equals(LogSortColumn.ID)) {
return (o1, o2) -> o1.id.compareTo(o2.id) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.CONTENT)) {
return (o1, o2) -> o1.content.compareTo(o2.content) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.LEVEL)) {
return (o1, o2) -> o1.level.compareTo(o2.level) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.MAP_NAME)) {
return (o1, o2) -> stringComparator.compare(o1.mapName, o2.mapName) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.OBJECT_CLASS)) {
return (o1, o2) -> stringComparator.compare(o1.objectClass, o2.objectClass) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.OBJECT_IDENTIFIER)) {
return (o1, o2) -> stringComparator.compare(o1.objectIdentifier, o2.objectIdentifier) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.SOURCE)) {
return (o1, o2) -> stringComparator.compare(o1.source, o2.source) * orderFactor;
} else if (sortColumnEnum.equals(LogSortColumn.TYPE)) {
return (o1, o2) -> stringComparator.compare(o1.type, o2.type) * orderFactor;
} else {
throw new QueryException("Sort order not implemented for: " + sortColumnEnum);
}
}
public List<Map<String, Object>> getSubmapConnections(String projectId) throws QueryException {
Project project = getProjectByProjectId(projectId);
Map<ElementProperty, List<? extends Object>> properties = new HashMap<>();
......
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