001/*- 002 * #%L 003 * Smile CDR - CDR 004 * %% 005 * Copyright (C) 2016 - 2024 Smile CDR, Inc. 006 * %% 007 * All rights reserved. 008 * #L% 009 */ 010package ca.cdr.api.transactionlog; 011 012import ca.cdr.api.model.enm.TransactionLogEventSubTypeEnum; 013import ca.cdr.api.model.enm.TransactionLogEventTypeEnum; 014import ca.cdr.api.model.json.TransactionLogEventsJson; 015import ca.cdr.api.model.json.TransactionLogStepJson; 016import com.fasterxml.jackson.core.JsonProcessingException; 017import com.fasterxml.jackson.databind.ObjectMapper; 018import org.apache.commons.lang3.StringUtils; 019import org.slf4j.Logger; 020import org.slf4j.LoggerFactory; 021 022import java.util.List; 023import java.util.Map; 024 025import static org.apache.commons.lang3.ObjectUtils.isEmpty; 026 027/** 028 * Parameter Object class encapsulating the properties required to persist a {@link TransactionLogEventsJson.TransactionLogEventJson} 029 * 030 * @apiNote myType the type of event that is logged 031 * @apiNote mySubType sub-categories precising the type 032 * @apiNote myTransactionGuid the GUID of the transaction log 033 * @apiNote myInitialSteps the {@link TransactionLogStepJson} to add to this transaction log 034 * @apiNote myTransactionId unique ID identifying the request for which this transaction is created 035 * @apiNote myModuleId the ID of the module creating the log 036 * @apiNote myUserPid the PID of the user creating the log 037 * @apiNote myAdditionalJson inline JSON of client provided key/value pairs 038 * 039 */ 040public class IncomingTransactionLog { 041 private static final Logger ourLog = LoggerFactory.getLogger(IncomingTransactionLog.class); 042 private final TransactionLogEventTypeEnum myType; 043 private final TransactionLogEventSubTypeEnum mySubType; 044 private final List<TransactionLogStepJson> myInitialSteps; 045 private final String myTransactionId; 046 private final String myTransactionGuid; 047 private final String myModuleId; 048 private final String myAdditionalJson; 049 050 private final Long myUserPid; 051 private final String myUsername; 052 053 public static IncomingTransactionLogBuilder builder() { 054 return new IncomingTransactionLogBuilder(); 055 } 056 057 public static IncomingTransactionLogBuilder from(IncomingTransactionLog theIncomingTransactionLog) { 058 return builder() 059 .withType(theIncomingTransactionLog.myType) 060 .withSubType(theIncomingTransactionLog.mySubType) 061 .withInitialSteps(theIncomingTransactionLog.myInitialSteps) 062 .withTransactionId(theIncomingTransactionLog.myTransactionId) 063 .withTransactionGuid(theIncomingTransactionLog.myTransactionGuid) 064 .withModuleId(theIncomingTransactionLog.myModuleId) 065 .withAdditionalJsonSting(theIncomingTransactionLog.myAdditionalJson) 066 .withUserPid(theIncomingTransactionLog.myUserPid) 067 .withUsername(theIncomingTransactionLog.myUsername); 068 } 069 070 private IncomingTransactionLog(IncomingTransactionLogBuilder theIncomingTransactionLogBuilder) { 071 myType = theIncomingTransactionLogBuilder.myType; 072 mySubType = theIncomingTransactionLogBuilder.mySubType; 073 myInitialSteps = theIncomingTransactionLogBuilder.myInitialSteps; 074 myTransactionId = theIncomingTransactionLogBuilder.myTransactionId; 075 myTransactionGuid = theIncomingTransactionLogBuilder.myTransactionGuid; 076 myModuleId = theIncomingTransactionLogBuilder.myModuleId; 077 myAdditionalJson = theIncomingTransactionLogBuilder.myAdditionalJson; 078 myUsername = theIncomingTransactionLogBuilder.myUsername; 079 myUserPid = theIncomingTransactionLogBuilder.myUserPid; 080 } 081 082 public static IncomingTransactionLog from(TransactionLogEventsJson.TransactionLogEventJson theLog) { 083 return builder() 084 .withType(theLog.getType()) 085 .withSubType(theLog.getSubType()) 086 .withInitialSteps(theLog.getEvents()) 087 .withTransactionId(theLog.getRequestId()) 088 .withTransactionGuid(theLog.getTransactionGuid()) 089 .withModuleId(theLog.getEndpointModuleId()) 090 .withAdditionalJsonSting(theLog.getAdditionalJson()) 091 .withUserPid(theLog.getUserId()) 092 .withUsername(theLog.getUsername()) 093 .build(); 094 } 095 096 public TransactionLogEventTypeEnum getType() { 097 return myType; 098 } 099 100 public TransactionLogEventSubTypeEnum getSubType() { 101 return mySubType; 102 } 103 104 public Long getUserPid() { 105 return myUserPid; 106 } 107 108 public String getUsername() { 109 return myUsername; 110 } 111 112 public List<TransactionLogStepJson> getInitialSteps() { 113 return myInitialSteps; 114 } 115 116 public String getTransactionId() { 117 return myTransactionId; 118 } 119 120 public String getTransactionGuid() { 121 return myTransactionGuid; 122 } 123 124 public boolean hasTransactionGuid() { 125 return StringUtils.isNotEmpty(myTransactionGuid); 126 } 127 128 public String getModuleId() { 129 return myModuleId; 130 } 131 132 public String getAdditionalJson() { 133 return myAdditionalJson; 134 } 135 136 public static final class IncomingTransactionLogBuilder { 137 private final ObjectMapper ourObjectMapper = new ObjectMapper(); 138 private TransactionLogEventTypeEnum myType; 139 private TransactionLogEventSubTypeEnum mySubType; 140 private List<TransactionLogStepJson> myInitialSteps; 141 private String myTransactionId; 142 private String myTransactionGuid; 143 private String myModuleId; 144 private String myAdditionalJson; 145 private String myUsername; 146 private Long myUserPid; 147 148 private IncomingTransactionLogBuilder() {} 149 150 public IncomingTransactionLogBuilder withType(TransactionLogEventTypeEnum theType) { 151 myType = theType; 152 return this; 153 } 154 155 public IncomingTransactionLogBuilder withSubType(TransactionLogEventSubTypeEnum theSubType) { 156 mySubType = theSubType; 157 return this; 158 } 159 160 public IncomingTransactionLogBuilder withInitialSteps(List<TransactionLogStepJson> theInitialSteps) { 161 myInitialSteps = theInitialSteps; 162 return this; 163 } 164 165 public IncomingTransactionLogBuilder withTransactionId(String theTransactionId) { 166 myTransactionId = theTransactionId; 167 return this; 168 } 169 170 public IncomingTransactionLogBuilder withTransactionGuid(String theTransactionGuid) { 171 myTransactionGuid = theTransactionGuid; 172 return this; 173 } 174 175 public IncomingTransactionLogBuilder withModuleId(String theModuleId) { 176 myModuleId = theModuleId; 177 return this; 178 } 179 180 public IncomingTransactionLogBuilder withUsername(String theUsername) { 181 myUsername = theUsername; 182 return this; 183 } 184 185 public IncomingTransactionLogBuilder withUserPid(Long theUserPid) { 186 myUserPid = theUserPid; 187 return this; 188 } 189 190 public IncomingTransactionLogBuilder withAdditionalProperties(Map<String, Object> theAdditionalProperties) { 191 if (isEmpty(theAdditionalProperties)) { 192 return this; 193 } 194 195 try { 196 myAdditionalJson = ourObjectMapper.writeValueAsString(theAdditionalProperties); 197 } catch (JsonProcessingException e) { 198 ourLog.error("Could not serialize the additional properties to json string", e); 199 } 200 201 return this; 202 } 203 204 public IncomingTransactionLog build() { 205 return new IncomingTransactionLog(this); 206 } 207 208 private IncomingTransactionLogBuilder withAdditionalJsonSting(String theAdditionalJsonSting) { 209 myAdditionalJson = theAdditionalJsonSting; 210 return this; 211 } 212 } 213}