// Path: src/main/java/org/killbill/billing/plugin/simpletax/config/http/CustomFieldService.java
// public static final String VATIN_CUSTOM_FIELD_NAME = "VATIdNum";
// 
// Path: src/main/java/org/killbill/billing/plugin/simpletax/internal/VATIN.java
// public class VATIN {
// 
//     private static final VATINValidator VALIDATOR = new VATINValidator();
// 
//     private String number;
// 
//     /**
//      * @param number
//      *            A valid VAT Identificaiton Number.
//      * @throws IllegalArgumentException
//      *             When the VAT number is invalid.
//      */
//     @JsonCreator
//     public VATIN(String number) throws IllegalArgumentException {
//         super();
//         checkArgument(VALIDATOR.apply(number), "Illegal VAT Identification Number: [%s]", number);
//         this.number = number;
//     }
// 
//     /**
//      * @return The VAT Identificaiton Number.
//      */
//     @JsonValue
//     public String getNumber() {
//         return number;
//     }
// 
//     @Override
//     public boolean equals(Object obj) {
//         if (obj == null) {
//             return false;
//         }
//         if (obj == this) {
//             return true;
//         }
//         if (obj.getClass() != getClass()) {
//             return false;
//         }
//         VATIN rhs = (VATIN) obj;
//         return new EqualsBuilder().append(number, rhs.number).isEquals();
//     }
// 
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder().append(number).toHashCode();
//     }
// 
//     @Override
//     public String toString() {
//         return new ToStringBuilder(this, SHORT_STYLE)//
//                 .append("number", number)//
//                 .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.VATIN_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.VATIN;
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 VAT Identification Numbers
 * (VATINs).
 *
 * @author Benjamin Gandon
 */
public class VatinController {
    private OSGIKillbillLogService logService;
    private CustomFieldService customFieldService;

    /**
     * Constructs a new controller for the end points related to VAT
     * Identification Numbers (VATINs).
     *
     * @param customFieldService
     *            The service to use when accessing custom fields.
     * @param logService
     *            The Kill Bill log service to use.
     */
    public VatinController(CustomFieldService customFieldService, OSGIKillbillLogService logService) {
        super();
        this.logService = logService;
        this.customFieldService = customFieldService;
    }

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

        List<CustomField> fields;
        if (accountId == null) {
            fields = customFieldService
// Path: src/main/java/org/killbill/billing/plugin/simpletax/config/http/CustomFieldService.java
// public static final String VATIN_CUSTOM_FIELD_NAME = "VATIdNum";
// 
// Path: src/main/java/org/killbill/billing/plugin/simpletax/internal/VATIN.java
// public class VATIN {
// 
//     private static final VATINValidator VALIDATOR = new VATINValidator();
// 
//     private String number;
// 
//     /**
//      * @param number
//      *            A valid VAT Identificaiton Number.
//      * @throws IllegalArgumentException
//      *             When the VAT number is invalid.
//      */
//     @JsonCreator
//     public VATIN(String number) throws IllegalArgumentException {
//         super();
//         checkArgument(VALIDATOR.apply(number), "Illegal VAT Identification Number: [%s]", number);
//         this.number = number;
//     }
// 
//     /**
//      * @return The VAT Identificaiton Number.
//      */
//     @JsonValue
//     public String getNumber() {
//         return number;
//     }
// 
//     @Override
//     public boolean equals(Object obj) {
//         if (obj == null) {
//             return false;
//         }
//         if (obj == this) {
//             return true;
//         }
//         if (obj.getClass() != getClass()) {
//             return false;
//         }
//         VATIN rhs = (VATIN) obj;
//         return new EqualsBuilder().append(number, rhs.number).isEquals();
//     }
// 
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder().append(number).toHashCode();
//     }
// 
//     @Override
//     public String toString() {
//         return new ToStringBuilder(this, SHORT_STYLE)//
//                 .append("number", number)//
//                 .toString();
//     }
// }


// Path: src/main/java/org/killbill/billing/plugin/simpletax/config/http/VatinController.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.VATIN_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.VATIN;
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 VAT Identification Numbers
 * (VATINs).
 *
 * @author Benjamin Gandon
 */
public class VatinController {
    private OSGIKillbillLogService logService;
    private CustomFieldService customFieldService;

    /**
     * Constructs a new controller for the end points related to VAT
     * Identification Numbers (VATINs).
     *
     * @param customFieldService
     *            The service to use when accessing custom fields.
     * @param logService
     *            The Kill Bill log service to use.
     */
    public VatinController(CustomFieldService customFieldService, OSGIKillbillLogService logService) {
        super();
        this.logService = logService;
        this.customFieldService = customFieldService;
    }

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

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