Skip to content

Commit

Permalink
Pushed some error reporting out from Sync and into Util
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCroftDKFZ committed Aug 27, 2024
1 parent a24c16a commit 5cba749
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
29 changes: 29 additions & 0 deletions src/main/java/de/samply/directory_sync_service/Util.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package de.samply.directory_sync_service;

import de.samply.directory_sync_service.sync.Sync;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -12,6 +15,8 @@
import static org.hl7.fhir.r4.model.OperationOutcome.IssueSeverity.ERROR;

public class Util {
private static final Logger logger = LoggerFactory.getLogger(Sync.class);

public static <K, V> Map<K, V> mapOf() {
return new HashMap<>();
}
Expand Down Expand Up @@ -78,4 +83,28 @@ 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;
}
}
51 changes: 15 additions & 36 deletions src/main/java/de/samply/directory_sync_service/sync/Sync.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import de.samply.directory_sync_service.fhir.model.FhirCollection;
import de.samply.directory_sync_service.converter.FhirToDirectoryAttributeConverter;
import de.samply.directory_sync_service.model.StarModelData;
import de.samply.directory_sync_service.service.Configuration;
import io.vavr.control.Either;
import io.vavr.control.Option;

Expand Down Expand Up @@ -131,46 +130,26 @@ public void syncWithDirectoryFailover() {
}

public boolean syncWithDirectory() {
// Re-initialize helper classes every time this method gets called
fhirApi = new FhirApi(fhirStoreUrl);
directoryApi = new DirectoryApi(directoryUrl, directoryMock, directoryUserName, directoryUserPass);
fhirReporting = new FhirReporting(fhirApi);
List<OperationOutcome> operationOutcomes;
operationOutcomes = generateDiagnosisCorrections(directoryDefaultCollectionId);
for (OperationOutcome operationOutcome : operationOutcomes) {
String errorMessage = Util.getErrorMessageFromOperationOutcome(operationOutcome);
if (errorMessage.length() > 0) {
logger.error("__________ syncWithDirectory: there was a problem during diagnosis corrections: " + errorMessage);
return false;
}
}
if (directoryAllowStarModel) {
operationOutcomes = sendStarModelUpdatesToDirectory(directoryDefaultCollectionId, directoryMinDonors, directoryMaxFacts);
for (OperationOutcome operationOutcome : operationOutcomes) {
String errorMessage = Util.getErrorMessageFromOperationOutcome(operationOutcome);
if (errorMessage.length() > 0) {
logger.error("__________ syncWithDirectory: there was a problem during star model update to Directory: " + errorMessage);
return false;
}
}
directoryApi = new DirectoryApi(directoryUrl, directoryMock, directoryUserName, directoryUserPass);

if (!Util.reportOperationOutcomes(generateDiagnosisCorrections(directoryDefaultCollectionId))) {
logger.warn("syncWithDirectory: there was a problem during diagnosis corrections");
return false;
}
operationOutcomes = sendUpdatesToDirectory(directoryDefaultCollectionId);
boolean failed = false;
for (OperationOutcome operationOutcome : operationOutcomes) {
String errorMessage = Util.getErrorMessageFromOperationOutcome(operationOutcome);
if (errorMessage.length() > 0) {
logger.error("__________ syncWithDirectory: there was a problem during sync to Directory: " + errorMessage);
failed = true;
if (directoryAllowStarModel)
if (!Util.reportOperationOutcomes(sendStarModelUpdatesToDirectory(directoryDefaultCollectionId, directoryMinDonors, directoryMaxFacts))) {
logger.warn("syncWithDirectory: there was a problem during star model update to Directory");
return false;
}
}
if (failed)
if (!Util.reportOperationOutcomes(sendUpdatesToDirectory(directoryDefaultCollectionId))) {
logger.warn("syncWithDirectory: there was a problem during sync to Directory");
return false;
operationOutcomes = updateAllBiobanksOnFhirServerIfNecessary();
for (OperationOutcome operationOutcome : operationOutcomes) {
String errorMessage = Util.getErrorMessageFromOperationOutcome(operationOutcome);
if (errorMessage.length() > 0) {
logger.error("__________ syncWithDirectory: there was a problem during sync from Directory: " + errorMessage);
// return false;
}
}
if (!Util.reportOperationOutcomes(updateAllBiobanksOnFhirServerIfNecessary())) {
logger.warn("syncWithDirectory: there was a problem during sync from Directory");
}

logger.info("__________ syncWithDirectory: all synchronization tasks finished");
Expand Down

0 comments on commit 5cba749

Please sign in to comment.