use of com.ibm.cohort.fhir.client.config.FhirClientBuilder in project quality-measure-and-cohort-service by Alvearie.
the class CohortEngineRestHandler method getMeasureParametersById.
@POST
@Path("/fhir/measure/{measure_id}/parameters")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Get measure parameters by id", notes = CohortEngineRestHandler.MEASURE_API_NOTES, response = MeasureParameterInfoList.class, nickname = "get_measure_parameters_by_id", tags = { "FHIR Measures" })
@ApiImplicitParams({ @ApiImplicitParam(name = FHIR_DATA_SERVER_CONFIG_PART, value = CohortEngineRestHandler.EXAMPLE_DATA_SERVER_CONFIG_JSON, dataTypeClass = FhirServerConfig.class, required = true, paramType = "form", type = "file") })
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful Operation", response = MeasureParameterInfoList.class), @ApiResponse(code = 400, message = "Bad Request", response = ServiceErrorList.class), @ApiResponse(code = 500, message = "Server Error", response = ServiceErrorList.class) })
public Response getMeasureParametersById(@ApiParam(value = ServiceBaseConstants.MINOR_VERSION_DESCRIPTION, required = true, defaultValue = ServiceBuildConstants.DATE) @QueryParam(CohortEngineRestHandler.VERSION) String version, @ApiParam(hidden = true, type = "file", required = true) IMultipartBody multipartBody, @ApiParam(value = CohortEngineRestHandler.MEASURE_ID_DESC, required = true) @PathParam(CohortEngineRestHandler.MEASURE_ID) String measureId) {
final String methodName = MethodNames.GET_MEASURE_PARAMETERS_BY_ID.getName();
Response response = null;
try {
// Perform api setup
Response errorResponse = ServiceBaseUtility.apiSetup(version, logger, methodName);
if (errorResponse != null) {
return errorResponse;
}
IAttachment dataSourceAttachment = multipartBody.getAttachment(FHIR_DATA_SERVER_CONFIG_PART);
if (dataSourceAttachment == null) {
throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", FHIR_DATA_SERVER_CONFIG_PART));
}
// deserialize the MeasuresEvaluation request
ObjectMapper om = new ObjectMapper();
FhirServerConfig fhirServerConfig = om.readValue(dataSourceAttachment.getDataHandler().getInputStream(), FhirServerConfig.class);
// validate the contents of the fhirServerConfig
validateBean(fhirServerConfig);
// get the fhir client object used to call to FHIR
FhirClientBuilder clientBuilder = FhirClientBuilderFactory.newInstance().newFhirClientBuilder();
IGenericClient measureClient = clientBuilder.createFhirClient(fhirServerConfig);
// resolve the measure, and return the parameter info for all the libraries linked to by the measure
FhirResourceResolver<Measure> measureResolver = R4FhirServerResourceResolverFactory.createMeasureResolver(measureClient);
List<MeasureParameterInfo> parameterInfoList = FHIRRestUtils.getParametersForMeasureId(measureResolver, measureId);
// return the results
MeasureParameterInfoList status = new MeasureParameterInfoList().measureParameterInfoList(parameterInfoList);
response = Response.ok(status).build();
} catch (Throwable e) {
// map any exceptions caught into the proper REST error response objects
return new CohortServiceExceptionMapper().toResponse(e);
} finally {
// Perform api cleanup
Response errorResponse = ServiceBaseUtility.apiCleanup(logger, methodName);
if (errorResponse != null) {
response = errorResponse;
}
}
return response;
}
use of com.ibm.cohort.fhir.client.config.FhirClientBuilder in project quality-measure-and-cohort-service by Alvearie.
the class CohortEngineRestHandler method getMeasureParameters.
@POST
@Path("/fhir/measure/identifier/{measure_identifier_value}/parameters")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Get measure parameters", notes = CohortEngineRestHandler.MEASURE_API_NOTES, response = MeasureParameterInfoList.class, authorizations = { @Authorization(value = "BasicAuth") }, responseContainer = "List", tags = { "FHIR Measures" })
@ApiImplicitParams({ @ApiImplicitParam(name = FHIR_DATA_SERVER_CONFIG_PART, value = CohortEngineRestHandler.EXAMPLE_DATA_SERVER_CONFIG_JSON, dataTypeClass = FhirServerConfig.class, required = true, paramType = "form", type = "file") })
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful Operation", response = MeasureParameterInfoList.class), @ApiResponse(code = 400, message = "Bad Request", response = ServiceErrorList.class), @ApiResponse(code = 500, message = "Server Error", response = ServiceErrorList.class) })
public Response getMeasureParameters(@ApiParam(value = ServiceBaseConstants.MINOR_VERSION_DESCRIPTION, required = true, defaultValue = ServiceBuildConstants.DATE) @QueryParam(CohortEngineRestHandler.VERSION) String version, @ApiParam(hidden = true, type = "file", required = true) IMultipartBody multipartBody, @ApiParam(value = CohortEngineRestHandler.MEASURE_IDENTIFIER_VALUE_DESC, required = true) @PathParam(CohortEngineRestHandler.MEASURE_IDENTIFIER_VALUE) String measureIdentifierValue, @ApiParam(value = CohortEngineRestHandler.MEASURE_IDENTIFIER_SYSTEM_DESC, required = false) @QueryParam(CohortEngineRestHandler.MEASURE_IDENTIFIER_SYSTEM) String measureIdentifierSystem, @ApiParam(value = CohortEngineRestHandler.MEASURE_VERSION_DESC, required = false) @QueryParam(CohortEngineRestHandler.MEASURE_VERSION) String measureVersion) {
final String methodName = MethodNames.GET_MEASURE_PARAMETERS.getName();
Response response = null;
try {
// Perform api setup
Response errorResponse = ServiceBaseUtility.apiSetup(version, logger, methodName);
if (errorResponse != null) {
return errorResponse;
}
IAttachment dataSourceAttachment = multipartBody.getAttachment(FHIR_DATA_SERVER_CONFIG_PART);
if (dataSourceAttachment == null) {
throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", FHIR_DATA_SERVER_CONFIG_PART));
}
// deserialize the MeasuresEvaluation request
ObjectMapper om = new ObjectMapper();
FhirServerConfig fhirServerConfig = om.readValue(dataSourceAttachment.getDataHandler().getInputStream(), FhirServerConfig.class);
// validate the contents of the fhirServerConfig
validateBean(fhirServerConfig);
// get the fhir client object used to call to FHIR
FhirClientBuilder clientBuilder = FhirClientBuilderFactory.newInstance().newFhirClientBuilder();
IGenericClient measureClient = clientBuilder.createFhirClient(fhirServerConfig);
// build the identifier object which is used by the fhir client
// to find the measure
Identifier identifier = new Identifier().setValue(measureIdentifierValue).setSystem(measureIdentifierSystem);
// resolve the measure, and return the parameter info for all the libraries linked to by the measure
FhirResourceResolver<Measure> measureResolver = R4FhirServerResourceResolverFactory.createMeasureResolver(measureClient);
List<MeasureParameterInfo> parameterInfoList = FHIRRestUtils.getParametersForMeasureIdentifier(measureResolver, identifier, measureVersion);
// return the results
MeasureParameterInfoList status = new MeasureParameterInfoList().measureParameterInfoList(parameterInfoList);
response = Response.ok(status).build();
} catch (Throwable e) {
// map any exceptions caught into the proper REST error response objects
return new CohortServiceExceptionMapper().toResponse(e);
} finally {
// Perform api cleanup
Response errorResponse = ServiceBaseUtility.apiCleanup(logger, methodName);
if (errorResponse != null) {
response = errorResponse;
}
}
return response;
}
use of com.ibm.cohort.fhir.client.config.FhirClientBuilder in project quality-measure-and-cohort-service by Alvearie.
the class CohortEngineRestHandler method createMeasureEvaluator.
private MeasureEvaluator createMeasureEvaluator(InputStream inputStream, FhirServerConfig dataServerConfig, FhirServerConfig terminologyServerConfig, Boolean expandValueSets, Integer searchPageSize, RetrieveCacheContext retrieveCacheContext, FhirContext fhirContext) throws IOException {
FhirClientBuilder clientBuilder = FhirClientBuilderFactory.newInstance().newFhirClientBuilder(fhirContext);
IGenericClient dataClient = clientBuilder.createFhirClient(dataServerConfig);
IGenericClient terminologyClient = dataClient;
if (terminologyServerConfig != null) {
terminologyClient = clientBuilder.createFhirClient(terminologyServerConfig);
}
IParser parser = dataClient.getFhirContext().newJsonParser();
String[] searchPaths = new String[] { "fhirResources", "fhirResources/libraries" };
R4QualityMeasureResolverFactory resolverFactory = new R4QualityMeasureResolverFactory(parser);
R4QualityMeasureResolvers resolvers = resolverFactory.fromZipStream(new ZipInputStream(inputStream), searchPaths);
FhirResourceResolver<Library> libraryResolver = resolvers.getLibraryResolver();
FhirResourceResolver<Measure> measureResolver = resolvers.getMeasureResolver();
R4LibraryDependencyGatherer libraryDependencyGatherer = new R4LibraryDependencyGatherer(libraryResolver);
CqlTerminologyProvider terminologyProvider = new R4RestFhirTerminologyProvider(terminologyClient);
if (expandValueSets == null) {
expandValueSets = R4DataProviderFactory.DEFAULT_IS_EXPAND_VALUE_SETS;
}
if (searchPageSize == null) {
searchPageSize = R4DataProviderFactory.DEFAULT_PAGE_SIZE;
}
Map<String, CqlDataProvider> dataProviders = R4DataProviderFactory.createDataProviderMap(dataClient, terminologyProvider, retrieveCacheContext, R4FhirModelResolverFactory.createCachingResolver(), expandValueSets, searchPageSize);
return new MeasureEvaluator(measureResolver, libraryResolver, libraryDependencyGatherer, terminologyProvider, dataProviders);
}
use of com.ibm.cohort.fhir.client.config.FhirClientBuilder in project quality-measure-and-cohort-service by Alvearie.
the class CohortEngineRestHandler method evaluateCohort.
@POST
@Path("/cohort-evaluation")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_JSON })
@ApiOperation(value = "Evaluates a specific define within a CQL for a set of patients", notes = COHORT_EVALUATION_API_NOTES, response = String.class, tags = { "Cohort Evaluation" }, nickname = "evaluate_cohort", extensions = { @Extension(properties = { @ExtensionProperty(name = DarkFeatureSwaggerFilter.DARK_FEATURE_NAME, value = CohortEngineRestConstants.DARK_LAUNCHED_COHORT_EVALUATION) }) })
@ApiImplicitParams({ // This is necessary for the dark launch feature
@ApiImplicitParam(access = DarkFeatureSwaggerFilter.DARK_FEATURE_CONTROLLED, paramType = "header", dataType = "string"), @ApiImplicitParam(name = REQUEST_DATA_PART, value = EXAMPLE_COHORT_REQUEST_DATA_JSON, dataTypeClass = CohortEvaluation.class, required = true, paramType = "form", type = "file"), @ApiImplicitParam(name = CQL_DEFINITION, value = CQL_REQUIREMENTS, dataTypeClass = File.class, required = true, paramType = "form", type = "file") })
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful Operation", response = CohortResult.class), @ApiResponse(code = 400, message = "Bad Request", response = ServiceErrorList.class), @ApiResponse(code = 500, message = "Server Error", response = ServiceErrorList.class) })
public Response evaluateCohort(@Context HttpServletRequest request, @ApiParam(value = ServiceBaseConstants.MINOR_VERSION_DESCRIPTION, required = true, defaultValue = ServiceBuildConstants.DATE) @QueryParam(CohortEngineRestHandler.VERSION) String version, @ApiParam(hidden = true, type = "file", required = true) IMultipartBody multipartBody) {
final String methodName = MethodNames.EVALUATE_COHORT.getName();
Response response = null;
// Error out if feature is not enabled
ServiceBaseUtility.isDarkFeatureEnabled(CohortEngineRestConstants.DARK_LAUNCHED_MEASURE_EVALUATION);
try {
// Perform api setup
Response errorResponse = ServiceBaseUtility.apiSetup(version, logger, methodName);
if (errorResponse != null) {
return errorResponse;
}
if (multipartBody == null) {
throw new IllegalArgumentException("A multipart/form-data body is required");
}
IAttachment metadataAttachment = multipartBody.getAttachment(REQUEST_DATA_PART);
if (metadataAttachment == null) {
throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", REQUEST_DATA_PART));
}
// deserialize the MeasuresEvaluation request
ObjectMapper om = new ObjectMapper();
CohortEvaluation evaluationRequest = om.readValue(metadataAttachment.getDataHandler().getInputStream(), CohortEvaluation.class);
FhirServerConfig dataServerConfig = evaluationRequest.getDataServerConfig();
FhirServerConfig terminologyServerConfig = evaluationRequest.getTerminologyServerConfig() == null ? evaluationRequest.getDataServerConfig() : evaluationRequest.getTerminologyServerConfig();
IAttachment cqlAttachment = multipartBody.getAttachment(CQL_DEFINITION);
if (cqlAttachment == null) {
throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", CQL_DEFINITION));
}
// validate the contents of the fhirServerConfig
validateBean(dataServerConfig);
validateBean(terminologyServerConfig);
FhirClientBuilderFactory clientBuilderFactory = FhirClientBuilderFactory.newInstance();
FhirClientBuilder clientBuilder = clientBuilderFactory.newFhirClientBuilder();
CqlLibraryProvider libraryProvider;
try (InputStream is = cqlAttachment.getDataHandler().getInputStream()) {
ZipInputStream zis = new ZipInputStream(is);
MapCqlLibraryProviderFactory libraryProviderFactory = new MapCqlLibraryProviderFactory();
String[] attachmentHeaders = cqlAttachment.getHeader("Content-Disposition").split(";");
String filename = null;
for (String header : attachmentHeaders) {
if (header.contains("filename")) {
filename = FilenameUtils.removeExtension(header.split("=")[1].replaceAll("\"", ""));
}
}
String[] searchPaths = new String[] { "cql", filename + "/cql" };
libraryProvider = libraryProviderFactory.fromZipStream(zis, searchPaths);
}
CqlLibraryProvider fhirClasspathProvider = new ClasspathCqlLibraryProvider();
libraryProvider = new PriorityCqlLibraryProvider(libraryProvider, fhirClasspathProvider);
CqlToElmTranslator translator = new CqlToElmTranslator();
libraryProvider = new TranslatingCqlLibraryProvider(libraryProvider, translator);
IGenericClient dataClient = clientBuilder.createFhirClient(dataServerConfig);
IGenericClient termClient = clientBuilder.createFhirClient(terminologyServerConfig);
CqlTerminologyProvider termProvider = new R4RestFhirTerminologyProvider(termClient);
List<String> passingPatients;
try (RetrieveCacheContext retrieveCacheContext = new DefaultRetrieveCacheContext()) {
passingPatients = evaluateCohort(dataClient, termProvider, libraryProvider, retrieveCacheContext, evaluationRequest);
}
response = Response.status(Response.Status.OK).header("Content-Type", "application/json").entity(new CohortResult(passingPatients)).build();
} catch (Throwable e) {
// map any exceptions caught into the proper REST error response objects
response = new CohortServiceExceptionMapper().toResponse(e);
} finally {
// Perform api cleanup
Response errorResponse = ServiceBaseUtility.apiCleanup(logger, methodName);
if (errorResponse != null) {
response = errorResponse;
}
}
return response;
}
use of com.ibm.cohort.fhir.client.config.FhirClientBuilder in project quality-measure-and-cohort-service by Alvearie.
the class PatientTestBase method setupTestFor.
protected CqlEvaluator setupTestFor(DomainResource resource, FhirServerConfig fhirConfig, String firstPackage, String... packages) {
mockFhirResourceRetrieval("/metadata?_format=json", getCapabilityStatement());
mockFhirResourceRetrieval(resource);
CqlEvaluator evaluator = null;
if (firstPackage != null) {
FhirClientBuilderFactory factory = FhirClientBuilderFactory.newInstance();
FhirClientBuilder fhirClientBuilder = factory.newFhirClientBuilder();
CqlLibraryProvider classpathCqlLibraryProvider = new ClasspathCqlLibraryProvider(firstPackage, packages);
CqlToElmTranslator translator = new CqlToElmTranslator();
CqlLibraryProvider libraryProvider = new TranslatingCqlLibraryProvider(classpathCqlLibraryProvider, translator);
IGenericClient testClient = fhirClientBuilder.createFhirClient(fhirConfig);
CqlTerminologyProvider termProvider = new R4RestFhirTerminologyProvider(testClient);
CqlDataProvider dataProvider = R4DataProviderFactory.createDataProvider(testClient, termProvider, null, R4FhirModelResolverFactory.createCachingResolver(), true, null);
evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setDataProvider(dataProvider).setTerminologyProvider(termProvider).setCacheContexts(false);
}
return evaluator;
}
Aggregations