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