diff --git a/src/main/java/eu/hbp/mip/controllers/PathologiesAPI.java b/src/main/java/eu/hbp/mip/controllers/PathologiesAPI.java index e53d8d354c98f36db958492e72302612e806878c..8949e9c5d081d57ec0e4f1bc69f2882383e4efd9 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; @@ -15,8 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import java.io.IOException; -import java.net.ConnectException; import java.util.*; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @@ -49,17 +48,23 @@ public class PathologiesAPI { Logger logger = new Logger(activeUserService.getActiveUser().getUsername(), "(GET) /pathologies"); logger.LogUserAction("Loading pathologies ..."); - Map<String, List<PathologyDTO.PathologyDatasetDTO>> datasetsPerPathology = getMIPEngineDatasetsPerPathology(logger); - System.out.println(datasetsPerPathology); + Map<String, List<PathologyDTO.EnumerationDTO>> datasetsPerPathology = getMIPEngineDatasetsPerPathology(logger); Map<String, MIPEngineAttributesDTO> mipEnginePathologyAttributes = getMIPEnginePathologyAttributes(logger); - System.out.println(mipEnginePathologyAttributes); 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); } - System.out.println(pathologyDTOS); // If authentication is disabled return everything if (!authenticationIsEnabled) { @@ -71,7 +76,7 @@ public class PathologiesAPI { return ResponseEntity.ok().body(gson.toJson(ClaimUtils.getAuthorizedPathologies(logger, authentication, pathologyDTOS))); } - public Map<String, List<PathologyDTO.PathologyDatasetDTO>> getMIPEngineDatasetsPerPathology(Logger logger) { + public Map<String, List<PathologyDTO.EnumerationDTO>> getMIPEngineDatasetsPerPathology(Logger logger) { Map<String, Map<String, MetadataHierarchyDTO.CommonDataElement>> mipEngineCDEsMetadata; // Get MIPEngine algorithms try { @@ -87,15 +92,17 @@ public class PathologiesAPI { return null; } - Map<String, List<PathologyDTO.PathologyDatasetDTO>> datasetsPerPathology = new HashMap<>(); + Map<String, List<PathologyDTO.EnumerationDTO>> datasetsPerPathology = new HashMap<>(); mipEngineCDEsMetadata.forEach( (pathology, cdePerDataset) -> { - List<PathologyDTO.PathologyDatasetDTO> pathologyDatasetDTOS = new ArrayList<>(); - cdePerDataset.forEach((dataset, cde) -> pathologyDatasetDTOS.add(new PathologyDTO.PathologyDatasetDTO(dataset, cde.getLabel()))); + List<PathologyDTO.EnumerationDTO> pathologyDatasetDTOS = new ArrayList<>(); + Map datasetEnumerations = (Map) cdePerDataset.get("dataset").getEnumerations(); + datasetEnumerations.forEach((code, label) -> pathologyDatasetDTOS.add(new PathologyDTO.EnumerationDTO((String) code, (String) label))); datasetsPerPathology.put(pathology, pathologyDatasetDTOS); }); + return datasetsPerPathology; } @@ -115,6 +122,14 @@ public class PathologiesAPI { return null; } - return mipEnginePathologyAttributes; + Map<String, MIPEngineAttributesDTO> mipEnginePathologyAttributesWithProperEnums = new HashMap<>(); + for (Map.Entry<String, MIPEngineAttributesDTO> entry : mipEnginePathologyAttributes.entrySet()) { + String pathology = entry.getKey(); + MIPEngineAttributesDTO attributes = entry.getValue(); + attributes.updateAttributesWithProperEnums(); + mipEnginePathologyAttributesWithProperEnums.put(pathology, attributes); + } + + return mipEnginePathologyAttributesWithProperEnums; } } diff --git a/src/main/java/eu/hbp/mip/models/DTOs/MIPEngineAttributesDTO.java b/src/main/java/eu/hbp/mip/models/DTOs/MIPEngineAttributesDTO.java index 2f81aee6a9044396960d1b940f36cb350340d13a..2c4794d7e0b691a34e82be01638f54207958b18a 100644 --- a/src/main/java/eu/hbp/mip/models/DTOs/MIPEngineAttributesDTO.java +++ b/src/main/java/eu/hbp/mip/models/DTOs/MIPEngineAttributesDTO.java @@ -1,10 +1,11 @@ package eu.hbp.mip.models.DTOs; import com.google.gson.annotations.SerializedName; -import eu.hbp.mip.models.DTOs.MetadataHierarchyDTO; import lombok.AllArgsConstructor; import lombok.Data; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,4 +18,19 @@ public class MIPEngineAttributesDTO { @SerializedName("tags") private Object tags; + public void updateAttributesWithProperEnums(){ + Map<String, List<MetadataHierarchyDTO>> updated_properties = new HashMap<>(); + for (Map.Entry<String, List<MetadataHierarchyDTO>> entry : this.properties.entrySet()) { + String pathology = entry.getKey(); + List<MetadataHierarchyDTO> hierarchyDTOS = entry.getValue(); + List<MetadataHierarchyDTO> updatedHierarchyDTOS = new ArrayList<>(); + + for (MetadataHierarchyDTO hierarchyDTO : hierarchyDTOS) { + hierarchyDTO.updateGroupWithProperEnums(); + updatedHierarchyDTOS.add(hierarchyDTO); + } + updated_properties.put(pathology,updatedHierarchyDTOS); + } + this.properties = updated_properties; + } } 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 e0db0c70d9430cf21b8a92c9ec70e4d8f46aa10a..fbfd8bd348a92230c82d77d564528a3037b1ec32 100644 --- a/src/main/java/eu/hbp/mip/models/DTOs/MetadataHierarchyDTO.java +++ b/src/main/java/eu/hbp/mip/models/DTOs/MetadataHierarchyDTO.java @@ -4,9 +4,9 @@ import com.google.gson.annotations.SerializedName; import lombok.AllArgsConstructor; import lombok.Data; -import java.util.Hashtable; +import java.util.ArrayList; import java.util.List; -import java.util.Optional; +import java.util.Map; @Data @AllArgsConstructor @@ -19,15 +19,15 @@ public class MetadataHierarchyDTO { private String code; @SerializedName("groups") - private Object groups; + private List<MetadataHierarchyDTO> groups; @SerializedName("label") private String label; @Data @AllArgsConstructor public static class CommonDataElement { - @SerializedName("isCategorical") - private Boolean isCategorical; + @SerializedName("is_categorical") + private Boolean is_categorical; @SerializedName("code") private String code; @@ -53,5 +53,79 @@ public class MetadataHierarchyDTO { @SerializedName("methodology") private String methodology; + @SerializedName("min") + private String min; + + @SerializedName("max") + private String max; + + private void updateEnumerations(){ + if (this.enumerations != null){ + Map old_enumeration = (Map) this.enumerations; + List<PathologyDTO.EnumerationDTO> enumerationDTOS = new ArrayList<>(); + old_enumeration.forEach((cdeCode, cdeLabel) -> { + enumerationDTOS.add(new PathologyDTO.EnumerationDTO((String) cdeCode, (String) cdeLabel)); + }); + setEnumerations(enumerationDTOS); + } + } + } + + public void updateVariableWithProperEnums(){ + List<CommonDataElement> updated_variables = new ArrayList<>(); + this.variables.forEach(commonDataElement -> { + commonDataElement.updateEnumerations(); + updated_variables.add(commonDataElement); + }); + setVariables(updated_variables); + } + + public void updateGroupWithProperEnums(){ + List<MetadataHierarchyDTO> updated_groups = new ArrayList<>(); + for (MetadataHierarchyDTO hierarchyDTO : this.groups) { + + if (hierarchyDTO.getVariables() != null) { + hierarchyDTO.updateVariableWithProperEnums(); + } + + if (hierarchyDTO.getGroups() != null) { + hierarchyDTO.updateGroupWithProperEnums(); + } + updated_groups.add(hierarchyDTO); + } + 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 8771aefd1aad8ad173982c4b2b0fdb9b8bce17a9..de85a0fa22bea20e9f8f2892ab0e27ecee422ae4 100644 --- a/src/main/java/eu/hbp/mip/models/DTOs/PathologyDTO.java +++ b/src/main/java/eu/hbp/mip/models/DTOs/PathologyDTO.java @@ -1,12 +1,12 @@ 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; import java.util.Arrays; import java.util.List; -import java.util.Optional; @Data @AllArgsConstructor @@ -26,19 +26,17 @@ public class PathologyDTO { private MetadataHierarchyDTO metadataHierarchyDTO; @SerializedName("datasets") - private List<PathologyDatasetDTO> datasets; + private List<EnumerationDTO> datasets; public PathologyDTO(){ } - public PathologyDTO(String pathology, MIPEngineAttributesDTO mipEngineAttributesDTO, List<PathologyDatasetDTO> pathologyDatasetDTOS) { + 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); @@ -50,7 +48,7 @@ public class PathologyDTO { @Data @AllArgsConstructor - public static class PathologyDatasetDTO { + public static class EnumerationDTO { @SerializedName("code") private String code; diff --git a/src/main/java/eu/hbp/mip/services/ExperimentService.java b/src/main/java/eu/hbp/mip/services/ExperimentService.java index de27f46fef43aae8426668f6227549c1238bd76d..a1c16a546d20079e89887aa5f647a8518b090386 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."); diff --git a/src/main/java/eu/hbp/mip/utils/ClaimUtils.java b/src/main/java/eu/hbp/mip/utils/ClaimUtils.java index df2a75abf5d4a509b3bb6ee534ff1ed0de4555cd..650ac9c65fc4cf461159a3d94673d406cfe19dce 100644 --- a/src/main/java/eu/hbp/mip/utils/ClaimUtils.java +++ b/src/main/java/eu/hbp/mip/utils/ClaimUtils.java @@ -68,8 +68,8 @@ public class ClaimUtils { List<PathologyDTO> userPathologies = new ArrayList<>(); for (PathologyDTO curPathology : allPathologies) { - List<PathologyDTO.PathologyDatasetDTO> userPathologyDatasets = new ArrayList<>(); - for (PathologyDTO.PathologyDatasetDTO dataset : curPathology.getDatasets()) { + List<PathologyDTO.EnumerationDTO> userPathologyDatasets = new ArrayList<>(); + for (PathologyDTO.EnumerationDTO dataset : curPathology.getDatasets()) { if (hasRoleAccess(authorities, ClaimUtils.getDatasetClaim(dataset.getCode()), logger)) { logger.LogUserAction("Added dataset: " + dataset.getCode()); userPathologyDatasets.add(dataset);