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

import org.apache.log4j.Category;
import jp.co.sint.config.*;

/**
 * @version $Id: SIException.java,v 1.0 2003/07/25 Exp $
 * @author  Jinwang Chen
 * <br>Description: カスタマイズの例外の基礎クラス
 * <p>History</p>
 * <p>Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reason</p>
 * ============&nbsp;&nbsp;&nbsp;==========&nbsp;&nbsp;===========================<br>
 * J.W.Chen       2003/07/25  Original
 */

public class SIException extends Exception {

  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  private String exceptionMessage ="";

  private int exceptionLevel;

  /**
   * <b>SIException</b>
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  protected SIException(){
    super();
    this.exceptionLevel=ISIExceptionUtil.DEBUG_EXCEPTION;
  }

  /**
   * <b>SIException</b>
   * コンストラクタ
   * @param exceptionLevel 例外のレベル
   * @return なし
   * @throws なし
   */
  protected SIException(int exceptionLevel) {
    super();
    setExceptionLevel(exceptionLevel);
    log();
  }

  /**
   * <b>SIException</b>
   * コンストラクタ
   * @param exceptionLevel 例外のレベル
   * @param exceptionMessage 例外メッセージ
   * @return なし
   * @throws なし
   */
  protected SIException(int exceptionLevel,String exceptionMessage){
    super(exceptionMessage);
    this.exceptionLevel=exceptionLevel;
    this.exceptionMessage = exceptionMessage;
    log();
  }

  protected SIException(int exceptionLevel,Exception e){
    super(e);
    this.exceptionLevel =exceptionLevel;
    this.exceptionMessage=e.toString();
    log();
  }
  /**
   * <b>setExceptionLevel</b>
   * 例外レベルの設定
   * @param exceptionLevel 例外のレベル
   * @return なし
   * @throws なし
   */
  protected void setExceptionLevel(int exceptionLevel){
    this.exceptionLevel = exceptionLevel;
  }

  /**
   * <b>setExceptionMessage</b>
   * 例外メッセージの設定
   * @param exceptionMessage 例外メッセージ
   * @return なし
   * @throws なし
   */
  protected void setExceptionMessage(String exceptionMessage){
    this.exceptionMessage = exceptionMessage;
  }

  /**
   * <b>getExceptionLevel</b>
   * 例外レベルの取得
   * @param なし
   * @return 例外レベル
   * @throws なし
   */
  public int getExceptionLevel(){
    return exceptionLevel;
  }

  /**
   * <b>getExceptionMessage</b>
   * 例外メッセージの取得
   * @param なし
   * @return 例外メッセージ
   * @throws なし
   */
  public String getExceptionMessage(){
    return exceptionMessage;
  }

  /**
   * <b>log</b>
   * 例外メッセージをログファイルに記録する
   * @param なし
   * @return なし
   * @throws なし
   */
  private void log(){
    StringBuffer logMsgBuf=new StringBuffer();

    logMsgBuf.append(ISIExceptionUtil.EXCEPTION_LEVEL_TITLE[this.exceptionLevel]+" 存在.");
    if (SIUtil.isNotNull(getExceptionMessage())){
      logMsgBuf.append("メッセージ=" +getExceptionMessage());
    }

    switch (this.exceptionLevel){
      case ISIExceptionUtil.DEBUG_LEVEL:
        log.debug(logMsgBuf.toString());
        break;
      case ISIExceptionUtil.INFORMATION_EXCEPTION:
        log.info(logMsgBuf.toString());
        break;
      case ISIExceptionUtil.WARNING_LEVEL:
        log.warn(logMsgBuf.toString());
        break;
      case ISIExceptionUtil.ERROR_LEVEL:
        log.error(logMsgBuf.toString());
        break;
      case ISIExceptionUtil.FATAL_LEVEL:
        log.fatal(logMsgBuf.toString());
        break;
      default:
    }
  }
}