private void insertAudits(final TableName tableName, final Long entityRecordId, final Long historyRecordId, final ChangeType changeType, final InternalCallContext contextMaybeWithoutAccountRecordId) {
final TableName destinationTableName = Objects.firstNonNull(tableName.getHistoryTableName(), tableName);
final EntityAudit audit = new EntityAudit(destinationTableName, historyRecordId, changeType, clock.getUTCNow());
final InternalCallContext context;
if (TableName.ACCOUNT.equals(tableName) && ChangeType.INSERT.equals(changeType)) {
context = new InternalCallContext(contextMaybeWithoutAccountRecordId, entityRecordId);
} else {
context = contextMaybeWithoutAccountRecordId;
}
sqlDao.insertAuditFromTransaction(audit, context);
if (tableName.getHistoryTableName() != null) {
final CacheController<Object, Object> cacheController = cacheControllerDispatcher.getCacheController(CacheType.AUDIT_LOG_VIA_HISTORY);
if (cacheController != null) {
final String key = buildCacheKey(ImmutableMap.<Integer, Object>of(0, tableName.getHistoryTableName(), 1, tableName.getHistoryTableName(), 2, entityRecordId));
cacheController.remove(key);
}
} else {
final CacheController<Object, Object> cacheController = cacheControllerDispatcher.getCacheController(CacheType.AUDIT_LOG);
if (cacheController != null) {
final String key = buildCacheKey(ImmutableMap.<Integer, Object>of(0, tableName, 1, entityRecordId));
cacheController.remove(key);
}
}
}