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.AuditException;
21  import org.openehealth.ipf.commons.audit.codes.EventActionCode;
22  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
23  import org.openehealth.ipf.commons.audit.codes.ParticipantObjectIdTypeCode;
24  import org.openehealth.ipf.commons.audit.event.PatientRecordBuilder;
25  import org.openehealth.ipf.commons.audit.model.ParticipantObjectIdentificationType;
26  import org.openehealth.ipf.commons.audit.types.EventType;
27  import org.openehealth.ipf.commons.audit.types.PurposeOfUse;
28  import org.openehealth.ipf.commons.ihe.core.atna.AuditDataset;
29  
30  import java.util.Arrays;
31  import java.util.Collections;
32  import java.util.Objects;
33  import java.util.regex.Pattern;
34  
35  /**
36   * Builder for building IHE-specific PatientRecord events. It automatically sets the AuditSource,
37   * local and remote ActiveParticipant and a Human Requestor and provides methods for adding patient IDs.
38   *
39   * @author Christian Ohr
40   * @since 3.5
41   */
42  public class PatientRecordEventBuilder<T extends PatientRecordEventBuilder<T>> extends IHEAuditMessageBuilder<T, PatientRecordBuilder> {
43  
44      private static final Pattern PATIENT_ID_PATTERN = Pattern.compile("^.+?\\^\\^\\^.*?&.+?&ISO(\\^.*){0,4}$");
45  
46      public PatientRecordEventBuilder(AuditContext auditContext, AuditDataset auditDataset, EventActionCode action, EventType eventType,
47                                       PurposeOfUse... purposesOfUse) {
48          this(auditContext, auditDataset,
49                  auditDataset.getEventOutcomeIndicator(),
50                  auditDataset.getEventOutcomeDescription(),
51                  action,
52                  eventType,
53                  purposesOfUse);
54      }
55  
56      public PatientRecordEventBuilder(AuditContext auditContext,
57                                       AuditDataset auditDataset,
58                                       EventOutcomeIndicator eventOutcomeIndicator,
59                                       String eventOutcomeDescription,
60                                       EventActionCode action,
61                                       EventType eventType,
62                                       PurposeOfUse... purposesOfUse) {
63          super(auditContext, new PatientRecordBuilder(
64                  eventOutcomeIndicator,
65                  eventOutcomeDescription,
66                  action,
67                  eventType,
68                  purposesOfUse));
69  
70          // First the source, then the destination
71          if (auditDataset.isServerSide()) {
72              setRemoteParticipant(auditDataset);
73              addHumanRequestor(auditDataset);
74              setLocalParticipant(auditDataset);
75          } else {
76              setLocalParticipant(auditDataset);
77              addHumanRequestor(auditDataset);
78              setRemoteParticipant(auditDataset);
79          }
80      }
81  
82      /**
83       * Adds each patient ID as ParticipantObject in the context of a single request
84       *
85       * @param requestIdDesignator request or message ID designator, e.g. MSH-10 for HL7v2 and II for HL7v3
86       * @param requestId           ID of the request or message
87       * @param patientIds          IDs of the patient
88       * @return this
89       */
90      public T addPatients(String requestIdDesignator, String requestId, String... patientIds) {
91          if (patientIds != null)
92              Arrays.stream(patientIds)
93                      .filter(Objects::nonNull)
94                      .forEach(patientId -> delegate.addPatient(patientId, null,
95                              requestIdDesignator != null && requestId != null ?
96                                      Collections.singletonList(getTypeValuePair(requestIdDesignator, requestId)) :
97                                      Collections.emptyList()));
98          return self();
99      }
100 
101     @Override
102     public void validate() {
103         super.validate();
104         ParticipantObjectIdentificationType patient = getMessage().findParticipantObjectIdentifications(poi -> ParticipantObjectIdTypeCode.PatientNumber.equals(poi.getParticipantObjectIDTypeCode())).get(0);
105         if (!PATIENT_ID_PATTERN.matcher(patient.getParticipantObjectID()).matches()) {
106             throw new AuditException("Patient ID should be in CX format " + PATIENT_ID_PATTERN.pattern());
107         }
108     }
109 }