Skip to content

Instantly share code, notes, and snippets.

@mherman22
Last active February 24, 2026 07:10
Show Gist options
  • Select an option

  • Save mherman22/578116291fbea26a11135eed15e04515 to your computer and use it in GitHub Desktop.

Select an option

Save mherman22/578116291fbea26a11135eed15e04515 to your computer and use it in GitHub Desktop.
package org.openelisglobal.fhir.utils;
import java.util.UUID;
import java.util.function.Supplier;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.hl7.fhir.r4.model.Resource;
import org.openelisglobal.common.log.LogEvent;
import org.openelisglobal.common.util.ControllerUtills;
import org.openelisglobal.dataexchange.fhir.service.FhirPersistanceService;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import jakarta.servlet.http.HttpServletRequest;
public final class FhirProviderUtils {
private FhirProviderUtils() {
// Utility class, no instances
}
/**
* Logs a debug message with standardized format.
*/
public static void logDebug(Class<?> clazz, String method, String message) {
LogEvent.logDebug(clazz.getSimpleName(), method, message);
}
/**
* Logs an info message with standardized format.
*/
public static void logInfo(Class<?> clazz, String method, String message) {
LogEvent.logInfo(clazz.getSimpleName(), method, message);
}
/**
* Logs an error message with standardized format.
*/
public static void logError(Class<?> clazz, String method, String message) {
LogEvent.logError(clazz.getSimpleName(), method, message);
}
public static void validateResourceNotNull(IBaseResource resource, String resourceType, Class<?> clazz, String method) {
if (resource == null) {
logError(clazz, method, resourceType + " resource is null");
throw new InvalidRequestException(resourceType + " resource cannot be null");
}
}
public static void validateIdForOperation(IdType id, String resourceType, String operation, Class<?> clazz, String method) {
if (id == null || !id.hasIdPart()) {
logError(clazz, method, "Missing " + resourceType + " ID for " + operation);
throw new InvalidRequestException(resourceType + " ID must be provided for " + operation);
}
}
public static void ensureResourceHasId(IBaseResource resource) {
if (resource.getIdElement().getIdPart() == null) {
resource.setId(UUID.randomUUID().toString());
}
}
public static UUID parseUuidSafely(String idString, String resourceType) {
try {
return UUID.fromString(idString);
} catch (IllegalArgumentException e) {
throw new ResourceNotFoundException(resourceType + "/" + idString);
}
}
public static void setSysUserId(Object entity, HttpServletRequest request) {
try {
String sysUserId = ControllerUtills.getSysUserId(request);
// Use reflection to set the sysUserId if the method exists
entity.getClass().getMethod("setSysUserId", String.class).invoke(entity, sysUserId);
} catch (Exception e) {
// If setSysUserId method doesn't exist or fails, continue silently
// This allows the utility to work with different entity types
}
}
public static MethodOutcome buildCreateOutcome(IBaseResource resource) {
MethodOutcome outcome = new MethodOutcome();
outcome.setId(resource.getIdElement());
outcome.setResource(resource);
outcome.setCreated(true);
outcome.setResponseStatusCode(201);
return outcome;
}
public static MethodOutcome buildUpdateOutcome(IBaseResource resource) {
MethodOutcome outcome = new MethodOutcome();
outcome.setId(resource.getIdElement());
outcome.setResource(resource);
outcome.setCreated(false);
outcome.setResponseStatusCode(200);
return outcome;
}
public static MethodOutcome buildDeleteOutcome(IdType id, String resourceType) {
MethodOutcome outcome = new MethodOutcome();
outcome.setId(id);
outcome.setResponseStatusCode(204);
OperationOutcome operationOutcome = new OperationOutcome();
operationOutcome.addIssue()
.setSeverity(OperationOutcome.IssueSeverity.INFORMATION)
.setDiagnostics(resourceType + " " + id.getIdPart() + " has been deleted");
outcome.setOperationOutcome(operationOutcome);
return outcome;
}
public static <T> T executeWithStandardExceptionHandling(
Supplier<T> operation,
String resourceType,
String operationName,
Class<?> clazz,
String method) {
try {
return operation.get();
} catch (UnprocessableEntityException | InvalidRequestException | ResourceNotFoundException e) {
throw e;
} catch (Exception e) {
logError(clazz, method, "Unexpected error while " + operationName + " " + resourceType + ": " + e.getMessage());
throw new InternalErrorException("Unexpected server error while " + operationName + " " + resourceType, e);
}
}
public static void syncToFhirStore(
FhirPersistanceService fhirPersistenceService,
Resource resource,
String operationName,
Class<?> clazz,
String method) {
try {
fhirPersistenceService.updateFhirResourceInFhirStore(resource);
} catch (Exception syncEx) {
logError(clazz, method,
"FHIR store sync failed during " + operationName + " (continuing anyway): " + syncEx.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment