1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.ihe.fhir.core;
17
18 import org.apache.camel.Exchange;
19 import org.apache.camel.Expression;
20 import org.apache.camel.Processor;
21 import org.apache.camel.support.ExpressionAdapter;
22 import org.hl7.fhir.instance.model.api.IIdType;
23 import org.openehealth.ipf.commons.ihe.fhir.Constants;
24 import org.openehealth.ipf.commons.ihe.fhir.translation.FhirTranslator;
25 import org.openehealth.ipf.commons.ihe.fhir.translation.ToFhirTranslator;
26 import org.openehealth.ipf.platform.camel.core.util.Exchanges;
27
28 import java.util.Map;
29
30
31
32
33
34
35
36 public final class FhirCamelTranslators {
37
38 private FhirCamelTranslators() {
39
40 }
41
42
43
44
45
46
47
48
49
50 public static Processor translateFhir(final FhirTranslator<?> translator) {
51 return exchange -> {
52 Object fhir = exchange.getIn().getBody();
53 Map<String, Object> parameters = exchange.getIn().getHeaders();
54 org.apache.camel.Message resultMessage = Exchanges.resultMessage(exchange);
55 resultMessage.setBody(translator.translateFhir(fhir, parameters));
56 resultMessage.getHeaders().putAll(parameters);
57 if (fhir instanceof IIdType) {
58 resultMessage.setHeader(Constants.FHIR_REQUEST_GET_ONLY, true);
59 }
60 };
61 }
62
63 public static Expression transformFhir(final FhirTranslator<?> translator) {
64 return new ExpressionAdapter() {
65 @Override
66 public Object evaluate(Exchange exchange) {
67 Object fhir = exchange.getIn().getBody();
68 Map<String, Object> parameters = exchange.getIn().getHeaders();
69 return translator.translateFhir(fhir, parameters);
70 }
71 };
72 }
73
74
75
76
77
78
79 public static <T> Processor translateToFhir(final ToFhirTranslator<T> translator, Class<T> clazz) {
80 return exchange -> {
81 T msg = exchange.getIn().getMandatoryBody(clazz);
82 Map<String, Object> parameters = exchange.getIn().getHeaders();
83 org.apache.camel.Message resultMessage = Exchanges.resultMessage(exchange);
84 resultMessage.setBody(translator.translateToFhir(msg, parameters));
85 resultMessage.getHeaders().putAll(parameters);
86 };
87 }
88
89 }