View Javadoc
1   /*
2    * Copyright 2010 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.hl7v3;
17  
18  import org.apache.camel.Exchange;
19  import org.apache.camel.Message;
20  import org.apache.camel.Processor;
21  import org.openehealth.ipf.commons.ihe.hl7v3.translation.Hl7TranslatorV2toV3;
22  import org.openehealth.ipf.commons.ihe.hl7v3.translation.Hl7TranslatorV3toV2;
23  import org.openehealth.ipf.platform.camel.core.util.Exchanges;
24  
25  /**
26   * Camel processors for translation of HL7 messages between versions 2 and 3.
27   * @author Dmytro Rud
28   */
29  abstract public class PixPdqV3CamelTranslators {
30  
31      /**
32       * Name of the Camel exchange property in which original 
33       * request messages will be saved before translation.
34       */
35      public static final String HL7V3_ORIGINAL_REQUEST_PROPERTY = "hl7v3.original.request";
36  
37      
38      /**
39       * Returns a processor for translating HL7v3 messages to Hl7v2 
40       * using the given translator instance. 
41       */
42      public static Processor translatorHL7v3toHL7v2(final Hl7TranslatorV3toV2 translator) {
43          return exchange -> {
44              ca.uhn.hl7v2.model.Message initial = exchange.getProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, ca.uhn.hl7v2.model.Message.class);
45              String xmlText = exchange.getIn().getMandatoryBody(String.class);
46              exchange.setProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, xmlText);
47              Message resultMessage = Exchanges.resultMessage(exchange);
48              resultMessage.getHeaders().putAll(exchange.getIn().getHeaders());
49              resultMessage.setBody(translator.translateV3toV2(xmlText, initial));
50          };
51      }
52      
53      
54      /**
55       * Returns a processor for translating HL7v2 messages to HL7v3 
56       * using the given translator instance. 
57       */
58      public static Processor translatorHL7v2toHL7v3(final Hl7TranslatorV2toV3 translator) {
59          return exchange -> {
60              String initial = exchange.getProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, String.class);
61              ca.uhn.hl7v2.model.Message msg = exchange.getIn().getMandatoryBody(ca.uhn.hl7v2.model.Message.class);
62              exchange.setProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, msg);
63              Message resultMessage = Exchanges.resultMessage(exchange);
64              String charset = exchange.getProperty(Exchange.CHARSET_NAME, "UTF-8", String.class);
65              resultMessage.getHeaders().putAll(exchange.getIn().getHeaders());
66              resultMessage.setBody(translator.translateV2toV3(msg, initial, charset));
67          };
68      }
69      
70  }