Rework the loop state
[clamp.git] / src / main / java / org / onap / clamp / loop / log / LoopLog.java
index 7b7fe1b..3feff25 100644 (file)
@@ -40,10 +40,10 @@ import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
 import javax.persistence.Table;
+
 import org.onap.clamp.loop.Loop;
 
 /**
- *
  * This class holds the logs created by the Clamp Backend. The Instant is always
  * rounded to the nearest second as the nano seconds can't be stored in the
  * database. The logs can be therefore exposed to the UI or the client doing
@@ -52,9 +52,9 @@ import org.onap.clamp.loop.Loop;
  */
 @Entity
 @Table(name = "loop_logs")
-public class LoopLog implements Serializable {
+public class LoopLog implements Serializable, Comparable<LoopLog> {
     /**
-     *
+     * The serial version ID.
      */
     private static final long serialVersionUID = 1988276670074437631L;
 
@@ -69,7 +69,11 @@ public class LoopLog implements Serializable {
     private LogType logType;
 
     @Expose
-    @Column(name = "message", nullable = false)
+    @Column(name = "log_component", nullable = false)
+    private String logComponent;
+
+    @Expose
+    @Column(name = "message", columnDefinition = "MEDIUMTEXT", nullable = false)
     private String message;
 
     @ManyToOne(fetch = FetchType.LAZY)
@@ -80,6 +84,16 @@ public class LoopLog implements Serializable {
     @Column(name = "log_instant", nullable = false)
     private Instant logInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS);
 
+    public LoopLog() {
+    }
+
+    public LoopLog(String message, LogType logType, String logComponent, Loop loop) {
+        this.message = message;
+        this.logType = logType;
+        this.loop = loop;
+        this.logComponent = logComponent;
+    }
+
     public Long getId() {
         return id;
     }
@@ -120,6 +134,14 @@ public class LoopLog implements Serializable {
         this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS);
     }
 
+    public String getLogComponent() {
+        return logComponent;
+    }
+
+    public void setLogComponent(String logComponent) {
+        this.logComponent = logComponent;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -130,19 +152,38 @@ public class LoopLog implements Serializable {
 
     @Override
     public boolean equals(Object obj) {
-        if (this == obj)
+        if (this == obj) {
             return true;
-        if (obj == null)
+        }
+        if (obj == null) {
             return false;
-        if (getClass() != obj.getClass())
+        }
+        if (getClass() != obj.getClass()) {
             return false;
+        }
         LoopLog other = (LoopLog) obj;
         if (id == null) {
-            if (other.id != null)
+            if (other.id != null) {
                 return false;
-        } else if (!id.equals(other.id))
+            }
+        } else if (!id.equals(other.id)) {
             return false;
+        }
         return true;
     }
 
+    @Override
+    public int compareTo(LoopLog arg0) {
+        // Reverse it, so that by default we have the latest
+        if (getId() == null) {
+            return 1;
+        }
+        if (arg0.getId() == null) {
+            return -1;
+        }
+
+        return arg0.getId().compareTo(this.getId());
+
+    }
+
 }