Skip to content

Commit

Permalink
Added spotless formatting and converted FhirPathR5 expression to be A…
Browse files Browse the repository at this point in the history
…ndroid compatible
  • Loading branch information
jkiddo committed Jun 21, 2024
1 parent a55f2b1 commit 1c49630
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public class SubscriptionTriggerMatcher {
private final MemoryCacheService myMemoryCacheService;

public SubscriptionTriggerMatcher(
SubscriptionTopicSupport theSubscriptionTopicSupport,
ResourceModifiedMessage theMsg,
SubscriptionTopic.SubscriptionTopicResourceTriggerComponent theTrigger,
MemoryCacheService theMemoryCacheService) {
SubscriptionTopicSupport theSubscriptionTopicSupport,
ResourceModifiedMessage theMsg,
SubscriptionTopic.SubscriptionTopicResourceTriggerComponent theTrigger,
MemoryCacheService theMemoryCacheService) {
mySubscriptionTopicSupport = theSubscriptionTopicSupport;
myOperation = theMsg.getOperationType();
myResource = theMsg.getPayload(theSubscriptionTopicSupport.getFhirContext());
Expand All @@ -74,19 +74,19 @@ public SubscriptionTriggerMatcher(

public InMemoryMatchResult match() {
List<Enumeration<SubscriptionTopic.InteractionTrigger>> supportedInteractions =
myTrigger.getSupportedInteraction();
myTrigger.getSupportedInteraction();
if (SubscriptionTopicUtil.matches(myOperation, supportedInteractions)) {
SubscriptionTopic.SubscriptionTopicResourceTriggerQueryCriteriaComponent queryCriteria =
myTrigger.getQueryCriteria();
myTrigger.getQueryCriteria();
String fhirPathCriteria = myTrigger.getFhirPathCriteria();
return match(queryCriteria, fhirPathCriteria);
}
return InMemoryMatchResult.noMatch();
}

private InMemoryMatchResult match(
SubscriptionTopic.SubscriptionTopicResourceTriggerQueryCriteriaComponent theQueryCriteria,
String theFhirPathCriteria) {
SubscriptionTopic.SubscriptionTopicResourceTriggerQueryCriteriaComponent theQueryCriteria,
String theFhirPathCriteria) {
String previousCriteria = theQueryCriteria.getPrevious();
String currentCriteria = theQueryCriteria.getCurrent();
InMemoryMatchResult previousMatches = InMemoryMatchResult.fromBoolean(previousCriteria == null);
Expand All @@ -104,32 +104,32 @@ private InMemoryMatchResult match(

if (previousCriteria != null) {
if (myOperation == ResourceModifiedMessage.OperationTypeEnum.UPDATE
|| myOperation == ResourceModifiedMessage.OperationTypeEnum.DELETE) {
|| myOperation == ResourceModifiedMessage.OperationTypeEnum.DELETE) {

Optional<IBaseResource> oPreviousVersion = myPreviousVersionReader.readPreviousVersion(myResource);
if (oPreviousVersion.isPresent()) {
previousMatches = matchResource(oPreviousVersion.get(), previousCriteria);
} else {
ourLog.warn(
"Resource {} has a version of 1, which should not be the case for a create or delete operation",
myResource.getIdElement().toUnqualifiedVersionless());
"Resource {} has a version of 1, which should not be the case for a create or delete operation",
myResource.getIdElement().toUnqualifiedVersionless());
}
}
}
// WIP STR5 implement resultForCreate and resultForDelete
if (theQueryCriteria.getRequireBoth()) {
return InMemoryMatchResult.and(
InMemoryMatchResult.and(previousMatches, currentMatches), fhirPathCriteriaEvaluationResult);
InMemoryMatchResult.and(previousMatches, currentMatches), fhirPathCriteriaEvaluationResult);
} else {
return InMemoryMatchResult.and(
InMemoryMatchResult.or(previousMatches, currentMatches), fhirPathCriteriaEvaluationResult);
InMemoryMatchResult.or(previousMatches, currentMatches), fhirPathCriteriaEvaluationResult);
}
}

private InMemoryMatchResult evaluateFhirPathCriteria(String theFhirPathCriteria) {
if (!Strings.isNullOrEmpty(theFhirPathCriteria)) {
IFhirPath fhirPathEngine =
mySubscriptionTopicSupport.getFhirContext().newFhirPath();
mySubscriptionTopicSupport.getFhirContext().newFhirPath();
fhirPathEngine.setEvaluationContext(new IFhirPathEvaluationContext() {

@Override
Expand All @@ -146,26 +146,26 @@ public List<IBase> resolveConstant(Object appContext, String name, boolean befor
});
try {
IFhirPath.IParsedExpression expression = myMemoryCacheService.get(
MemoryCacheService.CacheEnum.FHIRPATH_EXPRESSION, theFhirPathCriteria, exp -> {
try {
return fhirPathEngine.parse(exp);
} catch (FHIRException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(Msg.code(2534) + e.getMessage(), e);
}
});
MemoryCacheService.CacheEnum.FHIRPATH_EXPRESSION, theFhirPathCriteria, exp -> {
try {
return fhirPathEngine.parse(exp);
} catch (FHIRException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(Msg.code(2534) + e.getMessage(), e);
}
});

List<IBase> result = fhirPathEngine.evaluate(myResource, expression, IBase.class);

return parseResult(theFhirPathCriteria, result);

} catch (FHIRException fhirException) {
ourLog.warn(
"Subscription topic {} has a fhirPathCriteria that is not valid: {}",
myTrigger.getId(),
theFhirPathCriteria,
fhirException);
"Subscription topic {} has a fhirPathCriteria that is not valid: {}",
myTrigger.getId(),
theFhirPathCriteria,
fhirException);
return InMemoryMatchResult.unsupportedFromReason(fhirException.getMessage());
}
}
Expand All @@ -175,41 +175,40 @@ public List<IBase> resolveConstant(Object appContext, String name, boolean befor
private InMemoryMatchResult parseResult(String theFhirPathCriteria, List<IBase> result) {
if (result == null) {
return InMemoryMatchResult.unsupportedFromReason(MessageFormatter.format(
"FhirPath evaluation criteria '{}' from Subscription topic: '{}' resulted in null results.",
theFhirPathCriteria,
myTrigger.getId()).getMessage());
"FhirPath evaluation criteria '{}' from Subscription topic: '{}' resulted in null results.",
theFhirPathCriteria,
myTrigger.getId())
.getMessage());
}

if (result.size() != 1) {
return InMemoryMatchResult.unsupportedFromReason(MessageFormatter.arrayFormat(
"FhirPath evaluation criteria '{}' from Subscription topic: '{}' resulted in '{}' results. Expected 1.",
new String[]{
theFhirPathCriteria,
myTrigger.getId(),
String.valueOf(result.size())}).getMessage());
"FhirPath evaluation criteria '{}' from Subscription topic: '{}' resulted in '{}' results. Expected 1.",
new String[] {theFhirPathCriteria, myTrigger.getId(), String.valueOf(result.size())})
.getMessage());
}

if (!(result.get(0) instanceof BooleanType)) {
return InMemoryMatchResult.unsupportedFromReason(MessageFormatter.arrayFormat(
"FhirPath evaluation criteria '{}' from Subscription topic: '{}' resulted in a non-boolean result: '{}'",
new String[]{
theFhirPathCriteria,
myTrigger.getId(),
result.get(0).getClass().getName()}).getMessage());
"FhirPath evaluation criteria '{}' from Subscription topic: '{}' resulted in a non-boolean result: '{}'",
new String[] {
theFhirPathCriteria,
myTrigger.getId(),
result.get(0).getClass().getName()
})
.getMessage());
}
return InMemoryMatchResult.fromBoolean(
((BooleanType) result.get(0)).booleanValue());
return InMemoryMatchResult.fromBoolean(((BooleanType) result.get(0)).booleanValue());
}


private InMemoryMatchResult matchResource(IBaseResource theResource, String theCriteria) {
InMemoryMatchResult result =
mySubscriptionTopicSupport.getSearchParamMatcher().match(theCriteria, theResource, mySrd);
mySubscriptionTopicSupport.getSearchParamMatcher().match(theCriteria, theResource, mySrd);
if (!result.supported()) {
ourLog.warn(
"Subscription topic {} has a query criteria that is not supported in-memory: {}",
myTrigger.getId(),
theCriteria);
"Subscription topic {} has a query criteria that is not supported in-memory: {}",
myTrigger.getId(),
theCriteria);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hl7.fhir.r5.model.IdType;
import org.hl7.fhir.r5.model.ValueSet;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -100,9 +101,12 @@ public List<Base> resolveConstant(
boolean beforeContext,
boolean explicitConstant)
throws PathEngineException {
return theEvaluationContext.resolveConstant(appContext, name, beforeContext).stream()

return Collections.unmodifiableList(
theEvaluationContext.resolveConstant(appContext, name, beforeContext).stream()
.map(Base.class::cast)
.collect(Collectors.toUnmodifiableList());
.collect(Collectors.toList())
);
}

@Override
Expand Down

0 comments on commit 1c49630

Please sign in to comment.