/*
 * 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 Tebasic extends Tmanager {

    /**
     * Entry point
     */
    public static void init() throws NamingException, RemoteException {
        mgrInit();
        createTableSimple("ebasicSimpleEB");
        createTableSimple("ebasicSimpleEC");
        createTableAccount("ebasicAccountEC");
        createTablePerson("ebasicPersonEC");
        createTablePkAuto("jt2_pkautoobjectec");
    }

    /**
     * create a table for the Simple bean of ebasic
     */
    private static void createTableSimple(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_testname varchar(30) not null primary key,"+
                         "c_info integer, c_numtest integer )");
            stmt.execute("insert into "+name+" values('pk1', 10, 4)");
            stmt.execute("insert into "+name+" values('pk2', 10, 4)");
            stmt.execute("insert into "+name+" values('pk3', 10, 4)");
            stmt.execute("insert into "+name+" values('pk4', 40, 8)");
            stmt.execute("insert into "+name+" values('pk5', 50, 8)");
            stmt.execute("insert into "+name+" values('pk6', 60, 8)");
            stmt.execute("insert into "+name+" values('pk7', 70, 8)");
            stmt.execute("insert into "+name+" values('pk8', 45, 20)");
            stmt.execute("insert into "+name+" values('pk9', 55, 30)");
            stmt.execute("insert into "+name+" values('pk10',80, 80)");
            stmt.execute("insert into "+name+" values('pk11',80, 80)");
            stmt.execute("insert into "+name+" values('pk12',90, 90)");
            stmt.execute("insert into "+name+" values('pk13',100, 90)");
            stmt.execute("insert into "+name+" values('pk14',110, 90)");

            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");
    }

    /**
     * create a table for the Account bean of ebasic
     */
    private static void createTableAccount(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) )");
            stmt.execute("insert into "+name+" values(0, 'to be removed')");
            stmt.execute("insert into "+name+" values(10, 'Eric')");
            stmt.execute("insert into "+name+" values(20, 'Helene')");
            stmt.execute("insert into "+name+" values(30, 'Guilhem')");
            stmt.execute("insert into "+name+" values(40, 'Malva')");

            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");
    }

    /**
     * create a table for the Person bean of ebasic
     */
    private static void createTablePerson(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_name varchar(30) )");
            stmt.execute("insert into "+name+" values(0, 'to be removed')");
            stmt.execute("insert into "+name+" values(10, 'Eric')");
            stmt.execute("insert into "+name+" values(20, 'Helene')");
            stmt.execute("insert into "+name+" values(30, 'Guilhem')");
            stmt.execute("insert into "+name+" values(40, 'Malva')");

            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");
    }

    /**
     * create a table for the Simple bean of ebasic
     */
    private static void createTablePkAuto(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_field1 integer, c_field2 integer, pkautoincrementee integer not null primary key )");

            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");
    }


}
