protected Account createAccount(final AccountData accountData) throws AccountApiException { final Account account = accountApi.createAccount(accountData, callContext); final Long accountRecordId = nonEntityDao.retrieveRecordIdFromObject( account.getId(), ObjectType.ACCOUNT, controlCacheDispatcher.getCacheController(CacheType.RECORD_ID)); internalCallContext.setAccountRecordId(accountRecordId); internalCallContext.setReferenceDateTimeZone(account.getTimeZone()); return account; }
@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); } }