Search in sources :

Example 1 with TableName

use of org.killbill.billing.util.dao.TableName in project killbill by killbill.

the class TestDefaultAuditLog method testGetters.

@Test(groups = "fast")
public void testGetters() 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 UUID tenantId = UUID.randomUUID();
    final String userName = UUID.randomUUID().toString();
    final CallOrigin callOrigin = CallOrigin.EXTERNAL;
    final UserType userType = UserType.CUSTOMER;
    final UUID userToken = UUID.randomUUID();
    final ClockMock clock = new ClockMock();
    final CallContext callContext = new DefaultCallContext(tenantId, userName, callOrigin, userType, userToken, clock);
    final AuditLog auditLog = new DefaultAuditLog(new AuditLogModelDao(entityAudit, callContext), ObjectType.ACCOUNT_EMAIL, UUID.randomUUID());
    Assert.assertEquals(auditLog.getChangeType(), changeType);
    Assert.assertNull(auditLog.getComment());
    Assert.assertNotNull(auditLog.getCreatedDate());
    Assert.assertNull(auditLog.getReasonCode());
    Assert.assertEquals(auditLog.getUserName(), userName);
    Assert.assertEquals(auditLog.getUserToken(), userToken.toString());
}
Also used : CallOrigin(org.killbill.billing.util.callcontext.CallOrigin) ClockMock(org.killbill.clock.ClockMock) CallContext(org.killbill.billing.util.callcontext.CallContext) DefaultCallContext(org.killbill.billing.callcontext.DefaultCallContext) EntityAudit(org.killbill.billing.util.dao.EntityAudit) TableName(org.killbill.billing.util.dao.TableName) DefaultCallContext(org.killbill.billing.callcontext.DefaultCallContext) AuditLogModelDao(org.killbill.billing.util.audit.dao.AuditLogModelDao) UUID(java.util.UUID) UserType(org.killbill.billing.util.callcontext.UserType) Test(org.testng.annotations.Test)

Example 2 with TableName

use of org.killbill.billing.util.dao.TableName 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());
}
Also used : EntityAudit(org.killbill.billing.util.dao.EntityAudit) TableName(org.killbill.billing.util.dao.TableName) DefaultAuditLog(org.killbill.billing.util.audit.DefaultAuditLog) ChangeType(org.killbill.billing.util.audit.ChangeType) AuditLogModelDao(org.killbill.billing.util.audit.dao.AuditLogModelDao) AuditLog(org.killbill.billing.util.audit.AuditLog) DefaultAuditLog(org.killbill.billing.util.audit.DefaultAuditLog) Test(org.testng.annotations.Test)

Example 3 with TableName

use of org.killbill.billing.util.dao.TableName 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;
        }
    });
}
Also used : HashMap(java.util.HashMap) DefaultAuditLog(org.killbill.billing.util.audit.DefaultAuditLog) AuditLog(org.killbill.billing.util.audit.AuditLog) TableName(org.killbill.billing.util.dao.TableName) ObjectType(org.killbill.billing.ObjectType) DefaultAccountAuditLogsForObjectType(org.killbill.billing.util.audit.DefaultAccountAuditLogsForObjectType) DefaultAuditLog(org.killbill.billing.util.audit.DefaultAuditLog) RecordIdIdMappings(org.killbill.billing.util.dao.RecordIdIdMappings) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with TableName

use of org.killbill.billing.util.dao.TableName in project killbill by killbill.

the class DefaultAuditDao method doGetAuditLogsViaHistoryForId.

private List<AuditLog> doGetAuditLogsViaHistoryForId(final TableName tableName, final UUID objectId, final AuditLevel auditLevel, final InternalTenantContext context) {
    final TableName historyTableName = tableName.getHistoryTableName();
    if (historyTableName == null) {
        throw new IllegalStateException("History table shouldn't be null for " + tableName);
    }
    final Long targetRecordId = nonEntitySqlDao.getRecordIdFromObject(objectId.toString(), tableName.getTableName());
    final List<AuditLog> allAuditLogs = transactionalSqlDao.execute(new EntitySqlDaoTransactionWrapper<List<AuditLog>>() {

        @Override
        public List<AuditLog> inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final List<AuditLogModelDao> auditLogsViaHistoryForTargetRecordId = entitySqlDaoWrapperFactory.become(EntitySqlDao.class).getAuditLogsViaHistoryForTargetRecordId(historyTableName.name(), historyTableName.getTableName().toLowerCase(), targetRecordId, context);
            return buildAuditLogsFromModelDao(auditLogsViaHistoryForTargetRecordId, tableName.getObjectType(), objectId);
        }
    });
    return filterAuditLogs(auditLevel, allAuditLogs);
}
Also used : TableName(org.killbill.billing.util.dao.TableName) EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DefaultAuditLog(org.killbill.billing.util.audit.DefaultAuditLog) AuditLog(org.killbill.billing.util.audit.AuditLog)

Example 5 with TableName

use of org.killbill.billing.util.dao.TableName in project killbill by killbill.

the class EntitySqlDaoWrapperInvocationHandler method insertAudits.

private void insertAudits(final TableName tableName, final Long entityRecordId, final Long historyRecordId, final ChangeType changeType, final InternalCallContext contextMaybeWithoutAccountRecordId) {
    final TableName destinationTableName = MoreObjects.firstNonNull(tableName.getHistoryTableName(), tableName);
    final EntityAudit audit = new EntityAudit(destinationTableName, historyRecordId, changeType, clock.getUTCNow());
    final InternalCallContext context;
    // Populate the account record id when creating the account record
    if (TableName.ACCOUNT.equals(tableName) && ChangeType.INSERT.equals(changeType)) {
        context = internalCallContextFactory.createInternalCallContext(entityRecordId, contextMaybeWithoutAccountRecordId);
    } else {
        context = contextMaybeWithoutAccountRecordId;
    }
    sqlDao.insertAuditFromTransaction(audit, context);
    // TODO Knowledge on how the key is constructed is also in AuditSqlDao
    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);
        }
    }
}
Also used : EntityAudit(org.killbill.billing.util.dao.EntityAudit) TableName(org.killbill.billing.util.dao.TableName) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext)

Aggregations

TableName (org.killbill.billing.util.dao.TableName)7 UUID (java.util.UUID)4 AuditLog (org.killbill.billing.util.audit.AuditLog)4 EntityAudit (org.killbill.billing.util.dao.EntityAudit)4 DefaultAuditLog (org.killbill.billing.util.audit.DefaultAuditLog)3 AuditLogModelDao (org.killbill.billing.util.audit.dao.AuditLogModelDao)3 Test (org.testng.annotations.Test)3 DefaultCallContext (org.killbill.billing.callcontext.DefaultCallContext)2 CallContext (org.killbill.billing.util.callcontext.CallContext)2 CallOrigin (org.killbill.billing.util.callcontext.CallOrigin)2 UserType (org.killbill.billing.util.callcontext.UserType)2 ClockMock (org.killbill.clock.ClockMock)2 ImmutableList (com.google.common.collect.ImmutableList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ObjectType (org.killbill.billing.ObjectType)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 ChangeType (org.killbill.billing.util.audit.ChangeType)1 DefaultAccountAuditLogsForObjectType (org.killbill.billing.util.audit.DefaultAccountAuditLogsForObjectType)1