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