/*
 * 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.PreparedStatement;
import java.sql.Statement;
import java.sql.Timestamp;

import javax.naming.NamingException;

import org.objectweb.jonas.jtests.util.Env;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Class to create tables for the 'osqlts' bean of 'etype'.
 * @author Helene Joanin 
 */
public class Tosqlts extends Tmanager {

    /**
     * Entry point
     */
    public static void init() throws NamingException, RemoteException {
        mgrInit();
        createTable("JT_EtypeOsqltsEC");
    }

    /**
     * create a table for the osqlts bean of etype
     */
    private static void createTable(String name) throws RemoteException {

        // get connection
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch(Exception e) {
            throw new RemoteException("Cannot get Connection");
        }
	
        Statement stmt;
        PreparedStatement pStmt;
        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 {
            int dbType = Env.getDatabaseType(conn);
            String cTypeName;
            switch (dbType) {
            case Env.DB_ORACLE: cTypeName = "date"; break;
            default: cTypeName = "timestamp";	// standard jdbc type name
            }
            stmt = conn.createStatement();
            stmt.execute("create table " + name + 
                         "( c_pk varchar(30) not null primary key, c_f1 "+cTypeName+")");
            stmt.close();
            final long ONE_DAY = 24L*60L*60L*1000L;
            pStmt = conn.prepareStatement("insert into "+name+" values('pk1', ?)");
            pStmt.setTimestamp(1, new Timestamp(ONE_DAY));
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pk2', ?)");
            pStmt.setTimestamp(1, new Timestamp(2*ONE_DAY));
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pk3', ?)");
            pStmt.setTimestamp(1, new Timestamp(3*ONE_DAY));
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pk4', ?)");
            pStmt.setTimestamp(1, new Timestamp(4*ONE_DAY));
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pk5', ?)");
            pStmt.setTimestamp(1, new Timestamp(5*ONE_DAY));
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pk5bis', ?)");
            pStmt.setTimestamp(1, new Timestamp(5*ONE_DAY));
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pktoremove', ?)");
            pStmt.setTimestamp(1, new Timestamp(12*ONE_DAY));
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pknull', NULL)");
            pStmt.executeUpdate();
            pStmt.close();
            pStmt = conn.prepareStatement("insert into "+name+" values('pkchangenull', ?)");
            pStmt.setTimestamp(1, new Timestamp(12*ONE_DAY));
            pStmt.executeUpdate();
            pStmt.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");
    }

}
