From 2ce891e5c3e62b939646b11a40be8434cdd06148 Mon Sep 17 00:00:00 2001
From: Thomas <gltron3000@gmail.com>
Date: Fri, 22 May 2020 16:55:03 +0200
Subject: [PATCH] Added model performance & checksum

---
 .../java/mozen/business/LayerManager.java     |  7 +++
 .../java/mozen/business/ModelManager.java     |  5 ++
 .../main/java/mozen/model/CustomLayer.java    | 17 +++++-
 mozen/src/main/java/mozen/model/Model.java    | 54 ++++++++++++++++++-
 .../main/java/mozen/utils/DatabaseFiller.java | 15 ++----
 5 files changed, 83 insertions(+), 15 deletions(-)

diff --git a/mozen/src/main/java/mozen/business/LayerManager.java b/mozen/src/main/java/mozen/business/LayerManager.java
index b31a66e..0f9d418 100644
--- a/mozen/src/main/java/mozen/business/LayerManager.java
+++ b/mozen/src/main/java/mozen/business/LayerManager.java
@@ -1,5 +1,8 @@
 package mozen.business;
 
+import java.util.zip.CRC32;
+import java.util.zip.Checksum;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
@@ -33,8 +36,12 @@ public class LayerManager implements ILayerManager{
     if (!isModelAuthor(model, user))
       throw new Exception("Not the author");
 
+    Checksum crc32 = new CRC32();
+    crc32.update(file.getBytes(), 0, file.getBytes().length);
+    
     CustomLayer layer = new CustomLayer();
     layer.setName(name);
+    layer.setChecksum(crc32.getValue());
     layer.setFile(file.getBytes());
     layer.setFileType(file.getContentType());
     layer.setModel(model);
diff --git a/mozen/src/main/java/mozen/business/ModelManager.java b/mozen/src/main/java/mozen/business/ModelManager.java
index dec9495..61a3486 100644
--- a/mozen/src/main/java/mozen/business/ModelManager.java
+++ b/mozen/src/main/java/mozen/business/ModelManager.java
@@ -4,6 +4,8 @@ import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.zip.CRC32;
+import java.util.zip.Checksum;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
@@ -74,6 +76,9 @@ public class ModelManager implements IModelManager {
     if (!isModelAuthor(model, user))
       throw new Exception("Not the author");
 
+    Checksum crc32 = new CRC32();
+    crc32.update(file.getBytes(), 0, file.getBytes().length);
+    model.setChecksum(crc32.getValue());
     model.setFile(file.getBytes());
     model.setFileType(file.getContentType());
 
diff --git a/mozen/src/main/java/mozen/model/CustomLayer.java b/mozen/src/main/java/mozen/model/CustomLayer.java
index 071cb7f..b2e1e21 100644
--- a/mozen/src/main/java/mozen/model/CustomLayer.java
+++ b/mozen/src/main/java/mozen/model/CustomLayer.java
@@ -37,6 +37,10 @@ public class CustomLayer implements Serializable{
 	@Column
   private String fileType;
 
+  @Basic
+	@Column
+  private Long checksum;
+
   @Basic
   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   private Model model;
@@ -44,14 +48,15 @@ public class CustomLayer implements Serializable{
   public CustomLayer() {
   }
 
-  public CustomLayer(Long id, String name, byte[] file, String fileType, Model model) {
+  public CustomLayer(Long id, String name, byte[] file, String fileType, Long checksum, Model model) {
     this.id = id;
     this.name = name;
     this.file = file;
     this.fileType = fileType;
+    this.checksum = checksum;
     this.model = model;
   }
-
+  
   public Long getId() {
     return this.id;
   }
@@ -95,4 +100,12 @@ public class CustomLayer implements Serializable{
     this.model = model;
   }
 
+  public Long getChecksum() {
+    return this.checksum;
+  }
+
+  public void setChecksum(Long checksum) {
+    this.checksum = checksum;
+  }
+
 }
\ No newline at end of file
diff --git a/mozen/src/main/java/mozen/model/Model.java b/mozen/src/main/java/mozen/model/Model.java
index 70eab80..507c82d 100644
--- a/mozen/src/main/java/mozen/model/Model.java
+++ b/mozen/src/main/java/mozen/model/Model.java
@@ -64,6 +64,18 @@ public class Model implements Serializable{
 	@Column
   private int votes;
 
+  @Basic
+	@Column
+  private double performance;
+
+  @Basic
+	@Column
+  private String performanceUnit;
+
+  @Basic
+	@Column
+  private int parameterCount;
+
   @Lob
 	@Column(columnDefinition="BLOB")
   private byte[] file;
@@ -72,6 +84,10 @@ public class Model implements Serializable{
 	@Column
   private String fileType;
 
+  @Basic
+	@Column
+  private Long checksum;
+
   @Basic
   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
   private User author;
@@ -89,7 +105,7 @@ public class Model implements Serializable{
   public Model() {
   }
 
-  public Model(Long id, String name, String shortDescription, String longDescription, Date added, Date lastModified, int votes, byte[] file, String fileType, User author, Set<Tag> tags, Set<CustomLayer> customLayers) {
+  public Model(Long id, String name, String shortDescription, String longDescription, Date added, Date lastModified, int votes, double performance, String performanceUnit, int parameterCount, byte[] file, String fileType, Long checksum, User author, Set<Tag> tags, Set<CustomLayer> customLayers) {
     this.id = id;
     this.name = name;
     this.shortDescription = shortDescription;
@@ -97,8 +113,12 @@ public class Model implements Serializable{
     this.added = added;
     this.lastModified = lastModified;
     this.votes = votes;
+    this.performance = performance;
+    this.performanceUnit = performanceUnit;
+    this.parameterCount = parameterCount;
     this.file = file;
     this.fileType = fileType;
+    this.checksum = checksum;
     this.author = author;
     this.tags = tags;
     this.customLayers = customLayers;
@@ -201,4 +221,36 @@ public class Model implements Serializable{
     this.fileType = fileType;
   }
 
+  public double getPerformance() {
+    return this.performance;
+  }
+
+  public void setPerformance(double performance) {
+    this.performance = performance;
+  }
+
+  public String getPerformanceUnit() {
+    return this.performanceUnit;
+  }
+
+  public void setPerformanceUnit(String performanceUnit) {
+    this.performanceUnit = performanceUnit;
+  }
+
+  public int getParameterCount() {
+    return this.parameterCount;
+  }
+
+  public void setParameterCount(int parameterCount) {
+    this.parameterCount = parameterCount;
+  }
+  
+  public Long getChecksum() {
+    return this.checksum;
+  }
+
+  public void setChecksum(Long checksum) {
+    this.checksum = checksum;
+  }
+
 }
\ No newline at end of file
diff --git a/mozen/src/main/java/mozen/utils/DatabaseFiller.java b/mozen/src/main/java/mozen/utils/DatabaseFiller.java
index ac405fa..3734bd0 100644
--- a/mozen/src/main/java/mozen/utils/DatabaseFiller.java
+++ b/mozen/src/main/java/mozen/utils/DatabaseFiller.java
@@ -16,7 +16,6 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import mozen.model.CustomLayer;
 import mozen.model.Model;
 import mozen.model.Role;
 import mozen.model.Tag;
@@ -68,21 +67,13 @@ public class DatabaseFiller {
     m1.setShortDescription("Short description for model testo");
     m1.setLongDescription("# README \n model testo");
     m1.setVotes(0);
+    m1.setParameterCount(10);
+    m1.setPerformance(98.5);
+    m1.setPerformanceUnit("accuracy");
     m1.setAdded(new Date());
     m1.setLastModified(new Date());
     m1.setTags(tagLoader());
 
-    Set<CustomLayer> m1layers = new HashSet<CustomLayer>();
-    CustomLayer l1 = new CustomLayer();
-    l1.setName("super layer 1");
-
-    CustomLayer l2 = new CustomLayer();
-    l2.setName("super layer 2");
-
-    m1layers.add(l1);
-    m1layers.add(l2);
-    m1.setCustomLayers(m1layers);
-
     userRepo.save(u1);
     userRepo.save(u2);
     userRepo.save(admin);
-- 
GitLab