Search in sources :

Example 1 with SubscriptionEventModelDao

use of org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao in project killbill by killbill.

the class DefaultSubscriptionDao method findFutureEventFromTransaction.

private SubscriptionEventModelDao findFutureEventFromTransaction(final UUID subscriptionId, final EntitySqlDaoWrapperFactory dao, final EventType type, @Nullable final ApiEventType apiType, final InternalCallContext context) {
    SubscriptionEventModelDao futureEvent = null;
    final Date now = clock.getUTCNow().toDate();
    final List<SubscriptionEventModelDao> eventModels = dao.become(SubscriptionEventSqlDao.class).getFutureActiveEventForSubscription(subscriptionId.toString(), now, context);
    for (final SubscriptionEventModelDao cur : eventModels) {
        if (cur.getEventType() == type && (apiType == null || apiType == cur.getUserType())) {
            if (futureEvent != null) {
                throw new SubscriptionBaseError(String.format("Found multiple future events for type %s for subscriptions %s", type, subscriptionId.toString()));
            }
            futureEvent = cur;
        // To check that there is only one such event
        //break;
        }
    }
    return futureEvent;
}
Also used : SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao) Date(java.util.Date)

Example 2 with SubscriptionEventModelDao

use of org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao 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 3 with SubscriptionEventModelDao

use of org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao in project killbill by killbill.

the class DefaultSubscriptionDao method cancelFutureEventFromTransaction.

private void cancelFutureEventFromTransaction(final UUID subscriptionId, final EntitySqlDaoWrapperFactory dao, final EventType type, @Nullable final ApiEventType apiType, final InternalCallContext context) {
    final SubscriptionEventModelDao futureEvent = findFutureEventFromTransaction(subscriptionId, dao, type, apiType, context);
    unactivateEventFromTransaction(futureEvent, dao, context);
}
Also used : SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)

Example 4 with SubscriptionEventModelDao

use of org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao in project killbill by killbill.

the class DefaultSubscriptionDao method cancelSubscriptionFromTransaction.

private void cancelSubscriptionFromTransaction(final DefaultSubscriptionBase subscription, final SubscriptionBaseEvent cancelEvent, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context, final int seqId) throws EntityPersistenceException {
    final UUID subscriptionId = subscription.getId();
    cancelFutureEventsFromTransaction(subscriptionId, cancelEvent.getEffectiveDate(), entitySqlDaoWrapperFactory, true, context);
    entitySqlDaoWrapperFactory.become(SubscriptionEventSqlDao.class).create(new SubscriptionEventModelDao(cancelEvent), context);
    final boolean isBusEvent = cancelEvent.getEffectiveDate().compareTo(clock.getUTCNow()) <= 0;
    recordBusOrFutureNotificationFromTransaction(subscription, cancelEvent, entitySqlDaoWrapperFactory, isBusEvent, seqId, context);
    // Notify the Bus of the requested change
    notifyBusOfRequestedChange(entitySqlDaoWrapperFactory, subscription, cancelEvent, SubscriptionBaseTransitionType.CANCEL, context);
}
Also used : UUID(java.util.UUID) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)

Example 5 with SubscriptionEventModelDao

use of org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao 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)

Aggregations

SubscriptionEventModelDao (org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)6 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)3 Date (java.util.Date)2 UUID (java.util.UUID)2 EntityPersistenceException (org.killbill.billing.entity.EntityPersistenceException)2 SubscriptionNotificationKey (org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey)2 SubscriptionBaseError (org.killbill.billing.subscription.exceptions.SubscriptionBaseError)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 Interval (org.joda.time.Interval)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 Duration (org.killbill.billing.catalog.api.Duration)1 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)1 SubscriptionApiException (org.killbill.billing.entitlement.api.SubscriptionApiException)1