View Javadoc
1   /*
2    * Copyright 2012 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.xds.core.audit;
17  
18  import lombok.AllArgsConstructor;
19  import lombok.Getter;
20  import lombok.Setter;
21  import org.apache.commons.lang3.StringUtils;
22  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
23  
24  import java.io.Serializable;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.function.Function;
28  
29  /**
30   * Audit dataset specific for non-constructive operations (Read+Delete as opposed to Create+Update in CRUD)
31   * on document sets in an XDS Repository.
32   *
33   * @author Dmytro Rud
34   */
35  public class XdsNonconstructiveDocumentSetRequestAuditDataset extends XdsAuditDataset {
36      private static final long serialVersionUID = -8776033207572005899L;
37  
38      public enum Status {SUCCESSFUL, NOT_SUCCESSFUL}
39  
40      @AllArgsConstructor
41      public static class Document implements Serializable {
42          private static final long serialVersionUID = -2386699338508892135L;
43  
44          @Getter private final String documentUniqueId;
45          @Getter private final String repositoryUniqueId;
46          @Getter private final String homeCommunityId;
47          @Getter private final String studyInstanceUID;
48          @Getter private final String seriesInstanceUID;
49          @Getter @Setter private Status status;
50  
51          public boolean matches(String documentUniqueId, String repositoryUniqueId, String homeCommunityId) {
52              return StringUtils.equals(this.documentUniqueId, documentUniqueId)
53                      && StringUtils.equals(this.repositoryUniqueId, repositoryUniqueId)
54                      && StringUtils.equals(this.homeCommunityId, homeCommunityId);
55          }
56      }
57  
58      @Getter private final List<Document> documents = new ArrayList<>();
59  
60      public XdsNonconstructiveDocumentSetRequestAuditDataset(boolean serverSide) {
61          super(serverSide);
62      }
63  
64      public boolean hasDocuments(Status status) {
65          return documents.stream().anyMatch(x -> x.status == status);
66      }
67  
68      public EventOutcomeIndicator getEventOutcomeIndicator(Status status) {
69          return (status == Status.SUCCESSFUL)
70                  ? EventOutcomeIndicator.Success
71                  : EventOutcomeIndicator.SeriousFailure;
72      }
73  
74      @Override
75      public EventOutcomeIndicator getEventOutcomeIndicator() {
76          throw new UnsupportedOperationException("Call getEventOutcomeIndicator(Status status) instead");
77      }
78  
79      public void registerProcessedDocument(String documentUniqueId, String repositoryUniqueId, String homeCommunityId) {
80          documents.stream()
81                  .filter(x -> x.matches(documentUniqueId, repositoryUniqueId, homeCommunityId))
82                  .forEach(x -> x.setStatus(Status.SUCCESSFUL));
83      }
84  
85      private String[] extract(Status status, Function<Document, String> fieldExtractor) {
86          return documents.stream()
87                  .filter(x -> x.status == status)
88                  .map(fieldExtractor)
89                  .toArray(String[]::new);
90      }
91  
92      public String[] getDocumentIds      (Status status) { return extract(status, x -> x.documentUniqueId); }
93      public String[] getRepositoryIds    (Status status) { return extract(status, x -> x.repositoryUniqueId); }
94      public String[] getHomeCommunityIds (Status status) { return extract(status, x -> x.homeCommunityId); }
95      public String[] getStudyInstanceIds (Status status) { return extract(status, x -> x.studyInstanceUID); }
96      public String[] getSeriesInstanceIds(Status status) { return extract(status, x -> x.seriesInstanceUID); }
97  
98  }