From 31163db5b39913f7733ab510c01fe285828b30c8 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 28 Oct 2019 14:34:56 +0100
Subject: [PATCH] rest API uses new data structures for logs

---
 .../lcsb/mapviewer/model/ProjectLogEntry.java |  4 +
 .../api/projects/ProjectRestImpl.java         | 74 +++++++++++--------
 2 files changed, 47 insertions(+), 31 deletions(-)

diff --git a/model/src/main/java/lcsb/mapviewer/model/ProjectLogEntry.java b/model/src/main/java/lcsb/mapviewer/model/ProjectLogEntry.java
index cf2acaf40a..719176949f 100644
--- a/model/src/main/java/lcsb/mapviewer/model/ProjectLogEntry.java
+++ b/model/src/main/java/lcsb/mapviewer/model/ProjectLogEntry.java
@@ -113,4 +113,8 @@ public class ProjectLogEntry implements Serializable {
     this.type = type;
   }
 
+  public int getId() {
+    return id;
+  }
+
 }
diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java
index 397ba21b2f..193032c8b0 100644
--- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/ProjectRestImpl.java
@@ -17,11 +17,13 @@ import lcsb.mapviewer.annotation.services.MeSHParser;
 import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
 import lcsb.mapviewer.api.*;
 import lcsb.mapviewer.api.projects.models.publications.PublicationsRestImpl;
+import lcsb.mapviewer.common.comparator.StringComparator;
 import lcsb.mapviewer.common.exception.InvalidArgumentException;
 import lcsb.mapviewer.common.exception.NotImplementedException;
 import lcsb.mapviewer.converter.Converter;
 import lcsb.mapviewer.converter.zip.*;
 import lcsb.mapviewer.model.Project;
+import lcsb.mapviewer.model.ProjectLogEntry;
 import lcsb.mapviewer.model.cache.FileEntry;
 import lcsb.mapviewer.model.cache.UploadedFileEntry;
 import lcsb.mapviewer.model.graphics.MapCanvasType;
@@ -578,30 +580,13 @@ public class ProjectRestImpl extends BaseRestImpl {
   }
 
   private List<LogEntry> getEntries(Project project, String level) {
-    throw new NotImplementedException();
-//    List<LogEntry> result = new ArrayList<>();
-//    int id = 0;
-//    for (String s : project.getWarnings()) {
-//      if (!s.isEmpty()) {
-//        if (level.equalsIgnoreCase("warning") || level.equals("")) {
-//          result.add(new LogEntry(id, s, "WARNING"));
-//        }
-//        id++;
-//      }
-//    }
-//    String errors = project.getErrors();
-//    if (errors == null) {
-//      errors = "";
-//    }
-//    for (String s : errors.split("\n")) {
-//      if (!s.isEmpty()) {
-//        if (level.equalsIgnoreCase("error") || level.equals("")) {
-//          result.add(new LogEntry(id, s, "ERROR"));
-//        }
-//        id++;
-//      }
-//    }
-//    return result;
+    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)
@@ -612,13 +597,25 @@ public class ProjectRestImpl extends BaseRestImpl {
     } 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);
     }
@@ -714,6 +711,12 @@ public class ProjectRestImpl extends BaseRestImpl {
 
   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;
@@ -730,13 +733,22 @@ public class ProjectRestImpl extends BaseRestImpl {
     private static final long serialVersionUID = 1L;
     public Integer id;
     public String content;
-    @SuppressWarnings("unused")
     public String level;
-
-    public LogEntry(int id, String content, String level) {
-      this.id = id;
-      this.content = content;
-      this.level = level;
+    public String type;
+    public String objectIdentifier;
+    public String objectClass;
+    public String mapName;
+    public String source;
+
+    public LogEntry(ProjectLogEntry s) {
+      this.id = s.getId();
+      this.content = s.getContent();
+      this.level = s.getSeverity();
+      this.type = s.getType().toString();
+      this.objectIdentifier = s.getObjectIdentifier();
+      this.objectClass = s.getObjectClass();
+      this.mapName = s.getMapName();
+      this.source = s.getSource();
     }
   }
 
-- 
GitLab