/**
 * 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.SQLException;
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.basic.SIMsgTemp;
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.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @author shionoya
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class UIRegMsgTemp extends SIMsgTemp {
  
  //ログ用のインスタンスの生成
  private Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  /**
   * init
   * 入力したデータから、このbeansを設定します。
   * @param HttpServletRequest
   * @param SIURLParameter
   * @return なし
   * @throws なし
   */
  //7.1.1 ST0236 修正 ここから
  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.setMsgType((String)lUrlParam.getParam("msgType"));
    this.setClassify((String)lUrlParam.getParam("classify"));
    this.setTitle((String)lUrlParam.getParam("title"));
    this.setContent1((String)lUrlParam.getParam("content1"));
    this.setContent2((String)lUrlParam.getParam("content2"));
    this.setContent3((String)lUrlParam.getParam("content3"));
    
    this.setActionNameTxt((String)lUrlParam.getParam("actionNameTxt"));
    this.setEditModeTxt((String)lUrlParam.getParam("editModeTxt"));
  }
  //7.1.1 ST0236 修正 ここまで
  
  /**
   * initReload
   * リロードした際入力したデータから、このbeansを設定します。
   * @param HttpServletRequest
   * @param Connection
   * @param SIURLParameter
   * @return なし
   * @throws なし
   */
  public void initReload(HttpServletRequest lRequest,Connection lConnection,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    
    Statement lStatement=null;
    ResultSet lResultSet=null;
    
    this.setEncode(SIConfig.SIENCODE_NONE);
    if (lLogin.isShop()){  
      this.setMallShopCode(lLogin.getMallShopCode());
    }else{
      this.setMallShopCode((String)lUrlParam.getParam("mallShopCode"));//7.1.1 ST0236 修正
    }
    this.setMsgType((String)lUrlParam.getParam("msgType"));//7.1.1 ST0236 修正
    
    String lSql="SELECT * FROM MsgTemplateMTbl WHERE MallShopCode="+SIDBUtil.SQL2Str(this.getMallShopCode())
    +" AND msgType=" + SIDBUtil.SQL2Str(this.getMsgType());
    log.debug("reset:lSql="+lSql);
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSql);
      
      if (lResultSet.next()){
        this.setClassify(lResultSet.getString("classify"));
        this.setMsgType(lResultSet.getString("msgType"));
        this.setContent1(lResultSet.getString("content1"));
        this.setContent2(lResultSet.getString("content2"));
        this.setContent3(lResultSet.getString("content3"));
        this.setTitle(lResultSet.getString("title"));
      }
    }catch(SQLException sqle){
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    
    this.setActionNameTxt((String)lUrlParam.getParam("actionNameTxt"));//7.1.1 ST0236 修正
    this.setEditModeTxt((String)lUrlParam.getParam("editModeTxt"));//7.1.1 ST0236 修正
  }
  
  /**
   * 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.getMsgType(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "メッセージ種類", this.getMsgType(), 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, "本文１", this.getContent1(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2000);
    
    SICheckValid.checkValid(errors, "本文２", this.getContent2(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 6000);
    
    SICheckValid.checkValid(errors, "本文３", this.getContent3(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 4000);
    
    if (!errors.isEmpty())
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    return errors.isEmpty();
  }
  
  /**
   * reset
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request データベースへのコネンクション
   * @return
   * @throws SIDBAccessException
   */
  public void reset(Connection lConnection, SILogin lLogin) throws SIDBAccessException {
    Statement lStatement = null;
    ResultSet lResultSet = null;
    StringBuffer lSqlStatement = new StringBuffer();
    
    //ログインタイプの設定
    String displayFlg = SIMailTemp.getDisplayMode(this.getMallShopCode());
    
    lSqlStatement.append(" select * from msgtemplateMtbl 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);
    
    //7.3.0 ST2024 修正 ここから
//  if (displayFlg.equals("A")) {
//    lSqlStatement.append(" and (msgType=1 or msgType=3)");
//  }else if(displayFlg.equals("B")){
//    lSqlStatement.append(" and (msgType=2 or msgType=3)");
//  }else if(displayFlg.equals("C")){
//    lSqlStatement.append(" and msgType=3");
//  }else if(displayFlg.equals("D")){
//    lSqlStatement.append("");
//  }
    if (displayFlg.equals("A")) {//モール決済、モール管理者
      //受注受付とプライバシーと個人情報利用目的(PC向け)と個人情報利用目的(携帯向け)
      lSqlStatement.append(" and (msgType=1 or msgType=3 or msgType=4 or msgType=5)");
    }else if(displayFlg.equals("B")){//モール決済、ショップ管理者
      lSqlStatement.append(" and (msgType=2 or msgType=3)");//レビューとプライバシー
    }else if(displayFlg.equals("C")){//ショップ決済、モール管理者
      lSqlStatement.append(" and (msgType=3 or msgType=4 or msgType=5 )");
    }else if(displayFlg.equals("D")){//ショップ決済、ショップ管理者
      lSqlStatement.append("");//全部
    }
    //7.3.0 ST2024 修正 ここまで
    if (!displayFlg.equals("C")){
      DD = new SITableCondition("", "msgType", this.getMsgType(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
      DD.setBlankEnable(false);
      lConditionMan.add(DD);
    }
    lSqlStatement.append(lConditionMan.getCondtionSQL());
    
    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.setMsgType(lResultSet.getString("msgType"));
        this.setClassify(lResultSet.getString("classify"));
        this.setTitle(lResultSet.getString("title"));
        this.setContent1(lResultSet.getString("content1"));
        this.setContent2(lResultSet.getString("content2"));
        this.setContent3(lResultSet.getString("content3"));
      }
    } catch (Exception ex) {
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(lResultSet, lStatement);
    }
  }
  
  public Collection getCollectionCombo(Connection lConnection,String MallShopCode) throws SIDBAccessException {
    //7.2.0 ST0311 修正 ここから
    StringBuffer lSqlStatement = new StringBuffer();
    
    lSqlStatement.append("select classify, msgType from msgtemplateMtbl where mallShopCode=" + SIDBUtil.SQL2Str(MallShopCode, ""));
    lSqlStatement.append(" order by CLASSIFY asc");
    
    return SIDBUtil.getCollection(lConnection, lSqlStatement.toString());
    
//    StringBuffer lSqlStatement = new StringBuffer();
//    String displayFlg = SIMailTemp.getDisplayMode(MallShopCode);
//
//    lSqlStatement.append("select classify, msgType from msgtemplateMtbl where mallShopCode=" + SIDBUtil.SQL2Str(MallShopCode, ""));
//    if (displayFlg.equals("A")) {//モール決済、モール管理者
//      lSqlStatement.append(" and (msgType=1 or msgType=3)");//受注受付とプライバシー
//    }else if(displayFlg.equals("B")){//モール決済、ショップ管理者
//      lSqlStatement.append(" and (msgType=2 or msgType=3)");//レビューとプライバシー
//    }else if(displayFlg.equals("C")){//ショップ決済、モール管理者
//      lSqlStatement.append(" and msgType=3");
//    }else if(displayFlg.equals("D")){//ショップ決済、ショップ管理者
//      lSqlStatement.append("");//全部
//    }
//    lSqlStatement.append(" order by CLASSIFY asc");
//    return SIDBUtil.getCollection(lConnection, lSqlStatement.toString());
    //7.2.0 ST0311 修正 ここまで
  }
}