/**
 * Created on 2003/09/26
 *
 * 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 javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SILogin;
import jp.co.sint.basic.SIShopInfo;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.servlet.mallmgr.SIRegShopInfoSrv;
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;//7.1.1 ST0236 追加

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 UIRegShopInfo extends SIShopInfo{
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  /**
   * init
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request
   * @param lUrlParam
   * @return true:不正なデータがある false:ない
   * @throws なし
   */
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    if (lLogin.isShop()){
      this.setShopCode(lLogin.getMallShopCode());
    }else{
      this.setShopCode((String)lUrlParam.getParam("shopCode"));//7.1.1 ST0236 修正
    }
    //7.1.1 ST0236 修正 ここから
    this.setHTMLFileName((String)lUrlParam.getParam("hTMLFileName"));
    this.setTitle((String)lUrlParam.getParam("title"));
    this.setShopComment((String)lUrlParam.getParam("shopComment"));
    
    this.setActionNameTxt((String)lUrlParam.getParam("actionNameTxt"));
    this.setEditModeTxt((String)lUrlParam.getParam("editModeTxt"));
    //7.1.1 ST0236 修正 ここまで
  }
  
  /**
    * validate
    * 入力したデータをチェックします。不正なデータがあったら、エラーオブジェクトに入れて、
    * 画面に表示します。
    * @param lRequest クライアントからリクエスト
    * @param lConnection データベースへのコネクション
    * @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.getShopCode(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "ショップコード", this.getShopCode(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 16);
    
    SICheckValid.checkValid(errors, "タイトル", this.getTitle(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE,100);
    
    SICheckValid.checkValid(errors, "紹介文", this.getShopComment(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2000);
    
    SICheckValid.checkValid(errors, "ショップ紹介URL", this.getHTMLFileName(), SICheckDataConf.SICHECK_DATA_URL_TYPE);
    SICheckValid.checkValid(errors, "ショップ紹介URL", this.getHTMLFileName(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE,256);
    
    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) throws SIDBAccessException {
    Statement lStatement = null;
    ResultSet lResultSet = null;
    
    //基本のSQL
    String lSqlStatement = "SELECT * FROM shopinfomtbl WHERE shopcode=" + SIDBUtil.SQL2Str(this.getShopCode());
    
    log.debug("sqlStatement=" + lSqlStatement);
    
    //実行
    try {
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlStatement);
      
      if (lResultSet.next()) {
        //配送先送料
        this.setEncode(SIConfig.SIENCODE_NONE);
        
        this.setShopCode(lResultSet.getString("shopCode"));
        this.setHTMLFileName(lResultSet.getString("htmlfilename"));
        this.setTitle(lResultSet.getString("title"));
        this.setShopComment(lResultSet.getString("shopComment"));
      }else{
        SIRegShopInfoSrv.insertTableData(this,lConnection);
        lConnection.commit();
      }
    } catch (Exception ex) {
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(lResultSet, lStatement);
    }
  }
}