Пример #1
0
  @Override
  protected void postBusEventFromTransaction(
      final AccountModelDao account,
      final AccountModelDao savedAccount,
      final ChangeType changeType,
      final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory,
      final InternalCallContext context)
      throws BillingExceptionBase {
    // This is only called for the create call (see update below)
    switch (changeType) {
      case INSERT:
        break;
      default:
        return;
    }

    final Long recordId =
        entitySqlDaoWrapperFactory
            .become(AccountSqlDao.class)
            .getRecordId(savedAccount.getId().toString(), context);
    // We need to re-hydrate the callcontext with the account record id
    final InternalCallContext rehydratedContext =
        internalCallContextFactory.createInternalCallContext(recordId, context);
    final AccountCreationInternalEvent creationEvent =
        new DefaultAccountCreationEvent(
            new DefaultAccountData(savedAccount),
            savedAccount.getId(),
            rehydratedContext.getAccountRecordId(),
            rehydratedContext.getTenantRecordId(),
            rehydratedContext.getUserToken());
    try {
      eventBus.postFromTransaction(
          creationEvent, entitySqlDaoWrapperFactory.getHandle().getConnection());
    } catch (final EventBusException e) {
      log.warn("Failed to post account creation event for accountId='{}'", savedAccount.getId(), e);
    }
  }
  @Override
  public SubscriptionBase createSubscription(
      final UUID bundleId,
      final PlanPhaseSpecifier spec,
      final DateTime requestedDateWithMs,
      final InternalCallContext context)
      throws SubscriptionBaseApiException {
    try {
      final String realPriceList =
          (spec.getPriceListName() == null)
              ? PriceListSet.DEFAULT_PRICELIST_NAME
              : spec.getPriceListName();
      final DateTime now = clock.getUTCNow();
      final DateTime requestedDate =
          (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : now;
      if (requestedDate.isAfter(now)) {
        throw new SubscriptionBaseApiException(
            ErrorCode.SUB_INVALID_REQUESTED_DATE, now.toString(), requestedDate.toString());
      }
      final DateTime effectiveDate = requestedDate;

      final Catalog catalog = catalogService.getFullCatalog();
      final Plan plan =
          catalog.findPlan(
              spec.getProductName(), spec.getBillingPeriod(), realPriceList, requestedDate);

      final PlanPhase phase = plan.getAllPhases()[0];
      if (phase == null) {
        throw new SubscriptionBaseError(
            String.format(
                "No initial PlanPhase for Product %s, term %s and set %s does not exist in the catalog",
                spec.getProductName(), spec.getBillingPeriod().toString(), realPriceList));
      }

      final SubscriptionBaseBundle bundle = dao.getSubscriptionBundleFromId(bundleId, context);
      if (bundle == null) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_NO_BUNDLE, bundleId);
      }

      DateTime bundleStartDate = null;
      final DefaultSubscriptionBase baseSubscription =
          (DefaultSubscriptionBase) dao.getBaseSubscription(bundleId, context);
      switch (plan.getProduct().getCategory()) {
        case BASE:
          if (baseSubscription != null) {
            if (baseSubscription.getState() == EntitlementState.ACTIVE) {
              throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_BP_EXISTS, bundleId);
            }
          }
          bundleStartDate = requestedDate;
          break;
        case ADD_ON:
          if (baseSubscription == null) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_NO_BP, bundleId);
          }
          if (effectiveDate.isBefore(baseSubscription.getStartDate())) {
            throw new SubscriptionBaseApiException(
                ErrorCode.SUB_INVALID_REQUESTED_DATE,
                effectiveDate.toString(),
                baseSubscription.getStartDate().toString());
          }
          addonUtils.checkAddonCreationRights(baseSubscription, plan);
          bundleStartDate = baseSubscription.getStartDate();
          break;
        case STANDALONE:
          if (baseSubscription != null) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_BP_EXISTS, bundleId);
          }
          // Not really but we don't care, there is no alignment for STANDALONE subscriptions
          bundleStartDate = requestedDate;
          break;
        default:
          throw new SubscriptionBaseError(
              String.format(
                  "Can't create subscription of type %s",
                  plan.getProduct().getCategory().toString()));
      }

      final UUID tenantId =
          nonEntityDao.retrieveIdFromObject(context.getTenantRecordId(), ObjectType.TENANT);
      return apiService.createPlan(
          new SubscriptionBuilder()
              .setId(UUID.randomUUID())
              .setBundleId(bundleId)
              .setCategory(plan.getProduct().getCategory())
              .setBundleStartDate(bundleStartDate)
              .setAlignStartDate(effectiveDate),
          plan,
          spec.getPhaseType(),
          realPriceList,
          requestedDate,
          effectiveDate,
          now,
          context.toCallContext(tenantId));
    } catch (CatalogApiException e) {
      throw new SubscriptionBaseApiException(e);
    }
  }