/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.tools;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @version $Id: SIExecRanking.java,v 1.0 2004/04/06 Exp $
 * @author  Jinwang Chen
 * <br>Description:クラスを通して、ランキングのバッチを起動します。
 * <p>History</p>
 * 
 * Jinwang Chen   2004/04/06 14:35:03  Original
 */
public class SIExecRanking {
  
  private final static String configFile = "databaseConfig";

  public SIExecRanking(){
  }

  /**
   * loadParams
   * JDBCの設定パラメータデータの取得
   * @param  なし
   * @return なし
   * @throws IOException
   */
  public static Properties loadParams( ) throws IOException {
    Properties lProp = new Properties();
    ResourceBundle lBundle = ResourceBundle.getBundle(configFile,new Locale("ja","JP"));

    Enumeration lEnum = lBundle.getKeys();
    String lKey = null;

    while( lEnum.hasMoreElements() ) {
      lKey = (String)lEnum.nextElement();
      lProp.put( lKey, lBundle.getObject( lKey ) );
    }
    return lProp;
  }

  /**
   * getConnection
   * DBへのコネクションを生成します。
   * @param  なし
   * @return なし
   * @throws ClassNotFoundException
   * @throws SQLException
   * @throws IOException
   */
  public Connection getConnection() throws ClassNotFoundException,SQLException,IOException{
    Properties lProp =new  Properties();

    lProp=loadParams();
    System.out.println("driverClass="+lProp.getProperty("driverClass")+",url="+lProp.getProperty("url"));
    Class.forName(lProp.getProperty("driverClass"));
    Connection dbConnection = DriverManager.getConnection(lProp.getProperty("url"),
                                                          lProp.getProperty("user"),
                                                          lProp.getProperty("password"));
    dbConnection.setAutoCommit(false);
    return dbConnection;
  }
  
  /**
   * isOra
   * データベースのタイプ
   * @param  なし
   * @return オラクルかどうか
   * @throws なし
   */
  public boolean isOra(){
    Properties lProp =new  Properties();
    try {
      lProp=loadParams();
    } catch (IOException e) {
      e.printStackTrace();
    }
    String lType=lProp.getProperty("type");
    if (lType!=null && lType.equalsIgnoreCase("oracle")) return true;
    else return false;
  }
  
  /**
   * execRanking
   * ランキングバッチの実行
   * @param  なし
   * @return なし
   * @throws なし
   */
  public boolean execRanking(){
    boolean lResult=false;
    Connection lConnection=null;
    Statement lStatement=null;
    ResultSet lResultSet=null;
    CallableStatement lCallStatement=null;
    int ii=0;

    try {
      lConnection=getConnection();
      if (isOra()){
        System.out.println("oracle...");
        lCallStatement = lConnection.prepareCall("{CALL RANKINGPROC(?)}");
        lCallStatement.registerOutParameter(1,java.sql.Types.INTEGER);
        lCallStatement.execute();
        ii=lCallStatement.getInt(1);
      }else{
        System.out.println("postgresql...");
        lStatement=lConnection.createStatement();
        lResultSet=lStatement.executeQuery("SELECT * FROM RankingFunc()");
        if (lResultSet.next()){
          ii=lResultSet.getInt(1);
        }
        lConnection.commit();
      }
      if (ii==1)lResult=true;

    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{//後始末
      if (lResultSet!=null){
        try{lResultSet.close();}catch(SQLException sqle){sqle.printStackTrace();}
      }

      if (lStatement!=null){
        try{lStatement.close();}catch(SQLException sqle){sqle.printStackTrace();}
      }
      if (lCallStatement!=null){
        try{lCallStatement.close();}catch(SQLException sqle){sqle.printStackTrace();}
      }

      if (lConnection!=null){
        try{lConnection.close();}catch(SQLException sqle){sqle.printStackTrace();}
      }
    }

    return lResult;
  }

  public static void main(String[] argv) throws Exception{
    SIExecRanking lRanking=new SIExecRanking();
    if (lRanking.execRanking()){
    }else{
      throw new Exception();
    }
  }
}
