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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import jp.co.sint.config.SIConfig;
import jp.co.sint.tools.SIFatalException;
import jp.co.sint.tools.SIStringUtil;
import jp.co.sint.tools.SIUtil;

import org.apache.log4j.Category;

/**
 * @version $Id: SIMailUtil.java,v 1.0 2003/10/08 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>
 * Jinwang Chen   2003/10/08 15:13:53  Original
 */
public class SIMailUtil {

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

  //設定ファイルからパラメータのデータの取得
  private static ResourceBundle mailConfig=ResourceBundle.getBundle(SIConfig.SICONFIG_MAIL_FILE_NAME,new Locale("ja","JP"));

  /**
   * <b>getFrom</b>
   * 設定ファイルから送信者を取得します
   * @param なし
   * @return なし
   * @throws なし
   */
  public static InternetAddress getFrom(){
    String lFromAddress="";
    try {mailConfig.getString("from");}catch(Exception e){}

    try {
      if (SIUtil.isNull(lFromAddress)) return null;
      else return new InternetAddress(SIUtil.CP932ToJIS(lFromAddress));
    } catch (AddressException e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * <b>getFrom</b>
   * 設定ファイルから返信先リストを取得します
   * @param なし
   * @return 返信先リスト
   * @throws なし
   */
  public static Collection getReplyTo(){
    String lReplyTo="";
    try {lReplyTo=mailConfig.getString("replyTo");}catch(Exception e){}

    if (SIUtil.isNull(lReplyTo)) return new ArrayList();

    try {
      String[] lReplyToList=SIStringUtil.split(lReplyTo,";");
      Collection lReplyToAddress=new ArrayList();
      for (int ii=0;ii<lReplyToList.length;ii++){
        if (SIUtil.isNotNull( lReplyToList[ii])){
          lReplyToAddress.add(new InternetAddress(SIUtil.CP932ToJIS(lReplyToList[ii])));
        }
      }
      return lReplyToAddress;
    } catch (AddressException e) {
      e.printStackTrace();
      return new ArrayList();
    }
  }

  /**
   * <b>getMailHost</b>
   * 設定ファイルからメールサーバー名を取得します
   * @param なし
   * @return メールホスト
   * @throws なし
   */
  public static String getMailHost(){
    String lMailHost="";
    try {lMailHost=mailConfig.getString("host");}catch(Exception e){}

    if (SIUtil.isNull(lMailHost))lMailHost=SIConfig.SIMAIL_DEFAULT_HOST_NAME ;
    return lMailHost;
  }

  public static String getSession(){
    String lSessionName="";
    try {lSessionName=mailConfig.getString("mailSession");}catch(Exception e){}

    if (SIUtil.isNull(lSessionName))lSessionName=SIConfig.SIMAIL_DEFAULT_SESSION_NAME;
    return lSessionName;
  }

  public static String getHeaderName(){
    String lHaderName="";
    try {lHaderName=mailConfig.getString("X-Mail");}catch(Exception e){}

    if (SIUtil.isNull(lHaderName))lHaderName=SIConfig.SIMAIL_DEFAULT_HEADER_NAME;
    return lHaderName;
  }
  /**
   * <b>addAddress</b>
   * 指定リストにアドレスとを追加します
   * @param lCollection 指定リスト
   * @param lAddress アドレス
   * @return なし
   * @throws なし
   */
  public static void addAddress(Collection lCollection,String lAddress)throws SIFatalException{
    if (SIUtil.isNull(lAddress)) return;

    try {
      String[] lAddressList=SIStringUtil.split(lAddress,";");
      for(int ii=0;ii<lAddressList.length ;ii++){
        if (SIUtil.isNotNull(lAddressList[ii])){
          lCollection.add(new InternetAddress(SIUtil.CP932ToJIS(lAddressList[ii])));
        }
      }
    } catch (AddressException e) {
      e.printStackTrace();
      throw new SIFatalException(e);
    }
  }
}
