@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } if (!super.equals(o)) { return false; } final AuditLogModelDao that = (AuditLogModelDao) o; if (callContext != null ? !callContext.equals(that.callContext) : that.callContext != null) { return false; } return true; }
public AuditLogModelDao(final EntityAudit entityAudit, final CallContext callContext) { super(entityAudit.getId(), entityAudit.getTableName(), entityAudit.getTargetRecordId(), entityAudit.getChangeType(), entityAudit.getCreatedDate()); this.callContext = callContext; }
@Override public int hashCode() { int result = super.hashCode(); result = 31 * result + (callContext != null ? callContext.hashCode() : 0); return result; } }
@Override public AuditLogModelDao map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException { final UUID id = getUUID(r, "id"); final String tableName = r.getString("table_name"); final long targetRecordId = r.getLong("target_record_id"); final String changeType = r.getString("change_type"); final DateTime createdDate = getDateTime(r, "created_date"); final String createdBy = r.getString("created_by"); final String reasonCode = r.getString("reason_code"); final String comments = r.getString("comments"); final UUID userToken = getUUID(r, "user_token"); final EntityAudit entityAudit = new EntityAudit(id, TableName.valueOf(tableName), targetRecordId, ChangeType.valueOf(changeType), createdDate); // TODO - we have the tenant_record_id but not the tenant id here final CallContext callContext = new DefaultCallContext(null, createdBy, createdDate, reasonCode, comments, userToken); return new AuditLogModelDao(entityAudit, callContext); } }
@Test(groups = "fast") public void testEquals() 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 AuditLogModelDao auditLog = new AuditLogModelDao(entityAudit, callContext); Assert.assertEquals(auditLog, auditLog); final AuditLogModelDao sameAuditLog = new AuditLogModelDao(entityAudit, callContext); Assert.assertEquals(sameAuditLog, auditLog); clock.addMonths(1); final CallContext otherCallContext = new DefaultCallContext(tenantId, userName, callOrigin, userType, userToken, clock); final AuditLogModelDao otherAuditLog = new AuditLogModelDao(entityAudit, otherCallContext); Assert.assertNotEquals(otherAuditLog, auditLog); } }
@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()); }
@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()); } }
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; // Populate the account record id when creating the account record if (TableName.ACCOUNT.equals(tableName) && ChangeType.INSERT.equals(changeType)) { context = new InternalCallContext(contextMaybeWithoutAccountRecordId, entityRecordId); } else { context = contextMaybeWithoutAccountRecordId; } sqlDao.insertAuditFromTransaction(audit, context); // We need to invalidate the caches. There is a small window of doom here where caches will be stale. // 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); } } }