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  
17  package org.openehealth.ipf.commons.ihe.core.atna.event;
18  
19  import org.openehealth.ipf.commons.audit.AuditContext;
20  import org.openehealth.ipf.commons.audit.event.BaseAuditMessageBuilder;
21  import org.openehealth.ipf.commons.audit.event.DelegatingAuditMessageBuilder;
22  import org.openehealth.ipf.commons.audit.model.TypeValuePairType;
23  import org.openehealth.ipf.commons.ihe.core.atna.AuditDataset;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import static org.openehealth.ipf.commons.audit.utils.AuditUtils.getHostFromUrl;
29  import static org.openehealth.ipf.commons.audit.utils.AuditUtils.getProcessId;
30  
31  /**
32   * Base class for building DICOM audit messages as specified in the various IHE transactions.
33   * It provides some methods for setting audit event participants that are common across
34   * different IHE transactions, namely local participant, remote participant, and human requestor
35   *
36   * @author Christian Ohr
37   * @since 3.5
38   */
39  public abstract class IHEAuditMessageBuilder<T extends IHEAuditMessageBuilder<T, D>, D extends BaseAuditMessageBuilder<D>>
40          extends DelegatingAuditMessageBuilder<T, D> {
41  
42      public static final String IHE_HOME_COMMUNITY_ID = "ihe:homeCommunityID";
43      public static final String URN_IHE_ITI_XCA_2010_HOME_COMMUNITY_ID = "urn:ihe:iti:xca:2010:homeCommunityId";
44      public static final String QUERY_ENCODING = "QueryEncoding";
45      public static final String REPOSITORY_UNIQUE_ID = "Repository Unique Id";
46      public static final String STUDY_INSTANCE_UNIQUE_ID = "Study Instance Unique Id";
47      public static final String SERIES_INSTANCE_UNIQUE_ID = "Series Instance Unique Id";
48  
49      public IHEAuditMessageBuilder(AuditContext auditContext, D delegate) {
50          super(delegate);
51          delegate.setAuditSource(auditContext);
52      }
53  
54      /**
55       * Set the local participant, which is either the transaction destination (if it's
56       * server-side) or the transaction source (if it's client-side)
57       *
58       * @param auditDataset audit data set
59       * @return this
60       */
61      protected final T setLocalParticipant(AuditDataset auditDataset) {
62          if (auditDataset.isServerSide())
63              delegate.addDestinationActiveParticipant(auditDataset.getDestinationUserId(),
64                      getProcessId(),
65                      null,
66                      auditDataset.getLocalAddress(),
67                      auditDataset.isDestinationUserIsRequestor());
68          else
69              delegate.addSourceActiveParticipant(auditDataset.getSourceUserId(),
70                      getProcessId(),
71                      auditDataset.getSourceUserName(),
72                      auditDataset.getLocalAddress(),
73                      auditDataset.isSourceUserIsRequestor());
74          return self();
75      }
76  
77      /**
78       * Set the remote participant, which is either the transaction source (if it's
79       * server-side) or the transaction destination (if it's client-side)
80       *
81       * @param auditDataset audit data set
82       * @return this
83       */
84      protected final T setRemoteParticipant(AuditDataset auditDataset) {
85          if (auditDataset.isServerSide())
86              delegate.addSourceActiveParticipant(auditDataset.getSourceUserId(),
87                      null,
88                      auditDataset.getSourceUserName(),
89                      getHostFromUrl(auditDataset.getRemoteAddress()),
90                      auditDataset.isSourceUserIsRequestor());
91          else
92              delegate.addDestinationActiveParticipant(auditDataset.getDestinationUserId(),
93                      null,
94                      null,
95                      getHostFromUrl(auditDataset.getRemoteAddress()),
96                      auditDataset.isDestinationUserIsRequestor());
97          return self();
98      }
99  
100     protected final T addHumanRequestor(AuditDataset auditDataset) {
101         for (AuditDataset.HumanUser humanUser : auditDataset.getHumanUsers()) {
102             if (!humanUser.isEmpty()) {
103                 delegate.addActiveParticipant(humanUser.getId(), humanUser.getName(), humanUser.getId(),
104                         true, humanUser.getRoles(), null);
105             }
106         }
107         return self();
108     }
109 
110     public static List<TypeValuePairType> makeDocumentDetail(String repositoryId, String homeCommunityId, String seriesInstanceId, String studyInstanceId) {
111         List<TypeValuePairType> tvp = new ArrayList<>();
112         if (studyInstanceId != null) {
113             tvp.add(new TypeValuePairType(STUDY_INSTANCE_UNIQUE_ID, studyInstanceId));
114         }
115         if (seriesInstanceId != null) {
116             tvp.add(new TypeValuePairType(SERIES_INSTANCE_UNIQUE_ID, seriesInstanceId));
117         }
118         if (repositoryId != null) {
119             tvp.add(new TypeValuePairType(REPOSITORY_UNIQUE_ID, repositoryId));
120         }
121         if (homeCommunityId != null) {
122             tvp.add(new TypeValuePairType(IHE_HOME_COMMUNITY_ID, homeCommunityId));
123         }
124         return tvp;
125     }
126 }