Search in sources :

Example 71 with InternalTenantContext

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

the class DefaultCatalogCache method initializeCacheLoaderArgument.

// 
// Build the LoaderCallback that is required to build the catalog from the xml from a module that knows
// nothing about catalog.
// 
// This is a contract between the TenantCatalogCacheLoader and the DefaultCatalogCache
private CacheLoaderArgument initializeCacheLoaderArgument(final boolean filterTemplateCatalog) {
    final LoaderCallback loaderCallback = new LoaderCallback() {

        @Override
        public VersionedCatalog loadCatalog(final List<String> catalogXMLs, final Long tenantRecordId) throws CatalogApiException {
            final VersionedCatalog versionedCatalog = loader.load(catalogXMLs, filterTemplateCatalog, tenantRecordId);
            if (versionedCatalog != null) {
                initializeCatalog(versionedCatalog);
            }
            return versionedCatalog;
        }
    };
    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 : VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) LoaderCallback(org.killbill.billing.util.cache.TenantCatalogCacheLoader.LoaderCallback) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument)

Example 72 with InternalTenantContext

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

the class DefaultInvoiceUserApi method getInvoiceItemsByParentInvoice.

@Override
public List<InvoiceItem> getInvoiceItemsByParentInvoice(final UUID parentInvoiceId, final TenantContext context) throws InvoiceApiException {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(parentInvoiceId, ObjectType.INVOICE, context);
    final VersionedCatalog catalog = getCatalogSafelyForPrettyNames(internalTenantContext);
    return ImmutableList.copyOf(Collections2.transform(dao.getInvoiceItemsByParentInvoice(parentInvoiceId, internalTenantContext), new Function<InvoiceItemModelDao, InvoiceItem>() {

        @Override
        public InvoiceItem apply(final InvoiceItemModelDao input) {
            return InvoiceItemFactory.fromModelDaoWithCatalog(input, catalog);
        }
    }));
}
Also used : VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) Function(com.google.common.base.Function) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao)

Example 73 with InternalTenantContext

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

the class InvoiceDaoHelper method setParentInvoice.

private void setParentInvoice(final Iterable<InvoiceModelDao> childInvoices, final List<Tag> invoicesTags, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalTenantContext childContext) {
    final Collection<String> childInvoiceIds = new HashSet<String>();
    for (final InvoiceModelDao childInvoice : childInvoices) {
        childInvoiceIds.add(childInvoice.getId().toString());
    }
    // DAO: retrieve the mappings between parent and child invoices
    final InvoiceParentChildrenSqlDao invoiceParentChildrenSqlDao = entitySqlDaoWrapperFactory.become(InvoiceParentChildrenSqlDao.class);
    final List<InvoiceParentChildModelDao> mappings = invoiceParentChildrenSqlDao.getParentChildMappingsByChildInvoiceIds(childInvoiceIds, childContext);
    if (mappings.isEmpty()) {
        return;
    }
    final Map<UUID, InvoiceParentChildModelDao> mappingPerChildInvoiceId = new HashMap<UUID, InvoiceParentChildModelDao>();
    final Collection<String> parentInvoiceIdsAsStrings = new HashSet<String>();
    for (final InvoiceParentChildModelDao mapping : mappings) {
        mappingPerChildInvoiceId.put(mapping.getChildInvoiceId(), mapping);
        parentInvoiceIdsAsStrings.add(mapping.getParentInvoiceId().toString());
    }
    // DAO: retrieve all parents invoices in bulk, for all child invoices
    final InvoiceSqlDao invoiceSqlDao = entitySqlDaoWrapperFactory.become(InvoiceSqlDao.class);
    final List<InvoiceModelDao> parentInvoices = invoiceSqlDao.getByIds(parentInvoiceIdsAsStrings, childContext);
    // Group the parent invoices by (parent) account id (most likely, we only have one parent account group, except in re-parenting cases)
    final Map<UUID, List<InvoiceModelDao>> parentInvoicesGroupedByParentAccountId = new HashMap<UUID, List<InvoiceModelDao>>();
    // Create also a convenient mapping (needed below later)
    final Map<UUID, InvoiceModelDao> parentInvoiceByParentInvoiceId = new HashMap<UUID, InvoiceModelDao>();
    for (final InvoiceModelDao parentInvoice : parentInvoices) {
        if (parentInvoicesGroupedByParentAccountId.get(parentInvoice.getAccountId()) == null) {
            parentInvoicesGroupedByParentAccountId.put(parentInvoice.getAccountId(), new LinkedList<InvoiceModelDao>());
        }
        parentInvoicesGroupedByParentAccountId.get(parentInvoice.getAccountId()).add(parentInvoice);
        parentInvoiceByParentInvoiceId.put(parentInvoice.getId(), parentInvoice);
    }
    // DAO: populate the parent invoices in bulk
    for (final Entry<UUID, List<InvoiceModelDao>> entry : parentInvoicesGroupedByParentAccountId.entrySet()) {
        final List<InvoiceModelDao> parentInvoicesForOneParentAccountId = entry.getValue();
        final Long parentAccountRecordId = internalCallContextFactory.getRecordIdFromObject(entry.getKey(), ObjectType.ACCOUNT, internalCallContextFactory.createTenantContext(childContext));
        final InternalTenantContext parentContext = internalCallContextFactory.createInternalTenantContext(childContext.getTenantRecordId(), parentAccountRecordId);
        // Note the misnomer here, populateChildren simply populates the content of these invoices (unrelated to HA)
        populateChildren(parentInvoicesForOneParentAccountId, invoicesTags, entitySqlDaoWrapperFactory, parentContext);
    }
    for (final InvoiceModelDao invoice : childInvoices) {
        final InvoiceParentChildModelDao mapping = mappingPerChildInvoiceId.get(invoice.getId());
        if (mapping == null) {
            continue;
        }
        final InvoiceModelDao parentInvoice = parentInvoiceByParentInvoiceId.get(mapping.getParentInvoiceId());
        if (parentInvoice != null) {
            invoice.addParentInvoice(parentInvoice);
        }
    }
}
Also used : HashMap(java.util.HashMap) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 74 with InternalTenantContext

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

the class DefaultInvoiceUserApi method getInvoiceByPayment.

@Override
public Invoice getInvoiceByPayment(final UUID paymentId, final TenantContext context) throws InvoiceApiException {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(paymentId, ObjectType.PAYMENT, context);
    final UUID invoiceId = dao.getInvoiceIdByPaymentId(paymentId, internalTenantContext);
    if (invoiceId == null) {
        throw new InvoiceApiException(ErrorCode.INVOICE_NOT_FOUND, paymentId);
    }
    final InvoiceModelDao invoiceModelDao = dao.getById(invoiceId, internalTenantContext);
    return new DefaultInvoice(invoiceModelDao, getCatalogSafelyForPrettyNames(internalTenantContext));
}
Also used : InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice)

Example 75 with InternalTenantContext

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

the class DefaultInvoiceUserApi method getInvoicesByAccount.

@Override
public List<Invoice> getInvoicesByAccount(final UUID accountId, boolean includesMigrated, final boolean includeVoidedInvoices, final TenantContext context) {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(accountId, context);
    final List<InvoiceModelDao> invoicesByAccount = includesMigrated ? dao.getAllInvoicesByAccount(includeVoidedInvoices, internalTenantContext) : dao.getInvoicesByAccount(includeVoidedInvoices, internalTenantContext);
    return fromInvoiceModelDao(invoicesByAccount, getCatalogSafelyForPrettyNames(internalTenantContext));
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao)

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