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.commons.ihe.hl7v2.audit.iti8;
17  
18  import ca.uhn.hl7v2.model.Message;
19  import org.openehealth.ipf.commons.audit.AuditContext;
20  import org.openehealth.ipf.commons.audit.AuditException;
21  import org.openehealth.ipf.commons.audit.codes.EventActionCode;
22  import org.openehealth.ipf.commons.audit.model.AuditMessage;
23  import org.openehealth.ipf.commons.ihe.core.atna.AuditStrategySupport;
24  import org.openehealth.ipf.commons.ihe.core.atna.event.PatientRecordEventBuilder;
25  import org.openehealth.ipf.commons.ihe.hl7v2.audit.FeedAuditDataset;
26  import org.openehealth.ipf.commons.ihe.hl7v2.audit.codes.MllpEventTypeCode;
27  
28  import java.util.Map;
29  
30  public class Iti8AuditStrategy extends AuditStrategySupport<FeedAuditDataset> {
31  
32      public Iti8AuditStrategy(boolean serverSide) {
33          super(serverSide);
34      }
35  
36      @Override
37      public FeedAuditDataset enrichAuditDatasetFromRequest(FeedAuditDataset auditDataset, Object msg, Map<String, Object> parameters) {
38          Iti8AuditStrategyUtils.enrichAuditDatasetFromRequest(auditDataset, (Message) msg);
39          return auditDataset;
40      }
41  
42      @Override
43      public FeedAuditDataset createAuditDataset() {
44          return new FeedAuditDataset(isServerSide());
45      }
46  
47      @Override
48      public AuditMessage[] makeAuditMessage(AuditContext auditContext, FeedAuditDataset auditDataset) {
49          switch (auditDataset.getMessageType()) {
50              case "A01":
51              case "A04":
52              case "A05":
53                  return new AuditMessage[]{
54                          patientRecordAuditMessage(auditContext,auditDataset, EventActionCode.Create, true)
55                  };
56              case "A08":
57                  return new AuditMessage[]{
58                          patientRecordAuditMessage(auditContext,auditDataset, EventActionCode.Update, true)
59                  };
60              case "A40":
61                  return new AuditMessage[]{
62                          patientRecordAuditMessage(auditContext,auditDataset, EventActionCode.Delete, false),
63                          patientRecordAuditMessage(auditContext,auditDataset, EventActionCode.Update, true)
64                  };
65              default:
66                  throw new AuditException("Cannot create audit message for event " + auditDataset.getMessageType());
67          }
68  
69      }
70  
71      protected AuditMessage patientRecordAuditMessage(AuditContext auditContext,
72                                                       final FeedAuditDataset auditDataset,
73                                                       EventActionCode eventActionCode,
74                                                       boolean newPatientId) {
75          return new PatientRecordEventBuilder<>(auditContext, auditDataset, eventActionCode, MllpEventTypeCode.PatientIdentityFeed)
76  
77                  // Type=MSH-10 (the literal string), Value=the value of MSH-10 (from the message content, base64 encoded)
78                  .addPatients(
79                          "MSH-10", auditDataset.getMessageControlId(),
80                          newPatientId ? auditDataset.getPatientId() : auditDataset.getOldPatientId())
81                  .getMessage();
82      }
83  }