View Javadoc
1   /*
2    * Copyright 2015 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Camel processors for translation of messages between FHIR and something else
32   *
33   * @author Christian Ohr
34   * @since 3.1
35   */
36  public final class FhirCamelTranslators {
37  
38      private FhirCamelTranslators() {
39  
40      }
41  
42      /**
43       * Returns a processor for translating FHIR messages to Hl7v2
44       * using the given translator instance. 
45       */
46      /**
47       * Returns a processor for translating FHIR messages to XDS
48       * using the given translator instance.
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       * Returns a processor for translating HL7v2 messages to FHIR
77       * using the given translator instance.
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  }