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