// Path: src/main/java/org/killbill/billing/plugin/simpletax/config/http/CustomFieldService.java
// public static final String TAX_COUNTRY_CUSTOM_FIELD_NAME = "taxCountry";
// 
// Path: src/main/java/org/killbill/billing/plugin/simpletax/internal/Country.java
// public class Country {
// 
//     private static final Supplier<Set<String>> COUNTRIES = new ConcurrentLazyValue<Set<String>>() {
//         @Override
//         protected Set<String> initialize() {
//             return ImmutableSet.copyOf(getISOCountries());
//         }
//     };
// 
//     private String code;
// 
//     /**
//      * Constructs a new country. The country code must be an element of
//      * {@link Locale#getISOCountries()}.
//      *
//      * @param code
//      *            The ISO 3166-1 alpha-2 country code.
//      * @throws IllegalArgumentException
//      *             when the country code is not an element of
//      *             {@link Locale#getISOCountries()}.
//      */
//     @JsonCreator
//     public Country(String code) throws IllegalArgumentException {
//         super();
//         checkArgument(COUNTRIES.get().contains(code), "Illegal country code: [%s]", code);
//         this.code = code;
//     }
// 
//     /**
//      * Returns the ISO 3166-1 alpha-2 code for this country.
//      *
//      * @return The code of this country. Never {@code null}.
//      */
//     @JsonValue
//     public String getCode() {
//         return code;
//     }
// 
//     /**
//      * Computes the name of this country, in the specified language, or in
//      * English if the language is not {@linkplain Locale#getAvailableLocales()
//      * supported}.
//      *
//      * @param language
//      *            The preferred language in which the country name should be
//      *            expressed.
//      * @return The name of the country in the specified language, or in English.
//      */
//     public String computeName(Locale language) {
//         return new Locale("", code).getDisplayCountry(language);
//     }
// 
//     @Override
//     public boolean equals(Object obj) {
//         if (obj == null) {
//             return false;
//         }
//         if (obj == this) {
//             return true;
//         }
//         if (obj.getClass() != getClass()) {
//             return false;
//         }
//         Country rhs = (Country) obj;
//         return new EqualsBuilder().append(code, rhs.code).isEquals();
//     }
// 
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder().append(code).toHashCode();
//     }
// 
//     @Override
//     public String toString() {
//         return new ToStringBuilder(this, SHORT_STYLE)//
//                 .append("code", code)//
//                 .toString();
//     }
// }


import org.killbill.billing.util.customfield.CustomField;
import org.killbill.killbill.osgi.libs.killbill.OSGIKillbillLogService;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import static com.google.common.collect.Lists.newArrayList;
import static org.killbill.billing.plugin.simpletax.config.http.CustomFieldService.TAX_COUNTRY_CUSTOM_FIELD_NAME;
import static org.osgi.service.log.LogService.LOG_ERROR;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.killbill.billing.plugin.api.PluginTenantContext;
import org.killbill.billing.plugin.simpletax.internal.Country;
import org.killbill.billing.tenant.api.Tenant;
import org.killbill.billing.util.callcontext.TenantContext;
/*
 * Copyright 2015 Benjamin Gandon
 *
 * 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 org.killbill.billing.plugin.simpletax.config.http;






/**
 * A controller that serves the end points related to Tax Countries.
 *
 * @author Benjamin Gandon
 */
public class TaxCountryController {
    private OSGIKillbillLogService logService;
    private CustomFieldService customFieldService;

    /**
     * Constructs a new controller for tax country end points.
     *
     * @param customFieldService
     *            The service to use when accessing custom fields.
     * @param logService
     *            The Kill Bill log service to use.
     */
    public TaxCountryController(CustomFieldService customFieldService, OSGIKillbillLogService logService) {
        super();
        this.logService = logService;
        this.customFieldService = customFieldService;
    }

    /**
     * Lists JSON resources for tax countries taking any restrictions into
     * account.
     *
     * @param accountId
     *            Any account on which the tax countries should be restricted.
     *            Might be {@code null}.
     * @param tenant
     *            The tenant on which to operate.
     * @return A list of {@linkplain TaxCountryRsc account tax countries
     *         resources}. Never {@code null}.
     */
    // TODO: return a List<TaxCountryRsc>
    public Object listTaxCountries(@Nullable UUID accountId, Tenant tenant) {
        TenantContext tenantContext = new PluginTenantContext(tenant.getId());

        List<CustomField> fields;
        if (accountId == null) {
// Path: src/main/java/org/killbill/billing/plugin/simpletax/config/http/CustomFieldService.java
// public static final String TAX_COUNTRY_CUSTOM_FIELD_NAME = "taxCountry";
// 
// Path: src/main/java/org/killbill/billing/plugin/simpletax/internal/Country.java
// public class Country {
// 
//     private static final Supplier<Set<String>> COUNTRIES = new ConcurrentLazyValue<Set<String>>() {
//         @Override
//         protected Set<String> initialize() {
//             return ImmutableSet.copyOf(getISOCountries());
//         }
//     };
// 
//     private String code;
// 
//     /**
//      * Constructs a new country. The country code must be an element of
//      * {@link Locale#getISOCountries()}.
//      *
//      * @param code
//      *            The ISO 3166-1 alpha-2 country code.
//      * @throws IllegalArgumentException
//      *             when the country code is not an element of
//      *             {@link Locale#getISOCountries()}.
//      */
//     @JsonCreator
//     public Country(String code) throws IllegalArgumentException {
//         super();
//         checkArgument(COUNTRIES.get().contains(code), "Illegal country code: [%s]", code);
//         this.code = code;
//     }
// 
//     /**
//      * Returns the ISO 3166-1 alpha-2 code for this country.
//      *
//      * @return The code of this country. Never {@code null}.
//      */
//     @JsonValue
//     public String getCode() {
//         return code;
//     }
// 
//     /**
//      * Computes the name of this country, in the specified language, or in
//      * English if the language is not {@linkplain Locale#getAvailableLocales()
//      * supported}.
//      *
//      * @param language
//      *            The preferred language in which the country name should be
//      *            expressed.
//      * @return The name of the country in the specified language, or in English.
//      */
//     public String computeName(Locale language) {
//         return new Locale("", code).getDisplayCountry(language);
//     }
// 
//     @Override
//     public boolean equals(Object obj) {
//         if (obj == null) {
//             return false;
//         }
//         if (obj == this) {
//             return true;
//         }
//         if (obj.getClass() != getClass()) {
//             return false;
//         }
//         Country rhs = (Country) obj;
//         return new EqualsBuilder().append(code, rhs.code).isEquals();
//     }
// 
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder().append(code).toHashCode();
//     }
// 
//     @Override
//     public String toString() {
//         return new ToStringBuilder(this, SHORT_STYLE)//
//                 .append("code", code)//
//                 .toString();
//     }
// }


// Path: src/main/java/org/killbill/billing/plugin/simpletax/config/http/TaxCountryController.java
import org.killbill.billing.util.customfield.CustomField;
import org.killbill.killbill.osgi.libs.killbill.OSGIKillbillLogService;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import static com.google.common.collect.Lists.newArrayList;
import static org.killbill.billing.plugin.simpletax.config.http.CustomFieldService.TAX_COUNTRY_CUSTOM_FIELD_NAME;
import static org.osgi.service.log.LogService.LOG_ERROR;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.killbill.billing.plugin.api.PluginTenantContext;
import org.killbill.billing.plugin.simpletax.internal.Country;
import org.killbill.billing.tenant.api.Tenant;
import org.killbill.billing.util.callcontext.TenantContext;

/*
 * Copyright 2015 Benjamin Gandon
 *
 * 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 org.killbill.billing.plugin.simpletax.config.http;






/**
 * A controller that serves the end points related to Tax Countries.
 *
 * @author Benjamin Gandon
 */
public class TaxCountryController {
    private OSGIKillbillLogService logService;
    private CustomFieldService customFieldService;

    /**
     * Constructs a new controller for tax country end points.
     *
     * @param customFieldService
     *            The service to use when accessing custom fields.
     * @param logService
     *            The Kill Bill log service to use.
     */
    public TaxCountryController(CustomFieldService customFieldService, OSGIKillbillLogService logService) {
        super();
        this.logService = logService;
        this.customFieldService = customFieldService;
    }

    /**
     * Lists JSON resources for tax countries taking any restrictions into
     * account.
     *
     * @param accountId
     *            Any account on which the tax countries should be restricted.
     *            Might be {@code null}.
     * @param tenant
     *            The tenant on which to operate.
     * @return A list of {@linkplain TaxCountryRsc account tax countries
     *         resources}. Never {@code null}.
     */
    // TODO: return a List<TaxCountryRsc>
    public Object listTaxCountries(@Nullable UUID accountId, Tenant tenant) {
        TenantContext tenantContext = new PluginTenantContext(tenant.getId());

        List<CustomField> fields;
        if (accountId == null) {
            fields = customFieldService.findAllAccountFieldsByFieldNameAndTenant(TAX_COUNTRY_CUSTOM_FIELD_NAME,