/*
 * 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;

/**
 * Tables creation for the test beans/relation/lcp.
 * (test about CMP 2 and relationships).
 */

public class Tlcp extends Tmanager {

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

    /**
     * create tables for the test beans/relation/rcycle (CMP2 legacy)
     * @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 JT2_SIMPLEPARENT");
            logger.log(BasicLevel.INFO, "Table JT2_SIMPLEPARENT dropped");
            stmt.execute("DROP TABLE JT2_SIMPLECHILD");
            logger.log(BasicLevel.INFO, "Table JT2_SIMPLECHILD dropped");
            stmt.close();
        } catch (Exception e) {
            logger.log(BasicLevel.DEBUG, "Exception in drop Tables : " + e);
        }
        try {
            // 
            stmt = conn.createStatement();
            String tname = "JT2_SIMPLEPARENT";
            stmt.execute("create table " + tname
                         + "(SP_PK_ID VARCHAR(32) NOT NULL,"
                         + " SP_DESC VARCHAR(32),"
                         + " PRIMARY KEY (SP_PK_ID))");
            stmt.execute("insert into " + tname + " values('p0', 'p0')");
            stmt.execute("insert into " + tname + " values('p1', 'p1')");
            stmt.execute("insert into " + tname + " values('p2', 'p2')");
            logger.log(BasicLevel.INFO, "Table " + tname + " created");
            // 
            tname = "JT2_SIMPLECHILD";
            stmt.execute("create table " + tname
                         + "(SC_PK_ID VARCHAR(32) NOT NULL,"
                         + "SP_FK_ID VARCHAR(32),"
                         + "SC_DESC VARCHAR(32),"
                         + "PRIMARY KEY (SC_PK_ID))");
            stmt.execute("insert into " + tname  + " values('c0', NULL, 'c0')");
            stmt.execute("insert into " + tname + " values('c1_1', 'p1', 'c1_1')");
            stmt.execute("insert into " + tname + " values('c1_2', 'p1', 'c1_2')");
            stmt.execute("insert into " + tname + " values('c2_1', 'p2', 'c2_1')");
            stmt.execute("insert into " + tname + " values('c2_2', 'p2', 'c2_2')");
            logger.log(BasicLevel.INFO, "Table " + tname + " created");
            stmt.close();
            conn.close();     // release connection
        } catch (Exception e) {
            logger.log(BasicLevel.ERROR, "Exception in create Table : " + e);
            throw new RemoteException("Exception in create Table : " + e);
        }
    }

}


