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

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

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIBasic;
import jp.co.sint.basic.SICustAttr;
import jp.co.sint.basic.SILogin;
import jp.co.sint.basic.SINameValue;//7.2.0 ST0539 追加
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.SIUtil;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @version $Id : UICustAttributeListCond.java,v 1.0 Exp $
 * @author      : Jinwang Chen
 * <br>Description :顧客分類属性のbeansクラスを作成します。
 * <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/09/08  Original
 */

public class UICustAttributeListCond extends SIBasic {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //属性番号
  private String attrCode = "";
  //属性分類コード
  private String attrFlag = "0";
  
  //SQL検索のばあいに、条件文
  private String conditionSQL = "";
  //並び順の文
  private String orderBySQL = " ORDER BY Disporder asc , AttrName asc";
  
  //表示順
  private String dispOrder ="";
  
  /**
   * UIStndrdNameListCond
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  public UICustAttributeListCond() {
  }
  
  /**
   * UIStndrdNameListCond
   * コンストラクタ
   * @param HttpServletRequest
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public UICustAttributeListCond(HttpServletRequest lRequest,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    this.init(lRequest,lUrlParam);//7.1.1 ST0236 修正
  }
  
  /**
   * init
   * 入力したデータを基づいて、このbeansを設定します。
   * @param HttpServletRequest
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  //7.1.1 ST0236 修正 ここから
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    super.init(lRequest,lUrlParam);//7.1.1 ST0025 修正  
    this.setAttrFlag((String)lUrlParam.getParam("attrFlag")); //属性分類コード
    this.setPageNumberSel((String)lUrlParam.getParam("pageNumberSel")); //レコード数
    this.setPageNumberTxt((String)lUrlParam.getParam("pageNumberTxt")); //ページ番号
  }
  //7.1.1 ST0236 修正 ここまで
  
  public Collection getCollection(Connection lConnection) throws SIDBAccessException {
    return getCollection(lConnection, new SILogin());
  }
  
  /**
   * getCollection
   * 条件に合ったレコードを検索して、結果のコネクションを作成して、戻します。
   * @param lConnection データベースへの接続コネクション
   * @return レコードのセット
   * @throws SIDBAccessException
   */
  public Collection getCollection(Connection lConnection, SILogin lLogin) throws SIDBAccessException {
    Statement lStatement = null;
    ResultSet lResultSet = null;
    SICustAttr lCustAttribute = new SICustAttr();
    Collection lCustAttributeColl = new ArrayList();
    
    StringBuffer lSqlStatement = new StringBuffer();
    
    //基本のSQL
    lSqlStatement.append("SELECT * FROM custAttrMtbl ");
    lSqlStatement.append("WHERE 1=1 ");
    
    //初期表示の場合
    if (SIUtil.isNull(this.conditionSQL)) {
      SITableConditionManager lConditionMan = new SITableConditionManager();
      lConditionMan.add(new SITableCondition("", "attrFlag", this.getAttrFlag(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
      lSqlStatement.append(lConditionMan.getCondtionSQL());
    }
    
    //検索の条件
    lSqlStatement.append(this.conditionSQL);
    //出力順
    lSqlStatement.append(this.getOrderBySQL());
    log.debug("getCollection:sqlStatement=" + lSqlStatement.toString());
    //実行
    try {
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlStatement.toString());
      //商品レコードのセットの作成
      while (lResultSet.next()) {
        lCustAttribute = new SICustAttr();
        lCustAttribute.setEncode(SIConfig.SIENCODE_NONE);
        lCustAttribute.setAttrCode(lResultSet.getString("attrCode"));
        lCustAttribute.setAttrFlag(lResultSet.getString("attrFlag"));
        lCustAttribute.setAttrName(lResultSet.getString("attrName"));
        lCustAttribute.setDispOrder(lResultSet.getString("dispOrder"));
        lCustAttributeColl.add(lCustAttribute);
      }
    } catch (Exception ex) {
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(lResultSet, lStatement);
    }
    return lCustAttributeColl;
  }
  
  //7.2.0 ST0539 追加 ここから
  //顧客属性を取得
  public Collection getCollection(Connection lConnection,String lMallShopCode){
    Collection custAttrColl = new ArrayList();
    custAttrColl.add(new SINameValue(SIConfig.SIMALL.getCustAttribute0(),"0"));
    custAttrColl.add(new SINameValue(SIConfig.SIMALL.getCustAttribute1(),"1"));
    custAttrColl.add(new SINameValue(SIConfig.SIMALL.getCustAttribute2(),"2"));
    return custAttrColl;
  }
  //7.2.0 ST0539 追加 ここまで
  
  /**
   * validate
   * 入力したデータをチェックして、同時にSQLの条件文を作成します。
   * @param lRequest クライアントからのリクエスト
   * @return なし
   * @throws なし
   */
  public void validate(HttpServletRequest lRequest) {
    SICustomErrors errors = new SICustomErrors();
    SITableConditionManager lConditionMan = new SITableConditionManager();
    
    //規格分類名称
    if (SIUtil.isNotNull(this.getAttrFlag()) 
        || SICheckValid.checkValid(errors,"属性分類コード",this.getAttrFlag(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE + SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE,6)) {
      lConditionMan.add(new SITableCondition("", "attrFlag", this.getAttrFlag(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //エラーをセッションに設定します。
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    
    //条件文の設定
    this.setConditionSQL(lConditionMan.getCondtionSQL());
  }
  
  /**
   * @return
   */
  public String getAttrCode() {
    return attrCode;
  }
  
  /**
   * @return
   */
  public String getAttrFlag() {
    return attrFlag;
  }
  
  /**
   * @return
   */
  public String getConditionSQL() {
    return conditionSQL;
  }
  
  /**
   * @return
   */
  public Category getLog() {
    return log;
  }
  
  /**
   * @param string
   */
  public void setAttrCode(String string) {
    if (string == null) string = "";
    attrCode = SIUtil.changeTo(string.trim(), this.encode);
  }
  
  /**
   * @param string
   */
  public void setAttrFlag(String string) {
    if (string == null) string = "";
    attrFlag = SIUtil.changeTo(string.trim(), this.encode);
  }
  
  //setter of ページ番号
  public void setPageNumberTxt(String lPageNumberTxt) {
    if (SIUtil.isNull(lPageNumberTxt)) lPageNumberTxt = "";
    this.pageNumberTxt = SIUtil.changeTo(lPageNumberTxt.trim(), this.encode);
  }
  
  //setter of ページごとに表示するレコード数
  public void setPageNumberSel(String lPageNumberSel) {
    if (SIUtil.isNull(lPageNumberSel)) lPageNumberSel = "";
    this.pageNumberSel = SIUtil.changeTo(lPageNumberSel.trim(), this.encode);
  }
  
  //setter of 条件文
  public void setConditionSQL(String lConditionSQL) {
    if (lConditionSQL == null) lConditionSQL = "";
    this.conditionSQL = lConditionSQL;
  }
  
  //setter of 並び順文
  public void setOrderBySQL(String lOrderBySQL) {
    if (lOrderBySQL == null) lOrderBySQL = "";
    this.orderBySQL = lOrderBySQL;
  }
  
  //getter of ページ番号
  public String getPageNumberTxt() {
    return this.pageNumberTxt;
  }
  
  //getter of ページごとに表示するレコード数
  public String getPageNumberSel() {
    return this.pageNumberSel;
  }
  
  //getter of 条件文
  public String getCondtionSQL() {
    return this.conditionSQL;
  }
  
  //getter of 並び順文
  public String getOrderBySQL() {
    return this.orderBySQL;
  }
  
  //getter of 並び順
  public String getDispOrder() {
    return this.dispOrder;
  }
}