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.codes.ParticipantObjectTypeCode;
21  import org.openehealth.ipf.commons.audit.codes.ParticipantObjectTypeCodeRole;
22  import org.openehealth.ipf.commons.audit.event.QueryBuilder;
23  import org.openehealth.ipf.commons.audit.model.TypeValuePairType;
24  import org.openehealth.ipf.commons.audit.types.EventType;
25  import org.openehealth.ipf.commons.audit.types.ParticipantObjectIdType;
26  import org.openehealth.ipf.commons.audit.types.PurposeOfUse;
27  import org.openehealth.ipf.commons.ihe.core.atna.AuditDataset;
28  
29  import java.nio.charset.StandardCharsets;
30  import java.util.*;
31  
32  /**
33   * Builder for building IHE-specific Query events.
34   * It automatically sets the AuditSource, local and remote ActiveParticipant and a Human Requestor
35   * and provides methods for adding patient IDs.
36   *
37   * @author Christian Ohr
38   * @since 3.5
39   */
40  public class QueryInformationBuilder<T extends QueryInformationBuilder<T>> extends IHEAuditMessageBuilder<T, QueryBuilder> {
41  
42      public QueryInformationBuilder(AuditContext auditContext,
43                                     AuditDataset auditDataset,
44                                     EventType eventType,
45                                     PurposeOfUse... purposesOfUse) {
46          super(auditContext, new QueryBuilder(
47                  auditDataset.getEventOutcomeIndicator(),
48                  auditDataset.getEventOutcomeDescription(),
49                  eventType,
50                  purposesOfUse));
51  
52          // First the source, then the destination
53          if (auditDataset.isServerSide()) {
54              setRemoteParticipant(auditDataset);
55              addHumanRequestor(auditDataset);
56              setLocalParticipant(auditDataset);
57          } else {
58              setLocalParticipant(auditDataset);
59              addHumanRequestor(auditDataset);
60              setRemoteParticipant(auditDataset);
61          }
62      }
63  
64      public T addPatients(String... patientIds) {
65          if (patientIds != null) {
66              Arrays.stream(patientIds)
67                      .filter(Objects::nonNull)
68                      .forEach(patientId ->
69                              delegate.addPatientParticipantObject(patientId, null,
70                                      Collections.emptyList(), null));
71          }
72          return self();
73      }
74  
75      public T addPatients(Collection<String> patientIds) {
76          if (patientIds != null) {
77              patientIds.stream()
78                      .filter(Objects::nonNull)
79                      .forEach(patientId ->
80                              delegate.addPatientParticipantObject(patientId, null,
81                                      Collections.emptyList(), null));
82          }
83          return self();
84      }
85  
86      public T setQueryParameters(
87              String queryMessageIdentifier,
88              ParticipantObjectIdType participantObjectIdType,
89              String queryMessage) {
90          return setQueryParameters(
91                  queryMessageIdentifier,
92                  participantObjectIdType,
93                  queryMessage,
94                  null,
95                  null);
96      }
97  
98      public T setQueryParameters(
99              String queryMessageIdentifier,
100             ParticipantObjectIdType participantObjectIdType,
101             String queryMessage,
102             String messageIdDesignator,
103             String messageId) {
104         return setQueryParameters(
105                 queryMessageIdentifier,
106                 participantObjectIdType,
107                 queryMessage,
108                 messageIdDesignator != null ?
109                         Collections.singletonList(getTypeValuePair(messageIdDesignator, messageId)) :
110                         Collections.emptyList());
111     }
112 
113     public T setQueryParameters(
114             String queryMessageIdentifier,
115             ParticipantObjectIdType participantObjectIdType,
116             String queryMessage,
117             List<TypeValuePairType> details) {
118         delegate.addParticipantObjectIdentification(
119                 participantObjectIdType,
120                 null,
121                 Base64.getEncoder().encode(queryMessage.getBytes(StandardCharsets.UTF_8)),
122                 details,
123                 queryMessageIdentifier,
124                 ParticipantObjectTypeCode.System,
125                 ParticipantObjectTypeCodeRole.Query,
126                 null,
127                 null);
128         return self();
129     }
130 
131     @Override
132     public void validate() {
133         super.validate();
134     }
135 }