View Javadoc
1   /*
2    * Copyright 2014 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  
17  package org.openehealth.ipf.platform.camel.hl7.adapter;
18  
19  import ca.uhn.hl7v2.AcknowledgmentCode;
20  import ca.uhn.hl7v2.ErrorCode;
21  import ca.uhn.hl7v2.HL7Exception;
22  import ca.uhn.hl7v2.model.Message;
23  import org.apache.camel.util.ObjectHelper;
24  
25  /**
26   *
27   */
28  public class AcknowledgementAdapter extends HapiAdapter {
29  
30      @Override
31      protected Message doProcessMessage(Message message, Throwable t, Object... inputParams) {
32          AcknowledgmentCode acknowledgementCode = inputParams != null && inputParams.length > 0 ?
33                  (AcknowledgmentCode) inputParams[0] :
34                  AcknowledgmentCode.AA;
35          String errorMessage = inputParams != null && inputParams.length > 1 ?
36                  inputParams[1].toString() :
37                  "Error while processing HL7 message";
38          ErrorCode errorCode = inputParams != null && inputParams.length > 2 ?
39                  (ErrorCode) inputParams[2] :
40                  ErrorCode.APPLICATION_INTERNAL_ERROR;
41          try {
42              HL7Exception hl7e = generateHL7Exception(t, acknowledgementCode, errorMessage, errorCode);
43              if (t != null && acknowledgementCode == null) {
44                  acknowledgementCode = AcknowledgmentCode.AE;
45              }
46              return message.generateACK(acknowledgementCode == null ? AcknowledgmentCode.AA : acknowledgementCode, hl7e);
47          } catch (Exception e) {
48              throw ObjectHelper.wrapRuntimeCamelException(e);
49          }
50  
51      }
52  
53      private HL7Exception generateHL7Exception(Throwable t, AcknowledgmentCode acknowledgementCode, String errorMessage, ErrorCode errorCode) {
54          if (t == null) {
55              if (acknowledgementCode != null && !acknowledgementCode.name().endsWith("A")) {
56                  return new HL7Exception(errorMessage, errorCode);
57              } else {
58                  return null;
59              }
60          }
61          return t instanceof HL7Exception ?
62                  (HL7Exception) t :
63                  new HL7Exception(errorMessage != null ? errorMessage : t.getMessage(), errorCode, t);
64      }
65  
66  }