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.fhir.iti65;
17  
18  import org.hl7.fhir.dstu3.model.Bundle;
19  import org.hl7.fhir.dstu3.model.DocumentManifest;
20  import org.hl7.fhir.instance.model.api.IBaseResource;
21  import org.openehealth.ipf.commons.audit.AuditContext;
22  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
23  import org.openehealth.ipf.commons.ihe.fhir.support.FhirAuditStrategy;
24  
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.Set;
28  import java.util.stream.Collectors;
29  
30  /**
31   * @author Christian Ohr
32   * @since 3.4
33   */
34  public abstract class Iti65AuditStrategy extends FhirAuditStrategy<Iti65AuditDataset> {
35  
36      public Iti65AuditStrategy(boolean serverSide) {
37          super(serverSide);
38      }
39  
40      @Override
41      public Iti65AuditDataset createAuditDataset() {
42          return new Iti65AuditDataset(isServerSide());
43      }
44  
45      @Override
46      public Iti65AuditDataset enrichAuditDatasetFromRequest(Iti65AuditDataset auditDataset, Object request, Map<String, Object> parameters) {
47          Iti65AuditDataset dataset = super.enrichAuditDatasetFromRequest(auditDataset, request, parameters);
48          Bundle bundle = (Bundle) request;
49          //
50          DocumentManifest documentManifest = bundle.getEntry().stream()
51                  .map(Bundle.BundleEntryComponent::getResource)
52                  .filter(DocumentManifest.class::isInstance)
53                  .map(DocumentManifest.class::cast)
54                  .findFirst().orElseThrow(() -> new RuntimeException("ITI-65 bundle must contain DocumentManifest"));
55  
56          dataset.enrichDatasetFromDocumentManifest(documentManifest);
57          return dataset;
58      }
59  
60      @Override
61      public boolean enrichAuditDatasetFromResponse(Iti65AuditDataset auditDataset, Object response, AuditContext auditContext) {
62          Bundle bundle = (Bundle) response;
63          // Extract DocumentManifest (UU)IDs from the response bundle for auditing
64          bundle.getEntry().stream()
65                  .map(Bundle.BundleEntryComponent::getResponse)
66                  .filter(Objects::nonNull)
67                  .filter(r -> r.getLocation() != null && r.getLocation().startsWith("DocumentManifest"))
68                  .findFirst()
69                  .ifPresent(r -> auditDataset.setDocumentManifestUuid(r.getLocation()));
70          return super.enrichAuditDatasetFromResponse(auditDataset, response, auditContext);
71      }
72  
73      /**
74       * Look at the response codes in the bundle entries and derive the ATNA event outcome
75       * @param resource FHIR resource
76       * @return RFC3881EventOutcomeCode
77       */
78      @Override
79      protected EventOutcomeIndicator getEventOutcomeCodeFromResource(IBaseResource resource) {
80          Bundle bundle = (Bundle) resource;
81          Set<String> responseStatus = bundle.getEntry().stream()
82                  .map(Bundle.BundleEntryComponent::getResponse)
83                  .map(Bundle.BundleEntryResponseComponent::getStatus)
84                  .collect(Collectors.toSet());
85  
86          if (responseStatus.stream().anyMatch(s -> s.startsWith("4") || s.startsWith("5"))) {
87              return EventOutcomeIndicator.MajorFailure;
88          }
89          return EventOutcomeIndicator.Success;
90      }
91  }