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

import java.io.File;
import java.sql.Connection;

import jp.co.sint.basic.SIMailTemp;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.tools.SIFatalException;
import jp.co.sint.tools.SIUtil;

import org.apache.log4j.Category;

/**
 * @version $Id: SISendMail.java,v 1.0 2003/12/19 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/12/19 12:01:05  Original
 */
public class SISendMail {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  //宛先リスト
  private String[] toMailAddress=new String[0];
  //同報リスト
  private String[] ccMailAddress=new String[0];
  //メール内容
  private SIMailTemp mailTemp=new SIMailTemp();
  //本文１の前
  private StringBuffer insertContent1Buf=new StringBuffer();
  //本文１の後
  private StringBuffer appendContent1Buf=new StringBuffer();
  //本文2の前
  private StringBuffer insertContent2Buf=new StringBuffer();
  //本文2の後
  private StringBuffer appendContent2Buf=new StringBuffer();
  //本文3の前
  private StringBuffer insertContent3Buf=new StringBuffer();
  //本文3の後
  private StringBuffer appendContent3Buf=new StringBuffer();
  
  private File attachmentFile1=null;//添付ファイル1
  private File attachmentFile2=null;//添付ファイル2
  private File attachmentFile3=null;//添付ファイル3
  private File attachmentFile4=null;//添付ファイル4
  private File attachmentFile5=null;//添付ファイル5
  
  public SISendMail(){
  }
  
  public SISendMail(String[] lToMailAddress,SIMailTemp lMailTemp){
    setToMailAddress(lToMailAddress);
    setMailTemp(lMailTemp);
  }
  
  public void setToMailAddress(String[] lToMailAddress){
    this.toMailAddress=lToMailAddress;
  }
  
  public void setToMailAddress(String lToMailAddress){
    if (SIUtil.isNull(lToMailAddress)) return ;
    this.toMailAddress=new String[]{lToMailAddress};
  }
  
  public void setCCMailAddress(String[] lCCMailAddress){
    this.ccMailAddress=lCCMailAddress;
  }
  
  public void setMailTemp(SIMailTemp lMailTemp){
    if (lMailTemp==null)lMailTemp=new SIMailTemp();
    this.mailTemp=lMailTemp;
  }
  
  public void setFromMailAddress(String lFromMailAddress){
    if (SIUtil.isNull(lFromMailAddress)) return;
    mailTemp.setFromMail(lFromMailAddress);
  }
  
  public void setMailTemp(Connection lConnection,String lMallShopCode,String lMailType) throws SIFatalException{
    mailTemp=new SIMailTemp(lMallShopCode,lMailType);
    try {
      if (!mailTemp.reset(lConnection)){
        throw new SIFatalException("can not find mail template for mallshopcode("+lMallShopCode+") and Type("+lMailType+")");
      }
    } catch (SIDBAccessException e) {
      e.printStackTrace();
      throw new SIFatalException(e);
    }
  }
  
  public void appendMailTitle(String headStr,String tailStr){
    if(SIUtil.isNotNull(headStr)) this.mailTemp.setTitle(headStr+this.mailTemp.getTitle());
    if(SIUtil.isNotNull(tailStr)) this.mailTemp.setTitle(this.mailTemp.getTitle()+tailStr);
  }
  
  public void changeMailTitle(String titleStr){
    if(SIUtil.isNotNull(titleStr)) this.mailTemp.setTitle(titleStr);
  }
  
  public void insertContent1(String lInsertContent1){
    insertContent1Buf.append(lInsertContent1);
  }
  
  public void appendContent1(String lAppendContent1){
    appendContent1Buf.append(lAppendContent1);
  }
  
  public void insertContent2(String lInsertContent2){
    insertContent2Buf.append(lInsertContent2);
  }
  
  public void appendContent2(String lAppendContent2){
    appendContent2Buf.append(lAppendContent2);
  }
  
  public void insertContent3(String lInsertContent3){
    insertContent3Buf.append(lInsertContent3);
  }
  
  public void appendContent3(String lAppendContent3){
    appendContent3Buf.append(lAppendContent3);
  }
  
  public String getContent(){
    StringBuffer lContentBuf=new StringBuffer();
    
    //内容1
    if (SIUtil.isNotNull(insertContent1Buf.toString())){
      lContentBuf.append("\n").append(insertContent1Buf);
    }
    
    if (SIUtil.isNotNull(mailTemp.getContent1())){
      lContentBuf.append("\n").append(mailTemp.getContent1());
    }
    
    if (SIUtil.isNotNull(appendContent1Buf.toString())){
      lContentBuf.append("\n").append(appendContent1Buf);
    }
    
    //内容2
    if (SIUtil.isNotNull(insertContent2Buf.toString())){
      lContentBuf.append("\n").append(insertContent2Buf);
    }
    if (SIUtil.isNotNull(mailTemp.getContent2())){
      lContentBuf.append("\n").append(mailTemp.getContent2());
    }
    if (SIUtil.isNotNull(appendContent2Buf.toString())){
      lContentBuf.append("\n").append(appendContent2Buf);
    }
    
    //内容3
    if (SIUtil.isNotNull(insertContent3Buf.toString())){
      lContentBuf.append("\n").append(insertContent3Buf);
    }
    if (SIUtil.isNotNull(mailTemp.getContent3())){
      lContentBuf.append("\n").append(mailTemp.getContent3());
    }
    if (SIUtil.isNotNull(appendContent3Buf.toString())){
      lContentBuf.append("\n").append(appendContent3Buf);
    }
    
    //7.2.0 ST0269 修正 ここから
    if (SIUtil.isNotNull(mailTemp.getSignature())){
      lContentBuf.append("\n").append(mailTemp.getSignature());
    }
    lContentBuf.append("\n");
    //7.2.0 ST0269 修正 ここまで
    
    return lContentBuf.toString();
  }
  
  /**
   * 添付ファイル1を設定します。
   */
  public void setAttachmentFile(String path) throws SIFatalException{
    attachmentFile1 = new File(path);
    if(!attachmentFile1.exists()||!attachmentFile1.canRead()){
      throw new SIFatalException();
    }
  }
  
  /**
   * 添付ファイル2を設定します。
   */
  public void setAttachmentFile2(String path) throws SIFatalException{
    attachmentFile2 = new File(path);
    if(!attachmentFile2.exists()||!attachmentFile2.canRead()){
      throw new SIFatalException();
    }
  }
  
  /**
   * 添付ファイル3を設定します。
   */
  public void setAttachmentFile3(String path) throws SIFatalException{
    attachmentFile3 = new File(path);
    if(!attachmentFile3.exists()||!attachmentFile3.canRead()){
      throw new SIFatalException();
    }
  }
  
  /**
   * 添付ファイル4を設定します。
   */
  public void setAttachmentFile4(String path) throws SIFatalException{
    attachmentFile4 = new File(path);
    if(!attachmentFile4.exists()||!attachmentFile4.canRead()){
      throw new SIFatalException();
    }
  }
  
  /**
   * 添付ファイル5を設定します。
   */
  public void setAttachmentFile5(String path) throws SIFatalException{
    attachmentFile5 = new File(path);
    if(!attachmentFile5.exists()||!attachmentFile5.canRead()){
      throw new SIFatalException();
    }
  }
  
  public boolean execute()throws SIFatalException{
    boolean lRes=true;
    String lContent=getContent();
    SIMail lMail;
    
    try {
      lMail=new SIMail();
    } catch (SIFatalException e) {
      e.printStackTrace();
      throw new SIFatalException(e);
    }
    
    //7.2.1 ST2020 ST2025 ST2026 修正ここから
    if(mailTemp.getClassify().equals("RFM販促メール")//RFM販促メール
     || mailTemp.getClassify().equals("新着情報")//新着情報
     || mailTemp.getClassify().equals("")//メールテンプレートからのテスト送信
     || mailTemp.getClassify().equals("顧客登録"))
    {
      for (int ii=0;ii<toMailAddress.length ;ii++){
        try {
          lMail=new SIMail();
          lMail.setFrom(mailTemp.getFromMail());
          lMail.addTo(toMailAddress[ii]);
          if (ccMailAddress != null) lMail.addCc(ccMailAddress);
          lMail.setSubject(mailTemp.getTitle());
          lMail.setContent(lContent);
          if(attachmentFile1!=null) lMail.setAttachmentFile1(attachmentFile1,attachmentFile1.getName());
          if(attachmentFile2!=null) lMail.setAttachmentFile2(attachmentFile2,attachmentFile2.getName());
          if(attachmentFile3!=null) lMail.setAttachmentFile3(attachmentFile3,attachmentFile3.getName());
          if(attachmentFile4!=null) lMail.setAttachmentFile4(attachmentFile4,attachmentFile4.getName());
          if(attachmentFile5!=null) lMail.setAttachmentFile5(attachmentFile5,attachmentFile5.getName());
          if (lMail.sendMail())log.debug("<"+toMailAddress[ii]+"> 宛先に送信OK! メール種類="+mailTemp.getClassify());
          else log.debug("<"+toMailAddress[ii]+"> 宛先に送信NG! メール種類="+mailTemp.getClassify());
        } catch (SIFatalException e) {
           e.printStackTrace();
          lRes=false;
        }
      }
    }else{
      for (int ii=0;ii<toMailAddress.length ;ii++){
        try {
          lMail=new SIMail();
          lMail.setFrom(mailTemp.getFromMail());
          lMail.addTo(toMailAddress[ii]);
          if (ccMailAddress != null) lMail.addCc(ccMailAddress);
          lMail.addBcc(mailTemp.getMallShopEmail());
          lMail.setSubject(mailTemp.getTitle());
          lMail.setContent(lContent);
          if(attachmentFile1!=null) lMail.setAttachmentFile1(attachmentFile1,attachmentFile1.getName());
          if(attachmentFile2!=null) lMail.setAttachmentFile2(attachmentFile2,attachmentFile2.getName());
          if(attachmentFile3!=null) lMail.setAttachmentFile3(attachmentFile3,attachmentFile3.getName());
          if(attachmentFile4!=null) lMail.setAttachmentFile4(attachmentFile4,attachmentFile4.getName());
          if(attachmentFile5!=null) lMail.setAttachmentFile5(attachmentFile5,attachmentFile5.getName());
          if (lMail.sendMail())log.debug("<"+toMailAddress[ii]+"> 宛先に送信OK! メール種類="+mailTemp.getClassify());
          else log.debug("<"+toMailAddress[ii]+"> 宛先に送信NG! メール種類="+mailTemp.getClassify());
        } catch (SIFatalException e) {
          e.printStackTrace();
          lRes=false;
        }
      }
    }
    //7.2.1 ST2020 ST2025 ST2026 修正ここまで
    return lRes;
  }
}