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.TransactionLogBodyTypeEnum;
014import ca.cdr.api.model.enm.TransactionLogEventSubTypeEnum;
015import ca.cdr.api.model.enm.TransactionLogOutcomeEnum;
016import ca.cdr.api.model.enm.TransactionLogStepTypeEnum;
017import ca.cdr.api.model.json.jackson2deprecated.JsonDateDeserializerJacksonV2Deprecated;
018import ca.cdr.api.model.json.jackson2deprecated.JsonDateSerializerJacksonV2Deprecated;
019import ca.cdr.api.util.HttpRequestUtil;
020import ca.cdr.api.validator.enm.RequestValidatingResultEnum;
021import ca.uhn.fhir.model.api.IModelJson;
022import com.fasterxml.jackson.annotation.JsonProperty;
023import com.fasterxml.jackson.annotation.JsonValue;
024import io.swagger.v3.oas.annotations.media.Schema;
025import jakarta.servlet.http.HttpServletRequest;
026import tools.jackson.databind.annotation.JsonDeserialize;
027import tools.jackson.databind.annotation.JsonSerialize;
028
029import java.sql.Timestamp;
030import java.util.ArrayList;
031import java.util.Date;
032import java.util.List;
033
034import static java.util.Objects.nonNull;
035
036public class TransactionLogStepJson implements IModelJson, Cloneable {
037
038        @JsonProperty(value = "body")
039        private StringWrapper myBody;
040
041        @JsonProperty(value = "bodyType")
042        private TransactionLogBodyTypeEnum myBodyType;
043
044        @JsonProperty(value = "endpointLocalHost")
045        private String myEndpointLocalHost;
046
047        @JsonProperty(value = "endpointLocalPort")
048        private Integer myEndpointLocalPort;
049
050        @JsonProperty(value = "endpointRemoteHost")
051        private String myEndpointRemoteHost;
052
053        @JsonProperty(value = "endpointRemotePort")
054        private Integer myEndpointRemotePort;
055
056        @JsonProperty("id")
057        private Long myId;
058
059        @JsonProperty("initialTimestamp")
060        @com.fasterxml.jackson.databind.annotation.JsonSerialize(using = JsonDateSerializerJacksonV2Deprecated.class)
061        @JsonSerialize(using = JsonDateSerializer.class)
062        @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = JsonDateDeserializerJacksonV2Deprecated.class)
063        @JsonDeserialize(using = JsonDateDeserializer.class)
064        private Date myInitialTimestamp;
065
066        @JsonProperty("outcome")
067        private TransactionLogOutcomeEnum myOutcome;
068
069        @JsonProperty(value = "processingTime")
070        private Long myProcessingTime;
071
072        @JsonProperty("requestUrl")
073        private String myRequestUrl;
074
075        @JsonProperty("requestVerb")
076        private String myRequestVerb;
077
078        @JsonProperty("subType")
079        private TransactionLogEventSubTypeEnum mySubType;
080
081        @JsonProperty("type")
082        private TransactionLogStepTypeEnum myType;
083
084        @Schema(description = "The HTTP response status, e.g. 200 or 404")
085        @JsonProperty("responseStatus")
086        private Short myResponseStatus;
087
088        @JsonProperty("requestValidatingResult")
089        private RequestValidatingResultEnum myRequestValidatingResult;
090
091        @JsonProperty("subscriptionId")
092        private String mySubscriptionId;
093
094        @JsonProperty("storageOutcomes")
095        private List<TransactionLogStorageOutcomeJson> myStorageOutcomes;
096
097        /**
098         * Constructor
099         */
100        public TransactionLogStepJson() {
101                super();
102        }
103
104        private Long userId;
105        private String userModuleId;
106        private String userNodeId;
107        private String username;
108        private String userFamilyName;
109        private String userGivenName;
110        private Long myPid;
111        private String clientId;
112        private String clientNodeId;
113        private String clientModuleId;
114        private String clientPid;
115        private String clientName;
116
117        public List<TransactionLogStorageOutcomeJson> getStorageOutcomes() {
118                if (myStorageOutcomes == null) {
119                        myStorageOutcomes = new ArrayList<>();
120                }
121                return myStorageOutcomes;
122        }
123
124        public boolean hasStorageOutcomes() {
125                return myStorageOutcomes != null && !myStorageOutcomes.isEmpty();
126        }
127
128        public Long getUserId() {
129                return userId;
130        }
131
132        public void setUserId(Long userId) {
133                this.userId = userId;
134        }
135
136        public String getUserModuleId() {
137                return userModuleId;
138        }
139
140        public void setUserModuleId(String userModuleId) {
141                this.userModuleId = userModuleId;
142        }
143
144        public String getUserNodeId() {
145                return userNodeId;
146        }
147
148        public void setUserNodeId(String userNodeId) {
149                this.userNodeId = userNodeId;
150        }
151
152        public String getUsername() {
153                return username;
154        }
155
156        public void setUsername(String username) {
157                this.username = username;
158        }
159
160        public String getUserFamilyName() {
161                return userFamilyName;
162        }
163
164        public void setUserFamilyName(String userFamilyName) {
165                this.userFamilyName = userFamilyName;
166        }
167
168        public String getUserGivenName() {
169                return userGivenName;
170        }
171
172        public void setUserGivenName(String userGivenName) {
173                this.userGivenName = userGivenName;
174        }
175
176        public String getBody() {
177                return nonNull(this.myBody) ? this.myBody.getValue() : null;
178        }
179
180        public void setBody(String theBody) {
181                this.myBody = nonNull(theBody) ? new StringWrapper(theBody) : null;
182        }
183
184        public TransactionLogBodyTypeEnum getBodyType() {
185                return myBodyType;
186        }
187
188        public void setBodyType(TransactionLogBodyTypeEnum theBodyType) {
189                myBodyType = theBodyType;
190        }
191
192        public String getEndpointLocalHost() {
193                return myEndpointLocalHost;
194        }
195
196        public void setEndpointLocalHost(String theEndpointLocalHost) {
197                myEndpointLocalHost = theEndpointLocalHost;
198        }
199
200        public Integer getEndpointLocalPort() {
201                return myEndpointLocalPort;
202        }
203
204        public void setEndpointLocalPort(Integer theEndpointLocalPort) {
205                myEndpointLocalPort = theEndpointLocalPort;
206        }
207
208        public String getEndpointRemoteHost() {
209                return myEndpointRemoteHost;
210        }
211
212        public void setEndpointRemoteHost(String theEndpointRemoteHost) {
213                myEndpointRemoteHost = theEndpointRemoteHost;
214        }
215
216        public Integer getEndpointRemotePort() {
217                return myEndpointRemotePort;
218        }
219
220        public void setEndpointRemotePort(Integer theEndpointRemotePort) {
221                myEndpointRemotePort = theEndpointRemotePort;
222        }
223
224        public Long getId() {
225                return myId;
226        }
227
228        public void setId(Long thePid) {
229                myId = thePid;
230        }
231
232        public Date getInitialTimestamp() {
233                return myInitialTimestamp;
234        }
235
236        public void setInitialTimestamp(Date theTimestamp) {
237                myInitialTimestamp = theTimestamp;
238                if (myInitialTimestamp instanceof Timestamp) {
239                        myInitialTimestamp = new Date(myInitialTimestamp.getTime());
240                }
241        }
242
243        public TransactionLogOutcomeEnum getOutcome() {
244                return myOutcome;
245        }
246
247        public void setOutcome(TransactionLogOutcomeEnum theOutcome) {
248                myOutcome = theOutcome;
249        }
250
251        public Long getProcessingTime() {
252                return myProcessingTime;
253        }
254
255        public void setProcessingTime(Long theProcessingTime) {
256                myProcessingTime = theProcessingTime;
257        }
258
259        public String getRequestUrl() {
260                return myRequestUrl;
261        }
262
263        public void setRequestUrl(String theRequestUrl) {
264                myRequestUrl = theRequestUrl;
265        }
266
267        public String getRequestVerb() {
268                return myRequestVerb;
269        }
270
271        public void setRequestVerb(String theRequestVerb) {
272                myRequestVerb = theRequestVerb;
273        }
274
275        public TransactionLogEventSubTypeEnum getSubType() {
276                return mySubType;
277        }
278
279        public void setSubType(TransactionLogEventSubTypeEnum theSubType) {
280                mySubType = theSubType;
281        }
282
283        public TransactionLogStepTypeEnum getType() {
284                return myType;
285        }
286
287        public void setType(TransactionLogStepTypeEnum theType) {
288                myType = theType;
289        }
290
291        public Short getResponseStatus() {
292                return myResponseStatus;
293        }
294
295        public void setResponseStatus(Short theResponseStatus) {
296                myResponseStatus = theResponseStatus;
297        }
298
299        public RequestValidatingResultEnum getRequestValidatingResult() {
300                return myRequestValidatingResult;
301        }
302
303        public void setRequestValidatingResult(RequestValidatingResultEnum theRequestValidatingResult) {
304                myRequestValidatingResult = theRequestValidatingResult;
305        }
306
307        public String getSubscriptionId() {
308                return mySubscriptionId;
309        }
310
311        public void setSubscriptionId(String theSubscriptionId) {
312                mySubscriptionId = theSubscriptionId;
313        }
314
315        public Long getPid() {
316                return myPid;
317        }
318
319        public void setPid(Long thePid) {
320                myPid = thePid;
321        }
322
323        public String getClientId() {
324                return this.clientId;
325        }
326
327        public void setClientId(String theClientId) {
328                this.clientId = theClientId;
329        }
330
331        public String getClientModuleId() {
332                return clientModuleId;
333        }
334
335        public void setClientModuleId(String clientModuleId) {
336                this.clientModuleId = clientModuleId;
337        }
338
339        public String getClientNodeId() {
340                return clientNodeId;
341        }
342
343        public void setClientNodeId(String clientNodeId) {
344                this.clientNodeId = clientNodeId;
345        }
346
347        public String getClientPid() {
348                return clientPid;
349        }
350
351        public void setClientPid(String clientPid) {
352                this.clientPid = clientPid;
353        }
354
355        public String getClientName() {
356                return clientName;
357        }
358
359        public void setClientName(String clientName) {
360                this.clientName = clientName;
361        }
362
363        @Override
364        public String toString() {
365                String retval = null;
366                if (getType() != null) {
367                        retval = getType().toString();
368                        if (getSubType() != null) {
369                                retval += "/" + getSubType();
370                        }
371                }
372                return retval;
373        }
374
375        public void setRequestUrlFromServletRequest(HttpServletRequest theRequest) {
376                StringBuffer requestURL = theRequest.getRequestURL();
377                String queryString = theRequest.getQueryString();
378
379                if (queryString == null) {
380                        setRequestUrl(requestURL.toString());
381                } else {
382                        setRequestUrl(requestURL.append('?').append(queryString).toString());
383                }
384        }
385
386        public void populateRequestAddresses(HttpServletRequest theRequest) {
387                String originId = HttpRequestUtil.getRemoteAddressOfServletRequest(theRequest);
388                setEndpointRemoteHost(originId);
389                setEndpointRemotePort(theRequest.getRemotePort());
390                setEndpointLocalHost(theRequest.getLocalAddr());
391                setEndpointLocalPort(theRequest.getLocalPort());
392        }
393
394        @Override
395        public TransactionLogStepJson clone() {
396                try {
397                        TransactionLogStepJson clone = (TransactionLogStepJson) super.clone();
398                        if (this.hasStorageOutcomes()) {
399                                List<TransactionLogStorageOutcomeJson> clonedOutcomes = new ArrayList<>();
400                                for (TransactionLogStorageOutcomeJson outcome : this.getStorageOutcomes()) {
401                                        clonedOutcomes.add(outcome.clone());
402                                }
403                                clone.myStorageOutcomes = clonedOutcomes;
404                        }
405                        return clone;
406
407                } catch (CloneNotSupportedException theE) {
408                        throw new UnsupportedOperationException();
409                }
410        }
411
412        public void clearStorageOutcomes() {
413                if (myStorageOutcomes != null) {
414                        myStorageOutcomes.clear();
415                }
416        }
417
418        public static class StringWrapper {
419
420                private final String myValue;
421
422                public StringWrapper(String theValue) {
423                        this.myValue = theValue;
424                }
425
426                @JsonValue
427                public String getValue() {
428                        return myValue;
429                }
430        }
431}