View Javadoc
1   /*
2    * Copyright 2017 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.audit.model;
17  
18  import lombok.EqualsAndHashCode;
19  import lombok.Getter;
20  import lombok.Setter;
21  import org.openehealth.ipf.commons.audit.AuditException;
22  import org.openehealth.ipf.commons.audit.event.BaseAuditMessageBuilder;
23  import org.openehealth.ipf.commons.audit.marshal.dicom.Current;
24  
25  import java.io.Serializable;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.function.Predicate;
29  import java.util.stream.Collectors;
30  
31  
32  /**
33   * DICOM AuditMessage basis type. AuditMessage instances should always be built using
34   * {@link BaseAuditMessageBuilder} subclasses to
35   * ensure compliance with the specification.
36   *
37   * @author Christian Ohr
38   * @since 3.5
39   */
40  @EqualsAndHashCode
41  public class AuditMessage implements Serializable, Validateable {
42  
43      @Getter @Setter
44      private EventIdentificationType eventIdentification;
45  
46      private List<ActiveParticipantType> activeParticipants;
47  
48      @Getter @Setter
49      private AuditSourceIdentificationType auditSourceIdentification;
50      private List<ParticipantObjectIdentificationType> participantObjectIdentifications;
51  
52      public List<ActiveParticipantType> getActiveParticipants() {
53          if (activeParticipants == null) {
54              activeParticipants = new ArrayList<>();
55          }
56          return this.activeParticipants;
57      }
58  
59      public List<ActiveParticipantType> findActiveParticipants(Predicate<ActiveParticipantType> selector) {
60          return getActiveParticipants().stream()
61                  .filter(selector)
62                  .collect(Collectors.toList());
63      }
64  
65      public List<ParticipantObjectIdentificationType> getParticipantObjectIdentifications() {
66          if (participantObjectIdentifications == null) {
67              participantObjectIdentifications = new ArrayList<>();
68          }
69          return this.participantObjectIdentifications;
70      }
71  
72      public List<ParticipantObjectIdentificationType> findParticipantObjectIdentifications(Predicate<ParticipantObjectIdentificationType> selector) {
73          return getParticipantObjectIdentifications().stream()
74                  .filter(selector)
75                  .collect(Collectors.toList());
76      }
77  
78      /**
79       * Validates the constructed audit message against the specification, because API does not completely
80       * prevent constructing incomplete or inconsistent messages.
81       *
82       * @throws org.openehealth.ipf.commons.audit.AuditException AuditException in case validation fails
83       */
84      @Override
85      public void validate() {
86          if (eventIdentification == null) {
87              throw new AuditException("The event must be identified");
88          }
89          if (auditSourceIdentification == null) {
90              throw new AuditException("The event must be have an audit source");
91          }
92          if (getActiveParticipants().isEmpty()) {
93              throw new AuditException("The event must have one or more active participants");
94          }
95  
96          eventIdentification.validate();
97          auditSourceIdentification.validate();
98          activeParticipants.forEach(ActiveParticipantType::validate);
99          participantObjectIdentifications.forEach(ParticipantObjectIdentificationType::validate);
100     }
101 
102     /* (non-Javadoc)
103      * @see java.lang.Object#toString()
104      */
105     public String toString() {
106         return Current.toString(this, true);
107     }
108 }