// Copyright 2016 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 google.registry.model.ofy.ObjectifyService.ofy; import com.google.common.base.Function; import com.google.common.base.Supplier; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Maps; import com.google.common.collect.Maps.EntryTransformer; import com.google.common.collect.Ordering; import com.googlecode.objectify.cmd.Query; import google.registry.model.registrar.Registrar; import google.registry.model.registry.Registries; import google.registry.model.registry.Registry; import google.registry.util.CacheUtils; 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> CURRENCIES_CACHE = CacheUtils.memoizeWithShortExpiration( new Supplier>() { @Override public ImmutableSortedSet get() { return FluentIterable .from(Registries.getTlds()) .transform(new Function() { @Override public CurrencyUnit apply(String tld) { return Registry.get(tld).getCurrency(); }}) .toSortedSet(Ordering.natural()); } }); /** * Returns set of currencies in which registrars may be billed. * *

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 getCurrencies() { return CURRENCIES_CACHE.get(); } /** * Returns query of {@link RegistrarBillingEntry} for each currency, most recent first. * *

Note: Currency map keys are returned in sorted order, from {@link #getCurrencies()}. */ public static ImmutableMap> getBillingEntryQueries( final Registrar registrar) { return Maps.toMap(getCurrencies(), new Function>() { @Override public Query apply(CurrencyUnit currency) { return 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 loadBalance(Registrar registrar) { return Maps.transformEntries(getBillingEntryQueries(registrar), new EntryTransformer, Money>() { @Override public Money transformEntry( CurrencyUnit currency, Query query) { RegistrarBillingEntry entry = query.first().now(); return entry != null ? entry.getBalance() : Money.zero(currency); }}); } private RegistrarBillingUtils() {} }