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.event;
17  
18  
19  import org.openehealth.ipf.commons.audit.AuditException;
20  import org.openehealth.ipf.commons.audit.codes.*;
21  
22  import java.util.Collections;
23  
24  /**
25   * Builds an Audit Event representing a Audit Log Used event as specified in
26   * http://dicom.nema.org/medical/dicom/current/output/html/part15.html#sect_A.5.3.2
27   * <p>
28   * This message describes the event of a person or process reading a log of audit trail information.
29   * </p>
30   *
31   * @author Christian Ohr
32   * @since 3.5
33   */
34  public class AuditLogUsedBuilder extends BaseAuditMessageBuilder<AuditLogUsedBuilder> {
35  
36      public AuditLogUsedBuilder(EventOutcomeIndicator outcome) {
37          this(outcome, null);
38      }
39  
40      public AuditLogUsedBuilder(EventOutcomeIndicator outcome,
41                                 String eventOutcomeDescription) {
42          super();
43          setEventIdentification(outcome,
44                  eventOutcomeDescription,
45                  EventActionCode.Read,
46                  EventIdCode.AuditLogUsed,
47                  null
48          );
49      }
50  
51      /**
52       * Adds the Active Participant of the User or System that accessed the log
53       *
54       * @param userId    The person or process accessing the audit trail. If both are known,
55       *                  then two active participants shall be included (both the person and the process).
56       * @param altUserId The Active Participant's Alternate UserID
57       * @param userName  The Active Participant's UserName
58       * @param networkId The Active Participant's Network Access Point ID
59       */
60      public AuditLogUsedBuilder addAccessingParticipant(String userId,
61                                                         String altUserId,
62                                                         String userName,
63                                                         boolean userIsRequestor,
64                                                         String networkId) {
65          return addActiveParticipant(
66                          userId,
67                          altUserId,
68                          userName,
69                          userIsRequestor,
70                          Collections.emptyList(),
71                          networkId);
72      }
73  
74      /**
75       * Adds the Participant Object representing the audit log accessed
76       *
77       * @param auditLogUri The URI of the audit log that was accessed
78       */
79      public AuditLogUsedBuilder addAuditLogIdentity(String auditLogUri) {
80          return addParticipantObjectIdentification(
81                          ParticipantObjectIdTypeCode.URI,
82                          "Security Audit Log",
83                          null,
84                          Collections.emptyList(),
85                          auditLogUri,
86                          ParticipantObjectTypeCode.System,
87                          ParticipantObjectTypeCodeRole.SecurityResource,
88                          null,
89                          null);
90      }
91  
92      @Override
93      public void validate() {
94          super.validate();
95          if (getMessage().getActiveParticipants().isEmpty() || getMessage().getActiveParticipants().size() > 2) {
96              throw new AuditException("Must have one or two participants that started the Application");
97          }
98          if (getMessage().findParticipantObjectIdentifications(poi -> ParticipantObjectIdTypeCode.URI.equals(poi.getParticipantObjectTypeCode())).size() != 1) {
99              throw new AuditException("Must have exactly Audit Log Identity Participating Object ");
100         }
101     }
102 }