Search in sources :

Example 16 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method getPlanChangeResult.

@Override
public PlanChangeResult getPlanChangeResult(final DefaultSubscriptionBase subscription, final PlanSpecifier toPlanPhase, final DateTime effectiveDate, final TenantContext context) throws SubscriptionBaseApiException {
    final PlanChangeResult planChangeResult;
    try {
        final InternalTenantContext internalCallContext = createTenantContextFromBundleId(subscription.getBundleId(), context);
        final Plan currentPlan = subscription.getCurrentPlan();
        final PlanPhaseSpecifier fromPlanPhase = new PlanPhaseSpecifier(currentPlan.getName(), subscription.getCurrentPhase().getPhaseType());
        planChangeResult = catalogService.getFullCatalog(true, true, internalCallContext).planChange(fromPlanPhase, toPlanPhase, effectiveDate);
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
    return planChangeResult;
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanChangeResult(org.killbill.billing.catalog.api.PlanChangeResult) Plan(org.killbill.billing.catalog.api.Plan) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 17 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class TenantOverdueConfigCacheLoader method load.

@Override
public Object load(final Object key, final Object argument) {
    checkCacheLoaderStatus();
    if (!(key instanceof Long)) {
        throw new IllegalArgumentException("Unexpected key type of " + key.getClass().getName());
    }
    if (!(argument instanceof CacheLoaderArgument)) {
        throw new IllegalArgumentException("Unexpected argument type of " + argument.getClass().getName());
    }
    final Long tenantRecordId = (Long) key;
    final InternalTenantContext internalTenantContext = new InternalTenantContext(tenantRecordId);
    final CacheLoaderArgument cacheLoaderArgument = (CacheLoaderArgument) argument;
    if (cacheLoaderArgument.getArgs() == null || !(cacheLoaderArgument.getArgs()[0] instanceof LoaderCallback)) {
        throw new IllegalArgumentException("Missing LoaderCallback from the arguments");
    }
    final LoaderCallback callback = (LoaderCallback) cacheLoaderArgument.getArgs()[0];
    final String overdueXML = tenantApi.getTenantOverdueConfig(internalTenantContext);
    if (overdueXML == null) {
        return EMPTY_VALUE_PLACEHOLDER;
    }
    try {
        log.info("Loading overdue cache for tenant " + internalTenantContext.getTenantRecordId());
        return callback.loadOverdueConfig(overdueXML);
    } catch (final OverdueApiException e) {
        throw new IllegalStateException(String.format("Failed to de-serialize overdue config for tenant %s : %s", internalTenantContext.getTenantRecordId(), e.getMessage()), e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) OverdueApiException(org.killbill.billing.overdue.api.OverdueApiException)

Example 18 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class CacheConfig method initializeCacheLoaderArgument.

private CacheLoaderArgument initializeCacheLoaderArgument() {
    final LoaderCallback loaderCallback = new LoaderCallback() {

        @Override
        public Object loadConfig(@Nullable final String inputJson) throws IOException {
            return inputJson != null ? objectMapper.readValue(inputJson, PerTenantConfig.class) : new PerTenantConfig();
        }
    };
    final Object[] args = new Object[1];
    args[0] = loaderCallback;
    final ObjectType irrelevant = null;
    final InternalTenantContext notUsed = null;
    return new CacheLoaderArgument(irrelevant, args, notUsed);
}
Also used : ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) LoaderCallback(org.killbill.billing.util.cache.TenantConfigCacheLoader.LoaderCallback) Nullable(javax.annotation.Nullable) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument)

Example 19 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class EntitySqlDaoWrapperInvocationHandler method invokeWithCaching.

private Object invokeWithCaching(final Cachable cachableAnnotation, final Method method, final Object[] args) throws Throwable {
    final ObjectType objectType = getObjectType();
    final CacheType cacheType = cachableAnnotation.value();
    final CacheController<Object, Object> cache = cacheControllerDispatcher.getCacheController(cacheType);
    Object result = null;
    if (cache != null) {
        // Find all arguments marked with @CachableKey
        final Map<Integer, Object> keyPieces = new LinkedHashMap<Integer, Object>();
        final Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            for (int j = 0; j < annotations[i].length; j++) {
                final Annotation annotation = annotations[i][j];
                if (CachableKey.class.equals(annotation.annotationType())) {
                    // CachableKey position starts at 1
                    keyPieces.put(((CachableKey) annotation).value() - 1, args[i]);
                    break;
                }
            }
        }
        // Build the Cache key
        final String cacheKey = buildCacheKey(keyPieces);
        final InternalTenantContext internalTenantContext = (InternalTenantContext) Iterables.find(ImmutableList.copyOf(args), new Predicate<Object>() {

            @Override
            public boolean apply(final Object input) {
                return input instanceof InternalTenantContext;
            }
        }, null);
        final CacheLoaderArgument cacheLoaderArgument = new CacheLoaderArgument(objectType, args, internalTenantContext, handle);
        return cache.get(cacheKey, cacheLoaderArgument);
    }
    if (result == null) {
        result = prof.executeWithProfiling(ProfilingFeatureType.DAO_DETAILS, sqlDaoClass.getSimpleName() + "(raw) :" + method.getName(), new WithProfilingCallback() {

            @Override
            public Object execute() throws Throwable {
                return method.invoke(sqlDao, args);
            }
        });
    }
    return result;
}
Also used : Annotation(java.lang.annotation.Annotation) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument) CacheType(org.killbill.billing.util.cache.Cachable.CacheType) LinkedHashMap(java.util.LinkedHashMap) Predicate(com.google.common.base.Predicate) WithProfilingCallback(org.killbill.commons.profiling.Profiling.WithProfilingCallback) ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CachableKey(org.killbill.billing.util.cache.CachableKey)

Example 20 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultUsageUserApi method getAllUsageForSubscription.

@Override
public List<RolledUpUsage> getAllUsageForSubscription(final UUID subscriptionId, final List<LocalDate> transitionTimes, final TenantContext tenantContext) {
    final InternalTenantContext internalCallContext = internalCallContextFactory.createInternalTenantContext(subscriptionId, ObjectType.SUBSCRIPTION, tenantContext);
    List<RolledUpUsage> result = new ArrayList<RolledUpUsage>();
    LocalDate prevDate = null;
    for (LocalDate curDate : transitionTimes) {
        if (prevDate != null) {
            final List<RolledUpUsageModelDao> usageForSubscription = rolledUpUsageDao.getAllUsageForSubscription(subscriptionId, prevDate, curDate, internalCallContext);
            final List<RolledUpUnit> rolledUpAmount = getRolledUpUnits(usageForSubscription);
            result.add(new DefaultRolledUpUsage(subscriptionId, prevDate, curDate, rolledUpAmount));
        }
        prevDate = curDate;
    }
    return result;
}
Also used : RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ArrayList(java.util.ArrayList) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao) LocalDate(org.joda.time.LocalDate)

Aggregations

InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)56 UUID (java.util.UUID)9 ArrayList (java.util.ArrayList)8 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)8 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)7 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 LocalDate (org.joda.time.LocalDate)6 Predicate (com.google.common.base.Predicate)5 IOException (java.io.IOException)5 ObjectType (org.killbill.billing.ObjectType)5 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)5 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)5 InputStream (java.io.InputStream)4 URI (java.net.URI)4 DateTime (org.joda.time.DateTime)4 Account (org.killbill.billing.account.api.Account)4 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)4 Invoice (org.killbill.billing.invoice.api.Invoice)4