Search in sources :

Example 66 with InternalTenantContext

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

the class DefaultCatalogUserApi method getCatalog.

@Override
public VersionedCatalog getCatalog(final String catalogName, final TenantContext tenantContext) throws CatalogApiException {
    final InternalTenantContext internalTenantContext;
    if (tenantContext.getAccountId() != null) {
        internalTenantContext = internalCallContextFactory.createInternalTenantContext(tenantContext.getAccountId(), tenantContext);
    } else {
        internalTenantContext = createInternalTenantContext(tenantContext);
    }
    final VersionedCatalog fullCatalog = catalogService.getFullCatalog(true, true, internalTenantContext);
    return fullCatalog;
}
Also used : VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext)

Example 67 with InternalTenantContext

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

the class DefaultCatalogUserApi method deleteCatalog.

@Override
public void deleteCatalog(final CallContext callContext) throws CatalogApiException {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext);
    try {
        tenantApi.deleteTenantKey(TenantKey.CATALOG.toString(), callContext);
        catalogCache.clearCatalog(internalTenantContext);
        createDefaultEmptyCatalog(callContext.getCreatedDate(), callContext);
    } catch (final TenantApiException e) {
        throw new CatalogApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException)

Example 68 with InternalTenantContext

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

the class DefaultCatalogUserApi method addSimplePlan.

@Override
public void addSimplePlan(final SimplePlanDescriptor descriptor, @Nullable final DateTime effectiveDate, final CallContext callContext) throws CatalogApiException {
    try {
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext);
        final StandaloneCatalog currentCatalog = getCurrentStandaloneCatalogForTenant(internalTenantContext);
        final CatalogUpdater catalogUpdater = (currentCatalog != null) ? new CatalogUpdater(currentCatalog) : new CatalogUpdater(getSafeFirstCatalogEffectiveDate(effectiveDate, callContext), descriptor.getCurrency());
        catalogUpdater.addSimplePlanDescriptor(descriptor);
        tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(internalTenantContext), callContext);
        catalogCache.clearCatalog(internalTenantContext);
    } catch (TenantApiException e) {
        throw new CatalogApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) CatalogUpdater(org.killbill.billing.catalog.CatalogUpdater)

Example 69 with InternalTenantContext

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

the class DefaultCatalogUserApi method uploadCatalog.

@Override
public void uploadCatalog(final String catalogXML, final CallContext callContext) throws CatalogApiException {
    final InternalTenantContext internalTenantContext = createInternalTenantContext(callContext);
    try {
        VersionedCatalog versionedCatalog = catalogService.getFullCatalog(false, true, internalTenantContext);
        if (versionedCatalog == null) {
            // If this is the first version
            versionedCatalog = new DefaultVersionedCatalog();
        }
        // Validation purpose:  Will throw if bad XML or catalog validation fails
        final InputStream stream = new ByteArrayInputStream(catalogXML.getBytes());
        final StaticCatalog newCatalogVersion = XMLLoader.getObjectFromStream(stream, StandaloneCatalog.class);
        final ValidationErrors errors = new ValidationErrors();
        // Fix for https://github.com/killbill/killbill/issues/1481
        ((DefaultVersionedCatalog) versionedCatalog).add((StandaloneCatalog) newCatalogVersion);
        ((DefaultVersionedCatalog) versionedCatalog).validate(null, errors);
        if (!errors.isEmpty()) {
            // Bummer ValidationException CTOR is private to package...
            // final ValidationException validationException = new ValidationException(errors);
            // throw new CatalogApiException(errors, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
            logger.info("Failed to load new catalog version: " + errors.toString());
            throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
        }
        tenantApi.addTenantKeyValue(TenantKey.CATALOG.toString(), catalogXML, callContext);
        catalogCache.clearCatalog(internalTenantContext);
    } catch (final TenantApiException e) {
        throw new CatalogApiException(e);
    } catch (final ValidationException e) {
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
    } catch (final JAXBException e) {
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } catch (final TransformerException e) {
        throw new IllegalStateException(e);
    } catch (final SAXException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ValidationException(org.killbill.xmlloader.ValidationException) ValidationErrors(org.killbill.xmlloader.ValidationErrors) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) IOException(java.io.IOException) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) SAXException(org.xml.sax.SAXException) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) ByteArrayInputStream(java.io.ByteArrayInputStream) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) TransformerException(javax.xml.transform.TransformerException)

Example 70 with InternalTenantContext

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

the class DefaultCatalogCache method getCatalogFromPlugins.

private VersionedCatalog getCatalogFromPlugins(final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final TenantContext tenantContext = internalCallContextFactory.createTenantContext(internalTenantContext);
    final Set<String> allServices = pluginRegistry.getAllServices();
    for (final String service : allServices) {
        final CatalogPluginApi plugin = pluginRegistry.getServiceForName(service);
        // 
        // Beware plugin implementors:  latestCatalogUpdatedDate returned by the plugin should also match the effectiveDate of the VersionedCatalog.
        // 
        // However, this is the plugin choice to return one, or many catalog versions (StandaloneCatalog), Kill Bill catalog module does not care.
        // As a guideline, if plugin keeps seeing new Plans/Products, this can all fit into the same version; however if there is a true versioning
        // (e.g deleted Plans...), then multiple versions must be returned.
        // 
        final DateTime latestCatalogUpdatedDate = plugin.getLatestCatalogVersion(ImmutableList.<PluginProperty>of(), tenantContext);
        // A null latestCatalogUpdatedDate bypasses caching, by fetching full catalog from plugin below (compatibility mode with 0.18.x or non optimized plugin api mode)
        final boolean cacheable = latestCatalogUpdatedDate != null;
        if (cacheable) {
            final DefaultVersionedCatalog versionedCatalog = cacheController.get(internalTenantContext.getTenantRecordId(), cacheLoaderArgument);
            if (versionedCatalog != null) {
                if (versionedCatalog.getCurrentVersion().getEffectiveDate().compareTo(latestCatalogUpdatedDate.toDate()) == 0) {
                    // Current cached version matches the one from the plugin
                    return versionedCatalog;
                }
            }
        }
        final VersionedPluginCatalog pluginCatalog = plugin.getVersionedPluginCatalog(ImmutableList.<PluginProperty>of(), tenantContext);
        // First plugin that gets something (for that tenant) returns it
        if (pluginCatalog != null) {
            // The log entry is only interesting if there are multiple plugins
            if (allServices.size() > 1) {
                logger.info("Returning catalog from plugin {} on tenant {} ", service, internalTenantContext.getTenantRecordId());
            }
            final DefaultVersionedCatalog resolvedPluginCatalog = versionedCatalogMapper.toVersionedCatalog(pluginCatalog);
            // Always clear the cache for safety
            cacheController.remove(internalTenantContext.getTenantRecordId());
            if (cacheable) {
                cacheController.putIfAbsent(internalTenantContext.getTenantRecordId(), resolvedPluginCatalog);
            }
            return resolvedPluginCatalog;
        }
    }
    return null;
}
Also used : VersionedPluginCatalog(org.killbill.billing.catalog.plugin.api.VersionedPluginCatalog) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantContext(org.killbill.billing.util.callcontext.TenantContext) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) CatalogPluginApi(org.killbill.billing.catalog.plugin.api.CatalogPluginApi) DateTime(org.joda.time.DateTime)

Aggregations

InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)92 UUID (java.util.UUID)15 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)13 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)11 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)10 ArrayList (java.util.ArrayList)9 ObjectType (org.killbill.billing.ObjectType)9 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)9 TenantContext (org.killbill.billing.util.callcontext.TenantContext)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 DateTime (org.joda.time.DateTime)7 LocalDate (org.joda.time.LocalDate)7 Predicate (com.google.common.base.Predicate)6 AccountApiException (org.killbill.billing.account.api.AccountApiException)6 VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)6 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)6