/**
 * 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.SILogin;
import jp.co.sint.basic.SIUser;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIDBMultiConf;
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.SICustomErrors;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLParameter;
import jp.co.sint.tools.SICheckDataConf;
import jp.co.sint.tools.SICheckValid;

import org.apache.log4j.Category;

/**
 * @version $Id : UIUserListCond.java,v 1.0 Exp $
 * <br>Description :管理用ユーザの一覧・検索の画面に対するbeansクラスを作成します。
 */

public class UIUserListCond extends SIBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //ショップコード
  private String shopCodeTxt="";
  
  //ショップ名
  private String shopNameTxt="";
  
  //ユーザーコード
  private String userCode="";
  
  //管理ユーザーコード
  private String userCodeTxt="";
  
  //ユーザー名
  private String userNameTxt="";
  
  //SQL検索のばあいに、条件文
  private String conditionSQL="";
  
  //並び順の文
  private String orderBySQL=" ORDER BY a.MallShopCode,a.UserCode,a.UserName ";
  
  private boolean searchFlg = true;
  
  /**
   * UIUserListCond
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  public UIUserListCond(){}
  
  /**
   * UIUserListCond
   * コンストラクタ
   * @param lRequest リクエスト
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public UIUserListCond(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    this.init(lRequest,lUrlParam);
  }
  
  //setter of ショップコード
  public void setShopCodeTxt(String lshopCodeTxt){
    if (SIUtil.isNull(lshopCodeTxt)) lshopCodeTxt="";
    this.shopCodeTxt=SIUtil.changeTo(lshopCodeTxt.trim(),this.encode);
  }
  
  //setter of ショップ名
  public void setShopNameTxt(String lshopNameTxt){
    if (SIUtil.isNull(lshopNameTxt)) lshopNameTxt="";
    this.shopNameTxt=lshopNameTxt.trim();
  }
  
  //setter of 管理者ID
  public void setUserCodeTxt(String luserCodeTxt){
    if (SIUtil.isNull(luserCodeTxt)) luserCodeTxt="";
    this.userCodeTxt=SIUtil.changeTo(luserCodeTxt.trim(),this.encode);
  }
  
  //setter of 名前
  public void setUserNameTxt(String luserNameTxt){
    if (SIUtil.isNull(luserNameTxt)) luserNameTxt="";
    this.userNameTxt=SIUtil.changeTo(luserNameTxt.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 getShopNameTxt(){
    return this.shopNameTxt;
  }
  
  //getter of ショップコード
  public String getShopCodeTxt(){
    return this.shopCodeTxt;
  }
  
  //getter of ユーザコード
  public String getUserCode(){
    return this.userCode;
  }
  
  //getter of 管理者ID
  public String getUserCodeTxt(){
    return this.userCodeTxt;
  }
  
  //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 getUserNameTxt(){
    return this.userNameTxt;
  }
  
  /**
   * <b>init</b>
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request クライアントからリクエスト
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    super.init(lRequest,lUrlParam);
    if (lLogin.isShop()){
      this.setShopCodeTxt(lLogin.getMallShopCode());
    }else{
      this.setShopCodeTxt((String)lUrlParam.getParam("shopCodeTxt"));
    }
    this.setUserCodeTxt((String)lUrlParam.getParam("userCodeTxt"));
    this.setUserNameTxt((String)lUrlParam.getParam("userNameTxt"));
    this.setPageNumberSel((String)lUrlParam.getParam("pageNumberSel"));
  }
  
  /**
   * <b>getCollection</b>
   * 条件に合ったレコードを検索して、結果のコネクションを作成して、戻します。
   * @param lConnection データベースへの接続コネクション
   * @return レコードのセット
   * @throws SIDBAccessException
   */
  public Collection getCollection(Connection lConnection, SILogin lLogin) throws SIDBAccessException{
    Statement lStatement=null;
    ResultSet lResultSet=null;
    SIUser lUser = new SIUser();
    Collection lUserNameColl=new ArrayList();
    
    StringBuffer lSqlBuf=new StringBuffer();
    
    if(!this.searchFlg){
      return lUserNameColl;
    }
    
    //基本のSQL
    lSqlBuf.append("SELECT a.MallShopCode,a.UserCode,a.UserName ");
    lSqlBuf.append("FROM MNGUSERMTBL a ");
    if (lLogin.isShop()){
      lSqlBuf.append("WHERE MallShopCode=").append(SIDBUtil.SQL2Str(lLogin.getMallShopCode()," "));
    }else{
      lSqlBuf.append("WHERE 1=1 ");
    }
    //検索の条件
    lSqlBuf.append(this.conditionSQL);
    //出力順
    lSqlBuf.append(this.getOrderBySQL());
    log.debug("getCollection:lSqlBuf="+lSqlBuf.toString());
    //実行
    try{
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      
      //ユーザ情報レコードのセットの作成
      while (lResultSet.next()){
        lUser = new SIUser();
        lUser.setEncode(SIConfig.SIENCODE_NONE);
        lUser.setShopCode(lResultSet.getString("MallShopCode"));
        lUser.setUserCode(lResultSet.getString("UserCode"));
        lUser.setUserName(lResultSet.getString("UserName"));
        lUserNameColl.add(lUser);
      }
    }catch(Exception ex){
      throw new SIDBAccessException(ex);
    }finally{
      SIDBUtil.close(lResultSet,lStatement);
    }
    return lUserNameColl;
  }
  
  /**
   * <b>validate</b>
   * 入力したデータをチェックして、同時にSQLの条件文を作成します。
   * @param lRequest クライアントからのリクエスト
   * @return なし
   * @throws なし
   */
  public void validate(HttpServletRequest lRequest){
    SICustomErrors errors=new SICustomErrors();
    SITableConditionManager lConditionMan=new SITableConditionManager();
    
    //ショップコード
    if (SIUtil.isNotNull(this.getShopCodeTxt()) && SICheckValid.checkValid(errors, "ショップコード", this.getShopCodeTxt(), SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE)){
      lConditionMan.add(new SITableCondition("a", "MallShopCode", this.getShopCodeTxt(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    //管理者ID
    if (SIUtil.isNotNull(this.getUserCodeTxt()) && SICheckValid.checkValid(errors, "管理者ID", this.getUserCodeTxt(), SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE)){
      lConditionMan.add(new SITableCondition("a", "UserCode", this.getUserCodeTxt(), SIConfig.SICONDITION_TYPE_FRONTLIKE, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //名前
    if (SIUtil.isNotNull(this.getUserNameTxt())){
      lConditionMan.add(new SITableCondition("a", "UserName", this.getUserNameTxt(), SIConfig.SICONDITION_TYPE_LIKE, SIConfig.SICONDITION_TYPE_AND));
    }
    //エラーをセッションに設定します。
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    if (!errors.isEmpty()){
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY,errors);
      this.searchFlg=false;
    } else {
      lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    }
    
    //条件文の設定
    this.setConditionSQL(lConditionMan.getCondtionSQL());
  }
}