Skip to content

Commit

Permalink
Decoupled dependecy with hapifhir library
Browse files Browse the repository at this point in the history
  • Loading branch information
basiliskus committed Mar 22, 2024
1 parent c5ecc7d commit 468d98a
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gov.hhs.cdc.trustedintermediary.domainconnector.DomainRequest;
import gov.hhs.cdc.trustedintermediary.etor.metadata.EtorMetadataStep;
import gov.hhs.cdc.trustedintermediary.etor.ruleengine.RuleEngine;
import gov.hhs.cdc.trustedintermediary.external.hapi.HapiFhirResource;
import gov.hhs.cdc.trustedintermediary.external.hapi.HapiOrder;
import gov.hhs.cdc.trustedintermediary.wrappers.FhirParseException;
import gov.hhs.cdc.trustedintermediary.wrappers.HapiFhir;
Expand Down Expand Up @@ -30,7 +31,7 @@ public static OrderController getInstance() {
public Order<?> parseOrders(DomainRequest request) throws FhirParseException {
logger.logInfo("Parsing orders");
var fhirBundle = fhir.parseResource(request.getBody(), Bundle.class);
ruleEngine.validate(fhirBundle);
ruleEngine.validate(new HapiFhirResource(fhirBundle));
metadata.put(fhirBundle.getId(), EtorMetadataStep.RECEIVED_FROM_REPORT_STREAM);
return new HapiOrder(fhirBundle);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gov.hhs.cdc.trustedintermediary.etor.ruleengine;

public interface FhirResource<T> {
T getUnderlyingResource();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gov.hhs.cdc.trustedintermediary.etor.ruleengine;

import java.util.List;
import org.hl7.fhir.instance.model.api.IBaseResource;

/**
* The Rule interface defines the structure for a rule in the rule engine. Each rule has a name,
Expand All @@ -23,7 +22,7 @@ public interface Rule {

List<String> getValidations();

boolean isValid(IBaseResource resource);
boolean isValid(FhirResource<?> resource);

boolean appliesTo(IBaseResource resource);
boolean appliesTo(FhirResource<?> resource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.hl7.fhir.instance.model.api.IBaseResource;

/** Manages the application of rules loaded from a definitions file using the RuleLoader. */
public class RuleEngine {
Expand Down Expand Up @@ -48,7 +47,7 @@ private synchronized void loadRules() {
}
}

public void validate(IBaseResource resource) {
public void validate(FhirResource<?> resource) {
logger.logDebug("Validating FHIR resource");
ensureRulesLoaded();
for (Rule rule : rules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import gov.hhs.cdc.trustedintermediary.wrappers.HapiFhir;
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import java.util.List;
import org.hl7.fhir.instance.model.api.IBaseResource;

/**
* Implements the Rule interface. It represents a rule with a name, description, warning message,
Expand Down Expand Up @@ -62,12 +61,13 @@ public List<String> getValidations() {
}

@Override
public boolean isValid(IBaseResource resource) {
public boolean isValid(FhirResource<?> resource) {
return validations.stream()
.allMatch(
validation -> {
try {
return fhirEngine.evaluateCondition(resource, validation);
return fhirEngine.evaluateCondition(
resource.getUnderlyingResource(), validation);
} catch (Exception e) {
logger.logError(
"Rule ["
Expand All @@ -82,12 +82,13 @@ public boolean isValid(IBaseResource resource) {
}

@Override
public boolean appliesTo(IBaseResource resource) {
public boolean appliesTo(FhirResource<?> resource) {
return conditions.stream()
.allMatch(
condition -> {
try {
return fhirEngine.evaluateCondition(resource, condition);
return fhirEngine.evaluateCondition(
resource.getUnderlyingResource(), condition);
} catch (Exception e) {
logger.logError(
"Rule ["
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gov.hhs.cdc.trustedintermediary.external.hapi;

import gov.hhs.cdc.trustedintermediary.etor.ruleengine.FhirResource;
import org.hl7.fhir.instance.model.api.IBaseResource;

public class HapiFhirResource implements FhirResource<IBaseResource> {

private final IBaseResource innerResource;

public HapiFhirResource(IBaseResource innerResource) {
this.innerResource = innerResource;
}

@Override
public IBaseResource getUnderlyingResource() {
return innerResource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gov.hhs.cdc.trustedintermediary

import gov.hhs.cdc.trustedintermediary.etor.ruleengine.FhirResource

class FhirResourceMock<T> implements FhirResource<T> {

private T innerResource

FhirResourceMock(T innerResource) {
this.innerResource = innerResource
}

@Override
public T getUnderlyingResource() {
return innerResource
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ public String encodeResourceToJson(Object resource) {
/**
* Evaluate a FHIR Path expression for a given Resource to find if the expression has matches
*
* @param root FHIR resource the evaluation starts from.
* @param resource FHIR resource the evaluation starts from.
* @param expression FHIR Path statement to run evaluations on.
* @return True if the expression has at least one match for the given root, else false.
*/
@Override
public Boolean evaluateCondition(IBaseResource root, String expression) {
var result = PATH_ENGINE.evaluateFirst(root, expression, BooleanType.class);
public Boolean evaluateCondition(Object resource, String expression) {
var result =
PATH_ENGINE.evaluateFirst((IBaseResource) resource, expression, BooleanType.class);
return result.map(BooleanType::booleanValue).orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ <T extends IBaseResource> T parseResource(String fhirResource, Class<T> clazz)

String encodeResourceToJson(Object resource);

Boolean evaluateCondition(IBaseResource root, String expression);
Boolean evaluateCondition(Object resource, String expression);
}

0 comments on commit 468d98a

Please sign in to comment.