Search in sources :

Example 1 with EntitySqlDaoWrapperFactory

use of org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory in project killbill by killbill.

the class DefaultSubscriptionDao method uncancelSubscription.

@Override
public void uncancelSubscription(final DefaultSubscriptionBase subscription, final List<SubscriptionBaseEvent> uncancelEvents, final InternalCallContext context) {
    final InternalCallContext contextWithUpdatedDate = contextWithUpdatedDate(context);
    transactionalSqlDao.execute(new EntitySqlDaoTransactionWrapper<Void>() {

        @Override
        public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final SubscriptionEventSqlDao transactional = entitySqlDaoWrapperFactory.become(SubscriptionEventSqlDao.class);
            final UUID subscriptionId = subscription.getId();
            SubscriptionEventModelDao cancelledEvent = null;
            final Date now = clock.getUTCNow().toDate();
            final List<SubscriptionEventModelDao> eventModels = transactional.getFutureActiveEventForSubscription(subscriptionId.toString(), now, contextWithUpdatedDate);
            for (final SubscriptionEventModelDao cur : eventModels) {
                if (cur.getUserType() == ApiEventType.CANCEL) {
                    if (cancelledEvent != null) {
                        throw new SubscriptionBaseError(String.format("Found multiple cancelWithRequestedDate active events for subscriptions %s", subscriptionId.toString()));
                    }
                    cancelledEvent = cur;
                }
            }
            if (cancelledEvent != null) {
                final String cancelledEventId = cancelledEvent.getId().toString();
                transactional.unactiveEvent(cancelledEventId, contextWithUpdatedDate);
                for (final SubscriptionBaseEvent cur : uncancelEvents) {
                    transactional.create(new SubscriptionEventModelDao(cur), contextWithUpdatedDate);
                    recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory, cur.getEffectiveDate(), new SubscriptionNotificationKey(cur.getId()), contextWithUpdatedDate);
                }
                // Notify the Bus of the latest requested change
                notifyBusOfRequestedChange(entitySqlDaoWrapperFactory, subscription, uncancelEvents.get(uncancelEvents.size() - 1), SubscriptionBaseTransitionType.UNCANCEL, contextWithUpdatedDate);
            }
            return null;
        }
    });
}
Also used : InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) SubscriptionApiException(org.killbill.billing.entitlement.api.SubscriptionApiException) IOException(java.io.IOException) EventBusException(org.killbill.bus.api.PersistentBus.EventBusException) EntityPersistenceException(org.killbill.billing.entity.EntityPersistenceException) Date(java.util.Date) SubscriptionNotificationKey(org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey) SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 2 with EntitySqlDaoWrapperFactory

use of org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory in project killbill by killbill.

the class DefaultSubscriptionDao method updateChargedThroughDate.

@Override
public void updateChargedThroughDate(final DefaultSubscriptionBase subscription, final InternalCallContext context) {
    final Date ctd = (subscription.getChargedThroughDate() != null) ? subscription.getChargedThroughDate().toDate() : null;
    final InternalCallContext contextWithUpdatedDate = contextWithUpdatedDate(context);
    transactionalSqlDao.execute(new EntitySqlDaoTransactionWrapper<Void>() {

        @Override
        public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final SubscriptionSqlDao transactionalDao = entitySqlDaoWrapperFactory.become(SubscriptionSqlDao.class);
            transactionalDao.updateChargedThroughDate(subscription.getId().toString(), ctd, contextWithUpdatedDate);
            final BundleSqlDao bundleSqlDao = entitySqlDaoWrapperFactory.become(BundleSqlDao.class);
            final String bundleId = subscription.getBundleId().toString();
            bundleSqlDao.updateBundleLastSysTime(bundleId, clock.getUTCNow().toDate(), contextWithUpdatedDate);
            return null;
        }
    });
}
Also used : EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) Date(java.util.Date) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) SubscriptionApiException(org.killbill.billing.entitlement.api.SubscriptionApiException) IOException(java.io.IOException) EventBusException(org.killbill.bus.api.PersistentBus.EventBusException) EntityPersistenceException(org.killbill.billing.entity.EntityPersistenceException)

Example 3 with EntitySqlDaoWrapperFactory

use of org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory in project killbill by killbill.

the class TestInvoiceTrackingSqlDao method testWithBatch.

@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/1390")
public void testWithBatch() {
    LocalDate startRange = new LocalDate(2018, 8, 1);
    LocalDate endRange = new LocalDate(2018, 11, 23);
    final UUID invoiceId1 = UUID.randomUUID();
    final UUID subscriptionId = UUID.randomUUID();
    final List<InvoiceTrackingModelDao> inputs = new ArrayList<>();
    // The batch size is defined as 10000 -- See EntitySqlDaoWrapperInvocationHandler#BATCH_SIZE
    // so we chose a number right above this.
    // Local tests with postgres showed that limitation arose over 33K
    int NB_TRACKING_IDS = 10001;
    for (int i = 0; i < NB_TRACKING_IDS; i++) {
        final String trackingId = "tracking-" + i;
        final InvoiceTrackingModelDao input = new InvoiceTrackingModelDao(UUID.randomUUID(), clock.getUTCNow(), trackingId, invoiceId1, subscriptionId, "unit", startRange);
        inputs.add(input);
    }
    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);
            final List<InvoiceTrackingModelDao> result = dao.getTrackingsByDateRange(startRange.toDate(), endRange.toDate(), internalCallContext);
            Assert.assertEquals(result.size(), NB_TRACKING_IDS);
            final String expTrackingId = "tracking-" + (NB_TRACKING_IDS - 1);
            Assert.assertEquals(result.get(NB_TRACKING_IDS - 1).getTrackingId(), expTrackingId);
            return null;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) 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)

Example 4 with EntitySqlDaoWrapperFactory

use of org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory in project killbill by killbill.

the class DefaultOverduePosterBase method clearOverdueCheckNotifications.

@Override
public <T extends OverdueCheckNotificationKey> void clearOverdueCheckNotifications(final UUID accountId, final String overdueQueueName, final Class<T> clazz, final InternalCallContext context) {
    try {
        final NotificationQueue checkOverdueQueue = notificationQueueService.getNotificationQueue(DefaultOverdueService.OVERDUE_SERVICE_NAME, overdueQueueName);
        transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() {

            @Override
            public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
                final Iterable<NotificationEventWithMetadata<T>> futureNotifications = getFutureNotificationsForAccountInTransaction(entitySqlDaoWrapperFactory, checkOverdueQueue, clazz, context);
                final Iterator<NotificationEventWithMetadata<T>> iterator = futureNotifications.iterator();
                try {
                    while (iterator.hasNext()) {
                        final NotificationEventWithMetadata<T> notification = iterator.next();
                        checkOverdueQueue.removeNotificationFromTransaction(entitySqlDaoWrapperFactory.getHandle().getConnection(), notification.getRecordId());
                    }
                } finally {
                    // Go through all results to close the connection
                    while (iterator.hasNext()) {
                        iterator.next();
                    }
                }
                return null;
            }
        });
    } catch (final NoSuchNotificationQueue e) {
        log.error("Attempting to clear items from a non-existent queue (DefaultOverdueCheck).", e);
    }
}
Also used : NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) Iterator(java.util.Iterator) EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) NotificationEventWithMetadata(org.killbill.notificationq.api.NotificationEventWithMetadata) NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) NotificationQueue(org.killbill.notificationq.api.NotificationQueue)

Example 5 with EntitySqlDaoWrapperFactory

use of org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory in project killbill by killbill.

the class DefaultOverduePosterBase method insertOverdueNotification.

@Override
public <T extends OverdueCheckNotificationKey> void insertOverdueNotification(final UUID accountId, final DateTime futureNotificationTime, final String overdueQueueName, final T notificationKey, final InternalCallContext context) {
    try {
        final NotificationQueue overdueQueue = notificationQueueService.getNotificationQueue(DefaultOverdueService.OVERDUE_SERVICE_NAME, overdueQueueName);
        transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() {

            @Override
            public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
                // Check if we already have notifications for that key
                final Class<T> clazz = (Class<T>) notificationKey.getClass();
                final Iterable<NotificationEventWithMetadata<T>> futureNotifications = getFutureNotificationsForAccountInTransaction(entitySqlDaoWrapperFactory, overdueQueue, clazz, context);
                final boolean shouldInsertNewNotification = cleanupFutureNotificationsFormTransaction(entitySqlDaoWrapperFactory, futureNotifications, futureNotificationTime, overdueQueue);
                if (shouldInsertNewNotification) {
                    log.debug("Queuing overdue check notification. Account id: {}, timestamp: {}", accountId.toString(), futureNotificationTime.toString());
                    overdueQueue.recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory.getHandle().getConnection(), futureNotificationTime, notificationKey, context.getUserToken(), context.getAccountRecordId(), context.getTenantRecordId());
                } else {
                    log.debug("Skipping queuing overdue check notification. Account id: {}, timestamp: {}", accountId.toString(), futureNotificationTime.toString());
                }
                return null;
            }
        });
    } catch (final NoSuchNotificationQueue e) {
        log.error("Attempting to put items on a non-existent queue (DefaultOverdueCheck).", e);
    }
}
Also used : NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) NotificationQueue(org.killbill.notificationq.api.NotificationQueue)

Aggregations

EntitySqlDaoWrapperFactory (org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory)22 EntityPersistenceException (org.killbill.billing.entity.EntityPersistenceException)14 EventBusException (org.killbill.bus.api.PersistentBus.EventBusException)13 ImmutableList (com.google.common.collect.ImmutableList)12 List (java.util.List)12 UUID (java.util.UUID)12 ArrayList (java.util.ArrayList)10 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)9 IOException (java.io.IOException)8 LinkedList (java.util.LinkedList)8 SubscriptionApiException (org.killbill.billing.entitlement.api.SubscriptionApiException)8 LocalDate (org.joda.time.LocalDate)6 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)6 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)6 Predicate (com.google.common.base.Predicate)5 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)5 Tag (org.killbill.billing.util.tag.Tag)5 Test (org.testng.annotations.Test)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4