mirror of
https://github.com/google/nomulus.git
synced 2025-07-23 19:20:44 +02:00
Delete remnants of registrar-level credits
We never fully used this stuff but definitely no longer use it following our recent billing refactor. It's confusing to retain all of these entities and commands given that none of them are actually used by anything. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=201978094
This commit is contained in:
parent
4cddbdacff
commit
6706b99828
24 changed files with 1 additions and 2545 deletions
|
@ -16,9 +16,6 @@ package google.registry.model;
|
|||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.model.billing.BillingEvent;
|
||||
import google.registry.model.billing.RegistrarBillingEntry;
|
||||
import google.registry.model.billing.RegistrarCredit;
|
||||
import google.registry.model.billing.RegistrarCreditBalance;
|
||||
import google.registry.model.common.Cursor;
|
||||
import google.registry.model.common.EntityGroupRoot;
|
||||
import google.registry.model.common.GaeUserIdConverter;
|
||||
|
@ -103,10 +100,7 @@ public final class EntityClasses {
|
|||
PremiumList.PremiumListRevision.class,
|
||||
RdeRevision.class,
|
||||
Registrar.class,
|
||||
RegistrarBillingEntry.class,
|
||||
RegistrarContact.class,
|
||||
RegistrarCredit.class,
|
||||
RegistrarCreditBalance.class,
|
||||
Registry.class,
|
||||
ReservedList.class,
|
||||
ServerSecret.class,
|
||||
|
|
|
@ -1,206 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.model.billing;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static com.google.common.base.Strings.emptyToNull;
|
||||
import static com.google.common.base.Verify.verifyNotNull;
|
||||
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.annotation.Entity;
|
||||
import com.googlecode.objectify.annotation.Id;
|
||||
import com.googlecode.objectify.annotation.Index;
|
||||
import com.googlecode.objectify.annotation.Parent;
|
||||
import google.registry.model.Buildable;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.model.JsonMapBuilder;
|
||||
import google.registry.model.Jsonifiable;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.money.Money;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* Log of monthly invoices and payments for a Registrar customer.
|
||||
*
|
||||
* <p>This is a one-off single-entry bookkeeping system. There is a separate account for each
|
||||
* (registrar, currency) pair.
|
||||
*
|
||||
* <p>You should never update these entities once they've been inserted into Datastore. If you need
|
||||
* to change something, add a correction entry.
|
||||
*/
|
||||
@Entity
|
||||
public class RegistrarBillingEntry extends ImmutableObject implements Jsonifiable {
|
||||
|
||||
@Parent
|
||||
Key<Registrar> parent;
|
||||
|
||||
/** Arbitrary unique identifier. */
|
||||
@Id
|
||||
long id;
|
||||
|
||||
/**
|
||||
* External transaction identifier or {@code null} if this is an invoice entry.
|
||||
*
|
||||
* <p>This is the ID or token that the payment gateway gives us, which represents the transaction
|
||||
* in their database.
|
||||
*/
|
||||
@Nullable
|
||||
String transactionId;
|
||||
|
||||
/**
|
||||
* Time at which this entry was created.
|
||||
*
|
||||
* <p>This value is unique and monotonic for a given ({@link #parent}, {@link #currency}) pair.
|
||||
*/
|
||||
@Index
|
||||
DateTime created;
|
||||
|
||||
/** Completely arbitrary description of payment. */
|
||||
String description;
|
||||
|
||||
/**
|
||||
* Currency of transaction.
|
||||
*
|
||||
* <p>This field is identical to {@code amount.getCurrencyUnit()} and is only here so it can be
|
||||
* indexed in Datastore.
|
||||
*/
|
||||
@Index
|
||||
CurrencyUnit currency;
|
||||
|
||||
/**
|
||||
* Amount and currency of invoice or payment.
|
||||
*
|
||||
* <p>This field is positive for debits (e.g. monthly invoice entries) and negative for credits
|
||||
* (e.g. credit card payment transaction entries.)
|
||||
*/
|
||||
Money amount;
|
||||
|
||||
/**
|
||||
* Balance of account for this currency.
|
||||
*
|
||||
* <p>This is {@code amount + previous.balance}.
|
||||
*/
|
||||
Money balance;
|
||||
|
||||
public Key<Registrar> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getTransactionId() {
|
||||
return transactionId;
|
||||
}
|
||||
|
||||
public DateTime getCreated() {
|
||||
return verifyNotNull(created, "created missing: %s", this);
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return verifyNotNull(description, "description missing: %s", this);
|
||||
}
|
||||
|
||||
public CurrencyUnit getCurrency() {
|
||||
return verifyNotNull(currency, "currency missing: %s", this);
|
||||
}
|
||||
|
||||
public Money getAmount() {
|
||||
return verifyNotNull(amount, "amount missing: %s", this);
|
||||
}
|
||||
|
||||
public Money getBalance() {
|
||||
return verifyNotNull(balance, "balance missing: %s", this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toJsonMap() {
|
||||
return new JsonMapBuilder()
|
||||
.put("id", id)
|
||||
.put("transactionId", getTransactionId())
|
||||
.putString("created", getCreated())
|
||||
.put("description", getDescription())
|
||||
.putString("currency", getCurrency())
|
||||
.putString("amount", getAmount().getAmount())
|
||||
.putString("balance", getBalance().getAmount())
|
||||
.build();
|
||||
}
|
||||
|
||||
/** A builder for constructing a {@link RegistrarBillingEntry}, since it's immutable. */
|
||||
public static class Builder extends Buildable.Builder<RegistrarBillingEntry> {
|
||||
|
||||
@Nullable
|
||||
private RegistrarBillingEntry previous;
|
||||
|
||||
public Builder setParent(Registrar parent) {
|
||||
getInstance().parent = Key.create(parent);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCreated(DateTime created) {
|
||||
getInstance().created = created;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setPrevious(@Nullable RegistrarBillingEntry previous) {
|
||||
this.previous = previous;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTransactionId(@Nullable String transactionId) {
|
||||
getInstance().transactionId = transactionId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDescription(String description) {
|
||||
getInstance().description = checkNotNull(emptyToNull(description));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setAmount(Money amount) {
|
||||
checkArgument(!amount.isZero(), "Amount can't be zero");
|
||||
getInstance().amount = amount;
|
||||
getInstance().currency = amount.getCurrencyUnit();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegistrarBillingEntry build() {
|
||||
checkNotNull(getInstance().parent, "parent");
|
||||
checkNotNull(getInstance().created, "created");
|
||||
checkNotNull(getInstance().description, "description");
|
||||
checkNotNull(getInstance().amount, "amount");
|
||||
if (previous == null) {
|
||||
getInstance().balance = getInstance().amount;
|
||||
} else {
|
||||
getInstance().balance = previous.balance.plus(getInstance().amount);
|
||||
checkState(getInstance().parent.equals(previous.parent),
|
||||
"Parent not same as previous:\nNew: %s\nPrevious: %s",
|
||||
getInstance(), previous);
|
||||
checkState(getInstance().created.isAfter(previous.created),
|
||||
"Created timestamp not after previous:\nNew: %s\nPrevious: %s",
|
||||
getInstance(), previous);
|
||||
}
|
||||
return cloneEmptyToNull(super.build());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.model.billing;
|
||||
|
||||
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSortedSet;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.googlecode.objectify.cmd.Query;
|
||||
import google.registry.model.CacheUtils;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.model.registry.Registries;
|
||||
import google.registry.model.registry.Registry;
|
||||
import java.util.Map;
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.money.Money;
|
||||
|
||||
/** Utilities for managing the billing of {@link Registrar} customers. */
|
||||
public final class RegistrarBillingUtils {
|
||||
|
||||
private static final Supplier<ImmutableSortedSet<CurrencyUnit>> CURRENCIES_CACHE =
|
||||
CacheUtils.memoizeWithShortExpiration(
|
||||
() ->
|
||||
Registries.getTlds()
|
||||
.stream()
|
||||
.map((String tld) -> Registry.get(tld).getCurrency())
|
||||
.collect(toImmutableSortedSet(Ordering.natural())));
|
||||
|
||||
/**
|
||||
* Returns set of currencies in which registrars may be billed.
|
||||
*
|
||||
* <p>Each TLD has a currency associated with it. We don't do conversions. The registrar customer
|
||||
* gets a separate bill for each currency.
|
||||
*/
|
||||
public static ImmutableSortedSet<CurrencyUnit> getCurrencies() {
|
||||
return CURRENCIES_CACHE.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns query of {@link RegistrarBillingEntry} for each currency, most recent first.
|
||||
*
|
||||
* <p><b>Note:</b> Currency map keys are returned in sorted order, from {@link #getCurrencies()}.
|
||||
*/
|
||||
public static ImmutableMap<CurrencyUnit, Query<RegistrarBillingEntry>> getBillingEntryQueries(
|
||||
final Registrar registrar) {
|
||||
return Maps.toMap(
|
||||
getCurrencies(),
|
||||
(CurrencyUnit currency) ->
|
||||
ofy()
|
||||
.load()
|
||||
.type(RegistrarBillingEntry.class)
|
||||
.ancestor(registrar)
|
||||
.filter("currency", currency)
|
||||
.order("-created"));
|
||||
}
|
||||
|
||||
/** Returns amount of money registrar currently owes registry in each currency. */
|
||||
public static Map<CurrencyUnit, Money> loadBalance(Registrar registrar) {
|
||||
return Maps.transformEntries(
|
||||
getBillingEntryQueries(registrar),
|
||||
(CurrencyUnit currency, Query<RegistrarBillingEntry> query) -> {
|
||||
RegistrarBillingEntry entry = query.first().now();
|
||||
return entry != null ? entry.getBalance() : Money.zero(currency);
|
||||
});
|
||||
}
|
||||
|
||||
private RegistrarBillingUtils() {}
|
||||
}
|
|
@ -1,224 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.model.billing;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.collect.ImmutableList.toImmutableList;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.model.registry.Registries.assertTldExists;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.google.common.collect.Streams;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.annotation.Entity;
|
||||
import com.googlecode.objectify.annotation.Id;
|
||||
import com.googlecode.objectify.annotation.Parent;
|
||||
import google.registry.model.Buildable;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.model.annotations.ReportedOn;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.model.registry.Registry;
|
||||
import java.util.Optional;
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* A per-registrar billing credit, applied toward future charges for registrar activity.
|
||||
*
|
||||
* <p>NOTE: While credits are tracked within the model, there is no built-in support for actually
|
||||
* incorporating these credits into the generated billing data, and the model support for credits
|
||||
* should be considered quasi-deprecated (it may be removed without notice).
|
||||
*/
|
||||
@ReportedOn
|
||||
@Entity
|
||||
public final class RegistrarCredit extends ImmutableObject implements Buildable {
|
||||
|
||||
/**
|
||||
* The type of credit represented. The ordering below determines the order in which credits of
|
||||
* of different types will be applied to an invoice charge.
|
||||
*/
|
||||
// Note: Right now the ordering is actually maintained manually via a hard-coded table in the
|
||||
// relevant billing query, so if adding a credit type here, add it there as well.
|
||||
// TODO(b/19031546): make the query automatically reflect the order in this enum.
|
||||
public enum CreditType {
|
||||
/** Credit awarded as an incentive to participate in sunrise/landrush auctions. */
|
||||
AUCTION("Auction Credit"),
|
||||
|
||||
/** Credit awarded as part of a promotional deal. */
|
||||
PROMOTION("Promotional Credit");
|
||||
|
||||
/** A descriptive name for a credit of this type. */
|
||||
private final String descriptiveName;
|
||||
|
||||
CreditType(String descriptiveName) {
|
||||
this.descriptiveName = descriptiveName;
|
||||
}
|
||||
|
||||
public String getDescriptiveName() {
|
||||
return descriptiveName;
|
||||
}
|
||||
}
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
/** The registrar to whom this credit belongs. */
|
||||
@Parent
|
||||
Key<Registrar> parent;
|
||||
|
||||
/** The type of credit. */
|
||||
CreditType type;
|
||||
|
||||
/**
|
||||
* The time that this credit was created. If a registrar has multiple credits of a given type,
|
||||
* the older credits will be applied first.
|
||||
*/
|
||||
DateTime creationTime;
|
||||
|
||||
/** The currency in which the balance for this credit is stored. */
|
||||
CurrencyUnit currency;
|
||||
|
||||
/** The line item description to use when displaying this credit on an invoice. */
|
||||
String description;
|
||||
|
||||
/**
|
||||
* The TLD in which this credit applies.
|
||||
*
|
||||
* <p>For auction credits, this is also the TLD for which the relevant auctions occurred.
|
||||
*/
|
||||
String tld;
|
||||
|
||||
public Key<Registrar> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public CreditType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public DateTime getCreationTime() {
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
public CurrencyUnit getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getTld() {
|
||||
return tld;
|
||||
}
|
||||
|
||||
/** Returns a string representation of this credit. */
|
||||
public String getSummary() {
|
||||
String fields = Joiner.on(' ').join(type, creationTime, tld);
|
||||
return String.format("%s (%s/%d) - %s", description, parent.getName(), id, fields);
|
||||
}
|
||||
|
||||
/** Returns the default description for this {@link RegistrarCredit} instance. */
|
||||
private String getDefaultDescription() {
|
||||
return type.getDescriptiveName() + " for ." + tld;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder asBuilder() {
|
||||
return new Builder(clone(this));
|
||||
}
|
||||
|
||||
/** A Builder for {@link RegistrarCredit}. */
|
||||
public static class Builder extends Buildable.Builder<RegistrarCredit> {
|
||||
public Builder() {}
|
||||
|
||||
public Builder(RegistrarCredit instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
public Builder setParent(Registrar parent) {
|
||||
getInstance().parent = Key.create(parent);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setType(CreditType type) {
|
||||
getInstance().type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCreationTime(DateTime creationTime) {
|
||||
getInstance().creationTime = creationTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCurrency(CurrencyUnit currency) {
|
||||
getInstance().currency = currency;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDescription(String description) {
|
||||
getInstance().description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTld(String tld) {
|
||||
getInstance().tld = tld;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegistrarCredit build() {
|
||||
RegistrarCredit instance = getInstance();
|
||||
checkNotNull(instance.parent, "parent credit");
|
||||
checkNotNull(instance.type, "type");
|
||||
checkNotNull(instance.creationTime, "creationTime");
|
||||
checkNotNull(instance.currency, "currency");
|
||||
assertTldExists(checkNotNull(instance.tld, "tld"));
|
||||
checkArgument(
|
||||
Registry.get(instance.tld).getCurrency().equals(instance.currency),
|
||||
"Credits must be in the currency of the assigned TLD");
|
||||
instance.description =
|
||||
Optional.ofNullable(instance.description).orElse(instance.getDefaultDescription());
|
||||
return super.build();
|
||||
}
|
||||
}
|
||||
|
||||
/** Ordering that sorts credits first by type and then by creation time. */
|
||||
private static final Ordering<RegistrarCredit> CREDIT_PRIORITY_ORDERING =
|
||||
new Ordering<RegistrarCredit>() {
|
||||
@Override
|
||||
public int compare(RegistrarCredit left, RegistrarCredit right) {
|
||||
return ComparisonChain.start()
|
||||
.compare(left.type, right.type)
|
||||
.compare(left.creationTime, right.creationTime)
|
||||
.result();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Loads all RegistrarCredit entities for the given Registrar.
|
||||
*
|
||||
* <p>The resulting list sorts the credits first by type and then by creation time.
|
||||
*/
|
||||
public static ImmutableList<RegistrarCredit> loadAllForRegistrar(Registrar registrar) {
|
||||
return Streams.stream(ofy().load().type(RegistrarCredit.class).ancestor(registrar))
|
||||
.sorted(CREDIT_PRIORITY_ORDERING)
|
||||
.collect(toImmutableList());
|
||||
}
|
||||
}
|
|
@ -1,251 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.model.billing;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.ForwardingNavigableMap;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.annotation.Entity;
|
||||
import com.googlecode.objectify.annotation.Id;
|
||||
import com.googlecode.objectify.annotation.Parent;
|
||||
import google.registry.model.Buildable;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.model.annotations.ReportedOn;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.money.Money;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* The balance of a {@link RegistrarCredit} at a given point in time.
|
||||
*
|
||||
* <p>A credit balance has two related times in addition to the monetary amount: the effective time,
|
||||
* which represents the time at which the amount becomes the actual credit balance; and the
|
||||
* written time, which represents the time at which this balance object was saved.
|
||||
*
|
||||
* <p>The active balance of a credit object before (at) any given point in time T can be found by
|
||||
* taking the balance object with the latest effective time that is before (before or at) T, and
|
||||
* breaking any ties by choosing the mostly recently written among those balances.
|
||||
*
|
||||
* <p>NOTE: While credits are tracked within the model, there is no built-in support for actually
|
||||
* incorporating these credits into the generated billing data, and the model support for credits
|
||||
* should be considered quasi-deprecated (it may be removed without notice).
|
||||
*/
|
||||
@ReportedOn
|
||||
@Entity
|
||||
public final class RegistrarCreditBalance extends ImmutableObject implements Buildable {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
/** The registrar credit object for which this represents a balance. */
|
||||
@Parent
|
||||
Key<RegistrarCredit> parent;
|
||||
|
||||
/** The time at which this balance amount should become effective. */
|
||||
DateTime effectiveTime;
|
||||
|
||||
/**
|
||||
* The time at which this balance update was written.
|
||||
*
|
||||
* <p>Used to break ties in cases where there are multiple balances with the same effective time,
|
||||
* as the last written balance will take priority.
|
||||
*/
|
||||
DateTime writtenTime;
|
||||
|
||||
/** The monetary amount of credit balance remaining as of the effective time. */
|
||||
Money amount;
|
||||
|
||||
public Key<RegistrarCredit> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public DateTime getEffectiveTime() {
|
||||
return effectiveTime;
|
||||
}
|
||||
|
||||
public DateTime getWrittenTime() {
|
||||
return writtenTime;
|
||||
}
|
||||
|
||||
public Money getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder asBuilder() {
|
||||
return new Builder(clone(this));
|
||||
}
|
||||
|
||||
/** A Builder for an {@link RegistrarCreditBalance}. */
|
||||
public static class Builder extends Buildable.Builder<RegistrarCreditBalance> {
|
||||
|
||||
private CurrencyUnit currency;
|
||||
|
||||
public Builder() {}
|
||||
|
||||
public Builder(RegistrarCreditBalance instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
public RegistrarCreditBalance.Builder setParent(RegistrarCredit parent) {
|
||||
this.currency = parent.getCurrency();
|
||||
getInstance().parent = Key.create(parent);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegistrarCreditBalance.Builder setEffectiveTime(DateTime effectiveTime) {
|
||||
getInstance().effectiveTime = effectiveTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegistrarCreditBalance.Builder setWrittenTime(DateTime writtenTime) {
|
||||
getInstance().writtenTime = writtenTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RegistrarCreditBalance.Builder setAmount(Money amount) {
|
||||
checkArgument(amount.isPositiveOrZero(), "Credit balance amount cannot be negative");
|
||||
getInstance().amount = amount;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegistrarCreditBalance build() {
|
||||
RegistrarCreditBalance instance = getInstance();
|
||||
checkNotNull(instance.parent);
|
||||
checkNotNull(instance.effectiveTime);
|
||||
checkNotNull(instance.writtenTime);
|
||||
checkNotNull(instance.amount);
|
||||
checkState(
|
||||
instance.amount.getCurrencyUnit().equals(currency),
|
||||
"Currency of balance amount differs from credit currency (%s vs %s)",
|
||||
instance.amount.getCurrencyUnit(),
|
||||
currency);
|
||||
return super.build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A map of maps representing the historical credit balance information for a given credit.
|
||||
*
|
||||
* <p>Specifically, this class provides a high-level view of the balances for a given credit
|
||||
* by in essence grouping them first by effective time and then by written time. This facilitates
|
||||
* the printing of a readable representation of a credit's balance history, and the retrieval of
|
||||
* the active balance at a given time (as described above on RegistrarCreditBalance).
|
||||
*/
|
||||
public static class BalanceMap
|
||||
extends ForwardingNavigableMap<DateTime, ImmutableSortedMap<DateTime, Money>> {
|
||||
|
||||
/**
|
||||
* Constructs a BalanceMap for the given registrar credit by loading all RegistrarCreditBalance
|
||||
* entities for the credit and then inserting them into a map of maps keyed first by effective
|
||||
* time and then by written time with the balance amount as the value.
|
||||
*/
|
||||
public static BalanceMap createForCredit(RegistrarCredit registrarCredit) {
|
||||
// Build up the data in a mutable map of maps.
|
||||
Map<DateTime, Map<DateTime, Money>> map = new HashMap<>();
|
||||
for (RegistrarCreditBalance balance :
|
||||
ofy().load().type(RegistrarCreditBalance.class).ancestor(registrarCredit)) {
|
||||
// Create the submap at this key if it doesn't exist already.
|
||||
Map<DateTime, Money> submap =
|
||||
Optional.ofNullable(map.get(balance.effectiveTime)).orElse(new HashMap<>());
|
||||
submap.put(balance.writtenTime, balance.amount);
|
||||
map.put(balance.effectiveTime, submap);
|
||||
}
|
||||
// Wrap the mutable map of maps in an immutable BalanceMap.
|
||||
return new BalanceMap(map);
|
||||
}
|
||||
|
||||
/** The immutable map of maps used as the backing map. */
|
||||
private final ImmutableSortedMap<DateTime, ImmutableSortedMap<DateTime, Money>> delegate;
|
||||
|
||||
/**
|
||||
* Constructs an immutable BalanceMap from balance data provided as a map of maps.
|
||||
*
|
||||
* <p>The constructed BalanceMap delegates to an immutable copy of the provided map of maps.
|
||||
* This copy is created by first making a view of the map in which each submap is replaced by
|
||||
* an immutable copy, and then making an immutable copy of that view.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
BalanceMap(Map<DateTime, ? extends Map<DateTime, Money>> data) {
|
||||
delegate =
|
||||
ImmutableSortedMap.copyOf(
|
||||
Maps.transformValues(
|
||||
data,
|
||||
(Map<DateTime, Money> map) -> ImmutableSortedMap.copyOf(map, Ordering.natural())),
|
||||
Ordering.natural());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ImmutableSortedMap<DateTime, ImmutableSortedMap<DateTime, Money>> delegate() {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most recently written balance for the effective time corresponding to this entry,
|
||||
* or {@link Optional#absent()} if this entry is null.
|
||||
*/
|
||||
private Optional<Money> getMostRecentlyWrittenBalance(
|
||||
Map.Entry<DateTime, ImmutableSortedMap<DateTime, Money>> balancesAtEffectiveTime) {
|
||||
return balancesAtEffectiveTime == null
|
||||
? Optional.empty()
|
||||
// Don't use Optional.ofNullable() here since it's an error if there's a empty submap.
|
||||
: Optional.of(balancesAtEffectiveTime.getValue().lastEntry().getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the active balance at a given time as described above on RegistrarCreditBalance, or
|
||||
* {@link Optional#absent()} if no balance was active at that time (i.e. the time provided is
|
||||
* before the first effectiveTime of any balance for the credit this BalanceMap represents).
|
||||
*/
|
||||
public Optional<Money> getActiveBalanceAtTime(DateTime time) {
|
||||
return getMostRecentlyWrittenBalance(delegate.floorEntry(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the active balance before a given time as described above on RegistrarCreditBalance,
|
||||
* or {@link Optional#absent()} if no balance was active before that time (i.e. the time
|
||||
* provided is before or at the first effectiveTime of any balance for the credit).
|
||||
*/
|
||||
public Optional<Money> getActiveBalanceBeforeTime(DateTime time) {
|
||||
return getMostRecentlyWrittenBalance(delegate.lowerEntry(time));
|
||||
}
|
||||
|
||||
/** Returns a string representation of this BalanceMap's data. */
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Map.Entry<DateTime, ? extends Map<DateTime, Money>> entry : delegate.entrySet()) {
|
||||
builder.append(String.format(" - %s\n", entry.getKey()));
|
||||
for (Map.Entry<DateTime, Money> subEntry : entry.getValue().entrySet()) {
|
||||
builder.append(
|
||||
String.format(" - %s - %s\n", subEntry.getKey(), subEntry.getValue()));
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue