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