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