Skip to content

Commit

Permalink
context: cleanup callers
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Alexandre Meyer <pierre@mouraf.org>
  • Loading branch information
pierre committed Feb 15, 2016
1 parent eded2c8 commit 1d94a9d
Show file tree
Hide file tree
Showing 41 changed files with 218 additions and 428 deletions.
Expand Up @@ -50,7 +50,7 @@ public DateTime toDateTime(final DateTime dateTime, final DateTimeZone accountTi
/// DateTime <-> LocalDate transformations

// Create a DateTime object using the specified reference time and timezone (usually, the one on the account)
public DateTime toUTCDateTime(final LocalDate localDate, final DateTime referenceDateTime) {
public DateTime toUTCDateTime(final LocalDate localDate) {
validateContext();

final DateTime targetDateTime = new DateTime(localDate.getYear(),
Expand All @@ -65,7 +65,7 @@ public DateTime toUTCDateTime(final LocalDate localDate, final DateTime referenc
}

// Create a LocalDate object using the specified timezone (usually, the one on the account), respecting the offset at the time of the referenceDateTime
public LocalDate toLocalDate(final DateTime dateTime, final DateTime referenceDateTime) {
public LocalDate toLocalDate(final DateTime dateTime) {
validateContext();

return new LocalDate(dateTime, getFixedOffsetTimeZone());
Expand Down
Expand Up @@ -30,7 +30,7 @@

public interface EntitlementInternalApi {

AccountEntitlements getAllEntitlementsForAccountId(UUID accountId, InternalTenantContext context) throws EntitlementApiException;
AccountEntitlements getAllEntitlementsForAccount(InternalTenantContext context) throws EntitlementApiException;

Entitlement getEntitlementForId(final UUID uuid, final InternalTenantContext tenantContext) throws EntitlementApiException;

Expand Down
@@ -1,7 +1,9 @@
/*
* Copyright 2010-2011 Ning, Inc.
* Copyright 2010-2013 Ning, Inc.
* Copyright 2014-2016 Groupon, Inc
* Copyright 2014-2016 The Billing Project, LLC
*
* Ning licenses this file to you under the Apache License, version 2.0
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
Expand All @@ -23,7 +25,6 @@

import org.killbill.billing.catalog.api.BillingMode;
import org.killbill.billing.catalog.api.Usage;
import org.killbill.billing.util.AccountDateAndTimeZoneContext;

public interface BillingEventSet extends SortedSet<BillingEvent> {

Expand All @@ -33,7 +34,5 @@ public interface BillingEventSet extends SortedSet<BillingEvent> {

public List<UUID> getSubscriptionIdsWithAutoInvoiceOff();

public AccountDateAndTimeZoneContext getAccountDateAndTimeZoneContext();

public Map<String, Usage> getUsages();
}

This file was deleted.

Expand Up @@ -142,9 +142,9 @@ private void processEntitlementNotification(final EntitlementNotificationKey key
EntitlementNotificationKeyAction.CANCEL.equals(entitlementNotificationKeyAction)) {
blockAddOnsIfRequired(key, (DefaultEntitlement) entitlement, callContext, internalCallContext);
} else if (EntitlementNotificationKeyAction.PAUSE.equals(entitlementNotificationKeyAction)) {
entitlementInternalApi.pause(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate(), ((DefaultEntitlement) entitlement).getSubscriptionBase().getStartDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
entitlementInternalApi.pause(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
} else if (EntitlementNotificationKeyAction.RESUME.equals(entitlementNotificationKeyAction)) {
entitlementInternalApi.resume(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate(), ((DefaultEntitlement) entitlement).getSubscriptionBase().getStartDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
entitlementInternalApi.resume(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
}
} catch (final EntitlementApiException e) {
log.error("Error processing event for entitlement {}" + entitlement.getId(), e);
Expand Down
Expand Up @@ -258,7 +258,6 @@ private SubscriptionEvent toSubscriptionEvent(@Nullable final SubscriptionEvent
nextPriceList,
nextBillingPeriod,
in.getCreatedDate(),
referenceTime,
internalTenantContext);
}

Expand Down
Expand Up @@ -246,7 +246,7 @@ public EntitlementSourceType getSourceType() {

@Override
public LocalDate getEffectiveStartDate() {
return internalTenantContext.toLocalDate(getSubscriptionBase().getStartDate(), getSubscriptionBase().getStartDate());
return internalTenantContext.toLocalDate(getSubscriptionBase().getStartDate());
}

@Override
Expand Down Expand Up @@ -318,7 +318,7 @@ public Entitlement doCall(final EntitlementApi entitlementApi, final Entitlement
}

final InternalCallContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalCallContext(getAccountId(), callContext);
final DateTime effectiveCancelDate = dateHelper.fromLocalDateAndReferenceTime(localCancelDate, getSubscriptionBase().getStartDate(), contextWithValidAccountRecordId);
final DateTime effectiveCancelDate = dateHelper.fromLocalDateAndReferenceTime(localCancelDate, contextWithValidAccountRecordId);
try {
if (overrideBillingEffectiveDate) {
getSubscriptionBase().cancelWithDate(effectiveCancelDate, callContext);
Expand Down Expand Up @@ -448,7 +448,7 @@ public Entitlement doCall(final EntitlementApi entitlementApi, final Entitlement
// (we don't want an entitlement cancel date one second or so after the subscription cancel date or add-ons cancellations
// computations won't work).
final InternalCallContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalCallContext(getAccountId(), callContext);
final DateTime effectiveDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), getSubscriptionBase().getStartDate(), contextWithValidAccountRecordId);
final DateTime effectiveDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), contextWithValidAccountRecordId);

try {
// Cancel subscription base first, to correctly compute the add-ons entitlements we need to cancel (see below)
Expand Down Expand Up @@ -481,7 +481,7 @@ private LocalDate getLocalDateFromEntitlementPolicy(final EntitlementActionPolic
break;
case END_OF_TERM:
if (getSubscriptionBase().getChargedThroughDate() != null) {
cancellationDate = internalTenantContext.toLocalDate(getSubscriptionBase().getChargedThroughDate(), getSubscriptionBase().getStartDate());
cancellationDate = internalTenantContext.toLocalDate(getSubscriptionBase().getChargedThroughDate());
} else {
cancellationDate = clock.getToday(eventsStream.getAccountTimeZone());
}
Expand Down Expand Up @@ -579,7 +579,7 @@ public Entitlement doCall(final EntitlementApi entitlementApi, final Entitlement
}

final InternalCallContext context = internalCallContextFactory.createInternalCallContext(getAccountId(), callContext);
final DateTime effectiveChangeDateComputed = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), getSubscriptionBase().getStartDate(), context);
final DateTime effectiveChangeDateComputed = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), context);

final DateTime effectiveChangeDate;
try {
Expand Down
Expand Up @@ -143,7 +143,7 @@ public Entitlement doCall(final EntitlementApi entitlementApi, final Entitlement
final SubscriptionBaseBundle bundle = subscriptionBaseInternalApi.createBundleForAccount(accountId, externalKey, contextWithValidAccountRecordId);

final DateTime referenceTime = clock.getUTCNow();
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), referenceTime, contextWithValidAccountRecordId);
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), contextWithValidAccountRecordId);
final EntitlementSpecifier specifier = getFirstEntitlementSpecifier(updatedPluginContext.getEntitlementSpecifiers());
final SubscriptionBase subscription = subscriptionBaseInternalApi.createSubscription(bundle.getId(), specifier.getPlanPhaseSpecifier(), specifier.getOverrides(), requestedDate, contextWithValidAccountRecordId);

Expand Down Expand Up @@ -207,7 +207,7 @@ public Entitlement doCall(final EntitlementApi entitlementApi, final Entitlement
final SubscriptionBaseBundle bundle = subscriptionBaseInternalApi.createBundleForAccount(accountId, externalKey, contextWithValidAccountRecordId);

final DateTime referenceTime = clock.getUTCNow();
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), referenceTime, contextWithValidAccountRecordId);
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), contextWithValidAccountRecordId);
final SubscriptionBase subscription = subscriptionBaseInternalApi.createBaseSubscriptionWithAddOns(bundle.getId(), entitlementSpecifiers, requestedDate, contextWithValidAccountRecordId);

return new DefaultEntitlement(accountId, subscription.getId(), eventsStreamBuilder, entitlementApi, pluginExecution,
Expand Down Expand Up @@ -254,7 +254,7 @@ public Entitlement doCall(final EntitlementApi entitlementApi, final Entitlement
throw new EntitlementApiException(new BlockingApiException(ErrorCode.BLOCK_BLOCKED_ACTION, BlockingChecker.ACTION_CHANGE, BlockingChecker.TYPE_SUBSCRIPTION, eventsStreamForBaseSubscription.getEntitlementId().toString()));
}

final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), eventsStreamForBaseSubscription.getSubscriptionBase().getStartDate(), eventsStreamForBaseSubscription.getInternalTenantContext());
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), eventsStreamForBaseSubscription.getInternalTenantContext());

try {
final InternalCallContext context = internalCallContextFactory.createInternalCallContext(callContext);
Expand All @@ -280,7 +280,7 @@ public List<EntitlementAOStatusDryRun> getDryRunStatusForChange(final UUID bundl
final SubscriptionBase baseSubscription = subscriptionBaseInternalApi.getBaseSubscription(bundleId, internalContext);

final InternalTenantContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(bundle.getAccountId(), context);
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(effectiveDate, baseSubscription.getStartDate(), contextWithValidAccountRecordId);
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(effectiveDate, contextWithValidAccountRecordId);
return subscriptionBaseInternalApi.getDryRunChangePlanStatus(baseSubscription.getId(), targetProductName, requestedDate, contextWithValidAccountRecordId);
} catch (final SubscriptionBaseApiException e) {
throw new EntitlementApiException(e);
Expand Down Expand Up @@ -314,7 +314,7 @@ public boolean apply(final Entitlement input) {

@Override
public List<Entitlement> getAllEntitlementsForAccountIdAndExternalKey(final UUID accountId, final String externalKey, final TenantContext tenantContext) throws EntitlementApiException {
// getAllEntitlementsForAccountId should be fast (uses account_record_id)
// getAllEntitlementsForAccount should be fast (uses account_record_id)
return ImmutableList.<Entitlement>copyOf(Iterables.<Entitlement>filter(getAllEntitlementsForAccountId(accountId, tenantContext),
new Predicate<Entitlement>() {
@Override
Expand Down Expand Up @@ -416,7 +416,7 @@ public UUID doCall(final EntitlementApi entitlementApi, final EntitlementContext
throw new EntitlementApiException(new SubscriptionBaseApiException(ErrorCode.SUB_GET_INVALID_BUNDLE_KEY, externalKey));
}

final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), baseSubscription.getStartDate(), contextWithValidAccountRecordId);
final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(updatedPluginContext.getEffectiveDate(), contextWithValidAccountRecordId);
final SubscriptionBaseBundle newBundle = subscriptionBaseTransferApi.transferBundle(sourceAccountId, destAccountId, externalKey, requestedDate, true, cancelImm, context);

// Block all associated subscriptions - TODO Do we want to block the bundle as well (this will add an extra STOP_ENTITLEMENT event in the bundle timeline stream)?
Expand Down
Expand Up @@ -31,7 +31,7 @@ public class DefaultSubscription extends DefaultEntitlement implements Subscript

@Override
public LocalDate getBillingStartDate() {
return internalTenantContext.toLocalDate(getSubscriptionBase().getStartDate(), getSubscriptionBase().getStartDate());
return internalTenantContext.toLocalDate(getSubscriptionBase().getStartDate());
}

@Override
Expand All @@ -51,12 +51,12 @@ public LocalDate getBillingEndDate() {
futureOrCurrentEndDate = futureOrCurrentEndDateForSubscription;
}

return futureOrCurrentEndDate != null ? internalTenantContext.toLocalDate(futureOrCurrentEndDate, getSubscriptionBase().getStartDate()) : null;
return futureOrCurrentEndDate != null ? internalTenantContext.toLocalDate(futureOrCurrentEndDate) : null;
}

@Override
public LocalDate getChargedThroughDate() {
return getSubscriptionBase().getChargedThroughDate() != null ? internalTenantContext.toLocalDate(getSubscriptionBase().getChargedThroughDate(), getSubscriptionBase().getStartDate()) : null;
return getSubscriptionBase().getChargedThroughDate() != null ? internalTenantContext.toLocalDate(getSubscriptionBase().getChargedThroughDate()) : null;
}

@Override
Expand Down
Expand Up @@ -29,10 +29,7 @@
import javax.annotation.Nullable;
import javax.inject.Inject;

import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.killbill.billing.ErrorCode;
import org.killbill.billing.ObjectType;
import org.killbill.billing.OrderingType;
Expand Down Expand Up @@ -133,7 +130,7 @@ public Subscription getSubscriptionForEntitlementId(final UUID entitlementId, fi
try {
final UUID accountId = internalCallContextFactory.getAccountId(entitlementId, ObjectType.SUBSCRIPTION, tenantContext);
final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
accountEntitlements = entitlementInternalApi.getAllEntitlementsForAccountId(accountId, internalTenantContextWithValidAccountRecordId);
accountEntitlements = entitlementInternalApi.getAllEntitlementsForAccount(internalTenantContextWithValidAccountRecordId);
} catch (final EntitlementApiException e) {
throw new SubscriptionApiException(e);
}
Expand Down Expand Up @@ -432,16 +429,14 @@ private List<SubscriptionBundle> getSubscriptionBundlesForAccount(final UUID acc
// Retrieve entitlements
final AccountEntitlements accountEntitlements;
try {
accountEntitlements = entitlementInternalApi.getAllEntitlementsForAccountId(accountId, internalTenantContextWithValidAccountRecordId);
accountEntitlements = entitlementInternalApi.getAllEntitlementsForAccount(internalTenantContextWithValidAccountRecordId);
} catch (final EntitlementApiException e) {
throw new SubscriptionApiException(e);
}

// Build subscriptions
final Map<UUID, List<Subscription>> subscriptionsPerBundle = buildSubscriptionsFromEntitlements(accountEntitlements);

final DateTimeZone accountTimeZone = accountEntitlements.getAccount().getTimeZone();

// Build subscription bundles
final List<SubscriptionBundle> bundles = new LinkedList<SubscriptionBundle>();
for (final UUID bundleId : subscriptionsPerBundle.keySet()) {
Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.util.UUID;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.killbill.billing.callcontext.InternalTenantContext;
import org.killbill.billing.catalog.api.BillingPeriod;
Expand Down Expand Up @@ -52,7 +51,6 @@ public class DefaultSubscriptionEvent implements SubscriptionEvent {
private final PriceList nextPriceList;
private final BillingPeriod nextBillingPeriod;
private final DateTime createdDate;
private final DateTime referenceTime;
private final InternalTenantContext internalTenantContext;

public DefaultSubscriptionEvent(final UUID id,
Expand All @@ -74,7 +72,6 @@ public DefaultSubscriptionEvent(final UUID id,
final PriceList nextPriceList,
final BillingPeriod nextBillingPeriod,
final DateTime createDate,
final DateTime referenceTime,
final InternalTenantContext internalTenantContext) {
this.id = id;
this.entitlementId = entitlementId;
Expand All @@ -96,7 +93,6 @@ public DefaultSubscriptionEvent(final UUID id,
this.nextPriceList = nextPriceList;
this.nextBillingPeriod = nextBillingPeriod;
this.createdDate = createDate;
this.referenceTime = referenceTime;
this.internalTenantContext = internalTenantContext;
}

Expand All @@ -120,7 +116,7 @@ public UUID getEntitlementId() {

@Override
public LocalDate getEffectiveDate() {
return effectiveDate != null ? internalTenantContext.toLocalDate(effectiveDate, referenceTime) : null;
return effectiveDate != null ? internalTenantContext.toLocalDate(effectiveDate) : null;
}

@Override
Expand Down

1 comment on commit 1d94a9d

@sbrossie
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read through it; seemed very mechanical. 👍

Please sign in to comment.