Search in sources :

Example 31 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project quality-measure-and-cohort-service by Alvearie.

the class CohortEngineRestHandler method createValueSet.

@POST
@Path("/valueset/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_JSON })
@ApiImplicitParams({ // This is necessary for the dark launch feature
@ApiImplicitParam(access = DarkFeatureSwaggerFilter.DARK_FEATURE_CONTROLLED, paramType = "header", dataType = "string"), @ApiImplicitParam(name = FHIR_DATA_SERVER_CONFIG_PART, value = CohortEngineRestHandler.EXAMPLE_DATA_SERVER_CONFIG_JSON, dataTypeClass = FhirServerConfig.class, required = true, paramType = "form", type = "file"), @ApiImplicitParam(name = VALUE_SET_PART, value = VALUE_SET_DESC, dataTypeClass = File.class, required = true, paramType = "form", type = "file"), @ApiImplicitParam(name = CUSTOM_CODE_SYSTEM, value = CUSTOM_CODE_SYSTEM_DESC, dataTypeClass = File.class, paramType = "form", type = "file") })
@ApiResponses(value = { @ApiResponse(code = 201, message = "Successful Operation"), @ApiResponse(code = 400, message = "Bad Request", response = ServiceErrorList.class), @ApiResponse(code = 409, message = "Conflict", response = ServiceErrorList.class), @ApiResponse(code = 500, message = "Server Error", response = ServiceErrorList.class) })
@ApiOperation(value = "Insert a new value set to the fhir server or, if it already exists, update it in place", notes = CohortEngineRestHandler.VALUE_SET_API_NOTES, tags = { "ValueSet" }, nickname = "create_value_set", extensions = { @Extension(properties = { @ExtensionProperty(name = DarkFeatureSwaggerFilter.DARK_FEATURE_NAME, value = CohortEngineRestConstants.DARK_LAUNCHED_VALUE_SET_UPLOAD) }) })
public Response createValueSet(@DefaultValue(ServiceBuildConstants.DATE) @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.VALUE_SET_UPDATE_IF_EXISTS_DESC, defaultValue = "false") @DefaultValue("false") @QueryParam(CohortEngineRestHandler.UPDATE_IF_EXISTS_PARM) boolean updateIfExists) {
    String methodName = MethodNames.CREATE_VALUE_SET.getName();
    Response response;
    ServiceBaseUtility.isDarkFeatureEnabled(CohortEngineRestConstants.DARK_LAUNCHED_VALUE_SET_UPLOAD);
    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 terminologyClient = clientBuilder.createFhirClient(fhirServerConfig);
        IAttachment valueSetAttachment = multipartBody.getAttachment(VALUE_SET_PART);
        if (valueSetAttachment == null) {
            throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", VALUE_SET_PART));
        }
        IAttachment customCodes = multipartBody.getAttachment(CUSTOM_CODE_SYSTEM);
        Map<String, String> customCodeMap = null;
        if (customCodes != null) {
            customCodeMap = ValueSetUtil.getMapFromInputStream(customCodes.getDataHandler().getInputStream());
        }
        ValueSetArtifact artifact;
        try (InputStream is = valueSetAttachment.getDataHandler().getInputStream()) {
            artifact = ValueSetUtil.createArtifact(is, customCodeMap);
        }
        ValueSetUtil.validateArtifact(artifact);
        String valueSetId = ValueSetUtil.importArtifact(terminologyClient, artifact, updateIfExists);
        if (valueSetId == null) {
            return Response.status(Response.Status.CONFLICT).header("Content-Type", "application/json").entity("{\"message\":\"Value Set already exists! Rerun with updateIfExists set to true!\"}").build();
        }
        response = Response.status(Response.Status.CREATED).header("Content-Type", "application/json").entity("{\"valueSetId\":\"" + valueSetId + "\"}").build();
    } catch (Throwable e) {
        return new CohortServiceExceptionMapper().toResponse(e);
    } finally {
        // Perform api cleanup
        Response errorResponse = ServiceBaseUtility.apiCleanup(logger, methodName);
        if (errorResponse != null) {
            response = errorResponse;
        }
    }
    return response;
}
Also used : FhirClientBuilder(com.ibm.cohort.fhir.client.config.FhirClientBuilder) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ValueSetArtifact(com.ibm.cohort.valueset.ValueSetArtifact) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

IAttachment (com.ibm.websphere.jaxrs20.multipart.IAttachment)31 Response (javax.ws.rs.core.Response)22 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)19 IMultipartBody (com.ibm.websphere.jaxrs20.multipart.IMultipartBody)16 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FhirServerConfig (com.ibm.cohort.fhir.client.config.FhirServerConfig)15 Test (org.junit.Test)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Patient (org.hl7.fhir.r4.model.Patient)9 PatientListMeasureEvaluation (com.ibm.cohort.engine.api.service.model.PatientListMeasureEvaluation)8 FhirContext (ca.uhn.fhir.context.FhirContext)7 IParser (ca.uhn.fhir.parser.IParser)7 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)7 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponse (io.swagger.annotations.ApiResponse)7 ApiResponses (io.swagger.annotations.ApiResponses)7 File (java.io.File)7 Consumes (javax.ws.rs.Consumes)7 POST (javax.ws.rs.POST)7