Skip to content

Commit edd1438

Browse files
committed
Refactor FhirController
1 parent bc3fc4d commit edd1438

File tree

14 files changed

+625
-216
lines changed

14 files changed

+625
-216
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@
190190
<artifactId>mockito-junit-jupiter</artifactId>
191191
<scope>test</scope>
192192
</dependency>
193+
<dependency>
194+
<groupId>org.springframework.boot</groupId>
195+
<artifactId>spring-boot-starter-validation</artifactId>
196+
</dependency>
193197

194198
</dependencies>
195199

src/main/java/de/medizininformatikinitiative/torch/Torch.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.context.annotation.ComponentScan;
65

76

87
@SpringBootApplication
9-
@ComponentScan(basePackages = {
10-
"de.medizininformatikinitiative.torch.config",
11-
"de.medizininformatikinitiative.torch.rest",
12-
"de.medizininformatikinitiative.torch.service"
13-
})
148
public class Torch {
159

1610
public static void main(String[] args) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.medizininformatikinitiative.torch.model.crtdl;
2+
3+
import java.util.List;
4+
5+
public record ExtractDataParameters(Crtdl crtdl, List<String> patientIds) {
6+
}
7+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)