public static void populateCaches(final CacheControllerDispatcher cacheControllerDispatcher, final EntityModelDao model) { final CacheController<String, Long> cacheRecordId = cacheControllerDispatcher.getCacheController(CacheType.RECORD_ID); cacheRecordId.putIfAbsent(getKey(model.getId().toString(), CacheType.RECORD_ID, model.getTableName()), model.getRecordId()); final CacheController<String, UUID> cacheObjectId = cacheControllerDispatcher.getCacheController(CacheType.OBJECT_ID); cacheObjectId.putIfAbsent(getKey(model.getRecordId().toString(), CacheType.OBJECT_ID, model.getTableName()), model.getId()); if (model.getTenantRecordId() != null) { final CacheController<String, Long> cacheTenantRecordId = cacheControllerDispatcher.getCacheController(CacheType.TENANT_RECORD_ID); cacheTenantRecordId.putIfAbsent(getKey(model.getId().toString(), CacheType.TENANT_RECORD_ID, model.getTableName()), model.getTenantRecordId()); } if (model.getAccountRecordId() != null) { final CacheController<String, Long> cacheAccountRecordId = cacheControllerDispatcher.getCacheController(CacheType.ACCOUNT_RECORD_ID); cacheAccountRecordId.putIfAbsent(getKey(model.getId().toString(), CacheType.ACCOUNT_RECORD_ID, model.getTableName()), model.getAccountRecordId()); } }
@Override public M execute() throws Throwable { final M reHydratedEntity; if (changeType == ChangeType.DELETE) { reHydratedEntity = deletedEntity; } else { // See note above regarding "markAsInactive" operations reHydratedEntity = MoreObjects.firstNonNull(sqlDao.getById(entityId, context), deletedEntity); printSQLWarnings(); } Preconditions.checkNotNull(reHydratedEntity, "reHydratedEntity cannot be null"); final Long entityRecordId = reHydratedEntity.getRecordId(); final TableName tableName = reHydratedEntity.getTableName(); // Note: audit entries point to the history record id final Long historyRecordId; if (tableName.getHistoryTableName() != null) { historyRecordId = insertHistory(entityRecordId, reHydratedEntity, changeType, context); } else { historyRecordId = entityRecordId; } // Make sure to re-hydrate the object (especially needed for create calls) insertAudits(tableName, reHydratedEntity, entityRecordId, historyRecordId, changeType, context); return reHydratedEntity; } });
protected boolean checkEntityAlreadyExists(final EntitySqlDao<M, E> transactional, final M entity, final InternalCallContext context) { return transactional.getRecordId(entity.getId().toString(), context) != null; }
@Override public EntityHistoryModelDao<M, E> map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException { final UUID id = getUUID(r, "id"); final long targetRecordId = r.getLong("target_record_id"); final String changeType = r.getString("change_type"); final DateTime createdDate = getDateTime(r, "created_date"); // preserve history record id, as it is needed to reference it with audit log final long historyRecordId = r.getLong("history_record_id"); final M entityModelDao = entityMapper.map(index, r, ctx); // Hack -- remove the id as it is the history id, not the entity id ((EntityModelDaoBase) entityModelDao).setId(null); // Hack -- similarly, populate the right record_id ((EntityModelDaoBase) entityModelDao).setRecordId(targetRecordId); // Hack -- account is special if (entityModelDao.getAccountRecordId() == null) { ((EntityModelDaoBase) entityModelDao).setAccountRecordId(targetRecordId); } return new EntityHistoryModelDao(id, entityModelDao, targetRecordId, ChangeType.valueOf(changeType), historyRecordId, createdDate); } }
return modelDao.getTableName().getObjectType();
private <T extends EntitySqlDao<M, E>, M extends EntityModelDao<E>, E extends Entity> M extractEntityModelFromEntityWithTargetRecordId(final UUID entityId, final UUID auditLogId, final Class<T> sqlDao, final CallContext context, final boolean useHistory) { final M modelDaoThatGivesMeTableName = dbi.onDemand(sqlDao).getById(entityId.toString(), callContextFactory.createInternalCallContextWithoutAccountRecordId(context)); Integer targetRecordId = dbi.withHandle(new HandleCallback<Integer>() { @Override public Integer withHandle(final Handle handle) throws Exception { List<Map<String, Object>> res = handle.select("select target_record_id from audit_log where id = '" + auditLogId.toString() + "';"); return Integer.valueOf(res.get(0).get("target_record_id").toString()); } }); if (useHistory) { Long entityRecordId = nonEntityDao.retrieveHistoryTargetRecordId(Long.valueOf(targetRecordId), modelDaoThatGivesMeTableName.getHistoryTableName()); targetRecordId = new Integer(entityRecordId.intValue()); } return dbi.onDemand(sqlDao).getByRecordId(Long.valueOf(targetRecordId), callContextFactory.createInternalCallContextWithoutAccountRecordId(context)); }
public void delete(final M entity, final InternalCallContext context) { entities.remove(entity.getId()); }
@Override public M execute() throws Throwable { final M reHydratedEntity; if (changeType == ChangeType.DELETE) { reHydratedEntity = deletedEntity; } else { // See note above regarding "markAsInactive" operations reHydratedEntity = MoreObjects.firstNonNull(sqlDao.getById(entityId, context), deletedEntity); printSQLWarnings(); } Preconditions.checkNotNull(reHydratedEntity, "reHydratedEntity cannot be null"); final Long entityRecordId = reHydratedEntity.getRecordId(); final TableName tableName = reHydratedEntity.getTableName(); // Note: audit entries point to the history record id final Long historyRecordId; if (tableName.getHistoryTableName() != null) { historyRecordId = insertHistory(entityRecordId, reHydratedEntity, changeType, context); } else { historyRecordId = entityRecordId; } // Make sure to re-hydrate the object (especially needed for create calls) insertAudits(tableName, reHydratedEntity, entityRecordId, historyRecordId, changeType, context); return reHydratedEntity; } });
@Override public EntityHistoryModelDao<M, E> map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException { final UUID id = getUUID(r, "id"); final long targetRecordId = r.getLong("target_record_id"); final String changeType = r.getString("change_type"); final DateTime createdDate = getDateTime(r, "created_date"); // preserve history record id, as it is needed to reference it with audit log final long historyRecordId = r.getLong("history_record_id"); final M entityModelDao = entityMapper.map(index, r, ctx); // Hack -- remove the id as it is the history id, not the entity id ((EntityModelDaoBase) entityModelDao).setId(null); // Hack -- similarly, populate the right record_id ((EntityModelDaoBase) entityModelDao).setRecordId(targetRecordId); // Hack -- account is special if (entityModelDao.getAccountRecordId() == null) { ((EntityModelDaoBase) entityModelDao).setAccountRecordId(targetRecordId); } return new EntityHistoryModelDao(id, entityModelDao, targetRecordId, ChangeType.valueOf(changeType), historyRecordId, createdDate); } }
return modelDao.getTableName().getObjectType();
public static void populateCaches(final CacheControllerDispatcher cacheControllerDispatcher, final EntityModelDao model) { final CacheController<String, Long> cacheRecordId = cacheControllerDispatcher.getCacheController(CacheType.RECORD_ID); cacheRecordId.putIfAbsent(getKey(model.getId().toString(), CacheType.RECORD_ID, model.getTableName()), model.getRecordId()); final CacheController<String, UUID> cacheObjectId = cacheControllerDispatcher.getCacheController(CacheType.OBJECT_ID); cacheObjectId.putIfAbsent(getKey(model.getRecordId().toString(), CacheType.OBJECT_ID, model.getTableName()), model.getId()); if (model.getTenantRecordId() != null) { final CacheController<String, Long> cacheTenantRecordId = cacheControllerDispatcher.getCacheController(CacheType.TENANT_RECORD_ID); cacheTenantRecordId.putIfAbsent(getKey(model.getId().toString(), CacheType.TENANT_RECORD_ID, model.getTableName()), model.getTenantRecordId()); } if (model.getAccountRecordId() != null) { final CacheController<String, Long> cacheAccountRecordId = cacheControllerDispatcher.getCacheController(CacheType.ACCOUNT_RECORD_ID); cacheAccountRecordId.putIfAbsent(getKey(model.getId().toString(), CacheType.ACCOUNT_RECORD_ID, model.getTableName()), model.getAccountRecordId()); } }
@Override public void create(final M entity, final InternalCallContext context) throws U { entities.put(entity.getId(), ImmutableMap.<Long, M>of(autoIncrement.incrementAndGet(), entity)); }
protected boolean checkEntityAlreadyExists(final EntitySqlDao<M, E> transactional, final M entity, final InternalCallContext context) { return transactional.getRecordId(entity.getId().toString(), context) != null; }
public void delete(final M entity, final InternalCallContext context) { entities.remove(entity.getId()); }
@Override public void create(final M entity, final InternalCallContext context) throws U { entities.put(entity.getId(), ImmutableMap.<Long, M>of(autoIncrement.incrementAndGet(), entity)); }
private <T extends EntitySqlDao<M, E>, M extends EntityModelDao<E>, E extends Entity> void checkAuditLog(final ChangeType changeType, @Nullable final CallContext context, final AuditLog auditLog, final UUID entityId, Class<T> sqlDao, boolean useHistory, boolean checkContext) { Assert.assertEquals(auditLog.getChangeType(), changeType); if (checkContext) { Assert.assertEquals(auditLog.getUserName(), context.getUserName()); Assert.assertEquals(auditLog.getComment(), context.getComments()); //Assert.assertEquals(auditLog.getCreatedDate().comparesTo(callcontext.getCreatedDate())); // We can't take userToken oustide of the 'if' because for instance NextBillingDate invoice will not have it. Assert.assertEquals(auditLog.getUserToken(), context.getUserToken().toString()); } final M entityModel = extractEntityModelFromEntityWithTargetRecordId(entityId, auditLog.getId(), sqlDao, context, useHistory); Assert.assertEquals(entityModel.getId(), entityId); }