FHIR Chat · Named search parameter for all resources · hapi

Stream: hapi

Topic: Named search parameter for all resources


view this post on Zulip John Grimes (Feb 24 2020 at 20:45):

I'm trying to define a named search parameter that works across ALL resources.

This doesn't work, as there doesn't seem to be enough information within either the return type or the type parameter to the annotation for HAPI to associate it with a resource type:

  @Search(queryName = "foo")
  public IBundleProvider patientSearch(@OptionalParam(name = "bar") StringOrListParam bars) {
    return null;
  }

This works, but it requires me to have an annotated method for every resource type:

  @Search(type = Patient.class, queryName = "foo")
  public IBundleProvider patientSearch(@OptionalParam(name = "bar") StringOrListParam bars) {
    return null;
  }

  @Search(type = DiagnosticReport.class, queryName = "foo")
  public IBundleProvider reportSearch(@OptionalParam(name = "bar") StringOrListParam bars) {
    return null;
  }

...

Is there a way to define a named parameter across all resource types that allows me to receive the resource type at runtime and handle that myself? Do I have to generate code to implement this?

BTW, I also tried interceptors... but it seems to bomb out somewhere between SERVER_INCOMING_REQUEST_POST_PROCESSED and SERVER_INCOMING_REQUEST_PRE_HANDLED, which makes it difficult to get at both the search parameter and resource type information.

view this post on Zulip John Grimes (Feb 27 2020 at 06:30):

With the help of @Michael Lawley I figured out a nice way of doing this:

public class SearchProvider<T extends IBaseResource> implements IResourceProvider {

  private final Class<T> resourceType;

  public SearchProvider(Class<T> resourceType) {
    this.resourceType = resourceType;
  }

  @Override
  public Class<? extends IBaseResource> getResourceType() {
    return resourceType;
  }

  @Search(queryName = "foo")
  public IBundleProvider search(@OptionalParam(name = "bar") StringOrListParam bars) {
    return null;
  }

}

Then just register one for each resource type, using the built-in enum within HAPI:

for (ResourceType resourceType : ResourceType.values()) {
  Class<? extends IBaseResource> resourceTypeClass = getFhirContext()
      .getResourceDefinition(resourceType.name()).getImplementingClass();
  registerProvider(new SearchProvider(resourceTypeClass));
}

Last updated: Apr 12 2022 at 19:14 UTC