/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.tables;

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

import javax.naming.NamingException;

import org.objectweb.util.monolog.api.BasicLevel;

public class Tbeanexc extends Tmanager {

    /**
     * Entry point
     */
    public static void init() throws NamingException, RemoteException {
	mgrInit();
	createTable("beanexcAccountEB");
	createTable("beanexcAccountEC");
    }

    /**
     * create a table for CMP 1.x
     */
    private static void createTable(String name) throws RemoteException {

	// get connection
	Connection conn = null;
	try {
	    conn = dataSource.getConnection();
	} catch(Exception e) {
	    throw new RemoteException("Cannot get Connection");
	}
	
	Statement stmt;
	try {
	    stmt = conn.createStatement();
	    stmt.execute("DROP TABLE "+name);
	    stmt.close();
	    logger.log(BasicLevel.INFO, "Table "+name+" dropped");
	} catch(Exception e) {
	    logger.log(BasicLevel.DEBUG, "Exception in dropTable : \n"+e);
	}
	try {
	    stmt = conn.createStatement();
	    stmt.execute("create table " + name + 
			 "(c_number integer not null primary key,"+
			 "c_customer varchar(30), c_balance integer )");
	    stmt.execute("insert into "+name+" values(101,'Antoine de St Exupery',900)");
	    stmt.execute("insert into "+name+" values(102,'Alexandre Dumas fils',500)");
	    stmt.execute("insert into "+name+" values(103,'Conan Doyle',800)");
	    stmt.execute("insert into "+name+" values(104,'Alfred de Musset',140)");
	    stmt.execute("insert into "+name+" values(105,'Phileas Lebegue',235)");
	    stmt.execute("insert into "+name+" values(81, 'Philippe Coq',20)");
	    stmt.execute("insert into "+name+" values(82, 'Philippe Durieux',20)");
	    stmt.execute("insert into "+name+" values(83, 'Helene Joanin',20)");
	    stmt.execute("insert into "+name+" values(84, 'Adriana Danes',20)");
	    stmt.execute("insert into "+name+" values(85, 'Francois Exertier',20)");
	    stmt.execute("insert into "+name+" values(86, 'John Doo', 20)");
	    stmt.execute("insert into "+name+" values(87, 'Jim', 20)");
	    // Those following values are used to test ejbRemove() throwing RemoveException
	    stmt.execute("insert into "+name+" values(999990, 'For exception in ejbRemove', 20)");
	    stmt.execute("insert into "+name+" values(999991, 'For exception in ejbRemove', 20)");
	    stmt.execute("insert into "+name+" values(999992, 'For exception in ejbRemove', 20)");
	    stmt.execute("insert into "+name+" values(999993, 'For exception in ejbRemove', 20)");
	    stmt.close();
	    conn.close();		// release connection
	} catch(Exception e) {
	    logger.log(BasicLevel.ERROR, "Exception in createTable : "+e);
	    throw new RemoteException("Exception in createTable : "+e);
	}
	logger.log(BasicLevel.INFO, "Table "+name+" created");
    }

}


