/*
 * 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/rcycle.
 * (test about legacy CMP 2, cyles in relations, and relation between the same bean).
 */

public class Trcycle 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;
        // jt2_rcycle_person table associated to the bean Person
        String personTname = "JT2_RCYCLE_PERSON";
        // jt2_rcycle_j_par_child join table associated to the relation r-parents-children
        String jointTname = "JT2_RCYCLE_J_PAR_CHILD";
        try {
            stmt = conn.createStatement();
            stmt.execute("DROP TABLE " + personTname);
            logger.log(BasicLevel.INFO, "Table " + personTname + " dropped");
            stmt.execute("DROP TABLE " + jointTname);
            logger.log(BasicLevel.INFO, "Table " + jointTname + " dropped");
            stmt.close();
        } catch (Exception e) {
            logger.log(BasicLevel.DEBUG, "Exception in drop Tables : " + e);
        }
        try {
            // jt2_rcycle_person table associated to the bean Person
            stmt = conn.createStatement();
            stmt.execute("create table " + personTname
                         + "(c_id integer not null primary key,"
                         + " c_name varchar(40),"
                         + " c_sex integer,"
                         + " cfk_spouse integer,"
                         + " cfk_guardian integer)");
            stmt.execute("insert into " + personTname + " values(1, 'Laurent Eric', 1, 2, null)");
            stmt.execute("insert into " + personTname + " values(2, 'Joanin-Laurent Helene', 2, 1, null)");
            stmt.execute("insert into " + personTname + " values(3, 'Laurent Guilhem', 1, null, 1)");
            stmt.execute("insert into " + personTname + " values(4, 'Laurent Malva', 2, null, 1)");
            logger.log(BasicLevel.INFO, "Table " + personTname + " created");
            // jt2_rcycle_j_par_child join table associated to the relation r-parents-children
            stmt.execute("create table " + jointTname
                         + "(cfk_parents integer,"
                         + " cfk_children integer)");
            stmt.execute("insert into " + jointTname  + " values(1, 3)");
            stmt.execute("insert into " + jointTname + " values(1, 4)");
            stmt.execute("insert into " + jointTname + " values(2, 3)");
            stmt.execute("insert into " + jointTname + " values(2, 4)");
            logger.log(BasicLevel.INFO, "Table " + jointTname + " 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);
        }
    }

}


