//DBBean.java

package applis.ejb.perfs;


import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;


public class DBBean implements SessionBean {

    SessionContext     sc         = null;
    private DataSource dataSource = null;
    Connection         conn       = null;
    Statement          stmt;
    PreparedStatement  pStmt;


    void dropTable(String name) throws RemoteException{
        try {
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.execute("DROP TABLE "+name);
            stmt.close();
        } catch(Exception e) {
            System.err.println("Exception in dropTable : "+e);
            //throw new RemoteException("Exception in dropTable : "+e);
        }
    }
	    
    /**
     * used in CMP2 only (table is created by JORM)
     */
    public void initTable(int x, boolean irc) throws RemoteException {
        PerfsHome home = null;
        Context initialContext = null;
        String beanName = irc ? "Perfs3Home_" + x : "Perfs2Home_" + x;
        Object obj = null;
        try {
            initialContext = new InitialContext();
            obj = initialContext.lookup(beanName);
            home = (PerfsHome) PortableRemoteObject.narrow(obj, PerfsHome.class);
        } catch (Exception e) {
            System.err.println("DBBean inittable: Cannot lookup "+beanName+": "+e);
            System.err.println("JNDI Object Class = "+obj.getClass().getName());
            return;
        }

        // Create 16 entities
        try {
            for (int n = 1; n <= 16; n++) {
                String s = null;
                String value = "";
                switch (n) {
                case 1:
                    s = "AAAAAAAAAA";
                    break;
                case 2:
                    s = "BBBBBBBBBB";
                    break;
                case 3:
                    s = "CCCCCCCCCC";
                    break;
                case 4:
                    s = "DDDDDDDDDD";
                    break;
                case 5:
                    s = "EEEEEEEEEE";
                    break;
                case 6:
                    s = "FFFFFFFFFF";
                    break;
                case 7:
                    s = "GGGGGGGGGG";
                    break;
                case 8:
                    s = "HHHHHHHHHH";
                    break;
                case 9:
                    s = "IIIIIIIIII";
                    break;
                case 10:
                    s = "JJJJJJJJJJ";
                    break;
                case 11:
                    s = "KKKKKKKKKK";
                    break;
                case 12:
                    s = "LLLLLLLLLL";
                    break;
                case 13:
                    s = "MMMMMMMMMM";
                    break;
                case 14:
                    s = "NNNNNNNNNN";
                    break;
                case 15:
                    s = "OOOOOOOOOO";
                    break;
                case 16:
                    s = "PPPPPPPPPP";
                    break;
                }
                for (int i = 0; i < 5*n; i++) {
                    value = value.concat(s);
                }
                home.create(n, value);
            }
        } catch (Exception e) {
            System.err.println("Exception in initTable : "+e);
            throw new RemoteException("Exception in initTable : "+e);
        }
    }

    /**
     * used in CMP1 only
     */
    public void createTable(String name) throws RemoteException {
        // Drop the Table
        dropTable(name);
        // Create the table
        // Insert rows
        try{
            stmt = conn.createStatement();
            stmt.execute("CREATE   TABLE "+name+ "(" +
                         "v_code     INTEGER NOT NULL PRIMARY KEY,"+
                         "v_value  VARCHAR(1024) )");
            stmt.execute("INSERT INTO "+name+" VALUES(1, 'A')");
            stmt.execute("INSERT INTO "+name+" VALUES(2, 'B')");
            stmt.execute("INSERT INTO "+name+" VALUES(3, 'C')");
            stmt.execute("INSERT INTO "+name+" VALUES(4, 'D')");
            stmt.execute("INSERT INTO "+name+" VALUES(5, 'E')");
            stmt.execute("INSERT INTO "+name+" VALUES(6, 'F')");
            stmt.execute("INSERT INTO "+name+" VALUES(7, 'G')");
            stmt.execute("INSERT INTO "+name+" VALUES(8, 'H')");
            stmt.execute("INSERT INTO "+name+" VALUES(9, 'I')");
            stmt.execute("INSERT INTO "+name+" VALUES(10, 'J')");
            stmt.execute("INSERT INTO "+name+" VALUES(11, 'K')");
            stmt.execute("INSERT INTO "+name+" VALUES(12, 'L')");
            stmt.execute("INSERT INTO "+name+" VALUES(13, 'M')");
            stmt.execute("INSERT INTO "+name+" VALUES(14, 'N')");
            stmt.execute("INSERT INTO "+name+" VALUES(15, 'O')");
            stmt.execute("INSERT INTO "+name+" VALUES(16, 'P')");
            stmt.close();
        } catch (Exception e) {
            System.err.println("Exception in createTable (1er part) : "+e);
            throw new RemoteException("Exception in createTable (1er part): "+e);
        }

        // Init rows with good values
        try {
            for (int n=1; n<=16; n++) {
                String s;
                String value = "";
                if (n==1) {
                    s = "AAAAAAAAAA";
                } else if (n==2) {
                    s = "BBBBBBBBBB";
                } else if (n==3) {
                    s = "CCCCCCCCCC";
                } else if (n==4) {
                    s = "DDDDDDDDDD";
                } else if (n==5) {
                    s = "EEEEEEEEEE";
                } else if (n==6) {
                    s = "FFFFFFFFFF";
                } else if (n==7) {
                    s = "GGGGGGGGGG";
                } else if (n==8) {
                    s = "HHHHHHHHHH";
                } else if (n==9) {
                    s = "IIIIIIIIII";
                } else if (n==10) {
                    s = "JJJJJJJJJJ";
                } else if (n==11) {
                    s = "KKKKKKKKKK";
                } else if (n==12) {
                    s = "LLLLLLLLLL";
                } else if (n==13) {
                    s = "MMMMMMMMMM";
                } else if (n==14) {
                    s = "NNNNNNNNNN";
                } else if (n==15) {
                    s = "OOOOOOOOOO";
                } else if (n==16) {
                    s = "PPPPPPPPPP";
                } else {
                    s = "..........";
                }
                for (int i=0; i<5*n; i++) {
                    value = value.concat(s);
                }
                pStmt = conn.prepareStatement("update "+name+" set v_value=? where v_code=?");
                pStmt.setString(1, value);
                pStmt.setInt(2, n);
                pStmt.executeUpdate();
                pStmt.close();
            }
            conn.close();
        } catch (Exception e) {
            System.err.println("Exception in createTable (2nd part): "+e);
            throw new RemoteException("Exception in createTable (2nd part): "+e);
        }
        System.out.println("Table "+name+" created");
    }

    public void ejbCreate() {
    } 

    public void ejbRemove() {
    }

    public void ejbPassivate() {
    }

    public void ejbActivate() {
    }
    
    public void setSessionContext(SessionContext ctx) {
        sc = ctx;
        // Finds DataSource from JNDI
        if (dataSource == null) {
            try {
                InitialContext ictx = new InitialContext();
                dataSource = (DataSource) PortableRemoteObject.narrow(ictx.lookup("java:comp/env/jdbc/perfs"), DataSource.class);
            } catch (Exception e) {
                System.err.println("cannot lookup java:comp/env/jdbc/perfs");
                throw new EJBException(e);
            }
        }
    }		
}
