package oracle.byop.maf.data.collections.crud;

import java.util.ArrayList;

import oracle.byop.maf.data.entities.Customer;
import oracle.byop.maf.errors.ObjectExistsException;

/**
 *
 * Title:   Bring Your Own Project (BYOP): Hackathon
 * Schema:  ADF Summit
 * Type:    Service Object with hard-coded sample data
 *
 *
 * @author Frank Nimphius - August, 2014
 *  */

public class Customers {
    
    private ArrayList customers = new ArrayList();
    
    public Customers() {
        super();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
       customers.add(_createCustomer(new Integer(201),"Unisports","55-2066101","72 Via Bahia","Sao Paulo","", new Integer(1),"",new Integer(1), new Integer(12), "Customer usually orders large amounts and has a high order total.  This is okay as long as the credit rating remains excellent."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
       customers.add(_createCustomer(new Integer(202),"Simms Athletics","81-20101","Takashi","Osaka","", new Integer(2),"",new Integer(4), new Integer(14), "Customer should always pay by cash until his credit rating improves."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
       customers.add(_createCustomer(new Integer(203),"Delhi Sports","91-10351","Chanakya","New Delhi","", new Integer(3),"",new Integer(2), new Integer(14), "Customer specializes in baseball equipment and is the largest retailer in India."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
       customers.add(_createCustomer(new Integer(204),"Womansport","1-206-104-0103","281 King Street","Seattle","WA", new Integer(4),"98101",new Integer(3), new Integer(11), ""));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
       customers.add(_createCustomer(new Integer(205),"Kam's Sporting Goods","852-3692888","95 Queensway","Hong Kong","", new Integer(5),"",new Integer(1), new Integer(15), ""));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
       customers.add(_createCustomer(new Integer(206),"Sportique","33-2257201","4 Rue Chabaud","Cannes","", new Integer(6),"",new Integer(1), new Integer(15), "Customer specializes in Soccer.  Likes to order accessories in bright colors."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
       customers.add(_createCustomer(new Integer(208),"Muench Sports","49-527454","Marktplatz","Stuttgart","", new Integer(8),"",new Integer(3), new Integer(15), "Customer usually pays small orders by cash and large orders on credit."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
       customers.add(_createCustomer(new Integer(209),"Beisbol Si!","809-352689","","San Pedro de Macon's","", new Integer(9),"",new Integer(1), new Integer(11), ""));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
       customers.add(_createCustomer(new Integer(210),"Futbol Sonora","52-404562","256 W Sage Street","Nogales","", new Integer(10),"",new Integer(1), new Integer(12), "Customer is difficult to reach by phone.  Try mail."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
       customers.add(_createCustomer(new Integer(211),"Kuhn's Sports","42-111292","7 Modrany","Prague","", new Integer(11),"",new Integer(2), new Integer(15), ""));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
       customers.add(_createCustomer(new Integer(212),"Hamada Sport","20-1209211","","Alexandria","", new Integer(12),"",new Integer(4), new Integer(13), "Customer orders sea and water equipment."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
       customers.add(_createCustomer(new Integer(213),"Big John's Sports Emporium","1-415-555-6281","4783 18th Street","San Francisco","CA", new Integer(4),"94117",new Integer(1), new Integer(11), "Customer has a dependable credit record."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
       customers.add(_createCustomer(new Integer(214),"Ojibway Retail","1-716-555-7171","415 Main Street","Buffalo","NY", new Integer(4),"14202",new Integer(3), new Integer(11), ""));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
       customers.add(_createCustomer(new Integer(215),"Sporta Russia","7-3892456","100 Nevskiy prospekt","Saint Petersburg","", new Integer(13),"",new Integer(4), new Integer(15), "This customer is very friendly, but has difficulty paying bills.  Insist upon cash."));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
       customers.add(_createCustomer(new Integer(1),"ABC Company","","514 W Superior St","Kokomo","IN", new Integer(4),"46901",new Integer(1), new Integer(11), ""));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
   }
    
    //create credit rating
    private Customer _createCustomer(Integer id, String name, String phone, String address, 
                    String city, String state, Integer countryId, String zipCode, 
                    Integer creditRatingId, Integer salesRepId, String comments) {  
        
        return new Customer(id, name, phone, address, city, state, countryId, zipCode, creditRatingId, salesRepId, comments);
    }



    public Customer 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 {
        
            for (int i = 0; i < customers.size(); i++) {
              if(((Customer)customers.get(i)).getId().compareTo(id) == 0){
                 throw new ObjectExistsException("Customer with this ID exists");
              }
           }
        
           Customer cust = this._createCustomer(id,name, phone, address, city, state, countryId, zipCode, creditRatingId, salesRepId, comments);
           customers.add(cust);
           return cust;
            
        }
        
        
        public Customer createCustomer(Customer cust) throws ObjectExistsException {
            for (int i = 0; i < customers.size(); i++) {
              if(((Customer)customers.get(i)).getId().compareTo(cust.getId()) == 0){
                 throw new ObjectExistsException("Customer with this ID exists");
              }
            }
            customers.add(cust);
            return cust;
        }
        
        
        public void removeCustomer(Integer id) throws Exception {
        
            for (int i = 0; i < customers.size(); i++) {
              if(((Customer)customers.get(i)).getId().compareTo(id) == 0){
                 customers.remove(i);
                 break;
              }
           }        
        }
        
        public void updateCustomer(Customer cust) throws Exception {
        
            for (int i = 0; i < customers.size(); i++) {
              if(((Customer)customers.get(i)).getId().compareTo(cust.getId()) == 0){
                  
                  customers.remove(i);
                  customers.add(i,cust);
                  break;
              }
           }
        }

    public void setCustomers(Customer[] customer) { }

    public Customer[] getCustomers() {
        return (Customer[]) customers.toArray(new Customer[customers.size()]);
    }
    
    public Customer[] getCustomersByCountryId(Integer countryId) {
        
        ArrayList custs = new ArrayList();
        
        for (int i = 0; i < customers.size(); i++) {
            if(((Customer)customers.get(i)).getCountryId().compareTo(countryId) == 0){
                custs.add((Customer)customers.get(i));    
            }
       }

        return (Customer[]) custs.toArray(new Customer[custs.size()]);
    }
    
}
