View Javadoc
1   /*
2    * Copyright 2016 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.commons.ihe.hl7v2;
17  
18  import ca.uhn.hl7v2.AcknowledgmentCode;
19  import ca.uhn.hl7v2.HL7Exception;
20  import ca.uhn.hl7v2.model.AbstractMessage;
21  import ca.uhn.hl7v2.model.Message;
22  import ca.uhn.hl7v2.model.Segment;
23  import ca.uhn.hl7v2.util.Terser;
24  import org.apache.commons.lang3.Validate;
25  import org.openehealth.ipf.commons.ihe.hl7v2.audit.QueryAuditDataset;
26  import org.openehealth.ipf.modules.hl7.message.MessageUtils;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * NAK factory for PDQ, PDVQ and PIX Query.
32   * 
33   * @author Dmytro Rud
34   */
35  public class QpdAwareNakFactory extends NakFactory<QueryAuditDataset> {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(QpdAwareNakFactory.class);
38      private final String messageType, triggerEvent;
39  
40  
41      public QpdAwareNakFactory(Hl7v2TransactionConfiguration<QueryAuditDataset> config, String messageType, String triggerEvent) {
42          super(config);
43  
44          Validate.notEmpty(messageType);
45          Validate.notEmpty(triggerEvent);
46  
47          this.messageType = messageType;
48          this.triggerEvent = triggerEvent;
49      }
50  
51  
52      @Override
53      public Message createNak(Message originalMessage, HL7Exception t, AcknowledgmentCode ackTypeCode) {
54          try {
55              return (t instanceof Hl7v2AcceptanceException)
56                  ? super.createNak(originalMessage, t, ackTypeCode)
57                  : createNak0(originalMessage, t, ackTypeCode);
58          } catch (Exception e) {
59              throw new RuntimeException(e);
60          }
61      }
62  
63  
64      public Message createNak0(Message originalMessage, HL7Exception e, AcknowledgmentCode ackTypeCode)
65          throws HL7Exception {
66          AbstractMessage ack = (AbstractMessage) MessageUtils.response(
67                  originalMessage,
68                  messageType,
69                  triggerEvent);
70  
71          LOG.info("Creating NAK response event of type {}", ack.getClass().getName());
72  
73          e.populateResponse(ack, ackTypeCode, 0);
74  
75          Segment msa = (Segment) ack.get("MSA");
76          Terser.set(msa, 1, 0, 1, 1, ackTypeCode.name());
77  
78          Segment ackQak = (Segment) ack.get("QAK");
79          Segment origQpd = (Segment) originalMessage.get("QPD");
80          if (origQpd != null) {
81              String queryTag = Terser.get(origQpd, 2, 0, 1, 1);
82              Terser.set(ackQak, 1, 0, 1, 1, queryTag);
83              LOG.debug("Set QAK-1 to {}", queryTag);
84          }
85          Terser.set(ackQak, 2, 0, 1, 1, "AE");
86  
87          // create a dummy QPD segment, it will be replaced with proper contents by
88          // org.openehealth.ipf.platform.camel.ihe.hl7v2.intercept.consumer.ConsumerSegmentEchoingInterceptor
89          Segment ackQpd = (Segment) ack.get("QPD");
90          Terser.set(ackQpd, 1, 0, 1, 1, "dummy");
91  
92          return ack;
93      }
94  
95  
96      @Override
97      public Message createAck(Message originalMessage) {
98          throw new IllegalStateException("This transaction cannot return a simple ACK");
99      }
100 }