/*
 * 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 javax.sql.DataSource;

public class Tfolder {

    static DataSource dataSource = null;

    /**
     * Entry point
     */
    public static void init() throws NamingException, RemoteException {

	// Get DataSource
	dataSource = DBEnvSL.getDataSource("jdbc_1");

	// create tables
	createTable("folderFileECB", "c_count integer,  c_p1pk varchar(30), c_p2pk varchar(30)");
	createTable("folderPaperECL", "c_value integer");
    createTable("jt2_paper", "c_value integer");
    createTable2("jt2_paper2");
    createTable2("jt2_paper3");
    }

    /**
     * create a table
     */
    private static void createTable(String name, String fields) 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();
	} catch(Exception e) {
	}
	try {
	    stmt = conn.createStatement();
        stmt.execute("create table " + name + "(c_name varchar(30) not null primary key," + fields + ")");
        if(name=="jt2_paper") {
            stmt.execute("insert into "+name+" values('aaaa', 10)");
            stmt.execute("insert into "+name+" values('bbbb', 20)");
        }
	    stmt.close();
	    conn.close();		// release connection
	} catch(Exception e) {
	    System.err.println("Exception in createTable : "+e);
	    throw new RemoteException("Exception in createTable : "+e);
	}
    }

    /**
     * create tables for jt2_paper2 and jt2_paper3
     */
    private static void createTable2(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();
    } catch(Exception e) {
    }
    try {
        stmt = conn.createStatement();
        if(name=="jt2_paper2") {
            stmt.execute("create table " + name + "(c_id varchar(2) not null primary key, c_valeur integer, c_statut varchar(2)) ");
            stmt.execute("insert into "+name+" values('1', 40, '1')");
            stmt.execute("insert into "+name+" values('2', 20, '1')");
            stmt.execute("insert into "+name+" values('3', 10, '2')");

        } else {
            //jt2_paper3
            stmt.execute("create table " + name + "(c_statut3 varchar(2) not null primary key, c_valide varchar(2)) ");
            stmt.execute("insert into "+name+" values('1', '1')");
            stmt.execute("insert into "+name+" values('2', '0')");

        }
        stmt.close();
        conn.close();       // release connection
    } catch(Exception e) {
        System.err.println("Exception in createTable : "+e);
        throw new RemoteException("Exception in createTable : "+e);
    }
    }
}

