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

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

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

public abstract class ReservationBean implements javax.ejb.EntityBean {

    private SequenceSessionLocalHome seqHome = null;

    private SequenceSessionLocal seqLocal = null;

    private CustomerHomeLocal customerhl = null;

    private CruiseHomeLocal cruisehl = null;

    private CabinHomeLocal cabinhl = null;

    public Object ejbCreate(Integer cruiseId, Collection customers) throws CreateException {
        int id = seqLocal.getNextNumberInSequence("Reservation");
        setId(new Integer(id));
        return null;
    }

    public void ejbPostCreate(Integer cruiseId, Collection customers) {

        try {
            CruiseLocal cruise = cruisehl.findByPrimaryKey(cruiseId);
            setCruise(cruise);
        } catch (FinderException e) {
            System.out.println("Cruise not find :" + cruiseId);
        }

        Collection myCustomers = this.getCustomers();

        ArrayList al;
        if (customers == null)
            al = new ArrayList();
        else {
            if (customers.size() == -1)
                al = new ArrayList();
            else {
                al = new ArrayList(customers.size());
                for (Iterator it = customers.iterator(); it.hasNext();) {
                    Integer customerid = new Integer("-1");
                    try {
                        customerid = (Integer) it.next();
                        CustomerLocal customer = customerhl.findByPrimaryKey(customerid);
                        al.add(customer);
                    } catch (FinderException e) {
                        System.out.println("Customer not found " + customerid);
                    }
                }
            }
        }
        myCustomers.addAll(al);
    }

    // relationship fields
    public void setAllCabins(Set cabins) {

        ArrayList al;
        if (cabins == null)
            al = new ArrayList();
        else {
            if (cabins.size() == -1)
                al = new ArrayList();
            else {
                al = new ArrayList(cabins.size());
                for (Iterator it = cabins.iterator(); it.hasNext();) {
                    Integer cabinid = new Integer("-1");
                    try {
                        cabinid = (Integer) it.next();
                        CabinLocal cabin = cabinhl.findByPrimaryKey(cabinid);
                        al.add(cabin);
                    } catch (FinderException e) {
                        System.out.println("Cabin not found " + cabinid);
                    }
                }
            }
        }
        this.getCabins().addAll(al);
    }

    // persistent fields getter and setter
    public abstract CruiseLocal getCruise();

    public abstract void setCruise(CruiseLocal cruise);

    public abstract Set getCabins();

    public abstract void setCabins(Set cabins);

    public abstract Set getCustomers();

    public abstract void setCustomers(Set customers);

    public abstract Integer getId();

    public abstract void setId(Integer id);

    public abstract Date getDate();

    public abstract void setDate(Date date);

    public abstract double getAmountPaid();

    public abstract void setAmountPaid(double amount);

    // abstract ejbSelect() methods
    public abstract long ejbSelectCountOfReservations() throws FinderException;

    public abstract double ejbSelectMinAmountOfReservations() throws FinderException;

    public abstract double ejbSelectMinAmountForCruise(String cName) throws FinderException;

    public abstract Collection ejbSelectAmountsForCruise(String cName) throws FinderException;

    public abstract double ejbSelectMaxAmountOfReservations() throws FinderException;

    public abstract long ejbSelectCountOfReservationsForCustomer(CustomerLocal c) throws FinderException;

    public abstract long ejbSelectAmountOfReservationsForCustomer(CustomerLocal c) throws FinderException;

    // Public Home method required to test the private ejbSelectXXXX method
    public long ejbHomeGetCountOfReservations() throws FinderException {
        return this.ejbSelectCountOfReservations();
    }

    public double ejbHomeGetMinAmountOfReservations() throws FinderException {
        return this.ejbSelectMinAmountOfReservations();
    }

    public double ejbHomeGetMinAmountForCruise(String cName) throws FinderException {
        return this.ejbSelectMinAmountForCruise(cName);
    }

    public double ejbHomeGetMaxAmountOfReservations() throws FinderException {
        return this.ejbSelectMaxAmountOfReservations();
    }

    public Collection ejbHomeGetAmountsForCruise(String cName) throws FinderException {
        return this.ejbSelectAmountsForCruise(cName);
    }

    public long ejbHomeGetCountOfReservationsForCustomer(Integer custId) throws FinderException {
        return this.ejbSelectCountOfReservationsForCustomer(customerhl.findByPrimaryKey(custId));
    }

    public double ejbHomeGetAmountOfReservationsForCustomer(Integer custId) throws FinderException {
        return this.ejbSelectAmountOfReservationsForCustomer(customerhl.findByPrimaryKey(custId));
    }

    // standard call back methods
    public void setEntityContext(EntityContext ec) {
        try {
            InitialContext cntx = new InitialContext();
            seqHome = (SequenceSessionLocalHome) cntx.lookup("java:comp/env/ejb/SequenceSessionLocalHome");
            customerhl = (CustomerHomeLocal) cntx.lookup("java:comp/env/ejb/CustomerLocalHome");
            cruisehl = (CruiseHomeLocal) cntx.lookup("java:comp/env/ejb/CruiseLocalHome");
            cabinhl = (CabinHomeLocal) cntx.lookup("java:comp/env/ejb/CabinLocalHome");
            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 {
    }

}