|
| 1 | +package de.medizininformatikinitiative.torch.rest; |
| 2 | + |
| 3 | +import ca.uhn.fhir.context.FhirContext; |
| 4 | +import ca.uhn.fhir.parser.DataFormatException; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import de.medizininformatikinitiative.torch.model.crtdl.Crtdl; |
| 7 | +import de.medizininformatikinitiative.torch.model.crtdl.ExtractDataParameters; |
| 8 | +import org.hl7.fhir.r4.model.Base64BinaryType; |
| 9 | +import org.hl7.fhir.r4.model.Parameters; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static java.util.Objects.requireNonNull; |
| 17 | + |
| 18 | +@Component |
| 19 | +public class ExtractDataParametersParser { |
| 20 | + |
| 21 | + private final FhirContext fhirContext; |
| 22 | + private final ObjectMapper objectMapper; |
| 23 | + |
| 24 | + public ExtractDataParametersParser(FhirContext fhirContext, ObjectMapper objectMapper) { |
| 25 | + this.fhirContext = requireNonNull(fhirContext); |
| 26 | + this.objectMapper = requireNonNull(objectMapper); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Parses the CRDTL content from a byte array. |
| 31 | + * |
| 32 | + * @param content the byte array containing the CRDTL content |
| 33 | + * @return the parsed Crtdl object |
| 34 | + * @throws IOException if there is an error during parsing |
| 35 | + */ |
| 36 | + private Crtdl parseCrtdlContent(byte[] content) throws IOException { |
| 37 | + return objectMapper.readValue(content, Crtdl.class); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Parses a FHIR {@link Parameters} resource containing CRTDL data and optional patient IDs. |
| 42 | + * |
| 43 | + * <p>The input must be a valid JSON representation of a Parameters resource |
| 44 | + * that includes: |
| 45 | + * <ul> |
| 46 | + * <li>A parameter named {@code crtdl} with a {@code base64Binary} value containing the CRTDL content.</li> |
| 47 | + * <li>Optional parameters named {@code patient} with {@code string} values for patient IDs.</li> |
| 48 | + * </ul> |
| 49 | + * |
| 50 | + * @param body the JSON string representing a FHIR Parameters resource |
| 51 | + * @return an {@link ExtractDataParameters} containing the parsed CRTDL content and patient IDs |
| 52 | + * @throws IllegalArgumentException if the input cannot be parsed as a valid Parameters |
| 53 | + * resource, the Parameters resource is empty, no crtdl |
| 54 | + * parameter with base64 encoded content is found, or the |
| 55 | + * CRTDL content cannot be read due to an IOException |
| 56 | + */ |
| 57 | + public ExtractDataParameters parseParameters(String body) { |
| 58 | + Parameters parameters; |
| 59 | + try { |
| 60 | + parameters = fhirContext.newJsonParser().parseResource(Parameters.class, body); |
| 61 | + } catch (DataFormatException e) { |
| 62 | + throw new IllegalArgumentException("Input is not a valid Parameters Resource", e); |
| 63 | + } |
| 64 | + if (parameters.isEmpty()) { |
| 65 | + throw new IllegalArgumentException("Empty Parameters resource provided"); |
| 66 | + } |
| 67 | + try { |
| 68 | + byte[] crtdlContent = null; |
| 69 | + List<String> patientIds = new ArrayList<>(); |
| 70 | + |
| 71 | + for (var parameter : parameters.getParameter()) { |
| 72 | + if (parameter.hasValue()) { |
| 73 | + var value = parameter.getValue(); |
| 74 | + if ("crtdl".equals(parameter.getName()) && value.hasType("base64Binary")) { |
| 75 | + crtdlContent = ((Base64BinaryType) parameter.getValue()).getValue(); |
| 76 | + } |
| 77 | + if ("patient".equals(parameter.getName()) && value.hasType("string")) { |
| 78 | + patientIds.add(value.primitiveValue()); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + if (crtdlContent == null) { |
| 83 | + throw new IllegalArgumentException("No base64 encoded CRDTL content found in Parameters resource"); |
| 84 | + } |
| 85 | + // Process crtdl content, potentially using patientIds |
| 86 | + return new ExtractDataParameters(parseCrtdlContent(crtdlContent), patientIds); |
| 87 | + } catch (IOException e) { |
| 88 | + throw new IllegalArgumentException("Reading CRTDL Failed with IO Exception", e); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
0 commit comments