From 7128f32f9bd5d05c9bb75bdf7645b36437c4505b Mon Sep 17 00:00:00 2001
From: kfilippopolitis <kostasfilippop@gmail.com>
Date: Wed, 16 Nov 2022 19:06:49 +0200
Subject: [PATCH] Now the dataset enumeration will be updated even if it not
 present in the first level variables.

---
 .../hbp/mip/controllers/PathologiesAPI.java   | 12 ++++++-
 .../mip/models/DTOs/MetadataHierarchyDTO.java | 33 +++++++++++++++++++
 .../eu/hbp/mip/models/DTOs/PathologyDTO.java  |  7 ++--
 .../hbp/mip/services/ExperimentService.java   |  6 ++--
 4 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/src/main/java/eu/hbp/mip/controllers/PathologiesAPI.java b/src/main/java/eu/hbp/mip/controllers/PathologiesAPI.java
index e6a2de685..8949e9c5d 100644
--- a/src/main/java/eu/hbp/mip/controllers/PathologiesAPI.java
+++ b/src/main/java/eu/hbp/mip/controllers/PathologiesAPI.java
@@ -7,6 +7,7 @@ import eu.hbp.mip.models.DTOs.MetadataHierarchyDTO;
 import eu.hbp.mip.models.DTOs.PathologyDTO;
 import eu.hbp.mip.services.ActiveUserService;
 import eu.hbp.mip.utils.*;
+import eu.hbp.mip.utils.Exceptions.InternalServerError;
 import io.swagger.annotations.Api;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.ResponseEntity;
@@ -53,7 +54,16 @@ public class PathologiesAPI {
 
         List<PathologyDTO> pathologyDTOS = new ArrayList<>();
         for (String pathology : mipEnginePathologyAttributes.keySet()) {
-            pathologyDTOS.add(new PathologyDTO(pathology, mipEnginePathologyAttributes.get(pathology), datasetsPerPathology.get(pathology)));
+            PathologyDTO newPathology;
+            try {
+                newPathology = new PathologyDTO(pathology, mipEnginePathologyAttributes.get(pathology), datasetsPerPathology.get(pathology));
+            }
+            catch(InternalServerError e) {
+                logger.LogUserAction(e.getMessage());
+                continue;
+            }
+
+            pathologyDTOS.add(newPathology);
         }
 
         // If authentication is disabled return everything
diff --git a/src/main/java/eu/hbp/mip/models/DTOs/MetadataHierarchyDTO.java b/src/main/java/eu/hbp/mip/models/DTOs/MetadataHierarchyDTO.java
index 200523a91..fbfd8bd34 100644
--- a/src/main/java/eu/hbp/mip/models/DTOs/MetadataHierarchyDTO.java
+++ b/src/main/java/eu/hbp/mip/models/DTOs/MetadataHierarchyDTO.java
@@ -95,4 +95,37 @@ public class MetadataHierarchyDTO {
         }
         this.groups = updated_groups;
     }
+
+    public boolean isDatasetCDEPresent(){
+        if (this.variables != null) {
+            for (CommonDataElement variable : this.variables) {
+                if (variable.code.equals("dataset")){
+                    return true;
+                }
+            }
+
+        }
+        if (this.groups != null) {
+            for (MetadataHierarchyDTO group: this.groups){
+                if (group.isDatasetCDEPresent()){
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    public void updateDatasetCde(List<PathologyDTO.EnumerationDTO> pathologyDatasetDTOS){
+        if (this.variables != null) {
+            List<MetadataHierarchyDTO.CommonDataElement> variables = this.variables;
+            variables.stream().filter(cde -> cde.getCode().equals("dataset")).
+                    findAny().ifPresent(cde -> cde.setEnumerations(pathologyDatasetDTOS));
+        }
+
+        if (this.groups != null) {
+            for (MetadataHierarchyDTO group: this.groups){
+                group.updateDatasetCde(pathologyDatasetDTOS);
+            }
+        }
+    }
 }
diff --git a/src/main/java/eu/hbp/mip/models/DTOs/PathologyDTO.java b/src/main/java/eu/hbp/mip/models/DTOs/PathologyDTO.java
index e47a27a9f..de85a0fa2 100644
--- a/src/main/java/eu/hbp/mip/models/DTOs/PathologyDTO.java
+++ b/src/main/java/eu/hbp/mip/models/DTOs/PathologyDTO.java
@@ -1,6 +1,7 @@
 package eu.hbp.mip.models.DTOs;
 
 import com.google.gson.annotations.SerializedName;
+import eu.hbp.mip.utils.Exceptions.InternalServerError;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 
@@ -34,10 +35,8 @@ public class PathologyDTO {
 
     public PathologyDTO(String pathology, MIPEngineAttributesDTO mipEngineAttributesDTO, List<EnumerationDTO> pathologyDatasetDTOS) {
         MetadataHierarchyDTO metadataHierarchyDTO = mipEngineAttributesDTO.getProperties().get("cdes").get(0);
-        List<MetadataHierarchyDTO.CommonDataElement> variables = metadataHierarchyDTO.getVariables();
-        variables.stream().filter(cde -> cde.getCode().equals("dataset")).
-                findAny().ifPresent(cde -> cde.setEnumerations(pathologyDatasetDTOS));
-        metadataHierarchyDTO.setVariables(variables);
+        if (!metadataHierarchyDTO.isDatasetCDEPresent()) throw new InternalServerError("CommonDataElement Dataset was not present in the pathology:" + pathology);
+        metadataHierarchyDTO.updateDatasetCde(pathologyDatasetDTOS);
 
         List<String> pathology_info = Arrays.asList(pathology.split(":", 2));
         this.code = pathology_info.get(0);
diff --git a/src/main/java/eu/hbp/mip/services/ExperimentService.java b/src/main/java/eu/hbp/mip/services/ExperimentService.java
index de27f46fe..a1c16a546 100644
--- a/src/main/java/eu/hbp/mip/services/ExperimentService.java
+++ b/src/main/java/eu/hbp/mip/services/ExperimentService.java
@@ -152,7 +152,7 @@ public class ExperimentService {
         //Checking if check (POST) /experiments has proper input.
         checkPostExperimentProperInput(experimentDTO, logger);
 
-        // Get the type and name of algorithm
+        // Get the type of algorithm
         String algorithmType = experimentDTO.getAlgorithm().getType();
 
         if (algorithmType == null) {
@@ -190,9 +190,11 @@ public class ExperimentService {
         //Checking if check (POST) /experiments has proper input.
         checkPostExperimentProperInput(experimentDTO, logger);
 
-        // Get the type and name of algorithm
+        // Get the type of algorithm
         String algorithmType = experimentDTO.getAlgorithm().getType();
 
+        experimentDTO.setUuid(UUID.randomUUID());
+
         if (algorithmType.equals("workflow")) {
             logger.LogUserAction("You can not run workflow algorithms transiently.");
             throw new BadRequestException("You can not run workflow algorithms transiently.");
-- 
GitLab