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

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

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SICmdtyStndrd;
import jp.co.sint.basic.SIFrontBasic;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLParameter; //7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @version $Id: UICmdtyInfo.java,v 1.0 2003/10/24 Exp $
 * @author  Jinwang Chen
 * <br>Description:
 * <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/10/24 14:08:04  Original
 */
public class UICmdtyInfo extends SIFrontBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //ショップコード
  private String shopCodeTxt="";
  
  //商品コード
  private String cmdtyCodeTxt="";
  
  //在庫コード 7.3.0 PI-NES0501 追加 ここから
  private String individualCodeTxt="";
  
  //参照画像番号
  private String referImageInx="";
  
  //参照可能な画像の枚数
  private String maxImageCount="";
  
  //参照画像情報 7.3.0 PI-NES0501 追加 ここまで
  // EDBTG003-00 nagayoshi mod start
//  private String imageName[]={"","","","",""};
//  private String imageInx[]={"","","","",""};
  private String imageName[]={"","","","","","","","","","",""};
  private String imageInx[]={"","","","","","","","","","",""};
  // EDBTG003-00 nagayoshi mod end
  //規格内容コード1
  private String elementCodeSel1="";
  
  //規格内容コード2
  private String elementCodeSel2="";
  
  //規格を見つかった
  private boolean  found=true;
  
  //戻るURL
  private String backURL="";
  
  public UICmdtyInfo(){
    this.setReferImageInx("1");
  }
  
  //setter of ショップコード
  public void setShopCodeTxt(String lShopCodeTxt){
    if (SIUtil.isNull(lShopCodeTxt)) lShopCodeTxt="";
    this.shopCodeTxt=SIUtil.changeTo(lShopCodeTxt.trim(),this.encode);
  }
  
  //setter of 商品コード
  public void setCmdtyCodeTxt(String lCmdtyCodeTxt){
    if (SIUtil.isNull(lCmdtyCodeTxt)) lCmdtyCodeTxt="";
    this.cmdtyCodeTxt=SIUtil.changeTo(lCmdtyCodeTxt.trim(),this.encode);
  }
  
  //setter of 規格内容コード1
  public void setElementCodeSel1(String lElementCodeSel1){
    if (SIUtil.isNull(lElementCodeSel1)) lElementCodeSel1="";
    this.elementCodeSel1=SIUtil.changeTo(lElementCodeSel1.trim(),this.encode);
  }
  
  //setter of 規格内容コード2
  public void setElementCodeSel2(String lElementCodeSel2){
    if (SIUtil.isNull(lElementCodeSel2)) lElementCodeSel2="";
    this.elementCodeSel2=SIUtil.changeTo(lElementCodeSel2.trim(),this.encode);
  }
  
  //setter of 戻るURL
  public void setBackURL(String lBackURL){
    if (SIUtil.isNull(lBackURL)) lBackURL="";
    this.backURL=SIUtil.changeTo(lBackURL.trim(),this.encode);
  }
  
  public void setFound(boolean lFound){
    this.found=lFound;
  }
  
  //getter of ショップコード
  public String getShopCodeTxt(){
    return this.shopCodeTxt;
  }
  
  //getter of 商品コード
  public String getCmdtyCodeTxt(){
    return this.cmdtyCodeTxt;
  }
  
  //getter of 規格内容コード1
  public String getElementCodeSel1(){
    return this.elementCodeSel1;
  }
  
  //getter of 規格内容コード2
  public String getElementCodeSel2(){
    return this.elementCodeSel2;
  }
  
  //getter of 戻るURL
  public String getBackURL(){
    return this.backURL;
  }
  
  public boolean getFound(){
    return this.found;
  }
  //7.3.0 PI-NES0501 追加 ここから
  //getter of 個別コード
  public String getIndividualCodeTxt() {
    return individualCodeTxt;
  }
  
  //setter of 個別コード
  public void setIndividualCodeTxt(String individualCode) {
    this.individualCodeTxt=individualCode;
  }
  
  //getter of 参照画像番号
  public String getReferImageInx() {
    return this.referImageInx;
  }
  
  //setter of 参照画像番号
  public void setReferImageInx(String lReferImageInx){
    this.referImageInx=lReferImageInx;
  }
  
  //getter of 参照可能な画像の枚数
  public String getMaxImageCount() {
    return maxImageCount;
  }
  
  //getter of 参照画像名
  public String getImageName(String inx) {
    int count= Integer.parseInt(inx)-1;
    return imageName[count];
  }
  
  public String[] getImageName() {
    return imageName;
  }
  
  //getter of 参照画像整理番号
  public String getImageInx(String inx) {
    int count= Integer.parseInt(inx)-1;
    return imageInx[count];
  }
  
  //setter of 参照可能な画像の枚数
  public void setMaxImageCount(String maxImageCount) {
    this.maxImageCount=maxImageCount;
  }
  
  //setter of 参照画像名
  public void setImageName(int count,String name) {
    this.imageName[count]=name;
  }
  
  //setter of 参照画像整理番号
  public void setImageInx(int count,String inx) {
    this.imageInx[count]=inx;
  }
  //7.3.0 PI-NES0501 追加 ここまで
  
  /**
   * <b>init</b>
   * トップ画面に、顧客さんが選択データを取得して、設定します。
   * @param request クライアントからリクエスト
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  //7.1.1 ST0236 修正 ここから
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    super.init(lRequest,lUrlParam);
    
    this.setShopCodeTxt((String)lUrlParam.getParam("shc"));
    this.setCmdtyCodeTxt((String)lUrlParam.getParam("cmc"));
    this.setIndividualCodeTxt((String)lUrlParam.getParam("inc"));//PI-NES0501 追加
    this.setElementCodeSel1((String)lUrlParam.getParam("elementCodeSel1"));
    this.setElementCodeSel2((String)lUrlParam.getParam("elementCodeSel2"));
    this.setBackURL((String)lUrlParam.getParam("backURL"));
    // EDBTG003-00 nagayoshi add start
    this.setCmdtyCompositionFlg((String)lUrlParam.getParam("cmpt"));
    // EDBTG003-00 nagayoshi add end
  }
  //7.1.1 ST0236 修正 ここまで
  //7.3.0 PI-NES0501 追加 ここから
  /**
   * <b>initPopup</b>
   * 商品詳細ポップアップ用の初期値を設定します
   * @param request クライアントからリクエスト
   * @param lUrlParam
   * @return なし
   * @throws SIDBAccessException
   */
  
  public void initPopup(HttpServletRequest lRequest,Connection lConnection,SIURLParameter lUrlParam)throws SIDBAccessException{
    this.setReferImageInx((String)lUrlParam.getParam("referImageInxTxt"));
    this.init(lRequest,lUrlParam);
    
    Statement lStatement=null;
    ResultSet lResultSet=null;
    StringBuffer lSqlBuf = new StringBuffer();
    
    try{
      lSqlBuf.append("SELECT CASE WHEN imagename1 IS NULL THEN '' ELSE imagename1 END AS imagename1 ");
      lSqlBuf.append(",CASE WHEN imagename2 IS NULL THEN '' ELSE imagename2 END AS imagename2 ");
      lSqlBuf.append(",CASE WHEN imagename3 IS NULL THEN '' ELSE imagename3 END AS imagename3 ");
      lSqlBuf.append(",CASE WHEN imagename4 IS NULL THEN '' ELSE imagename4 END AS imagename4 ");
      lSqlBuf.append(",CASE WHEN imagename5 IS NULL THEN '' ELSE imagename5 END AS imagename5 ");
      // EDBTG003-00 nagayoshi add start
      lSqlBuf.append(",CASE WHEN imagename6  IS NULL THEN '' ELSE imagename6 END AS imagename6 ");
      lSqlBuf.append(",CASE WHEN imagename7  IS NULL THEN '' ELSE imagename7 END AS imagename7 ");
      lSqlBuf.append(",CASE WHEN imagename8  IS NULL THEN '' ELSE imagename8 END AS imagename8 ");
      lSqlBuf.append(",CASE WHEN imagename9  IS NULL THEN '' ELSE imagename9 END AS imagename9 ");
      lSqlBuf.append(",CASE WHEN imagename10 IS NULL THEN '' ELSE imagename10 END AS imagename10 ");
      lSqlBuf.append(",CASE WHEN imagename11 IS NULL THEN '' ELSE imagename11 END AS imagename11 ");
      // EDBTG003-00 nagayoshi add end
      lSqlBuf.append(" FROM ").append("individualtbl");
      lSqlBuf.append(" WHERE cmdtycode = ").append(SIDBUtil.SQL2Str(this.cmdtyCodeTxt));
      lSqlBuf.append(" AND individualcode = ").append(SIDBUtil.SQL2Str(this.individualCodeTxt));
      
      log.debug("getImageInfo:lSqlBuf="+lSqlBuf.toString());
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      
      int count=0;
      String imageName1="";
      String imageName2="";
      String imageName3="";
      String imageName4="";
      String imageName5="";
      // EDBTG003-00 nagayoshi add start
      String imageName6="";
      String imageName7="";
      String imageName8="";
      String imageName9="";
      String imageName10="";
      String imageName11="";
      // EDBTG003-00 nagayoshi add end
      if (lResultSet.next()){
        
        imageName1=lResultSet.getString("imagename1");
        if (SIUtil.isNotNull(imageName1)){
          this.setImageInx(count,"1");
          this.setImageName(count,imageName1);
          count++;
        }
        
        imageName2=lResultSet.getString("imagename2");
        if (SIUtil.isNotNull(imageName2)){
          this.setImageInx(count,"2");
          this.setImageName(count,imageName2);
          count++;
        }
        
        imageName3=lResultSet.getString("imagename3");
        if (SIUtil.isNotNull(imageName3)){
          this.setImageInx(count,"3");
          this.setImageName(count,imageName3);
          count++;
        }
        
        imageName4=lResultSet.getString("imagename4");
        if (SIUtil.isNotNull(imageName4)){
          this.setImageInx(count,"4");
          this.setImageName(count,imageName4);
          count++;
        }
        
        imageName5=lResultSet.getString("imagename5");
        if (SIUtil.isNotNull(imageName5)){
          this.setImageInx(count,"5");
          this.setImageName(count,imageName5);
          count++;
        }
        // EDBTG003-00 nagayoshi add start
        imageName6=lResultSet.getString("imagename6");
        if (SIUtil.isNotNull(imageName6)){
          this.setImageInx(count,"6");
          this.setImageName(count,imageName6);
          count++;
        }
        
        imageName7=lResultSet.getString("imagename7");
        if (SIUtil.isNotNull(imageName7)){
          this.setImageInx(count,"7");
          this.setImageName(count,imageName7);
          count++;
        }
        
        imageName8=lResultSet.getString("imagename8");
        if (SIUtil.isNotNull(imageName8)){
          this.setImageInx(count,"8");
          this.setImageName(count,imageName8);
          count++;
        }
        
        imageName9=lResultSet.getString("imagename9");
        if (SIUtil.isNotNull(imageName9)){
          this.setImageInx(count,"9");
          this.setImageName(count,imageName9);
          count++;
        }
        
        imageName10=lResultSet.getString("imagename10");
        if (SIUtil.isNotNull(imageName10)){
          this.setImageInx(count,"10");
          this.setImageName(count,imageName10);
          count++;
        }
        
        imageName11=lResultSet.getString("imagename11");
        if (SIUtil.isNotNull(imageName11)){
          this.setImageInx(count,"11");
          this.setImageName(count,imageName11);
          count++;
        }
        // EDBTG003-00 nagayoshi add end
      }
      this.setMaxImageCount(String.valueOf(count));
    }catch(Exception ex){
      throw new SIDBAccessException(ex);
    }finally{
      SIDBUtil.close(lResultSet,lStatement);
    }
  }
  
  public String getCrrInx(){
    String crrInx = "";
    
    for (int i=0;i<5;i++){
      if(this.imageInx[i].equals(referImageInx)) crrInx=String.valueOf(i+1);
    }
    return crrInx;
  }
  
  public String getNextVal(String inx,boolean minflg){
    String nextInx ="";
    
    if (minflg){
      if(inx.equals("1")) nextInx=this.getMaxImageCount();
      else nextInx=SIUtil.sub(inx,"1");
    }else{
      if(inx.equals(this.getMaxImageCount())) nextInx="1";
      else nextInx=SIUtil.add(inx,"1");
    }
    return nextInx;
  }
  //7.3.0 PI-NES0501 追加 ここまで
  //7.1.1 ST0236 修正 ここから
  public void initStndrd(HttpServletRequest lRequest,Connection lConnection,SIURLParameter lUrlParam){
    this.init(lRequest,lUrlParam);
    
    this.setCartShopCodeTxt((String)lUrlParam.getParam("shc"));
    this.setCartCmdtyCodeTxt((String)lUrlParam.getParam("cmc"));
    this.setIndividualCodeTxt((String)lUrlParam.getParam("inc"));//PI-NES0501 追加
    this.setCartElementCodeTxt1((String)lUrlParam.getParam("elementCodeSel1"));//カートエレメント1
    this.setCartElementCodeTxt2((String)lUrlParam.getParam("elementCodeSel2"));//カートエレメント2
    this.setBackURL((String)lUrlParam.getParam("backURL"));
    //7.1.1 ST0236 修正 ここまで
    //商品規格関連
    SICmdtyStndrd cmdtyStndrd=new SICmdtyStndrd(getCartShopCodeTxt(),getCartCmdtyCodeTxt());
    try {
      cmdtyStndrd.reset(lConnection);
      boolean found=cmdtyStndrd.check(lConnection,getCartElementCodeTxt1(),getCartElementCodeTxt2());//チェック
      setFound(found);
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    
    this.setCartStndrdCodeTxt1(cmdtyStndrd.getStndrdCode1());//規格1
    this.setCartStndrdCodeTxt2(cmdtyStndrd.getStndrdCode2());//規格2
    this.setCartAmountTxt("1");
  }
}