Search in sources :

Example 1 with Entity

use of org.killbill.billing.util.entity.Entity in project killbill by killbill.

the class EntitySqlDaoTransactionalJdbiWrapper method execute.

/**
 * @param <ReturnType>                   object type to return from the transaction
 * @param requestedRO                    hint as whether to use the read-only connection
 * @param entitySqlDaoTransactionWrapper transaction to execute
 * @return result from the transaction fo type ReturnType
 */
public <ReturnType> ReturnType execute(final boolean requestedRO, final EntitySqlDaoTransactionWrapper<ReturnType> entitySqlDaoTransactionWrapper) {
    final String debugInfo = logger.isDebugEnabled() ? getDebugInfo() : null;
    final Handle handle = dbRouter.getHandle(requestedRO);
    logger.debug("DBI handle created, transaction: {}", debugInfo);
    try {
        final EntitySqlDao<EntityModelDao<Entity>, Entity> entitySqlDao = handle.attach(InitialEntitySqlDao.class);
        // The transaction isolation level is now set at the pool level: this avoids 3 roundtrips for each transaction
        // Note that if the pool isn't used (tests or PostgreSQL), the transaction level will depend on the DB configuration
        // return entitySqlDao.inTransaction(TransactionIsolationLevel.READ_COMMITTED, new JdbiTransaction<ReturnType, EntityModelDao<Entity>, Entity>(handle, entitySqlDaoTransactionWrapper));
        logger.debug("Starting transaction {}", debugInfo);
        final ReturnType returnType = entitySqlDao.inTransaction(new JdbiTransaction<ReturnType, EntityModelDao<Entity>, Entity>(handle, entitySqlDaoTransactionWrapper));
        logger.debug("Exiting  transaction {}, returning {}", debugInfo, returnType);
        return returnType;
    } finally {
        handle.close();
        logger.debug("DBI handle closed,  transaction: {}", debugInfo);
    }
}
Also used : Entity(org.killbill.billing.util.entity.Entity) Handle(org.skife.jdbi.v2.Handle)

Example 2 with Entity

use of org.killbill.billing.util.entity.Entity in project killbill by killbill.

the class EntitySqlDaoWrapperInvocationHandler method extractEntityIdsFromBatchArgument.

private Builder<String> extractEntityIdsFromBatchArgument(final Iterable arg) {
    final Iterator iterator = arg.iterator();
    final Builder<String> entityIds = new Builder<String>();
    while (iterator.hasNext()) {
        final Object object = iterator.next();
        if (!(object instanceof Entity)) {
            // No good - ignore
            return null;
        } else {
            entityIds.add(((Entity) object).getId().toString());
        }
    }
    return entityIds;
}
Also used : Entity(org.killbill.billing.util.entity.Entity) Builder(com.google.common.collect.ImmutableList.Builder) Iterator(java.util.Iterator)

Example 3 with Entity

use of org.killbill.billing.util.entity.Entity in project killbill by killbill.

the class EntitySqlDaoWrapperInvocationHandler method retrieveEntityIdsFromArguments.

private List<String> retrieveEntityIdsFromArguments(final Method method, final Object[] args) {
    final Annotation[][] parameterAnnotations = getAnnotations(method);
    int i = -1;
    for (final Object arg : args) {
        i++;
        // This is true for e.g. create calls
        if (arg instanceof Entity) {
            return ImmutableList.<String>of(((Entity) arg).getId().toString());
        }
        // For Batch calls, the first argument will be of type List<Entity>
        if (arg instanceof Iterable) {
            final Builder<String> entityIds = extractEntityIdsFromBatchArgument((Iterable) arg);
            if (entityIds != null) {
                return entityIds.build();
            }
        }
        for (final Annotation annotation : parameterAnnotations[i]) {
            if (arg instanceof String && Bind.class.equals(annotation.annotationType()) && ("id").equals(((Bind) annotation).value())) {
                return ImmutableList.<String>of((String) arg);
            } else if (arg instanceof Collection && BindIn.class.equals(annotation.annotationType()) && ("ids").equals(((BindIn) annotation).value())) {
                return ImmutableList.<String>copyOf((Collection) arg);
            }
        }
    }
    return ImmutableList.<String>of();
}
Also used : Entity(org.killbill.billing.util.entity.Entity) Bind(org.skife.jdbi.v2.sqlobject.Bind) BindIn(org.skife.jdbi.v2.unstable.BindIn) Collection(java.util.Collection) Annotation(java.lang.annotation.Annotation)

Example 4 with Entity

use of org.killbill.billing.util.entity.Entity in project killbill by killbill.

the class TestInvoiceTrackingSqlDao method testInvalidation.

@Test(groups = "slow")
public void testInvalidation() {
    LocalDate startRange = new LocalDate(2019, 1, 1);
    LocalDate endRange = new LocalDate(2019, 1, 31);
    final UUID invoiceId1 = UUID.randomUUID();
    final UUID invoiceId2 = UUID.randomUUID();
    final UUID subscriptionId = UUID.randomUUID();
    // invoiceId1
    final InvoiceTrackingModelDao input1 = new InvoiceTrackingModelDao(UUID.randomUUID(), clock.getUTCNow(), "trackingId1", invoiceId1, subscriptionId, "unit", new LocalDate(2019, 1, 1));
    final InvoiceTrackingModelDao input2 = new InvoiceTrackingModelDao(UUID.randomUUID(), clock.getUTCNow(), "trackingId2", invoiceId1, subscriptionId, "unit", new LocalDate(2019, 1, 2));
    final InvoiceTrackingModelDao input3 = new InvoiceTrackingModelDao(UUID.randomUUID(), clock.getUTCNow(), "trackingId3", invoiceId1, subscriptionId, "unit", new LocalDate(2019, 1, 3));
    // invoiceId2
    final InvoiceTrackingModelDao input4 = new InvoiceTrackingModelDao(UUID.randomUUID(), clock.getUTCNow(), "trackingId4", invoiceId2, subscriptionId, "unit", new LocalDate(2019, 1, 5));
    final List<InvoiceTrackingModelDao> inputs = new ArrayList<>();
    inputs.add(input1);
    inputs.add(input2);
    inputs.add(input3);
    inputs.add(input4);
    // Create state
    transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() {

        @Override
        public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final InvoiceTrackingSqlDao dao = entitySqlDaoWrapperFactory.become(InvoiceTrackingSqlDao.class);
            dao.create(inputs, internalCallContext);
            return null;
        }
    });
    // Verify audit logs from existing state
    final List<InvoiceTrackingModelDao> initialTrackingIdsByRange = transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<List<InvoiceTrackingModelDao>>() {

        @Override
        public List<InvoiceTrackingModelDao> inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final InvoiceTrackingSqlDao dao = entitySqlDaoWrapperFactory.become(InvoiceTrackingSqlDao.class);
            final List<InvoiceTrackingModelDao> result = dao.getTrackingsByDateRange(startRange.toDate(), endRange.toDate(), internalCallContext);
            Assert.assertEquals(result.size(), 4);
            final List<EntityHistoryModelDao> entityHistoryModelDaos = new ArrayList<>();
            final List<AuditLogModelDao> auditLogsPostCreate = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                List<AuditLogModelDao> tmp1 = dao.getAuditLogsViaHistoryForTargetRecordId(TableName.INVOICE_TRACKING_ID_HISTORY.name(), TableName.INVOICE_TRACKING_ID_HISTORY.getTableName().toLowerCase(), result.get(i).getRecordId(), internalCallContext);
                auditLogsPostCreate.addAll(tmp1);
                final List<EntityHistoryModelDao<InvoiceTrackingModelDao, Entity>> tmp2 = dao.getHistoryForTargetRecordId(true, result.get(i).getRecordId(), internalCallContext);
                entityHistoryModelDaos.addAll(tmp2);
            }
            Assert.assertEquals(auditLogsPostCreate.size(), 4);
            Assert.assertEquals(entityHistoryModelDaos.size(), 4);
            for (int i = 0; i < 4; i++) {
                Assert.assertEquals(auditLogsPostCreate.get(i).getChangeType(), ChangeType.INSERT);
                // From the audit log entry, lookup the matching history entry
                final int curIdx = i;
                final EntityHistoryModelDao history = Iterables.find(entityHistoryModelDaos, new Predicate<EntityHistoryModelDao>() {

                    @Override
                    public boolean apply(final EntityHistoryModelDao input) {
                        return Objects.equals(input.getHistoryRecordId(), auditLogsPostCreate.get(curIdx).getTargetRecordId());
                    }
                });
                Assert.assertEquals(auditLogsPostCreate.get(i).getTargetRecordId(), history.getHistoryRecordId());
            }
            return result;
        }
    });
    clock.addDays(1);
    // Create state
    transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() {

        @Override
        public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final InvoiceTrackingSqlDao dao = entitySqlDaoWrapperFactory.become(InvoiceTrackingSqlDao.class);
            final InternalCallContext updatedContext = new InternalCallContext(internalCallContext.getTenantRecordId(), internalCallContext.getAccountRecordId(), internalCallContext.getFixedOffsetTimeZone(), clock.getUTCNow(), internalCallContext.getUserToken(), "invalidation-user", internalCallContext.getCallOrigin(), internalCallContext.getContextUserType(), internalCallContext.getReasonCode(), internalCallContext.getComments(), internalCallContext.getCreatedDate(), clock.getUTCNow());
            dao.deactivateByIds(ImmutableList.<String>of(input1.getId().toString(), input2.getId().toString(), input3.getId().toString()), updatedContext);
            return null;
        }
    });
    // Verify audit logs from existing state
    transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() {

        @Override
        public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final InvoiceTrackingSqlDao dao = entitySqlDaoWrapperFactory.become(InvoiceTrackingSqlDao.class);
            final List<InvoiceTrackingModelDao> result2 = dao.getTrackingsByDateRange(startRange.toDate(), endRange.toDate(), internalCallContext);
            Assert.assertEquals(result2.size(), 1);
            final List<AuditLogModelDao> auditLogsPostDelete = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                List<AuditLogModelDao> tmp = dao.getAuditLogsViaHistoryForTargetRecordId(TableName.INVOICE_TRACKING_ID_HISTORY.name(), TableName.INVOICE_TRACKING_ID_HISTORY.getTableName().toLowerCase(), initialTrackingIdsByRange.get(i).getRecordId(), internalCallContext);
                auditLogsPostDelete.addAll(tmp);
            }
            Assert.assertEquals(auditLogsPostDelete.size(), 7);
            // First 3 records will show an INSERT & DELETE
            for (int i = 0; i < 3; i++) {
                Assert.assertEquals(auditLogsPostDelete.get(2 * i).getChangeType(), ChangeType.INSERT);
                Assert.assertEquals(auditLogsPostDelete.get(2 * i + 1).getChangeType(), ChangeType.DELETE);
            }
            // Last record will only show an INSERT
            Assert.assertEquals(auditLogsPostDelete.get(6).getChangeType(), ChangeType.INSERT);
            return null;
        }
    });
}
Also used : Entity(org.killbill.billing.util.entity.Entity) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) LocalDate(org.joda.time.LocalDate) Predicate(com.google.common.base.Predicate) EntityHistoryModelDao(org.killbill.billing.util.dao.EntityHistoryModelDao) EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Aggregations

Entity (org.killbill.billing.util.entity.Entity)4 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 Builder (com.google.common.collect.ImmutableList.Builder)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 UUID (java.util.UUID)1 LocalDate (org.joda.time.LocalDate)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 EntityHistoryModelDao (org.killbill.billing.util.dao.EntityHistoryModelDao)1 EntitySqlDaoWrapperFactory (org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory)1 Handle (org.skife.jdbi.v2.Handle)1 Bind (org.skife.jdbi.v2.sqlobject.Bind)1 BindIn (org.skife.jdbi.v2.unstable.BindIn)1 Test (org.testng.annotations.Test)1