package org.objectweb.jonas.jtests.beans.ejbql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.naming.InitialContext;


public abstract class AddressBean implements javax.ejb.EntityBean {		
    
    private SequenceSessionLocalHome seqHome = null;
    private SequenceSessionLocal  seqLocal = null;

    public Integer ejbCreateAddress(String street, String city, String state,  String zip )
    throws javax.ejb.CreateException {
	setStreet(street);
	setCity(city);
	setState(state);
	setZip(zip);
        int id = seqLocal.getNextNumberInSequence("Address");
	setId(new Integer(id));
	return null;
    }

    public void ejbPostCreateAddress (String street, String city,String state,  String zip){
    }

    // persistent fields
    public abstract Integer getId();
    public abstract void setId(Integer id);
    public abstract String getStreet();
    public abstract void setStreet(String street);
    public abstract String getCity();
    public abstract void setCity(String city);
    public abstract String getState();
    public abstract void setState(String state);
    public abstract String getZip();
    public abstract void setZip(String zip);

    // abstract ejbSelect() methods
    public abstract long ejbSelectCountOfCities() throws FinderException;
    public abstract long ejbSelectCountOfDistinctCities() throws FinderException;
    public abstract Collection ejbSelectCities() throws FinderException;
    public abstract Collection ejbSelectCreditCompanies() throws FinderException;
    public abstract Collection ejbSelectCreditCompanyNames() throws FinderException;
    public abstract Collection ejbSelectCreditCompanyIds() throws FinderException;
    public abstract Collection ejbSelectCreditCompanyNums() throws FinderException;

	// Public Home method required to test the private ejbSelectXXXX method
	public long ejbHomeGetCountOfCities() throws FinderException {
		return this.ejbSelectCountOfCities();
	}
	public long ejbHomeGetCountOfDistinctCities() throws FinderException {
		return this.ejbSelectCountOfDistinctCities();
	}
	public Collection ejbHomeGetAllCities() throws FinderException {
		return this.ejbSelectCities();
	}
	public Collection ejbHomeGetAllCreditCompanies() throws FinderException {
		Collection ccs = this.ejbSelectCreditCompanies();
        Iterator iCCs = ccs.iterator();
        ArrayList ccsId = new ArrayList();
        while (iCCs.hasNext()) {
            CreditCompanyLocal cclb = (CreditCompanyLocal) iCCs.next();
            if (cclb != null) {
                ccsId.add(cclb.getId());
            }
        }
        return ccsId;
	}
	public Collection ejbHomeGetAllCreditCompanyNames() throws FinderException {
        return this.ejbSelectCreditCompanyNames();
	}
	public Collection ejbHomeGetAllCreditCompanyIds() throws FinderException {
        return this.ejbSelectCreditCompanyIds();
	}
	public Collection ejbHomeGetAllCreditCompanyNums() throws FinderException {
        return this.ejbSelectCreditCompanyNums();
	}

    // standard call back methods    
    public void setEntityContext(EntityContext ec){
	try {
	    InitialContext cntx = new InitialContext( );
	    SequenceSessionLocalHome  seqHome  = 
		    (SequenceSessionLocalHome)cntx.lookup("java:comp/env/ejb/SequenceSessionLocalHome");
	    seqLocal = seqHome.create();
	} catch(Exception e) {
	    throw new javax.ejb.EJBException(e);
	}
	    
    }

    public void unsetEntityContext(){}
    public void ejbLoad(){}
    public void ejbStore(){}
    public void ejbActivate(){}
    public void ejbPassivate(){}
    public void ejbRemove() throws javax.ejb.RemoveException{}

}
