/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)rsSchema.java	1.17 02/01/12
 */
package com.sun.cts.tests.jdbc.ee.common;

import java.io.*;
import java.util.*;
import java.sql.*;

import com.sun.cts.util.*;
import com.sun.cts.harness.*;

import java.rmi.RemoteException;


/**
 * The rsSchema class creates the database and tables using 
 * Sun's J2EE Reference Implementation.
 * @author  
 * @version 1.7, 06/16/99
 */

public class rsSchema extends ServiceEETest  
{
	private Properties props = null;
	private Statement stmt = null ;
			  
	public void createTab(String sTableName, Properties sqlProps, Connection conn) throws RemoteException 
	{  
		String execString = null;
		String sKeyName = null;
		String binarySize=null;
		String varbinarySize=null;
		String createString=null,createString1=null,createString2=null;
		
		
		TestUtil.logTrace("createTab");
		// drop table if it exists
		try
		{
			props=sqlProps;
			dropTab(sTableName,conn);
			TestUtil.logTrace("deleted rows from table " + sTableName);
		} 
		catch (Exception e) 
		{
			TestUtil.logTrace("Exception encountered deleting rows from  table: " + sTableName + ": " + e.getMessage());
		}

		try
		{
			
			stmt = conn.createStatement();
			if((sTableName.startsWith("Binary_Tab")))
			{
				binarySize = props.getProperty("binarySize");
				logMsg("Binary Table Size : " + binarySize);

				String insertString = props.getProperty("Binary_Tab_Insert");
				logMsg("Insert String "+insertString);

				logMsg("Adding rows to the table");
				stmt.executeUpdate(insertString);
				logMsg("Successfully inserted the row");
			
			}
			else if((sTableName.startsWith("Varbinary_Tab")))
			{
				varbinarySize = props.getProperty("varbinarySize");
				logMsg("Varbinary Table Size : " + varbinarySize);
				String insertString = props.getProperty("Varbinary_Tab_Insert");
				logMsg("Insert String "+insertString);
	
				logMsg("Adding rows to the table");
				stmt.executeUpdate(insertString);
				logMsg("Successfully inserted the row");
				
			}				
			else
			{

				// Add  the prescribed table rows
				
				TestUtil.logTrace("Adding rows to the table" + sTableName);
	
				sKeyName = sTableName.concat("_Insert");
	
				TestUtil.logTrace("sKeyName :"+sKeyName);
				execString = sqlProps.getProperty(sKeyName);
				stmt.executeUpdate(execString);
				logMsg("Rows added to the table " + sTableName);
				//stmt.close();
			}
			
				
				
		}
		catch (SQLException e) 
		{ 
			TestUtil.logErr("SQLException creating the Table" + sTableName);
			TestUtil.logErr(e.getMessage());
			dropTab(sTableName, conn);
			throw new RemoteException( e.getMessage() );
		}      
		catch  ( Exception e ) 
		{
			logErr("Setup Failed!");
			System.exit(1);
		}
		finally
		{
			if(stmt != null)
			{
				try
				{
					stmt.close();	
				}
				catch(SQLException sqle)
				{
					throw new RemoteException( sqle.getMessage() );
				}											
			}					
		}			
			
	}
	public void dropTab(String sTableName, Connection conn) throws RemoteException 
	{   
		logMsg("dropTab");	
		// Delete the Table
		String sTag = sTableName.concat("_Delete");
		String removeString = props.getProperty(sTag);
		logMsg("Executable String " + removeString);
		try
		{
			//Since scrollable resultSet is optional, the parameters
			//are commented out.
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(removeString);
			stmt.close();
		} 
		catch (SQLException e) 
		{ 
			throw new RemoteException( e.getMessage() );
		}      
	}

	public void dbUnConnect(Connection conn) throws RemoteException 
	{
		TestUtil.logTrace("dbUnConnect");
		// Close the DB connections
		try
		{
			conn.close();
			TestUtil.logMsg("Closed the Data Base connection");
		} 
		catch (Exception e) 
		{
			TestUtil.logErr("Exception occured while trying to close the DB connection");
			throw new RemoteException( e.getMessage() );
		}
	}
	public String extractVal(String sTableName, int count,Properties sqlProps, Connection conn) throws Fault
	{
		String sKeyName = null,insertString=null;
		String retStr=null,parameters=null;
		StringTokenizer sToken=null;
		try
		{
			sKeyName=sTableName.concat("_Insert");

			insertString=sqlProps.getProperty(sKeyName,"");

			parameters=insertString.substring(insertString.indexOf("(",1)+1,insertString.indexOf(")",1));

			sToken= new StringTokenizer(parameters,",");
			int i=1;
			do
			{
				retStr=(String) sToken.nextElement();
			}
			while(count!=i++);

			return retStr;
		}
		catch(Exception e)
		{
			logErr("Exception " + e.getMessage());
			throw new Fault("Call to extractVal is Failed!");
		}
	}
}
