package oracle.maf.data.dc;

import oracle.adfmf.java.beans.ProviderChangeListener;
import oracle.adfmf.java.beans.ProviderChangeSupport;

import oracle.maf.data.collections.crud.Customers;
import oracle.maf.data.collections.readonly.CountriesRO;
import oracle.maf.data.entities.Country;
import oracle.maf.data.entities.Customer;
import oracle.maf.errors.ObjectExistsException;

/**
 *
 * @author Frank Nimphius - February, 2015
 *
 */
public class CustomersDC {

    private Customers _customersDataSrc = null;
    private CountriesRO _countriesDataSrc = null;
    private Customer[] customers = null;
    private Country[] countries = null;

    public CustomersDC() {
        super();
    }

    public void setCountries(Country[] countries) {
        this.countries = countries;
    }

    public Country[] getCountries() {
        initializeCountriesDataSource();
        countries = this._countriesDataSrc.getCountries();
        return countries;
    }


    public void setCustomers(Customer[] customers) {
    }

    //handle customers
    public Customer[] getCustomers() {
        initializeCustomersDataSource();
        customers = this._customersDataSrc.getCustomers();
        return customers;
    }


    public void createCustomer(Integer id, String name, String phone, String address, String city, String state,
                               Integer countryId, String zipCode, Integer creditRatingId, Integer salesRepId,
                               String comments) throws Exception {
        initializeCustomersDataSource();
        Customer cust =
            _customersDataSrc.createCustomer(id, name, phone, address, city, state, countryId, zipCode, creditRatingId,
                                             salesRepId, comments);
        providerChangeSupport.fireProviderCreate("customers", id, cust);
    }

    public void createCustomer(Customer cust) throws ObjectExistsException {
        initializeCustomersDataSource();
        _customersDataSrc.createCustomer(cust);
        providerChangeSupport.fireProviderCreate("customers", cust.getId(), cust);
    }

    public void removeCustomer(Integer id) throws Exception {
        initializeCustomersDataSource();
        _customersDataSrc.removeCustomer(id);
        providerChangeSupport.fireProviderDelete("customers", id);
    }

    public void updateCustomer(Customer cust) throws Exception {
        initializeCustomersDataSource();
        _customersDataSrc.updateCustomer(cust);
        providerChangeSupport.fireProviderRefresh("customers");
    }

    private void initializeCountriesDataSource() {
        if (countries == null) {
            _countriesDataSrc = new CountriesRO();
        }
    }

    private void initializeCustomersDataSource() {
        if (customers == null) {
            _customersDataSrc = new Customers();
        }
    }

    //PROVIDER CHANGE

    protected transient ProviderChangeSupport providerChangeSupport = new ProviderChangeSupport(this);

    public void addProviderChangeListener(ProviderChangeListener l) {
        providerChangeSupport.addProviderChangeListener(l);
    }

    public void removeProviderChangeListener(ProviderChangeListener l) {
        providerChangeSupport.removeProviderChangeListener(l);
    }
}
