From 460ef31064e19ad1c1740cd8e1be1fc9fd28014e Mon Sep 17 00:00:00 2001 From: David Randolph Phillips Date: Tue, 23 Jul 2024 14:13:07 -0400 Subject: [PATCH 1/4] [BI-2126] WIP --- .../brapi/v2/services/BrAPITrialService.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java index 2f6d1d881..51a649104 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java +++ b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java @@ -300,6 +300,23 @@ public Dataset getDatasetData(Program program, UUID experimentId, UUID datasetId log.debug("fetching dataset: " + datasetId + " for experiment: " + experimentId + ". including stats: " + stats); log.debug("fetching observationUnits for dataset: " + datasetId); List datasetOUs = ouDAO.getObservationUnitsForDataset(datasetId.toString(), program); + + //Add years to addition_data elements + Map studyDbId2Year = new HashMap<>(); + for (BrAPIObservationUnit ou: datasetOUs + ) { + String studyDbId = ou.getStudyDbId(); + if( !studyDbId2Year.containsKey( studyDbId)) { + + BrAPIStudy study = studyDAO.getStudyByDbId(studyDbId, program).orElseThrow(() -> new DoesNotExistException(String.format("Study Id '%s' not found.", studyDbId))); + String seasonId = study.getSeasons().get(0); + BrAPISeason season = seasonDAO.getSeasonById(seasonId, program.getId()); + Integer year = season.getYear(); + studyDbId2Year.put(studyDbId, year); + } + ou.putAdditionalInfoItem(BrAPIAdditionalInfoFields.ENV_YEAR, studyDbId2Year.get(studyDbId)); + } + log.debug("fetching dataset variables dataset: " + datasetId); List datasetObsVars = getDatasetObsVars(datasetId.toString(), program); List ouDbIds = datasetOUs.stream().map(BrAPIObservationUnit::getObservationUnitDbId).collect(Collectors.toList()); From 1ddcfd859ee2f1fe37994b46e384abbd8de30b02 Mon Sep 17 00:00:00 2001 From: David Randolph Phillips Date: Wed, 24 Jul 2024 10:59:51 -0400 Subject: [PATCH 2/4] [BI-2126] Added comments --- .../brapi/v2/services/BrAPITrialService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java index 51a649104..afcfc6897 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java +++ b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java @@ -301,13 +301,13 @@ public Dataset getDatasetData(Program program, UUID experimentId, UUID datasetId log.debug("fetching observationUnits for dataset: " + datasetId); List datasetOUs = ouDAO.getObservationUnitsForDataset(datasetId.toString(), program); - //Add years to addition_data elements - Map studyDbId2Year = new HashMap<>(); + //Add years to the addition_data elements + Map studyDbId2Year = new HashMap<>(); // used to prevent the same study from being fetched repeatedly. for (BrAPIObservationUnit ou: datasetOUs ) { String studyDbId = ou.getStudyDbId(); if( !studyDbId2Year.containsKey( studyDbId)) { - + // Get the Study and extract the year from its Season BrAPIStudy study = studyDAO.getStudyByDbId(studyDbId, program).orElseThrow(() -> new DoesNotExistException(String.format("Study Id '%s' not found.", studyDbId))); String seasonId = study.getSeasons().get(0); BrAPISeason season = seasonDAO.getSeasonById(seasonId, program.getId()); From 834e8eeeaedb2cac30f88632c38924850cdee49e Mon Sep 17 00:00:00 2001 From: David Randolph Phillips Date: Wed, 24 Jul 2024 14:32:32 -0400 Subject: [PATCH 3/4] [BI-2126] Added defensive code --- .../brapi/v2/services/BrAPITrialService.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java index afcfc6897..91f206c2f 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java +++ b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java @@ -301,19 +301,26 @@ public Dataset getDatasetData(Program program, UUID experimentId, UUID datasetId log.debug("fetching observationUnits for dataset: " + datasetId); List datasetOUs = ouDAO.getObservationUnitsForDataset(datasetId.toString(), program); - //Add years to the addition_data elements + //Add years to the addition_info elements Map studyDbId2Year = new HashMap<>(); // used to prevent the same study from being fetched repeatedly. - for (BrAPIObservationUnit ou: datasetOUs - ) { + for ( BrAPIObservationUnit ou: datasetOUs ) { String studyDbId = ou.getStudyDbId(); - if( !studyDbId2Year.containsKey( studyDbId)) { + + if( !studyDbId2Year.containsKey( studyDbId )) { // Get the Study and extract the year from its Season - BrAPIStudy study = studyDAO.getStudyByDbId(studyDbId, program).orElseThrow(() -> new DoesNotExistException(String.format("Study Id '%s' not found.", studyDbId))); + BrAPIStudy study = studyDAO.getStudyByDbId(studyDbId, program).orElseThrow( () -> new DoesNotExistException(String.format("Study Id '%s' not found.", studyDbId)) ); + if(study.getSeasons().isEmpty()){ + throw new DoesNotExistException(String.format("No Seasons found in Study Id = '%s'.", studyDbId)); + } String seasonId = study.getSeasons().get(0); BrAPISeason season = seasonDAO.getSeasonById(seasonId, program.getId()); + if(season==null){ + throw new DoesNotExistException(String.format("Seasons not found for Id = '%s'.", seasonId)); + } Integer year = season.getYear(); studyDbId2Year.put(studyDbId, year); } + ou.putAdditionalInfoItem(BrAPIAdditionalInfoFields.ENV_YEAR, studyDbId2Year.get(studyDbId)); } From 17cc8ef65372a0469f8fe5f743ade72e4edf903f Mon Sep 17 00:00:00 2001 From: David Randolph Phillips Date: Fri, 26 Jul 2024 10:07:40 -0400 Subject: [PATCH 4/4] [BI-2126] fetch Study with studyDAO.getStudyByEnvironmentId() to make use of Redis cache change variable name to conform to starndard practices --- .../brapi/v2/services/BrAPITrialService.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java index 91f206c2f..333ceae60 100644 --- a/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java +++ b/src/main/java/org/breedinginsight/brapi/v2/services/BrAPITrialService.java @@ -302,15 +302,17 @@ public Dataset getDatasetData(Program program, UUID experimentId, UUID datasetId List datasetOUs = ouDAO.getObservationUnitsForDataset(datasetId.toString(), program); //Add years to the addition_info elements - Map studyDbId2Year = new HashMap<>(); // used to prevent the same study from being fetched repeatedly. + //TODO yearByStudyDbId will no longer be needed, and should be removed, once the seasonDAO uses the redis cache (BI-2261). + Map yearByStudyDbId = new HashMap<>(); // used to prevent the same season from being fetched repeatedly. for ( BrAPIObservationUnit ou: datasetOUs ) { - String studyDbId = ou.getStudyDbId(); - - if( !studyDbId2Year.containsKey( studyDbId )) { + String environmentId = Utilities.getExternalReference(ou.getExternalReferences(), this.referenceSource, ExternalReferenceSource.STUDIES) + .orElseThrow( ()-> new DoesNotExistException("No BI external reference for STUDIES was found")) + .getReferenceId(); + if( !yearByStudyDbId.containsKey( environmentId )) { // Get the Study and extract the year from its Season - BrAPIStudy study = studyDAO.getStudyByDbId(studyDbId, program).orElseThrow( () -> new DoesNotExistException(String.format("Study Id '%s' not found.", studyDbId)) ); + BrAPIStudy study = studyDAO.getStudyByEnvironmentId(UUID.fromString(environmentId), program).orElseThrow( () -> new DoesNotExistException(String.format("Study Id '%s' not found.", environmentId)) ); if(study.getSeasons().isEmpty()){ - throw new DoesNotExistException(String.format("No Seasons found in Study Id = '%s'.", studyDbId)); + throw new DoesNotExistException(String.format("No Seasons found in Study Id = '%s'.", environmentId)); } String seasonId = study.getSeasons().get(0); BrAPISeason season = seasonDAO.getSeasonById(seasonId, program.getId()); @@ -318,10 +320,10 @@ public Dataset getDatasetData(Program program, UUID experimentId, UUID datasetId throw new DoesNotExistException(String.format("Seasons not found for Id = '%s'.", seasonId)); } Integer year = season.getYear(); - studyDbId2Year.put(studyDbId, year); + yearByStudyDbId.put(environmentId, year); } - ou.putAdditionalInfoItem(BrAPIAdditionalInfoFields.ENV_YEAR, studyDbId2Year.get(studyDbId)); + ou.putAdditionalInfoItem(BrAPIAdditionalInfoFields.ENV_YEAR, yearByStudyDbId.get(environmentId)); } log.debug("fetching dataset variables dataset: " + datasetId);