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