Search in sources :

Example 1 with SubscriptionNotificationKey

use of org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey 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 SubscriptionNotificationKey

use of org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey in project killbill by killbill.

the class DefaultSubscriptionDao method transferBundleDataFromTransaction.

private void transferBundleDataFromTransaction(final BundleTransferData bundleTransferData, final EntitySqlDao transactional, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context) throws EntityPersistenceException {
    final SubscriptionSqlDao transSubDao = entitySqlDaoWrapperFactory.become(SubscriptionSqlDao.class);
    final BundleSqlDao transBundleDao = entitySqlDaoWrapperFactory.become(BundleSqlDao.class);
    final DefaultSubscriptionBaseBundle bundleData = bundleTransferData.getData();
    final List<SubscriptionBundleModelDao> existingBundleModels = transBundleDao.getBundlesFromAccountAndKey(bundleData.getAccountId().toString(), bundleData.getExternalKey(), context);
    if (!existingBundleModels.isEmpty()) {
        log.warn("Bundle already exists for accountId='{}', bundleExternalKey='{}'", bundleData.getAccountId(), bundleData.getExternalKey());
        return;
    }
    for (final SubscriptionTransferData curSubscription : bundleTransferData.getSubscriptions()) {
        final DefaultSubscriptionBase subData = curSubscription.getData();
        for (final SubscriptionBaseEvent curEvent : curSubscription.getInitialEvents()) {
            transactional.create(new SubscriptionEventModelDao(curEvent), context);
            recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory, curEvent.getEffectiveDate(), new SubscriptionNotificationKey(curEvent.getId()), context);
        }
        transSubDao.create(new SubscriptionModelDao(subData), context);
        // Notify the Bus of the latest requested change
        final SubscriptionBaseEvent finalEvent = curSubscription.getInitialEvents().get(curSubscription.getInitialEvents().size() - 1);
        notifyBusOfRequestedChange(entitySqlDaoWrapperFactory, subData, finalEvent, SubscriptionBaseTransitionType.TRANSFER, context);
    }
    transBundleDao.create(new SubscriptionBundleModelDao(bundleData), context);
}
Also used : SubscriptionNotificationKey(org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey) SubscriptionModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionModelDao) DefaultSubscriptionBaseBundle(org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle) SubscriptionBundleModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionBundleModelDao) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionTransferData(org.killbill.billing.subscription.api.transfer.SubscriptionTransferData) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)

Example 3 with SubscriptionNotificationKey

use of org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey in project killbill by killbill.

the class MockSubscriptionDaoMemory method changePlan.

@Override
public void changePlan(final DefaultSubscriptionBase subscription, final List<SubscriptionBaseEvent> changeEvents, final List<DefaultSubscriptionBase> subscriptionsToBeCancelled, final List<SubscriptionBaseEvent> cancelEvents, final InternalCallContext context) {
    synchronized (events) {
        cancelNextChangeEvent(subscription.getId());
        cancelNextPhaseEvent(subscription.getId(), context);
        events.addAll(changeEvents);
        for (final SubscriptionBaseEvent cur : changeEvents) {
            recordFutureNotificationFromTransaction(null, cur.getEffectiveDate(), new SubscriptionNotificationKey(cur.getId()), context);
        }
    }
    cancelSubscriptions(subscriptionsToBeCancelled, cancelEvents, context);
}
Also used : SubscriptionNotificationKey(org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 4 with SubscriptionNotificationKey

use of org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey in project killbill by killbill.

the class MockSubscriptionDaoMemory method createSubscription.

/*
    @Override
    public List<SubscriptionBase> getSubscriptionsForAccountAndKey(final UUID accountId, final String bundleKey, final InternalTenantContext callcontext) {

        for (final SubscriptionBaseBundle cur : bundles) {
            if (cur.getExternalKey().equals(bundleKey) && cur.getAccountId().equals(bundleKey)) {
                return getSubscriptions(cur.getId(), callcontext);
            }
        }
        return Collections.emptyList();
    }
    */
@Override
public void createSubscription(final DefaultSubscriptionBase subscription, final List<SubscriptionBaseEvent> initialEvents, final InternalCallContext context) {
    synchronized (events) {
        events.addAll(initialEvents);
        for (final SubscriptionBaseEvent cur : initialEvents) {
            recordFutureNotificationFromTransaction(null, cur.getEffectiveDate(), new SubscriptionNotificationKey(cur.getId()), context);
        }
    }
    final SubscriptionBase updatedSubscription = buildSubscription(subscription, context);
    subscriptions.add(updatedSubscription);
    mockNonEntityDao.addTenantRecordIdMapping(updatedSubscription.getId(), context);
}
Also used : SubscriptionNotificationKey(org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 5 with SubscriptionNotificationKey

use of org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey in project killbill by killbill.

the class MockSubscriptionDaoMemory method insertEvent.

private void insertEvent(final SubscriptionBaseEvent event, final InternalCallContext context) {
    synchronized (events) {
        events.add(event);
        mockNonEntityDao.addTenantRecordIdMapping(event.getId(), context);
        recordFutureNotificationFromTransaction(null, event.getEffectiveDate(), new SubscriptionNotificationKey(event.getId()), context);
    }
}
Also used : SubscriptionNotificationKey(org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey)

Aggregations

SubscriptionNotificationKey (org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey)6 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)5 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)3 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)2 SubscriptionEventModelDao (org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 UUID (java.util.UUID)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 SubscriptionApiException (org.killbill.billing.entitlement.api.SubscriptionApiException)1 EntityPersistenceException (org.killbill.billing.entity.EntityPersistenceException)1 SubscriptionBaseWithAddOns (org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns)1 SubscriptionTransferData (org.killbill.billing.subscription.api.transfer.SubscriptionTransferData)1 DefaultSubscriptionBaseBundle (org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle)1 DefaultSubscriptionBaseWithAddOns (org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseWithAddOns)1