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

import java.util.Collection;

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

public abstract class CruiseBean implements javax.ejb.EntityBean {

    private SequenceSessionLocalHome seqHome = null;
    private SequenceSessionLocal  seqLocal = null;
    private ShipHomeLocal  shipHome = null;

    public Integer ejbCreate(String name,Integer ship)  throws CreateException {
	setName(name);
	int id = seqLocal.getNextNumberInSequence("Cruise");
	setId(new Integer(id));
 	return null;
    }
	
    public void ejbPostCreate(String name, Integer ship) {
        try {
           ShipLocal shiploc = shipHome.findByPrimaryKey(ship);
	   setShip(shiploc);
	} catch (FinderException e) {
	    System.out.println("Ship not found during Cruise creation :"+ship);
	}
    }

    // persistent fields

    public abstract void setId(Integer id);
    public abstract Integer getId();
    public abstract void setName(String name);
    public abstract String getName( );

    public abstract void setShip(ShipLocal ship);
    public abstract ShipLocal getShip( );

    // relationship fields

    public abstract void setReservations(Collection res);
    public abstract Collection getReservations( );

    // standard call back methods
    
    public void setEntityContext(EntityContext ec){
	try {
	    InitialContext cntx = new InitialContext( );
	    SequenceSessionLocalHome  seqHome  = 
		(SequenceSessionLocalHome)cntx.lookup("java:comp/env/ejb/SequenceSessionLocalHome");
	    shipHome  = 
		(ShipHomeLocal)cntx.lookup("java:comp/env/ejb/ShipLocalHome");

	    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 RemoveException  {}

}
