package oracle.byop.maf.data.dc;

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

import oracle.byop.maf.data.collections.crud.Customers;
import oracle.byop.maf.data.entities.Customer;
import oracle.byop.maf.errors.ObjectExistsException;

/**
 *
 * Title:   Bring Your Own Project (BYOP): Hackathon
 * Schema:  ADF Summit
 * Type:    REST service
 *
 * This POJO class directs requests to the Customers class that it creates an instance of. The idea of this
 * POJO is to hide implementation details and to help reducing the number of data controls. The latter is 
 * considered a best practice in MAF
 *
 * @author Frank Nimphius - August, 2014
 *
 */
public class CustomersDC {
    
    private Customers _customersDataSrc = null; 
    private Customer[] customers= null;


    public CustomersDC() {
        super();
    }
    

    public void setCustomers(Customer[] customers) {}

    //handle customers
    public Customer[] getCustomers() {        
        initializeCustomersDataSource();
        return _customersDataSrc.getCustomers();
    }
  
    
    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 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);
    }
}
