use of org.killbill.billing.util.audit.DefaultAuditLog in project killbill by killbill.
the class TestAuditLogJson method testConstructor.
@Test(groups = "fast")
public void testConstructor() throws Exception {
final TableName tableName = TableName.ACCOUNT_EMAIL_HISTORY;
final long recordId = Long.MAX_VALUE;
final ChangeType changeType = ChangeType.DELETE;
final EntityAudit entityAudit = new EntityAudit(tableName, recordId, changeType, null);
final AuditLog auditLog = new DefaultAuditLog(new AuditLogModelDao(entityAudit, callContext), ObjectType.ACCOUNT_EMAIL, UUID.randomUUID());
final AuditLogJson auditLogJson = new AuditLogJson(auditLog);
Assert.assertEquals(auditLogJson.getChangeType(), changeType.toString());
Assert.assertNotNull(auditLogJson.getChangeDate());
Assert.assertEquals(auditLogJson.getChangedBy(), callContext.getUserName());
Assert.assertEquals(auditLogJson.getReasonCode(), callContext.getReasonCode());
Assert.assertEquals(auditLogJson.getComments(), callContext.getComments());
Assert.assertEquals(auditLogJson.getUserToken(), callContext.getUserToken().toString());
}
use of org.killbill.billing.util.audit.DefaultAuditLog in project killbill by killbill.
the class DefaultAuditDao method buildAuditLogsFromModelDao.
private Iterator<AuditLog> buildAuditLogsFromModelDao(final Iterator<AuditLogModelDao> auditLogsForAccountRecordId, final InternalTenantContext tenantContext) {
final Map<TableName, Map<Long, UUID>> recordIdIdsCache = new HashMap<TableName, Map<Long, UUID>>();
final Map<TableName, Map<Long, UUID>> historyRecordIdIdsCache = new HashMap<TableName, Map<Long, UUID>>();
return Iterators.<AuditLogModelDao, AuditLog>transform(auditLogsForAccountRecordId, new Function<AuditLogModelDao, AuditLog>() {
@Override
public AuditLog apply(final AuditLogModelDao input) {
// If input is for e.g. TAG_DEFINITION_HISTORY, retrieve TAG_DEFINITIONS
// For tables without history, e.g. TENANT, originalTableNameForHistoryTableName will be null
final TableName originalTableNameForHistoryTableName = findTableNameForHistoryTableName(input.getTableName());
final ObjectType objectType;
final UUID auditedEntityId;
if (originalTableNameForHistoryTableName != null) {
// input point to a history entry
objectType = originalTableNameForHistoryTableName.getObjectType();
if (historyRecordIdIdsCache.get(originalTableNameForHistoryTableName) == null) {
if (TableName.ACCOUNT.equals(originalTableNameForHistoryTableName)) {
final Iterable<RecordIdIdMappings> mappings = nonEntitySqlDao.getHistoryRecordIdIdMappingsForAccountsTable(originalTableNameForHistoryTableName.getTableName(), input.getTableName().getTableName(), tenantContext);
historyRecordIdIdsCache.put(originalTableNameForHistoryTableName, RecordIdIdMappings.toMap(mappings));
} else if (TableName.TAG_DEFINITIONS.equals(originalTableNameForHistoryTableName)) {
final Iterable<RecordIdIdMappings> mappings = nonEntitySqlDao.getHistoryRecordIdIdMappingsForTablesWithoutAccountRecordId(originalTableNameForHistoryTableName.getTableName(), input.getTableName().getTableName(), tenantContext);
historyRecordIdIdsCache.put(originalTableNameForHistoryTableName, RecordIdIdMappings.toMap(mappings));
} else {
final Iterable<RecordIdIdMappings> mappings = nonEntitySqlDao.getHistoryRecordIdIdMappings(originalTableNameForHistoryTableName.getTableName(), input.getTableName().getTableName(), tenantContext);
historyRecordIdIdsCache.put(originalTableNameForHistoryTableName, RecordIdIdMappings.toMap(mappings));
}
}
auditedEntityId = historyRecordIdIdsCache.get(originalTableNameForHistoryTableName).get(input.getTargetRecordId());
} else {
objectType = input.getTableName().getObjectType();
if (recordIdIdsCache.get(input.getTableName()) == null) {
final Iterable<RecordIdIdMappings> mappings = nonEntitySqlDao.getRecordIdIdMappings(input.getTableName().getTableName(), tenantContext);
recordIdIdsCache.put(input.getTableName(), RecordIdIdMappings.toMap(mappings));
}
auditedEntityId = recordIdIdsCache.get(input.getTableName()).get(input.getTargetRecordId());
}
return new DefaultAuditLog(input, objectType, auditedEntityId);
}
private TableName findTableNameForHistoryTableName(final TableName historyTableName) {
for (final TableName tableName : TableName.values()) {
if (historyTableName.equals(tableName.getHistoryTableName())) {
return tableName;
}
}
return null;
}
});
}
Aggregations