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.fhir.iti83;
17  
18  import org.hl7.fhir.dstu3.model.Identifier;
19  import org.hl7.fhir.dstu3.model.Parameters;
20  import org.hl7.fhir.dstu3.model.StringType;
21  import org.hl7.fhir.dstu3.model.Type;
22  import org.openehealth.ipf.commons.audit.AuditContext;
23  import org.openehealth.ipf.commons.audit.model.AuditMessage;
24  import org.openehealth.ipf.commons.ihe.core.atna.event.QueryInformationBuilder;
25  import org.openehealth.ipf.commons.ihe.fhir.Constants;
26  import org.openehealth.ipf.commons.ihe.fhir.audit.FhirQueryAuditDataset;
27  import org.openehealth.ipf.commons.ihe.fhir.audit.codes.FhirEventTypeCode;
28  import org.openehealth.ipf.commons.ihe.fhir.audit.codes.FhirParticipantObjectIdTypeCode;
29  import org.openehealth.ipf.commons.ihe.fhir.support.FhirQueryAuditStrategy;
30  
31  import java.util.Map;
32  
33  /**
34   * Strategy for auditing ITI-83 transactions
35   *
36   * @author Christian Ohr
37   * @since 3.4
38   */
39  public class Iti83AuditStrategy extends FhirQueryAuditStrategy {
40  
41      public Iti83AuditStrategy(boolean serverSide) {
42          super(serverSide);
43      }
44  
45      @Override
46      public AuditMessage[] makeAuditMessage(AuditContext auditContext, FhirQueryAuditDataset auditDataset) {
47          return new QueryInformationBuilder<>(auditContext, auditDataset, FhirEventTypeCode.MobilePatientIdentifierCrossReferenceQuery)
48                  .addPatients(auditDataset.getPatientIds())
49                  .setQueryParameters(
50                          "PIXmQuery",
51                          FhirParticipantObjectIdTypeCode.MobilePatientIdentifierCrossReferenceQuery,
52                          auditDataset.getQueryString())
53  
54                  .getMessages();
55      }
56  
57      @Override
58      public FhirQueryAuditDataset enrichAuditDatasetFromRequest(FhirQueryAuditDataset auditDataset, Object request, Map<String, Object> parameters) {
59          FhirQueryAuditDataset dataset = super.enrichAuditDatasetFromRequest(auditDataset, request, parameters);
60  
61          Parameters params = (Parameters) request;
62          if (params != null) {
63              Type sourceIdentifier = params.getParameter().stream()
64                      .filter(ppc -> Constants.SOURCE_IDENTIFIER_NAME.equals(ppc.getName()))
65                      .map(Parameters.ParametersParameterComponent::getValue)
66                      .findFirst().orElseThrow(() -> new RuntimeException("No sourceIdentifier in PIX query"));
67  
68              if (sourceIdentifier instanceof Identifier) {
69                  Identifier identifier = (Identifier) sourceIdentifier;
70                  dataset.getPatientIds().add(String.format("%s|%s", identifier.getSystem(), identifier.getValue()));
71              } else if (sourceIdentifier instanceof StringType) {
72                  StringType identifier = (StringType) sourceIdentifier;
73                  dataset.getPatientIds().add(identifier.getValue());
74              } else {
75                  dataset.getPatientIds().add(sourceIdentifier.toString());
76              }
77          }
78          return dataset;
79      }
80  
81      @Override
82      public boolean enrichAuditDatasetFromResponse(FhirQueryAuditDataset auditDataset, Object response, AuditContext auditContext) {
83          boolean result = super.enrichAuditDatasetFromResponse(auditDataset, response, auditContext);
84          if (auditContext.isIncludeParticipantsFromResponse()) {
85          // TODO
86          }
87          return result;
88      }
89  }