001package ca.cdr.api.model.json;
002
003/*
004 * #%L
005 * Smile CDR - CDR
006 * %%
007 * Copyright (C) 2016 - 2026 Smile CDR, Inc.
008 * %%
009 * All rights reserved.
010 * #L%
011 */
012
013import ca.cdr.api.model.enm.AuthenticatedUserTypeEnum;
014import ca.cdr.api.model.json.jackson2deprecated.JsonDateDeserializerJacksonV2Deprecated;
015import ca.cdr.api.model.json.jackson2deprecated.JsonDateSerializerJacksonV2Deprecated;
016import com.fasterxml.jackson.annotation.JsonProperty;
017import io.swagger.v3.oas.annotations.Parameter;
018import io.swagger.v3.oas.annotations.media.Schema;
019import jakarta.validation.Valid;
020import jakarta.validation.constraints.NotBlank;
021import jakarta.validation.constraints.NotNull;
022import tools.jackson.databind.annotation.JsonDeserialize;
023import tools.jackson.databind.annotation.JsonSerialize;
024
025import java.time.Instant;
026import java.util.ArrayList;
027import java.util.Collection;
028import java.util.Date;
029import java.util.List;
030
031import static org.apache.commons.lang3.StringUtils.isNotBlank;
032
033/**
034 * This class represents a single Audit Event, with nested objects for all related audit data.
035 *
036 * Also see {@link StringifiedCollectionMixin} to see how we flatten high cardinality elements during CSV export.
037 */
038public class AuditEventJson implements IModelJson {
039        @JsonProperty("id")
040        private Long myId;
041
042        @JsonProperty("transactionGuid")
043        private String myTransactionGuid;
044
045        @JsonProperty("requestId")
046        private String myRequestId;
047
048        @NotBlank
049        @JsonProperty("endpointModuleId")
050        private String myEndpointModuleId;
051
052        @NotBlank
053        @JsonProperty("endpointNodeId")
054        private String myEndpointNodeId;
055        // Nullable
056        @JsonProperty("remoteAddress")
057        private String myRemoteAddress;
058
059        @Valid
060        @JsonProperty("targetModules")
061        private Collection<AuditEventTargetModuleJson> myTargetModules;
062
063        @Valid
064        @JsonProperty("targetResources")
065        private List<AuditEventTargetResourceJson> myTargetResources;
066
067        @Valid
068        @JsonProperty("targetUsers")
069        private Collection<AuditEventTargetUserJson> myTargetUsers;
070
071        @JsonProperty("targetRequestId")
072        @Parameter(
073                        description =
074                                        "If the target of this audit event is a transaction log item (i.e. we are auditing the viewing of the transaction log) this is the Request ID for the transaction log entry in question")
075        private String myTargetRequestId;
076
077        @NotNull
078        @JsonProperty("timestamp")
079        @com.fasterxml.jackson.databind.annotation.JsonSerialize(using = JsonDateSerializerJacksonV2Deprecated.class)
080        @JsonSerialize(using = JsonDateSerializer.class)
081        @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = JsonDateDeserializerJacksonV2Deprecated.class)
082        @JsonDeserialize(using = JsonDateDeserializer.class)
083        private Date myTimestamp;
084
085        @NotBlank
086        @JsonProperty("typeCode")
087        private String myTypeCode;
088
089        @NotBlank
090        @JsonProperty("typeDisplay")
091        private String myTypeDisplay;
092
093        @NotBlank
094        @JsonProperty("typeSystem")
095        private String myTypeSystem;
096
097        @JsonProperty("familyName")
098        private String myUserFamilyName;
099
100        @JsonProperty("givenName")
101        private String myUserGivenName;
102
103        @JsonProperty("userId")
104        private Long myUserId;
105
106        @JsonProperty("username")
107        private String myUsername;
108
109        @JsonProperty("userModuleId")
110        private String myUserModuleId;
111
112        @JsonProperty("userNodeId")
113        private String myUserNodeId;
114
115        @JsonProperty("authenticatedUserType")
116        @NotNull
117        @Parameter(
118                        description =
119                                        "Was the authentication performed by a user (generally a person) or a client (generally a system)")
120        private AuthenticatedUserTypeEnum myAuthenticatedUserType;
121
122        @JsonProperty("clientNodeId")
123        @Parameter(
124                        description =
125                                        "If this event was received from an OAuth2 client, this is the node ID of the module that supplied the client definition")
126        private String myClientNodeId;
127
128        @JsonProperty("clientModuleId")
129        @Parameter(
130                        description =
131                                        "If this event was received from an OAuth2 client, this is the module ID of the module that supplied the client definition")
132        private String myClientModuleId;
133
134        @JsonProperty("clientId")
135        @Parameter(description = "If this event was received from an OAuth2 client, this is the client ID")
136        private String myClientId;
137
138        @JsonProperty("clientPid")
139        @Schema(
140                        description = "If this event was received from an OAuth2 client, this is the client PID",
141                        accessMode = Schema.AccessMode.READ_ONLY)
142        private Long myClientPid;
143
144        @JsonProperty("clientName")
145        @Parameter(description = "If this event was received from an OAuth2 client, this is the client name")
146        private String myClientName;
147
148        @JsonProperty("headers")
149        @Parameter(
150                        description =
151                                        "A list of headers to be stored in the audit event. This can be configured to change which headers are persisted.")
152        private Collection<AuditEventHeaderJson> myHeaders;
153
154        @JsonProperty("additionalJson")
155        @Parameter(description = "Additional desired JSON to be logged")
156        private String myAdditionalJson;
157        /**
158         * Constructor
159         */
160        public AuditEventJson() {
161                super();
162        }
163
164        public String getRequestId() {
165                return myRequestId;
166        }
167
168        public void setRequestId(String theRequestId) {
169                myRequestId = theRequestId;
170        }
171
172        public Long getClientPid() {
173                return myClientPid;
174        }
175
176        public void setClientPid(Long theClientPid) {
177                myClientPid = theClientPid;
178        }
179
180        public String getClientName() {
181                return myClientName;
182        }
183
184        public void setClientName(String theClientName) {
185                myClientName = theClientName;
186        }
187
188        public String getClientNodeId() {
189                return myClientNodeId;
190        }
191
192        public void setClientNodeId(String theClientNodeId) {
193                myClientNodeId = theClientNodeId;
194        }
195
196        public String getClientModuleId() {
197                return myClientModuleId;
198        }
199
200        public void setClientModuleId(String theClientModuleId) {
201                myClientModuleId = theClientModuleId;
202        }
203
204        public String getClientId() {
205                return myClientId;
206        }
207
208        public void setClientId(String theClientId) {
209                myClientId = theClientId;
210        }
211
212        public String getEndpointModuleId() {
213                return myEndpointModuleId;
214        }
215
216        public void setEndpointModuleId(String theModuleId) {
217                myEndpointModuleId = theModuleId;
218        }
219
220        public String getEndpointNodeId() {
221                return myEndpointNodeId;
222        }
223
224        public void setEndpointNodeId(String theNodeId) {
225                myEndpointNodeId = theNodeId;
226        }
227
228        public Long getId() {
229                return myId;
230        }
231
232        public void setId(Long theId) {
233                myId = theId;
234        }
235
236        public String getUserModuleId() {
237                return myUserModuleId;
238        }
239
240        public void setUserModuleId(String theModuleId) {
241                myUserModuleId = theModuleId;
242        }
243
244        public String getUserNodeId() {
245                return myUserNodeId;
246        }
247
248        public void setUserNodeId(String theNodeId) {
249                myUserNodeId = theNodeId;
250        }
251
252        public String getRemoteAddress() {
253                return myRemoteAddress;
254        }
255
256        public void setRemoteAddress(String theRemoteAddress) {
257                myRemoteAddress = theRemoteAddress;
258        }
259
260        public Collection<AuditEventTargetModuleJson> getTargetModules() {
261                if (myTargetModules == null) {
262                        myTargetModules = new ArrayList<>();
263                }
264                return myTargetModules;
265        }
266
267        public List<AuditEventTargetResourceJson> getTargetResources() {
268                if (myTargetResources == null) {
269                        myTargetResources = new ArrayList<>();
270                }
271                return myTargetResources;
272        }
273
274        public Collection<AuditEventTargetUserJson> getTargetUsers() {
275                if (myTargetUsers == null) {
276                        myTargetUsers = new ArrayList<>();
277                }
278                return myTargetUsers;
279        }
280
281        public Collection<AuditEventHeaderJson> getHeaders() {
282                if (myHeaders == null) {
283                        myHeaders = new ArrayList<>();
284                }
285                return myHeaders;
286        }
287
288        public String getAdditionalJson() {
289                return myAdditionalJson;
290        }
291
292        public void setAdditionalJson(String theAdditionalJson) {
293                myAdditionalJson = theAdditionalJson;
294        }
295
296        public Date getTimestamp() {
297                return myTimestamp;
298        }
299
300        public void setTimestamp(Instant theTimestamp) {
301                myTimestamp = new Date(theTimestamp.toEpochMilli());
302        }
303
304        public void setTimestamp(Date theTimestamp) {
305                myTimestamp = theTimestamp;
306        }
307
308        public String getTypeCode() {
309                return myTypeCode;
310        }
311
312        public void setTypeCode(String theCode) {
313                myTypeCode = theCode;
314        }
315
316        public String getTypeDisplay() {
317                return myTypeDisplay;
318        }
319
320        public void setTypeDisplay(String theTypeDisplay) {
321                myTypeDisplay = theTypeDisplay;
322        }
323
324        public String getTypeSystem() {
325                return myTypeSystem;
326        }
327
328        public void setTypeSystem(String theSystem) {
329                myTypeSystem = theSystem;
330        }
331
332        public String getUserFamilyName() {
333                return myUserFamilyName;
334        }
335
336        public void setUserFamilyName(String theUserFamilyName) {
337                myUserFamilyName = theUserFamilyName;
338        }
339
340        public String getUserGivenName() {
341                return myUserGivenName;
342        }
343
344        public void setUserGivenName(String theUserGivenName) {
345                myUserGivenName = theUserGivenName;
346        }
347
348        public Long getUserId() {
349                return myUserId;
350        }
351
352        public void setUserId(Long theUserId) {
353                myUserId = theUserId;
354        }
355
356        public String getUsername() {
357                return myUsername;
358        }
359
360        public void setUsername(String theUsername) {
361                myUsername = theUsername;
362        }
363
364        @SuppressWarnings("unused") // Called by Web Admin Console
365        public boolean hasTargets() {
366                return getTargetModules().isEmpty() == false
367                                || getTargetResources().isEmpty() == false
368                                || getTargetUsers().isEmpty() == false
369                                || isNotBlank(myTargetRequestId);
370        }
371
372        public AuthenticatedUserTypeEnum getAuthenticatedUserType() {
373                return myAuthenticatedUserType;
374        }
375
376        public void setAuthenticatedUserType(AuthenticatedUserTypeEnum theAuthenticatedUserType) {
377                myAuthenticatedUserType = theAuthenticatedUserType;
378        }
379
380        /**
381         * If the target of this audit event is a transaction log item (i.e. we are auditing the viewing of the transaction log) this is the Request ID for the transaction log entry in question
382         */
383        public String getTargetRequestId() {
384                return myTargetRequestId;
385        }
386
387        /**
388         * If the target of this audit event is a transaction log item (i.e. we are auditing the viewing of the transaction log) this is the Request ID for the transaction log entry in question
389         */
390        public void setTargetRequestId(String theTargetRequestId) {
391                myTargetRequestId = theTargetRequestId;
392        }
393
394        public String getTransactionGuid() {
395                return myTransactionGuid;
396        }
397
398        public void setTransactionGuid(String theTransactionGuid) {
399                myTransactionGuid = theTransactionGuid;
400        }
401}