HAPI: How to customize resource fetching during validation?

56 views
Skip to first unread message

Haimanot Tekie (HT Show)

unread,
Mar 22, 2023, 6:05:54 AM3/22/23
to HAPI FHIR

Hello 👋 
How can I configure FhirInstanceValidator so that a validation of a QuestionnaireResponse can be performed against a base Questionnaire that is passed as an object, instead of trying to get the Questionnaire by resolving the questionnaire url that is present in QuestionnaireResponse?

Currently, HAPI is trying to fetch the resource using http when encountering a URI, but I would like to fetch the resource from a database instead.

Here is my code below:

My question is: how can I configure FhirInstanceValidator so that the questionnaire can be fetched from memory/db, and not from an a rest endpoint 

@JsonCreator

public FhirQuestionnaireDataReportContent(JsonNode response, JsonNode questionnaire) {
//instantiate the context
FhirContext ctx = FhirContext.forR4();

final IParser iParser = ctx.newJsonParser();
iParser.setParserErrorHandler(new StrictErrorHandler());

// the resource that I want to validate is the response, and I have the questionnaire that the response is based on
this.response = iParser.parseResource(QuestionnaireResponse.class, JsonUtil.toJSONString(response));
this.questionnaire = iParser.parseResource(Questionnaire.class, JsonUtil.toJSONString(questionnaire));

// Create a validator
FhirValidator validator = ctx.newValidator();

// Create a validation support chain
ValidationSupportChain validationSupportChain = new ValidationSupportChain(
new DefaultProfileValidationSupport(ctx),
new InMemoryTerminologyServerValidationSupport(ctx),
new CommonCodeSystemsTerminologyService(ctx)
);

FhirInstanceValidator instanceValidator = new FhirInstanceValidator(validationSupportChain);
//Question: How can I configure the instanceValidator to make the resourceFetcher to use the questionnaire object that I have?
validator.registerValidatorModule(instanceValidator);

instanceValidator.setAnyExtensionsAllowed(true);

ValidationResult result = validator.validateWithResult(this.response);
if (result.isSuccessful()) {
System.out.println("Success!");
} else {
System.out.println("Failed with " + result.getMessages().size() + " errors");
for (SingleValidationMessage next : result.getMessages()) {
System.out.println("* " + next);
}
}
}

James Agnew

unread,
Mar 22, 2023, 10:07:23 AM3/22/23
to Haimanot Tekie (HT Show), HAPI FHIR
Hi Haimanot,

I believe the answer here would be to add your own implementation of IValidationSupport to the chain, probably at the top.

In your implementation, you'd only need to implement the "fetchResource" method. Because your implementation is at the top of the chain, it'll get called for much more than just resolving Questionnaires, but you can return null for anything you aren't handing and the chain will move on to the next implementation.

Cheers,
James

--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/2f5dae63-b6cc-46f8-984c-fb4c6e6e8ef5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages