From b67fe2e822b50861c501a96fcbebad055bc2df46 Mon Sep 17 00:00:00 2001 From: DavidCroftDKFZ <46788708+DavidCroftDKFZ@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:57:54 +0200 Subject: [PATCH] Abstracted the public methods out of DirectoryApi In the future we may want to have a GraphQL extension of this class. --- .../samply/directory_sync_service/Util.java | 51 ------------------- .../directory/DirectoryApiRest.java | 27 +++++----- .../sync/BiobanksUpdater.java | 10 ++-- .../sync/CollectionUpdater.java | 17 +++---- .../sync/DiagnosisCorrections.java | 8 +-- .../sync/StarModelUpdater.java | 12 ++--- .../directory_sync_service/sync/Sync.java | 15 +++--- 7 files changed, 44 insertions(+), 96 deletions(-) diff --git a/src/main/java/de/samply/directory_sync_service/Util.java b/src/main/java/de/samply/directory_sync_service/Util.java index 0367210..66ae34e 100644 --- a/src/main/java/de/samply/directory_sync_service/Util.java +++ b/src/main/java/de/samply/directory_sync_service/Util.java @@ -24,19 +24,6 @@ public static Map mapOf() { } /** - * Creates a new map with a single entry consisting of the specified key and value. - * - * @param key the key for the entry - * @param value the value associated with the key - * @return a new map containing the specified key-value pair - */ - public static Map mapOf(K key, V value) { - HashMap map = new HashMap<>(); - map.put(key, value); - return map; - } - - /** * Get a printable stack trace from an Exception object. * @param e * @return @@ -48,21 +35,6 @@ public static String traceFromException(Exception e) { return sw.toString(); } - /** - * Creates a list containing a single {@link OperationOutcome} that represents an error. - *

- * The generated {@code OperationOutcome} will have an issue with a severity level of - * {@code ERROR} and will include the provided diagnostic message. - * - * @param diagnostics A diagnostic message that provides details about the error. - * @return A list containing a single {@code OperationOutcome} object indicating an error. - */ - public static List createErrorOutcome(String diagnostics) { - OperationOutcome outcome = new OperationOutcome(); - outcome.addIssue().setSeverity(ERROR).setDiagnostics(diagnostics); - return Collections.singletonList(outcome); - } - /** * Extracts and concatenates error messages from an {@link OperationOutcome}. *

@@ -86,27 +58,4 @@ public static String getErrorMessageFromOperationOutcome(OperationOutcome operat return errorMessage; } - /** - * Reports a list of {@link OperationOutcome} objects by logging any errors found. - *

- * This method iterates through the provided list of {@code OperationOutcome} objects, - * extracting and logging any error messages. If any errors are encountered, they are logged - * at the {@code ERROR} level and the method will return {@code false}. - * If no errors are found, the method returns {@code true}. - * - * @param operationOutcomes A list of {@code OperationOutcome} objects to be checked for errors. - * @return {@code true} if no errors were found in any of the {@code OperationOutcome} objects; - * {@code false} if at least one error was found and logged. - */ - public static boolean reportOperationOutcomes(List operationOutcomes) { - boolean failed = false; - for (OperationOutcome operationOutcome : operationOutcomes) { - String errorMessage = Util.getErrorMessageFromOperationOutcome(operationOutcome); - if (errorMessage.length() > 0) { - logger.error(errorMessage); - failed = true; - } - } - return !failed; - } } diff --git a/src/main/java/de/samply/directory_sync_service/directory/DirectoryApiRest.java b/src/main/java/de/samply/directory_sync_service/directory/DirectoryApiRest.java index 1ccce13..3b18066 100644 --- a/src/main/java/de/samply/directory_sync_service/directory/DirectoryApiRest.java +++ b/src/main/java/de/samply/directory_sync_service/directory/DirectoryApiRest.java @@ -23,14 +23,9 @@ * and performing various validation and correction operations. * It supports a mock mode for testing purposes, where no real Directory interactions are performed. */ -public class DirectoryApiRest { - private static final Logger logger = LoggerFactory.getLogger(DirectoryApiRest.class); +public class DirectoryApiRest extends DirectoryApi { private DirectoryRestCalls directoryRestCalls; - // Setting this variable to true will prevent any contact being made to the Directory. - // All public methods will return feasible fake results. - private boolean mockDirectory = false; - /** * Constructs a new DirectoryApiRest instance. * If we are not in mocking mode, log in to the Directory. @@ -41,7 +36,7 @@ public class DirectoryApiRest { * @param password The password for authenticating with the Directory. */ public DirectoryApiRest(String baseUrl, boolean mockDirectory, String username, String password) { - this.mockDirectory = mockDirectory; + super(baseUrl, mockDirectory, username, password); this.directoryRestCalls = new DirectoryRestCalls(baseUrl, username, password); } @@ -136,16 +131,22 @@ public DirectoryCollectionGet fetchCollectionGetOutcomes(String countryCode, Lis * @param directoryCollectionPut Summary information about one or more collections * @return an outcome, either successful or null */ - public OperationOutcome updateEntities(DirectoryCollectionPut directoryCollectionPut) { - if (mockDirectory) + public boolean updateEntities(DirectoryCollectionPut directoryCollectionPut) { + if (mockDirectory) { // Dummy return if we're in mock mode - return DirectoryUtils.success("DirectoryApiRest.updateEntities: in mock mode, skip update"); + logger.info("DirectoryApiRest.updateEntities: in mock mode, skip update"); + return true; + } String response = directoryRestCalls.put(DirectoryRestEndpoints.getCollectionEndpoint(directoryCollectionPut.getCountryCode()), directoryCollectionPut); - if (response == null) - return DirectoryUtils.error("entity update, PUT problem"); + if (response == null) { + logger.warn("entity update, PUT problem"); + return false; + } - return DirectoryUtils.success("DirectoryApiRest.updateEntities: successfully put " + directoryCollectionPut.size() + " collections to the Directory"); + logger.info("DirectoryApiRest.updateEntities: successfully put " + directoryCollectionPut.size() + " collections to the Directory"); + + return true; } /** diff --git a/src/main/java/de/samply/directory_sync_service/sync/BiobanksUpdater.java b/src/main/java/de/samply/directory_sync_service/sync/BiobanksUpdater.java index 613732d..53653a9 100644 --- a/src/main/java/de/samply/directory_sync_service/sync/BiobanksUpdater.java +++ b/src/main/java/de/samply/directory_sync_service/sync/BiobanksUpdater.java @@ -1,7 +1,7 @@ package de.samply.directory_sync_service.sync; import de.samply.directory_sync_service.Util; -import de.samply.directory_sync_service.directory.DirectoryApiRest; +import de.samply.directory_sync_service.directory.DirectoryApi; import de.samply.directory_sync_service.directory.model.Biobank; import de.samply.directory_sync_service.fhir.FhirApi; import de.samply.directory_sync_service.model.BbmriEricId; @@ -29,7 +29,7 @@ public class BiobanksUpdater { * * @return the individual {@link OperationOutcome}s from each update */ - public static boolean updateBiobanksInFhirStore(FhirApi fhirApi, DirectoryApiRest directoryApiRest) { + public static boolean updateBiobanksInFhirStore(FhirApi fhirApi, DirectoryApi directoryApi) { // Retrieve the list of all biobanks List organizations = fhirApi.listAllBiobanks(); @@ -42,7 +42,7 @@ public static boolean updateBiobanksInFhirStore(FhirApi fhirApi, DirectoryApiRes // If successful, process each biobank and update it on the FHIR server if necessary for (Organization organization : organizations) { // Update each biobank and report any errors - if (!updateBiobankInFhirStore(fhirApi, directoryApiRest, organization)) { + if (!updateBiobankInFhirStore(fhirApi, directoryApi, organization)) { logger.warn("updateBiobankOnFhirServerIfNecessary: problem updating: " + organization.getIdElement().getValue()); succeeded = false; } @@ -58,7 +58,7 @@ public static boolean updateBiobanksInFhirStore(FhirApi fhirApi, DirectoryApiRes * @param fhirBiobank the biobank to update. * @return the {@link OperationOutcome} from the FHIR server update */ - private static boolean updateBiobankInFhirStore(FhirApi fhirApi, DirectoryApiRest directoryApiRest, Organization fhirBiobank) { + private static boolean updateBiobankInFhirStore(FhirApi fhirApi, DirectoryApi directoryApi, Organization fhirBiobank) { logger.info("updateBiobankOnFhirServerIfNecessary: entered"); // Retrieve the biobank's BBMRI-ERIC identifier from the FHIR organization @@ -76,7 +76,7 @@ private static boolean updateBiobankInFhirStore(FhirApi fhirApi, DirectoryApiRes logger.info("updateBiobankOnFhirServerIfNecessary: bbmriEricId: " + bbmriEricId); // Fetch the corresponding biobank from the Directory API - Biobank directoryBiobank = directoryApiRest.fetchBiobank(bbmriEricId); + Biobank directoryBiobank = directoryApi.fetchBiobank(bbmriEricId); logger.info("updateBiobankOnFhirServerIfNecessary: directoryBiobank: " + directoryBiobank); diff --git a/src/main/java/de/samply/directory_sync_service/sync/CollectionUpdater.java b/src/main/java/de/samply/directory_sync_service/sync/CollectionUpdater.java index 5dd6247..1e32530 100644 --- a/src/main/java/de/samply/directory_sync_service/sync/CollectionUpdater.java +++ b/src/main/java/de/samply/directory_sync_service/sync/CollectionUpdater.java @@ -2,14 +2,13 @@ import de.samply.directory_sync_service.Util; import de.samply.directory_sync_service.converter.FhirCollectionToDirectoryCollectionPutConverter; -import de.samply.directory_sync_service.directory.DirectoryApiRest; +import de.samply.directory_sync_service.directory.DirectoryApi; import de.samply.directory_sync_service.directory.MergeDirectoryCollectionGetToDirectoryCollectionPut; import de.samply.directory_sync_service.directory.model.DirectoryCollectionGet; import de.samply.directory_sync_service.directory.model.DirectoryCollectionPut; import de.samply.directory_sync_service.fhir.FhirApi; import de.samply.directory_sync_service.fhir.model.FhirCollection; import de.samply.directory_sync_service.model.BbmriEricId; -import org.hl7.fhir.r4.model.OperationOutcome; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +39,7 @@ public class CollectionUpdater { * @param defaultCollectionId The default collection ID to use for fetching collections from the FHIR store. * @return A list of OperationOutcome objects indicating the outcome of the update operation. */ - public static boolean sendUpdatesToDirectory(FhirApi fhirApi, DirectoryApiRest directoryApiRest, Map correctedDiagnoses, String defaultCollectionId) { + public static boolean sendUpdatesToDirectory(FhirApi fhirApi, DirectoryApi directoryApi, Map correctedDiagnoses, String defaultCollectionId) { try { BbmriEricId defaultBbmriEricCollectionId = BbmriEricId .valueOf(defaultCollectionId) @@ -62,8 +61,8 @@ public static boolean sendUpdatesToDirectory(FhirApi fhirApi, DirectoryApiRest d List collectionIds = directoryCollectionPut.getCollectionIds(); String countryCode = directoryCollectionPut.getCountryCode(); - directoryApiRest.login(); - DirectoryCollectionGet directoryCollectionGet = directoryApiRest.fetchCollectionGetOutcomes(countryCode, collectionIds); + directoryApi.login(); + DirectoryCollectionGet directoryCollectionGet = directoryApi.fetchCollectionGetOutcomes(countryCode, collectionIds); if (directoryCollectionGet == null) { logger.warn("Problem getting collections from Directory"); return false; @@ -81,12 +80,10 @@ public static boolean sendUpdatesToDirectory(FhirApi fhirApi, DirectoryApiRest d directoryCollectionPut.applyDiagnosisCorrections(correctedDiagnoses); logger.info("__________ sendUpdatesToDirectory: 2 directoryCollectionPut.getCollectionIds().size()): " + directoryCollectionPut.getCollectionIds().size()); - directoryApiRest.login(); - OperationOutcome updateOutcome = directoryApiRest.updateEntities(directoryCollectionPut); - String errorMessage = Util.getErrorMessageFromOperationOutcome(updateOutcome); + directoryApi.login(); - if (!errorMessage.isEmpty()) { - logger.warn("sendUpdatesToDirectory: Problem during star model update"); + if (!directoryApi.updateEntities(directoryCollectionPut)) { + logger.warn("sendUpdatesToDirectory: Problem during collection update"); return false; } diff --git a/src/main/java/de/samply/directory_sync_service/sync/DiagnosisCorrections.java b/src/main/java/de/samply/directory_sync_service/sync/DiagnosisCorrections.java index 5d5aa3d..ebf148a 100644 --- a/src/main/java/de/samply/directory_sync_service/sync/DiagnosisCorrections.java +++ b/src/main/java/de/samply/directory_sync_service/sync/DiagnosisCorrections.java @@ -2,7 +2,7 @@ import de.samply.directory_sync_service.Util; import de.samply.directory_sync_service.converter.FhirToDirectoryAttributeConverter; -import de.samply.directory_sync_service.directory.DirectoryApiRest; +import de.samply.directory_sync_service.directory.DirectoryApi; import de.samply.directory_sync_service.fhir.FhirApi; import de.samply.directory_sync_service.model.BbmriEricId; import org.slf4j.Logger; @@ -28,12 +28,12 @@ public class DiagnosisCorrections { * * Collects corrected diagnosis codes from the Directory API based on the MIRIAM-compatible codes. *

* @param fhirApi - * @param directoryApiRest + * @param directoryApi * @param defaultCollectionId Default collection ID. May be null. * @return A list containing diagnosis corrections. * If any errors occur during the process, null is returned. */ - public static Map generateDiagnosisCorrections(FhirApi fhirApi, DirectoryApiRest directoryApiRest, String defaultCollectionId) { + public static Map generateDiagnosisCorrections(FhirApi fhirApi, DirectoryApi directoryApi, String defaultCollectionId) { try { Map correctedDiagnoses = new HashMap(); // Convert string version of collection ID into a BBMRI ERIC ID. @@ -59,7 +59,7 @@ public static Map generateDiagnosisCorrections(FhirApi fhirApi, logger.info("__________ generateDiagnosisCorrections: 1 correctedDiagnoses.size(): " + correctedDiagnoses.size()); // Get corrected diagnosis codes from the Directory - directoryApiRest.collectDiagnosisCorrections(correctedDiagnoses); + directoryApi.collectDiagnosisCorrections(correctedDiagnoses); logger.info("__________ generateDiagnosisCorrections: 2 correctedDiagnoses.size(): " + correctedDiagnoses.size()); return correctedDiagnoses; diff --git a/src/main/java/de/samply/directory_sync_service/sync/StarModelUpdater.java b/src/main/java/de/samply/directory_sync_service/sync/StarModelUpdater.java index dc26df5..c06b4a4 100644 --- a/src/main/java/de/samply/directory_sync_service/sync/StarModelUpdater.java +++ b/src/main/java/de/samply/directory_sync_service/sync/StarModelUpdater.java @@ -2,7 +2,7 @@ import de.samply.directory_sync_service.Util; import de.samply.directory_sync_service.directory.CreateFactTablesFromStarModelInputData; -import de.samply.directory_sync_service.directory.DirectoryApiRest; +import de.samply.directory_sync_service.directory.DirectoryApi; import de.samply.directory_sync_service.fhir.FhirApi; import de.samply.directory_sync_service.fhir.PopulateStarModelInputData; import de.samply.directory_sync_service.model.BbmriEricId; @@ -27,7 +27,7 @@ public class StarModelUpdater { * The method handles errors by returning a list of OperationOutcome objects describing the issues. *

* @param fhirApi - * @param directoryApiRest + * @param directoryApi * @param correctedDiagnoses * @param defaultCollectionId The default BBMRI-ERIC collection ID for fetching data from the FHIR store. * @param minDonors The minimum number of donors required for a fact to be included in the star model output. @@ -36,7 +36,7 @@ public class StarModelUpdater { * * @throws IllegalArgumentException if the defaultCollectionId is not a valid BbmriEricId. */ - public static boolean sendStarModelUpdatesToDirectory(FhirApi fhirApi, DirectoryApiRest directoryApiRest, Map correctedDiagnoses, String defaultCollectionId, int minDonors, int maxFacts) { + public static boolean sendStarModelUpdatesToDirectory(FhirApi fhirApi, DirectoryApi directoryApi, Map correctedDiagnoses, String defaultCollectionId, int minDonors, int maxFacts) { logger.info("__________ sendStarModelUpdatesToDirectory: minDonors: " + minDonors); try { BbmriEricId defaultBbmriEricCollectionId = BbmriEricId @@ -52,7 +52,7 @@ public static boolean sendStarModelUpdatesToDirectory(FhirApi fhirApi, Directory } logger.info("__________ sendStarModelUpdatesToDirectory: number of collection IDs: " + starModelInputData.getInputCollectionIds().size()); - directoryApiRest.login(); + directoryApi.login(); // Hypercubes containing less than the minimum number of donors will not be // included in the star model output. @@ -69,8 +69,8 @@ public static boolean sendStarModelUpdatesToDirectory(FhirApi fhirApi, Directory logger.info("__________ sendStarModelUpdatesToDirectory: 2 starModelInputData.getFactCount(): " + starModelInputData.getFactCount()); // Send fact tables to Direcory. - directoryApiRest.login(); - OperationOutcome updateOutcome = directoryApiRest.updateStarModel(starModelInputData); + directoryApi.login(); + OperationOutcome updateOutcome = directoryApi.updateStarModel(starModelInputData); String errorMessage = Util.getErrorMessageFromOperationOutcome(updateOutcome); if (!errorMessage.isEmpty()) { diff --git a/src/main/java/de/samply/directory_sync_service/sync/Sync.java b/src/main/java/de/samply/directory_sync_service/sync/Sync.java index a3fcaae..7d07aee 100644 --- a/src/main/java/de/samply/directory_sync_service/sync/Sync.java +++ b/src/main/java/de/samply/directory_sync_service/sync/Sync.java @@ -1,5 +1,6 @@ package de.samply.directory_sync_service.sync; +import de.samply.directory_sync_service.directory.DirectoryApi; import de.samply.directory_sync_service.directory.DirectoryApiRest; import de.samply.directory_sync_service.fhir.FhirApi; @@ -56,14 +57,14 @@ private static boolean syncWithDirectory(String fhirStoreUrl, String directoryUr Map correctedDiagnoses = null; // Re-initialize helper classes every time this method gets called FhirApi fhirApi = new FhirApi(fhirStoreUrl); - DirectoryApiRest directoryApiRest = new DirectoryApiRest(directoryUrl, directoryMock, directoryUserName, directoryUserPass); + DirectoryApi directoryApi = new DirectoryApiRest(directoryUrl, directoryMock, directoryUserName, directoryUserPass); - if (!directoryApiRest.isAvailable()) { + if (!directoryApi.isAvailable()) { logger.warn("syncWithDirectory: Directory REST API is not available"); return false; } - if (!directoryApiRest.login()) { + if (!directoryApi.login()) { logger.warn("syncWithDirectory: there was a problem during login to Directory"); return false; } @@ -74,22 +75,22 @@ private static boolean syncWithDirectory(String fhirStoreUrl, String directoryUr return true; } - correctedDiagnoses = DiagnosisCorrections.generateDiagnosisCorrections(fhirApi, directoryApiRest, directoryDefaultCollectionId); + correctedDiagnoses = DiagnosisCorrections.generateDiagnosisCorrections(fhirApi, directoryApi, directoryDefaultCollectionId); if (correctedDiagnoses == null) { logger.warn("syncWithDirectory: there was a problem during diagnosis corrections"); return false; } if (directoryAllowStarModel) - if (!StarModelUpdater.sendStarModelUpdatesToDirectory(fhirApi, directoryApiRest, correctedDiagnoses, directoryDefaultCollectionId, directoryMinDonors, directoryMaxFacts)) { + if (!StarModelUpdater.sendStarModelUpdatesToDirectory(fhirApi, directoryApi, correctedDiagnoses, directoryDefaultCollectionId, directoryMinDonors, directoryMaxFacts)) { logger.warn("syncWithDirectory: there was a problem during star model update to Directory"); return false; } - if (!CollectionUpdater.sendUpdatesToDirectory(fhirApi, directoryApiRest, correctedDiagnoses, directoryDefaultCollectionId)) { + if (!CollectionUpdater.sendUpdatesToDirectory(fhirApi, directoryApi, correctedDiagnoses, directoryDefaultCollectionId)) { logger.warn("syncWithDirectory: there was a problem during sync to Directory"); return false; } - if (!BiobanksUpdater.updateBiobanksInFhirStore(fhirApi, directoryApiRest)) { + if (!BiobanksUpdater.updateBiobanksInFhirStore(fhirApi, directoryApi)) { logger.warn("syncWithDirectory: there was a problem during sync from Directory"); return false; }