protected Collection<Payment> getPaymentsWithPluginInfoByAccountId(
      final UUID accountId, final TenantContext context) throws AnalyticsRefreshException {
    PaymentApiException error;

    final PaymentApi paymentApi = getPaymentUserApi();
    try {
      return paymentApi.getAccountPayments(accountId, true, PLUGIN_PROPERTIES, context);
    } catch (PaymentApiException e) {
      error = e;
      if (e.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT_PLUGIN.getCode()) {
        logService.log(
            LogService.LOG_WARNING,
            e.getMessage() + ". Analytics tables will be missing plugin specific information");

        try {
          return paymentApi.getAccountPayments(accountId, false, PLUGIN_PROPERTIES, context);
        } catch (PaymentApiException e1) {
          error = e1;
        }
      }
    }

    logService.log(
        LogService.LOG_WARNING, "Error retrieving payments for account id " + accountId, error);
    throw new AnalyticsRefreshException(error);
  }
  protected List<PaymentMethod> getPaymentMethodsForAccount(
      final UUID accountId, final TenantContext context) throws AnalyticsRefreshException {
    PaymentApiException error;

    final PaymentApi paymentApi = getPaymentUserApi();
    try {
      // Try to get all payment methods, with plugin information
      // TODO this will not return deleted payment methods
      return paymentApi.getAccountPaymentMethods(accountId, true, PLUGIN_PROPERTIES, context);
    } catch (PaymentApiException e) {
      error = e;
      if (e.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT_PLUGIN.getCode()) {
        logService.log(
            LogService.LOG_WARNING,
            e.getMessage() + ". Analytics tables will be missing plugin specific information");

        try {
          return paymentApi.getAccountPaymentMethods(accountId, false, PLUGIN_PROPERTIES, context);
        } catch (PaymentApiException e1) {
          error = e1;
        }
      }
    }

    logService.log(
        LogService.LOG_WARNING,
        "Error retrieving payment methods for account id " + accountId,
        error);
    throw new AnalyticsRefreshException(error);
  }
  protected Account getAccount(final UUID accountId, final TenantContext context)
      throws AnalyticsRefreshException {
    final AccountUserApi accountUserApi = getAccountUserApi();

    try {
      return accountUserApi.getAccountById(accountId, context);
    } catch (AccountApiException e) {
      logService.log(LogService.LOG_WARNING, "Error retrieving account for id " + accountId, e);
      throw new AnalyticsRefreshException(e);
    }
  }
 public void rebuildAnalyticsForAccount(final UUID accountId, final CallContext context)
     throws AnalyticsRefreshException {
   final BusinessContextFactory businessContextFactory =
       new BusinessContextFactory(
           accountId,
           context,
           currencyConversionDao,
           logService,
           osgiKillbillAPI,
           osgiConfigPropertiesService,
           clock);
   logService.log(
       LogService.LOG_INFO,
       "Starting Analytics refresh for account " + businessContextFactory.getAccountId());
   // TODO Should we take the account lock?
   allBusinessObjectsDao.update(businessContextFactory);
   logService.log(
       LogService.LOG_INFO,
       "Finished Analytics refresh for account " + businessContextFactory.getAccountId());
 }
  protected AuditLog getTagCreationAuditLog(
      final UUID tagId, final AccountAuditLogs accountAuditLogs) throws AnalyticsRefreshException {
    final List<AuditLog> auditLogsForTag = accountAuditLogs.getAuditLogsForTag(tagId);
    for (final AuditLog auditLog : auditLogsForTag) {
      if (auditLog.getChangeType().equals(ChangeType.INSERT)) {
        return auditLog;
      }
    }

    logService.log(LogService.LOG_WARNING, "Unable to find Tag creation audit log for id " + tagId);
    return null;
  }
  protected List<SubscriptionBundle> getSubscriptionBundlesForAccount(
      final UUID accountId, final TenantContext context) throws AnalyticsRefreshException {
    final SubscriptionApi subscriptionApi = getSubscriptionApi();

    try {
      return subscriptionApi.getSubscriptionBundlesForAccountId(accountId, context);
    } catch (SubscriptionApiException e) {
      logService.log(
          LogService.LOG_WARNING, "Error retrieving bundles for account id " + accountId, e);
      throw new AnalyticsRefreshException(e);
    }
  }
 protected Plan getPlanFromInvoiceItem(final InvoiceItem invoiceItem, final TenantContext context)
     throws AnalyticsRefreshException {
   try {
     final Catalog catalog = getCatalog(context);
     return catalog.findPlan(
         invoiceItem.getPlanName(), invoiceItem.getStartDate().toDateTimeAtStartOfDay());
   } catch (CatalogApiException e) {
     logService.log(
         LogService.LOG_INFO,
         "Unable to retrieve plan for invoice item " + invoiceItem.getId(),
         e);
     return null;
   }
 }
  protected AuditLog getInvoicePaymentCreationAuditLog(
      final UUID invoicePaymentId, final AccountAuditLogs accountAuditLogs)
      throws AnalyticsRefreshException {
    final List<AuditLog> auditLogsForInvoicePayment =
        accountAuditLogs.getAuditLogsForInvoicePayment(invoicePaymentId);
    for (final AuditLog auditLog : auditLogsForInvoicePayment) {
      if (auditLog.getChangeType().equals(ChangeType.INSERT)) {
        return auditLog;
      }
    }

    logService.log(
        LogService.LOG_WARNING,
        "Unable to find Invoice payment creation audit log for id " + invoicePaymentId);
    return null;
  }
  protected AuditLog getBlockingStateCreationAuditLog(
      final UUID blockingStateId, final AccountAuditLogs accountAuditLogs)
      throws AnalyticsRefreshException {
    final List<AuditLog> auditLogsForBlockingState =
        accountAuditLogs.getAuditLogsForBlockingState(blockingStateId);
    for (final AuditLog auditLog : auditLogsForBlockingState) {
      if (auditLog.getChangeType().equals(ChangeType.INSERT)) {
        return auditLog;
      }
    }

    logService.log(
        LogService.LOG_WARNING,
        "Unable to find Blocking state creation audit log for id " + blockingStateId);
    return null;
  }
  protected AuditLog getSubscriptionEventCreationAuditLog(
      final UUID subscriptionEventId,
      final ObjectType objectType,
      final AccountAuditLogs accountAuditLogs)
      throws AnalyticsRefreshException {
    final List<AuditLog> auditLogsForSubscriptionEvent =
        accountAuditLogs.getAuditLogs(objectType).getAuditLogs(subscriptionEventId);
    for (final AuditLog auditLog : auditLogsForSubscriptionEvent) {
      if (auditLog.getChangeType().equals(ChangeType.INSERT)) {
        return auditLog;
      }
    }

    logService.log(
        LogService.LOG_WARNING,
        "Unable to find Subscription event creation audit log for id " + subscriptionEventId);
    return null;
  }
 protected PlanPhase getPlanPhaseFromInvoiceItem(
     final InvoiceItem invoiceItem,
     final LocalDate subscriptionStartDate,
     final TenantContext context)
     throws AnalyticsRefreshException {
   try {
     final Catalog catalog = getCatalog(context);
     // TODO - Inaccurate timing
     return catalog.findPhase(
         invoiceItem.getPhaseName(),
         invoiceItem.getStartDate().toDateTimeAtStartOfDay(),
         subscriptionStartDate.toDateTimeAtStartOfDay());
   } catch (CatalogApiException e) {
     logService.log(
         LogService.LOG_INFO,
         "Unable to retrieve phase for invoice item " + invoiceItem.getId(),
         e);
     return null;
   }
 }
  protected SubscriptionBundle getLatestSubscriptionBundleForExternalKey(
      final String bundleExternalKey, final TenantContext context)
      throws AnalyticsRefreshException {
    final SubscriptionApi subscriptionApi = getSubscriptionApi();

    try {
      final List<SubscriptionBundle> bundles =
          subscriptionApi.getSubscriptionBundlesForExternalKey(bundleExternalKey, context);
      if (bundles.size() == 0) {
        throw new AnalyticsRefreshException(
            "Unable to retrieve latest bundle for bundle external key " + bundleExternalKey);
      }
      return bundles.get(bundles.size() - 1);
    } catch (SubscriptionApiException e) {
      logService.log(
          LogService.LOG_WARNING,
          "Error retrieving bundles for bundle external key " + bundleExternalKey,
          e);
      throw new AnalyticsRefreshException(e);
    }
  }