Search in sources :

Example 6 with ImmutableAccountData

use of org.killbill.billing.account.api.ImmutableAccountData in project killbill by killbill.

the class CompletionTaskBase method tryToDoJanitorOperationWithAccountLock.

protected <T> T tryToDoJanitorOperationWithAccountLock(final JanitorIterationCallback callback, final InternalTenantContext internalTenantContext) throws LockFailedException {
    GlobalLock lock = null;
    try {
        final ImmutableAccountData account = accountInternalApi.getImmutableAccountDataByRecordId(internalTenantContext.getAccountRecordId(), internalTenantContext);
        lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), account.getId().toString(), paymentConfig.getMaxGlobalLockRetries());
        return callback.doIteration();
    } catch (final AccountApiException e) {
        log.warn("Error retrieving accountRecordId='{}'", internalTenantContext.getAccountRecordId(), e);
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
    return null;
}
Also used : GlobalLock(org.killbill.commons.locker.GlobalLock) ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) AccountApiException(org.killbill.billing.account.api.AccountApiException)

Example 7 with ImmutableAccountData

use of org.killbill.billing.account.api.ImmutableAccountData in project killbill by killbill.

the class TestOverdueStateApplicator method testApplicator.

@Test(groups = "slow")
public void testApplicator() throws Exception {
    final InputStream is = new ByteArrayInputStream(testOverdueHelper.getConfigXml().getBytes());
    final DefaultOverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, DefaultOverdueConfig.class);
    final ImmutableAccountData account = Mockito.mock(ImmutableAccountData.class);
    Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
    final OverdueStateSet overdueStateSet = config.getOverdueStatesAccount();
    final OverdueState clearState = config.getOverdueStatesAccount().findState(OverdueWrapper.CLEAR_STATE_NAME);
    OverdueState state;
    state = config.getOverdueStatesAccount().findState("OD1");
    applicator.apply(clock.getUTCNow(), overdueStateSet, null, account, clearState, state, internalCallContext);
    testOverdueHelper.checkStateApplied(state);
    checkBussEvent("OD1");
    state = config.getOverdueStatesAccount().findState("OD2");
    applicator.apply(clock.getUTCNow(), overdueStateSet, null, account, clearState, state, internalCallContext);
    testOverdueHelper.checkStateApplied(state);
    checkBussEvent("OD2");
    state = config.getOverdueStatesAccount().findState("OD3");
    applicator.apply(clock.getUTCNow(), overdueStateSet, null, account, clearState, state, internalCallContext);
    testOverdueHelper.checkStateApplied(state);
    checkBussEvent("OD3");
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) OverdueStateSet(org.killbill.billing.overdue.config.api.OverdueStateSet) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultOverdueConfig(org.killbill.billing.overdue.config.DefaultOverdueConfig) OverdueState(org.killbill.billing.overdue.api.OverdueState) Test(org.testng.annotations.Test)

Example 8 with ImmutableAccountData

use of org.killbill.billing.account.api.ImmutableAccountData in project killbill by killbill.

the class TestBillingStateCalculator method createBSCalc.

public BillingStateCalculator createBSCalc() {
    now = new LocalDate();
    final Collection<Invoice> invoices = new ArrayList<Invoice>();
    invoices.add(createInvoice(now, BigDecimal.ZERO, null));
    invoices.add(createInvoice(now.plusDays(1), BigDecimal.TEN, null));
    invoices.add(createInvoice(now.plusDays(2), new BigDecimal("100.0"), null));
    Mockito.when(invoiceApi.getUnpaidInvoicesByAccountId(Mockito.<UUID>any(), Mockito.<LocalDate>any(), Mockito.<InternalTenantContext>any())).thenReturn(invoices);
    return new BillingStateCalculator(invoiceApi, clock, tagInternalApi) {

        @Override
        public BillingState calculateBillingState(final ImmutableAccountData overdueable, final InternalTenantContext context) {
            return null;
        }
    };
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) Invoice(org.killbill.billing.invoice.api.Invoice) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 9 with ImmutableAccountData

use of org.killbill.billing.account.api.ImmutableAccountData in project killbill by killbill.

the class DefaultSubscriptionApi method getBlockingStates.

@Override
public Iterable<BlockingState> getBlockingStates(final UUID accountId, @Nullable final List<BlockingStateType> typeFilter, @Nullable final List<String> svcsFilter, final OrderingType orderingType, final int timeFilter, final TenantContext tenantContext) throws EntitlementApiException {
    try {
        final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
        final List<BlockingState> allBlockingStates = blockingStateDao.getBlockingAllForAccountRecordId(internalTenantContextWithValidAccountRecordId);
        final ImmutableAccountData account = accountApi.getImmutableAccountDataById(accountId, internalTenantContextWithValidAccountRecordId);
        final Iterable<BlockingState> filteredByTypes = typeFilter != null && !typeFilter.isEmpty() ? Iterables.filter(allBlockingStates, new Predicate<BlockingState>() {

            @Override
            public boolean apply(final BlockingState input) {
                return typeFilter.contains(input.getType());
            }
        }) : allBlockingStates;
        final Iterable<BlockingState> filteredByTypesAndSvcs = svcsFilter != null && !svcsFilter.isEmpty() ? Iterables.filter(filteredByTypes, new Predicate<BlockingState>() {

            @Override
            public boolean apply(final BlockingState input) {
                return svcsFilter.contains(input.getService());
            }
        }) : filteredByTypes;
        final LocalDate localDateNowInAccountTimezone = new LocalDate(clock.getUTCNow(), account.getTimeZone());
        final List<BlockingState> result = new ArrayList<BlockingState>();
        for (final BlockingState cur : filteredByTypesAndSvcs) {
            final LocalDate eventDate = new LocalDate(cur.getEffectiveDate(), account.getTimeZone());
            final int comp = eventDate.compareTo(localDateNowInAccountTimezone);
            if ((comp <= 1 && ((timeFilter & SubscriptionApi.PAST_EVENTS) == SubscriptionApi.PAST_EVENTS)) || (comp == 0 && ((timeFilter & SubscriptionApi.PRESENT_EVENTS) == SubscriptionApi.PRESENT_EVENTS)) || (comp >= 1 && ((timeFilter & SubscriptionApi.FUTURE_EVENTS) == SubscriptionApi.FUTURE_EVENTS))) {
                result.add(cur);
            }
        }
        return orderingType == OrderingType.ASCENDING ? result : Lists.reverse(result);
    } catch (AccountApiException e) {
        throw new EntitlementApiException(e);
    }
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ArrayList(java.util.ArrayList) AccountApiException(org.killbill.billing.account.api.AccountApiException) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) EntitlementLoggingHelper.logAddBlockingState(org.killbill.billing.entitlement.logging.EntitlementLoggingHelper.logAddBlockingState) LocalDate(org.joda.time.LocalDate) Predicate(com.google.common.base.Predicate)

Example 10 with ImmutableAccountData

use of org.killbill.billing.account.api.ImmutableAccountData in project killbill by killbill.

the class DefaultSubscriptionApi method updateExternalKey.

@Override
public void updateExternalKey(final UUID bundleId, final String newExternalKey, final CallContext callContext) throws EntitlementApiException {
    logUpdateExternalKey(log, bundleId, newExternalKey);
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(callContext);
    final SubscriptionBaseBundle bundle;
    final ImmutableAccountData account;
    try {
        bundle = subscriptionBaseInternalApi.getBundleFromId(bundleId, internalCallContext);
        account = accountApi.getImmutableAccountDataById(bundle.getAccountId(), internalCallContext);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    } catch (AccountApiException e) {
        throw new EntitlementApiException(e);
    }
    final LocalDate effectiveDate = new LocalDate(clock.getUTCNow(), account.getTimeZone());
    final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(bundleId, newExternalKey, new ArrayList<EntitlementSpecifier>(), effectiveDate, effectiveDate, false);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.UPDATE_BUNDLE_EXTERNAL_KEY, bundle.getAccountId(), null, baseEntitlementWithAddOnsSpecifierList, null, ImmutableList.<PluginProperty>of(), callContext);
    final WithEntitlementPlugin<Void> updateExternalKeyWithPlugin = new WithEntitlementPlugin<Void>() {

        final InternalCallContext internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(account.getId(), callContext);

        @Override
        public Void doCall(final EntitlementApi entitlementApi, final EntitlementContext updatedPluginContext) throws EntitlementApiException {
            subscriptionBaseInternalApi.updateExternalKey(bundleId, newExternalKey, internalCallContextWithValidAccountId);
            return null;
        }
    };
    pluginExecution.executeWithPlugin(updateExternalKeyWithPlugin, pluginContext);
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) LocalDate(org.joda.time.LocalDate) AccountApiException(org.killbill.billing.account.api.AccountApiException) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext)

Aggregations

ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)14 UUID (java.util.UUID)7 AccountApiException (org.killbill.billing.account.api.AccountApiException)7 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)6 ArrayList (java.util.ArrayList)4 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)4 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)4 LocalDate (org.joda.time.LocalDate)3 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)3 HashMap (java.util.HashMap)2 DateTime (org.joda.time.DateTime)2 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)2 BlockingState (org.killbill.billing.entitlement.api.BlockingState)2 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)2 WithEntitlementPlugin (org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin)2 EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)2 Invoice (org.killbill.billing.invoice.api.Invoice)2 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)2 Test (org.testng.annotations.Test)2 Predicate (com.google.common.base.Predicate)1