Make Cancellation OneTime vs Recurring logic explicit

This CL does a very similar cleanup to Cancellation as the diffbase
does to GracePeriod - see []  It simplifes logic where we
used to overload methods to accept eitherw OneTime or Recurring billing
events refs, despite storing them in separate fields in the entity
(since BillingEvent is not a polymorphic superclass, just a Java-only
one, you can't store them as refs to BillingEvent).

That overloading was ultimately only there as a convenience/hack from
when we added Recurring events and didn't want to go back and change
everything.  It obfuscates what's really going on, requires extra
casting/loss of type-safety, and relies on indirect signals (e.g. the
Billing event reason being AUTO_RENEW) to guess what the right billing
event type is.  That latter aspect will likely no longer work in a
monthly billing world, and was brittle anyways (as came up in the
context of removing the AUTO_RENEW reason - see []
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=117165174
This commit is contained in:
nickfelt 2016-03-14 13:39:59 -07:00 committed by Justine Tunney
parent 9786e6732f
commit 7e80b5646f
7 changed files with 143 additions and 54 deletions

View file

@ -201,7 +201,7 @@ public class DomainTransferRequestFlow
.setClientId(existingResource.getCurrentSponsorClientId())
.setEventTime(automaticTransferTime)
.setBillingTime(expirationTime.plus(registry.getAutoRenewGracePeriodLength()))
.setEventRef(existingResource.getAutorenewBillingEvent())
.setRecurringEventRef(existingResource.getAutorenewBillingEvent())
.setParent(historyEntry)
.build();
ofy().save().entity(autorenewCancellation);

View file

@ -376,24 +376,30 @@ public abstract class BillingEvent extends ImmutableObject
GracePeriodStatus.TRANSFER, Reason.TRANSFER);
/**
* Creates a cancellation billing event for the provided grace period parented on the provided
* history entry (and with the same event time) that will cancel out the
* grace-period-originating billing event on the supplied targetId.
* Creates a cancellation billing event (parented on the provided history entry, and with the
* history entry's event time) that will cancel out the provided grace period's billing event,
* using the supplied targetId and deriving other metadata (clientId, billing time, and the
* cancellation reason) from the grace period.
*/
public static BillingEvent.Cancellation forGracePeriod(
GracePeriod gracePeriod, HistoryEntry historyEntry, String targetId) {
return new BillingEvent.Cancellation.Builder()
checkArgument(gracePeriod.hasBillingEvent(),
"Cannot create cancellation for grace period without billing event");
BillingEvent.Cancellation.Builder builder = new BillingEvent.Cancellation.Builder()
.setReason(checkNotNull(GRACE_PERIOD_TO_REASON.get(gracePeriod.getType())))
.setTargetId(targetId)
.setClientId(gracePeriod.getClientId())
.setEventTime(historyEntry.getModificationTime())
// The charge being cancelled will take place at the grace period's expiration time.
.setBillingTime(gracePeriod.getExpirationTime())
.setEventRef(firstNonNull(
gracePeriod.getOneTimeBillingEvent(),
gracePeriod.getRecurringBillingEvent()))
.setParent(historyEntry)
.build();
.setParent(historyEntry);
// Set the grace period's billing event using the appropriate Cancellation builder method.
if (gracePeriod.getOneTimeBillingEvent() != null) {
builder.setOneTimeEventRef(gracePeriod.getOneTimeBillingEvent());
} else if (gracePeriod.getRecurringBillingEvent() != null) {
builder.setRecurringEventRef(gracePeriod.getRecurringBillingEvent());
}
return builder.build();
}
@Override
@ -404,8 +410,6 @@ public abstract class BillingEvent extends ImmutableObject
/** A builder for {@link Cancellation} since it is immutable. */
public static class Builder extends BillingEvent.Builder<Cancellation, Builder> {
private Ref<? extends BillingEvent> refTemp;
public Builder() {}
private Builder(Cancellation instance) {
@ -417,28 +421,23 @@ public abstract class BillingEvent extends ImmutableObject
return this;
}
public Builder setEventRef(Ref<? extends BillingEvent> eventRef) {
refTemp = eventRef;
public Builder setOneTimeEventRef(Ref<BillingEvent.OneTime> eventRef) {
getInstance().refOneTime = eventRef;
return this;
}
public Builder setRecurringEventRef(Ref<BillingEvent.Recurring> eventRef) {
getInstance().refRecurring = eventRef;
return this;
}
@Override
@SuppressWarnings("unchecked")
public Cancellation build() {
Cancellation instance = getInstance();
checkNotNull(instance.billingTime);
checkNotNull(instance.reason);
// If refTemp is set, use it to populate the correct ref.
if (refTemp != null) {
if (Reason.AUTO_RENEW.equals(instance.reason)) {
instance.refRecurring = (Ref<BillingEvent.Recurring>) refTemp;
} else {
instance.refOneTime = (Ref<BillingEvent.OneTime>) refTemp;
}
}
// Ensure that even if refTemp was not set, the builder has exactly one of the two refs
// set to a non-null value (using != as an XOR for booleans).
checkState((instance.refOneTime == null) != (instance.refRecurring == null));
checkState((instance.refOneTime == null) != (instance.refRecurring == null),
"Cancellations must have exactly one billing event ref set");
return super.build();
}
}