/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2004 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;

/**
 * Tables creation for the test beans/relation/tier.
 * (test about legacy CMP 2and EJBQL).
 */

public class Ttier extends Tmanager {
    private static final String T_TIER = "JT2_TIERS";
    private static final String T_TIERMOR = "JT2_TIEMOR";
    private static final String T_TIERPHY = "JT2_TIEPHY";

    /**
     * Entry point
     * @throws NamingException 
     * @throws RemoteException 
     */
    public static void init() throws NamingException, RemoteException {
        mgrInit();
        createTables();
    }

    /**
     * create tables for the test beans/relation/tier
     * @throws RemoteException
     */
    private static void createTables() 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 " + T_TIER);
            logger.log(BasicLevel.INFO, "Table " + T_TIER + " dropped");
            stmt.execute("DROP TABLE " + T_TIERMOR);
            logger.log(BasicLevel.INFO, "Table " + T_TIERMOR + " dropped");
            stmt.execute("DROP TABLE " + T_TIERPHY);
            logger.log(BasicLevel.INFO, "Table " + T_TIERPHY + " dropped");
            stmt.close();
        } catch (Exception e) {
            logger.log(BasicLevel.DEBUG, "Exception in drop Tables : " + e);
        }
        try {
            // T_TIER table associated to the bean Tier
            stmt = conn.createStatement();
            stmt.execute("create table " + T_TIER
                         + "(NUMINTTIE INTEGER NOT NULL,"
                         + "EMAILTIE VARCHAR(40), "
                         + "PRIMARY KEY (NUMINTTIE))");
            stmt.execute("insert into " + T_TIER + " values(1, 'admin@bull.net')");
            stmt.execute("insert into " + T_TIER + " values(2, 'admin@objectweb.org')");
            stmt.execute("insert into " + T_TIER + " values(100, 'helene.joanin@bull.net')");
            stmt.execute("insert into " + T_TIER + " values(101, 'adriana.danes@bull.net')");
            logger.log(BasicLevel.INFO, "Table " + T_TIER + " created");
            // T_TIERMOR table associated to the bean Tiemor
            stmt = conn.createStatement();
            stmt.execute("create table " + T_TIERMOR
                         + "(NUMINTTIE INTEGER NOT NULL,"
                         + "RAISOCTIE VARCHAR(40), "
                         + "PRIMARY KEY (NUMINTTIE))");
            stmt.execute("insert into " + T_TIERMOR + " values(1, 'bull')");
            stmt.execute("insert into " + T_TIERMOR + " values(2, 'objectweb')");
            logger.log(BasicLevel.INFO, "Table " + T_TIERMOR + " created");
            // T_TIERPHY table associated to the bean Tiephy
            stmt = conn.createStatement();
            stmt.execute("create table " + T_TIERPHY
                         + "(NUMINTTIE INTEGER NOT NULL,"
                         + "NOMUSATIE VARCHAR(40), "
                         + "PRIMARY KEY (NUMINTTIE))");
            stmt.execute("insert into " + T_TIERPHY + " values(100, 'helene joanin')");
            stmt.execute("insert into " + T_TIERPHY + " values(101, 'adriana danes')");
            logger.log(BasicLevel.INFO, "Table " + T_TIERPHY + " created");
            stmt.close();
            conn.close();     // release connection
        } catch (Exception e) {
            logger.log(BasicLevel.ERROR, "Exception in create/init Table : " + e);
            throw new RemoteException("Exception in create/init Table : " + e);
        }
    }

}


