/**
 * Created on 2003/09/30
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package jp.co.sint.beans.mallmgr;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SILogin;
import jp.co.sint.basic.SIMailTemp;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SITableCondition;
import jp.co.sint.database.SITableConditionManager;
import jp.co.sint.tools.SICheckDataConf;
import jp.co.sint.tools.SICheckValid;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIURLParameter;

import org.apache.log4j.Category;

/**
 * @author arai
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class UIRegMailTemp extends SIMailTemp {

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

  /**
   * init
   * 入力したデータから、このbeansを設定します。
   * @param HttpServletRequest
   * @param SIURLParameter
   * @return なし
   * @throws なし
   */
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    if (lLogin.isShop()){
      this.setMallShopCode(lLogin.getMallShopCode());
    }else{
      this.setMallShopCode((String)lUrlParam.getParam("mallShopCode"));
    }
    this.setMailType((String)lUrlParam.getParam("mailType"));
    this.setClassify((String)lUrlParam.getParam("classify"));
    this.setTitle((String)lUrlParam.getParam("title"));
    this.setFromMail((String)lUrlParam.getParam("fromMail"));
    this.setContent1((String)lUrlParam.getParam("content1"));
    this.setContent2((String)lUrlParam.getParam("content2"));
    this.setContent3((String)lUrlParam.getParam("content3"));
    this.setSignature((String)lUrlParam.getParam("signature"));
    this.setActionNameTxt((String)lUrlParam.getParam("actionNameTxt"));
    this.setEditModeTxt((String)lUrlParam.getParam("editModeTxt"));
  }
  
  /**
   * validate
   * 入力したデータをチェックします。
   * 不正なデータがある場合、エラーをオブジェクトに格納します。
   * そのオブジェクトは、エラーメッセージとして画面に表示されます。
   * @param HttpServletRequest ，Connection
   * @return true:エラーがない false:エラーが１つ以上ある
   * @throws なし
   */
  public boolean validate(HttpServletRequest lRequest, Connection lConnection) {
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    
    SICheckValid.checkValid(errors, "管理コード", this.getMallShopCode(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "管理コード", this.getMallShopCode(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 16);
    SICheckValid.checkValid(errors, "メール種類", this.getMailType(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "メール種類", this.getMailType(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2);
    SICheckValid.checkValid(errors, "メールタイトル", this.getTitle(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "メールタイトル", this.getTitle(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 300);
    SICheckValid.checkValid(errors, "Fromアドレス", this.getFromMail(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_MAIL_TYPE);
    SICheckValid.checkValid(errors, "Fromアドレス", this.getFromMail(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 128);
    SICheckValid.checkValid(errors, "本文１", this.getContent1(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2000);
    SICheckValid.checkValid(errors, "本文２", this.getContent2(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2000);
    SICheckValid.checkValid(errors, "本文３", this.getContent3(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2000);
    SICheckValid.checkValid(errors, "署名", this.getSignature(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    if (!errors.isEmpty())
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    return errors.isEmpty();
  }
  
  /**
   * reset
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request データベースへのコネンクション
   * @return
   * @throws SIDBAccessException
   */
  public boolean reset(Connection lConnection) throws SIDBAccessException {
    boolean lRes=false;
    Statement lStatement = null;
    ResultSet lResultSet = null;
    StringBuffer lSqlStatement = new StringBuffer();
    
    lSqlStatement.append(" select * from mailtemplateMtbl where 1=1 ");
    
    SITableConditionManager lConditionMan = new SITableConditionManager();
    SITableCondition DD = new SITableCondition("", "mallshopcode", this.getMallShopCode(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(false);
    lConditionMan.add(DD);
    
    DD = new SITableCondition("", "mailType", this.getMailType(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(false);
    lConditionMan.add(DD);
    lSqlStatement.append(lConditionMan.getCondtionSQL());
    lSqlStatement.append(" ORDER BY classify DESC ");
    log.debug("sqlStatement=" + lSqlStatement.toString());
    
    try {
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlStatement.toString());
      
      if (lResultSet.next()) {
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setMallShopCode(lResultSet.getString("mallShopCode"));
        this.setMailType(lResultSet.getString("mailType"));
        this.setClassify(lResultSet.getString("classify"));
        this.setTitle(lResultSet.getString("title"));
        this.setFromMail(lResultSet.getString("frommail"));
        this.setContent1(lResultSet.getString("content1"));
        this.setContent2(lResultSet.getString("content2"));
        this.setContent3(lResultSet.getString("content3"));
        this.setSignature(lResultSet.getString("signature"));
        lRes=true;
      }
    } catch (Exception ex) {
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(lResultSet, lStatement);
    }
    return lRes;
  }
  
  public Collection getCollectionCombo(Connection lConnection,String MallShopCode) throws SIDBAccessException {
    StringBuffer lSqlStatement = new StringBuffer();
    String displayFlg = "OneSP";//一店舗版、全件表示
    
    lSqlStatement.append("select classify, mailType from mailtemplateMtbl where mallShopCode=" + SIDBUtil.SQL2Str(MallShopCode, ""));
    
    if (displayFlg.equals("A")) {
      lSqlStatement.append(" and mailType IN ('1','2','3','5','6','10','11','14','15','16')"); //全件  7.3.0 ST2025,ST2026 修正 16を追加(ST2025) 14,15を追加(ST2026)
    } else if (displayFlg.equals("B")) {
      lSqlStatement.append(" and mailType IN ('1','4','7','8','9','12','13') ");
    } else if (displayFlg.equals("C")) {
      lSqlStatement.append(" and mailType IN ('1','6','14','15','16')");//全部 7.3.0 ST2025,ST2026 修正 16を追加(ST2025) 14,15を追加(ST2026)
    } else if (displayFlg.equals("D")) {
      lSqlStatement.append(" and mailType NOT IN ('6','14','15','16') "); //7.3.0 ST2025,ST2026 修正 16を追加(ST2025) 14,15を追加(ST2026)
    }
    lSqlStatement.append(" order by CLASSIFY desc"); //全件
    
    displayFlg = null;
    
    return SIDBUtil.getCollection(lConnection, lSqlStatement.toString());
  }
  
}
