1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.audit.event;
17
18 import org.openehealth.ipf.commons.audit.AuditContext;
19 import org.openehealth.ipf.commons.audit.codes.*;
20 import org.openehealth.ipf.commons.audit.model.*;
21 import org.openehealth.ipf.commons.audit.types.*;
22
23 import java.time.Instant;
24 import java.util.*;
25 import java.util.regex.Pattern;
26
27 import static java.util.Objects.requireNonNull;
28
29
30
31
32
33
34
35
36
37
38 public abstract class BaseAuditMessageBuilder<T extends BaseAuditMessageBuilder<T>> implements AuditMessageBuilder<T> {
39
40 private static final Pattern IPV4 = Pattern.compile("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$");
41 private static final Pattern IPV6 = Pattern.compile("^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$", Pattern.CASE_INSENSITIVE);
42 private AuditMessage auditMessage;
43
44 public BaseAuditMessageBuilder() {
45 this.auditMessage = new AuditMessage();
46 }
47
48 @Override
49 public void validate() {
50 auditMessage.validate();
51 }
52
53
54
55
56 public AuditMessage getMessage() {
57 return auditMessage;
58 }
59
60
61
62
63
64
65
66 public T setAuditSource(String sourceId) {
67 return setAuditSource(sourceId, null);
68 }
69
70
71
72
73
74
75
76
77
78 public T setAuditSource(String sourceId, String enterpriseSiteId) {
79 return setAuditSource(sourceId, enterpriseSiteId, (AuditSource[]) null);
80 }
81
82
83
84
85
86
87
88
89
90
91 public T setAuditSource(String sourceId,
92 String enterpriseSiteId,
93 AuditSource... typeCodes) {
94 return setAuditSourceIdentification(sourceId, enterpriseSiteId, typeCodes);
95 }
96
97
98
99
100
101
102
103 public T setAuditSource(AuditContext auditContext) {
104 return setAuditSourceIdentification(
105 auditContext.getAuditSourceId(),
106 auditContext.getAuditEnterpriseSiteId(),
107 auditContext.getAuditSource());
108 }
109
110
111
112
113
114
115
116
117
118
119
120 public T setEventIdentification(EventOutcomeIndicator outcome,
121 String eventOutcomeDescription,
122 EventActionCode action,
123 EventId id,
124 EventType type,
125 PurposeOfUse... purposesOfUse) {
126 return setEventIdentification(outcome, eventOutcomeDescription, action, id, type,
127 purposesOfUse != null ? Arrays.asList(purposesOfUse) : Collections.emptyList());
128 }
129
130
131
132
133
134
135
136
137
138
139 public T setEventIdentification(EventOutcomeIndicator outcome,
140 String eventOutcomeDescription,
141 EventActionCode action,
142 EventId id,
143 EventType type,
144 Collection<PurposeOfUse> purposesOfUse) {
145 EventIdentificationType eventIdentification = new EventIdentificationType(id, Instant.now(), outcome);
146 eventIdentification.setEventActionCode(action);
147 eventIdentification.setEventOutcomeDescription(eventOutcomeDescription);
148 if (type != null) {
149 eventIdentification.getEventTypeCode().add(type);
150 }
151 if (purposesOfUse != null) {
152 purposesOfUse.stream()
153 .filter(Objects::nonNull)
154 .forEach(pou -> eventIdentification.getPurposesOfUse().add(pou));
155 }
156 auditMessage.setEventIdentification(eventIdentification);
157 return self();
158 }
159
160
161
162
163
164
165
166
167
168 public T setAuditSourceIdentification(String sourceID,
169 String enterpriseSiteID,
170 AuditSource... typeCodes) {
171 return setAuditSourceIdentification(sourceID, enterpriseSiteID,
172 typeCodes != null ? Arrays.asList(typeCodes) : Collections.emptyList());
173 }
174
175
176
177
178
179
180
181
182
183 public T setAuditSourceIdentification(String sourceID,
184 String enterpriseSiteID,
185 Collection<AuditSource> typeCodes) {
186 AuditSourceIdentificationType asi = new AuditSourceIdentificationType(sourceID);
187 if (typeCodes != null) {
188 typeCodes.stream()
189 .filter(Objects::nonNull)
190 .forEach(typeCode -> asi.getAuditSourceType().add(typeCode));
191 }
192 asi.setAuditEnterpriseSiteID(enterpriseSiteID);
193 return setAuditSourceIdentification(asi);
194 }
195
196 public T setAuditSourceIdentification(AuditSourceIdentificationType auditSourceIdentificationType) {
197 auditMessage.setAuditSourceIdentification(auditSourceIdentificationType);
198 return self();
199 }
200
201
202
203
204
205
206
207
208
209
210
211 public T addSourceActiveParticipant(String userId,
212 String altUserId,
213 String userName,
214 String networkId,
215 boolean isRequestor) {
216 return addActiveParticipant(
217 userId,
218 altUserId,
219 userName,
220 isRequestor,
221 Collections.singletonList(ActiveParticipantRoleIdCode.Source),
222 networkId);
223 }
224
225
226
227
228
229
230
231
232
233
234
235 public T addDestinationActiveParticipant(String userId,
236 String altUserId,
237 String userName,
238 String networkAccessPointId,
239 boolean userIsRequestor) {
240 return addActiveParticipant(
241 userId,
242 altUserId,
243 userName,
244 userIsRequestor,
245 Collections.singletonList(ActiveParticipantRoleIdCode.Destination),
246 networkAccessPointId);
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 public T addActiveParticipant(String userID,
263 String altUserID,
264 String userName,
265 Boolean userIsRequestor,
266 List<ActiveParticipantRoleId> roleIdCodes,
267 String networkAccessPointID) {
268
269 return addActiveParticipant(
270 userID,
271 altUserID,
272 userName,
273 userIsRequestor,
274 roleIdCodes,
275 networkAccessPointID,
276 getNetworkAccessPointCodeFromAddress(networkAccessPointID),
277 null,
278 null);
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public T addActiveParticipant(String userID,
294 String altUserID,
295 String userName,
296 Boolean userIsRequestor,
297 List<ActiveParticipantRoleId> roleIdCodes,
298 String networkAccessPointID,
299 NetworkAccessPointTypeCode networkAccessPointTypeCode,
300 String mediaIdentifier,
301 MediaType mediaType) {
302 ActiveParticipantType ap = new ActiveParticipantType(userID, userIsRequestor);
303 ap.setAlternativeUserID(altUserID);
304 ap.setUserName(userName);
305 if (roleIdCodes != null) {
306 roleIdCodes.stream()
307 .filter(Objects::nonNull)
308 .forEach(roleId -> ap.getRoleIDCodes().add(roleId));
309 }
310 ap.setNetworkAccessPointID(networkAccessPointID);
311 ap.setNetworkAccessPointTypeCode(networkAccessPointTypeCode);
312 ap.setMediaIdentifier(mediaIdentifier);
313 ap.setMediaType(mediaType);
314 return addActiveParticipant(ap);
315 }
316
317 public T addActiveParticipant(ActiveParticipantType activeParticipantType) {
318 auditMessage.getActiveParticipants().add(activeParticipantType);
319 return self();
320 }
321
322
323
324
325
326
327
328
329 public T addPatientParticipantObject(String patientId, String patientName,
330 List<TypeValuePairType> details,
331 ParticipantObjectDataLifeCycle lifecycle) {
332 return addParticipantObjectIdentification(
333 ParticipantObjectIdTypeCode.PatientNumber,
334 patientName,
335 null,
336 details,
337 requireNonNull(patientId),
338 ParticipantObjectTypeCode.Person,
339 ParticipantObjectTypeCodeRole.Patient,
340 lifecycle,
341 null);
342 }
343
344
345
346
347
348
349
350
351 public T addStudyParticipantObject(String studyId, List<TypeValuePairType> objectDetails) {
352 return addParticipantObjectIdentification(
353 ParticipantObjectIdTypeCode.StudyInstanceUID,
354 studyId,
355 null,
356 objectDetails,
357 requireNonNull(studyId),
358 ParticipantObjectTypeCode.System,
359 ParticipantObjectTypeCodeRole.Report,
360 null,
361 null);
362 }
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 public T addParticipantObjectIdentification(ParticipantObjectIdType objectIDTypeCode,
380 String objectName,
381 byte[] objectQuery,
382 List<TypeValuePairType> objectDetails,
383 String objectID,
384 ParticipantObjectTypeCode objectTypeCode,
385 ParticipantObjectTypeCodeRole objectTypeCodeRole,
386 ParticipantObjectDataLifeCycle objectDataLifeCycle,
387 String objectSensitivity) {
388 ParticipantObjectIdentificationType poit = new ParticipantObjectIdentificationType(objectID, objectIDTypeCode);
389
390 poit.setParticipantObjectName(objectName);
391 poit.setParticipantObjectQuery(objectQuery);
392 if (objectDetails != null) {
393 objectDetails.stream()
394 .filter(Objects::nonNull)
395 .forEach(objectDetail -> poit.getParticipantObjectDetails().add(objectDetail));
396 }
397 poit.setParticipantObjectTypeCode(objectTypeCode);
398 poit.setParticipantObjectTypeCodeRole(objectTypeCodeRole);
399 poit.setParticipantObjectDataLifeCycle(objectDataLifeCycle);
400 poit.setParticipantObjectSensitivity(objectSensitivity);
401 return addParticipantObjectIdentification(poit);
402 }
403
404 public T addParticipantObjectIdentification(ParticipantObjectIdentificationType poit) {
405 auditMessage.getParticipantObjectIdentifications().add(poit);
406 return self();
407 }
408
409 protected NetworkAccessPointTypeCode getNetworkAccessPointCodeFromAddress(String address) {
410 if (address == null) {
411 return null;
412 }
413 return IPV4.matcher(address).matches() || IPV6.matcher(address).matches() ?
414 NetworkAccessPointTypeCode.IPAddress :
415 NetworkAccessPointTypeCode.MachineName;
416 }
417
418
419
420
421
422
423
424
425 public TypeValuePairType getTypeValuePair(String type, Object value) {
426 return new TypeValuePairType(requireNonNull(type), requireNonNull(value).toString());
427 }
428
429
430 }