Skip to content

Commit

Permalink
Abstracted the public methods out of DirectoryApi
Browse files Browse the repository at this point in the history
In the future we may want to have a GraphQL extension of this class.
  • Loading branch information
DavidCroftDKFZ committed Sep 25, 2024
1 parent cd20de9 commit b67fe2e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 96 deletions.
51 changes: 0 additions & 51 deletions src/main/java/de/samply/directory_sync_service/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ public static <K, V> Map<K, V> 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 <K, V> Map<K, V> mapOf(K key, V value) {
HashMap<K, V> map = new HashMap<>();
map.put(key, value);
return map;
}

/**
* Get a printable stack trace from an Exception object.
* @param e
* @return
Expand All @@ -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.
* <p>
* 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<OperationOutcome> 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}.
* <p>
Expand All @@ -86,27 +58,4 @@ public static String getErrorMessageFromOperationOutcome(OperationOutcome operat
return errorMessage;
}

/**
* Reports a list of {@link OperationOutcome} objects by logging any errors found.
* <p>
* 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<OperationOutcome> operationOutcomes) {
boolean failed = false;
for (OperationOutcome operationOutcome : operationOutcomes) {
String errorMessage = Util.getErrorMessageFromOperationOutcome(operationOutcome);
if (errorMessage.length() > 0) {
logger.error(errorMessage);
failed = true;
}
}
return !failed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Organization> organizations = fhirApi.listAllBiobanks();

Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, String> correctedDiagnoses, String defaultCollectionId) {
public static boolean sendUpdatesToDirectory(FhirApi fhirApi, DirectoryApi directoryApi, Map<String, String> correctedDiagnoses, String defaultCollectionId) {
try {
BbmriEricId defaultBbmriEricCollectionId = BbmriEricId
.valueOf(defaultCollectionId)
Expand All @@ -62,8 +61,8 @@ public static boolean sendUpdatesToDirectory(FhirApi fhirApi, DirectoryApiRest d

List<String> 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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,12 +28,12 @@ public class DiagnosisCorrections {
* * Collects corrected diagnosis codes from the Directory API based on the MIRIAM-compatible codes.
* <p>
* @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<String, String> generateDiagnosisCorrections(FhirApi fhirApi, DirectoryApiRest directoryApiRest, String defaultCollectionId) {
public static Map<String, String> generateDiagnosisCorrections(FhirApi fhirApi, DirectoryApi directoryApi, String defaultCollectionId) {
try {
Map<String, String> correctedDiagnoses = new HashMap<String, String>();
// Convert string version of collection ID into a BBMRI ERIC ID.
Expand All @@ -59,7 +59,7 @@ public static Map<String, String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@ public class StarModelUpdater {
* The method handles errors by returning a list of OperationOutcome objects describing the issues.
* </p>
* @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.
Expand All @@ -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<String, String> correctedDiagnoses, String defaultCollectionId, int minDonors, int maxFacts) {
public static boolean sendStarModelUpdatesToDirectory(FhirApi fhirApi, DirectoryApi directoryApi, Map<String, String> correctedDiagnoses, String defaultCollectionId, int minDonors, int maxFacts) {
logger.info("__________ sendStarModelUpdatesToDirectory: minDonors: " + minDonors);
try {
BbmriEricId defaultBbmriEricCollectionId = BbmriEricId
Expand All @@ -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.
Expand All @@ -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()) {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/de/samply/directory_sync_service/sync/Sync.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -56,14 +57,14 @@ private static boolean syncWithDirectory(String fhirStoreUrl, String directoryUr
Map<String, String> 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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit b67fe2e

Please sign in to comment.