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

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

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.tools.SIFlagUtil;
import jp.co.sint.tools.SIUtil;

import org.apache.log4j.Category;

/**
 * @version $Id: SICtgry.java,v 1.0 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>
 * J.W.Chen 2003/06/25 Original
 */

public class SICtgry extends SIBasic {
  // ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  private String encode = SIConfig.SIENCODE_NONE;
  
  // カテゴリーコード
  private String ctgryCode = "";
  // カテゴリ名
  private String ctgryName = "";
  // 親カテゴリコード
  private String parentCtgryCode = "";
  
  // 親カテゴリ名
  private String parentCtgryName = "";
  
  // カテゴリ略語（ソート順）
  private String abbvCtgryName = "";
  
  // 7.1.1 ST0176 追加 ここから
  // カテゴリ件数
  private int cmdtyAmount = 0;
  // 7.1.1 ST0176 追加 ここまで
  
  // 7.2.0 ST0301 追加 ここから
  // 商品の初期表示
  private String cmdtyFlagTxt = SIConfig.SICMDTY_FLAG_ADVICE;
  
  // 7.2.0 ST0301 追加 ここまで
  
  /**
   * <b>SICtgry</b> コンストラクタ
   * 
   * @param なし
   * @return なし
   * @throws なし
   */
  public SICtgry() {}
  
  public SICtgry(String lCtgryCode) {
    this.ctgryCode = lCtgryCode;
  }
  
  public void setEncode(String lEncode) {
    if (SIUtil.isNull(lEncode)) encode = SIConfig.SIENCODE_NONE;
    encode = lEncode;
  }
  
  /**
   * <b>SICtgry</b> コンストラクタ
   * 
   * @param code カテゴリーコード
   * @param parentCtgryCode 親カテゴリコード
   * @param name カテゴリー名
   * @param abbvCtgryName カテゴリー略語
   * @return なし
   * @throws なし
   */
  public SICtgry(String ctgryCode, String parentCtgryCode, String ctgryName, String abbvCtgryName) {
    this.setCtgryCode(ctgryCode);
    this.setParentCtgryCode(parentCtgryCode);
    this.setCtgryName(ctgryName);
    this.setAbbvCtgryName(abbvCtgryName);
  }
  
  // setter of カテゴリーコード
  public void setCtgryCode(String lCtgryCode) {
    if (SIUtil.isNull(lCtgryCode)) lCtgryCode = "";
    this.ctgryCode = SIUtil.changeTo(lCtgryCode.trim(), this.encode);
  }
  
  // setter of 親カテゴリーコード
  public void setParentCtgryCode(String lParentCtgryCode) {
    if (SIUtil.isNull(lParentCtgryCode)) lParentCtgryCode = "";
    this.parentCtgryCode = SIUtil.changeTo(lParentCtgryCode.trim(), this.encode);
  }
  
  // setter of 親カテゴリー名
  public void setParentCtgryName(String lParentCtgryName) {
    if (SIUtil.isNull(lParentCtgryName)) lParentCtgryName = "";
    this.parentCtgryName = SIUtil.changeTo(lParentCtgryName.trim(), this.encode);
  }
  
  // setter of カテゴリー名
  public void setCtgryName(String lCtgryName) {
    if (SIUtil.isNull(lCtgryName)) lCtgryName = "";
    this.ctgryName = SIUtil.changeTo(lCtgryName.trim(), this.encode);
  }
  
  // setter of カテゴリー略語（ソート順）
  public void setAbbvCtgryName(String lAbbvCtgryName) {
    if (SIUtil.isNull(lAbbvCtgryName)) lAbbvCtgryName = "";
    this.abbvCtgryName = SIUtil.changeTo(lAbbvCtgryName.trim(), this.encode);
  }
  
  // 7.1.1 ST0176 追加 ここから
  // setter of カテゴリ件数
  public void setCmdtyAmount(int lCmdtyAmount) {
    this.cmdtyAmount = lCmdtyAmount;
  }
  
  // 7.1.1 ST0176 追加 ここまで
  
  // 7.2.0 ST0301 追加 ここから
  // setter of 商品の初期表示
  public void setCmdtyFlagTxt(String lCmdtyFlagTxt) {
    if (SIUtil.isNull(lCmdtyFlagTxt)) lCmdtyFlagTxt = SIConfig.SICMDTY_FLAG_ADVICE;
    this.cmdtyFlagTxt = lCmdtyFlagTxt;
  }
  
  // 7.2.0 ST0301 追加 ここまで
  
  // getter of カテゴリーコード
  public String getCtgryCode() {
    return this.ctgryCode;
  }
  
  // getter of 親カテゴリーコード
  public String getParentCtgryCode() {
    return this.parentCtgryCode;
  }
  
  // getter of 親カテゴリー名
  public String getParentCtgryName() {
    return this.parentCtgryName;
  }
  
  // getter of カテゴリー名
  public String getCtgryName() {
    return this.ctgryName;
  }
  
  // getter of カテゴリー略語（ソート順）
  public String getAbbvCtgryName() {
    return this.abbvCtgryName;
  }
  
  // 7.1.1 ST0176 追加 ここから
  // getter of カテゴリ件数
  public int getCmdtyAmount() {
    return this.cmdtyAmount;
  }
  
  // 7.1.1 ST0176 追加 ここまで
  
  // 7.2.0 ST0301 追加 ここから
  // getter of 商品の初期表示
  public String getCmdtyFlagTxt() {
    return this.cmdtyFlagTxt;
  }
  
  // 7.2.0 ST0301 追加 ここまで
  
  /**
   * <b>reset</b> カテゴリー名を取得します
   * 
   * @param lConnection
   * @return
   * @throws なし
   */
  public void reset(Connection lConnection) {
    Statement lStatement = null;
    ResultSet lResultSet = null;
    StringBuffer lSqlBuf = new StringBuffer();
    
    lSqlBuf.append("SELECT aa.*,");
    lSqlBuf.append("(SELECT CtgryName FROM CtgryMTbl WHERE CtgryCode=aa.ParentCtgryCode) AS ParentCtgryName ");
    lSqlBuf.append("FROM CtgryMTbl").append(SIDBMultiConf.SIALIAS_CURR_NAME).append("aa ");
    lSqlBuf.append("WHERE CtgryCode=" + SIDBUtil.SQL2Str(ctgryCode));
    log.debug("reset:lSqlBuf=" + lSqlBuf.toString());
    
    try {
      this.setEncode(SIConfig.SIENCODE_NONE);
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlBuf.toString());
      // テロップメッセージの構成
      if (lResultSet.next()) {
        this.setParentCtgryCode(lResultSet.getString("ParentCtgryCode"));
        this.setParentCtgryName(lResultSet.getString("ParentCtgryname"));
        this.setCtgryName(lResultSet.getString("CtgryName"));
        this.setAbbvCtgryName(lResultSet.getString("AbbvCtgryName"));
        this.setCmdtyFlagTxt(lResultSet.getString("CmdtyFlag"));// 7.2.0 ST0301 追加
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      SIDBUtil.close(lStatement, lResultSet);
    }
  }
  
  /**
   * <b>getChildCollection</b> 子カテゴリーのコレクションを取得します
   * 
   * @param Connection
   * @param lCtgryCode カテゴリーコード
   * @return Collection 子カテゴリー
   * @throws なし
   */
  public static Collection getChildCollection(Connection lConnection, String lCtgryCode) {
    Collection lResColl = new ArrayList();
    Statement lStatement = null;
    ResultSet lResultSet = null;
    StringBuffer lSqlBuf = new StringBuffer("SELECT * FROM CtgryMTbl ");
    lSqlBuf.append("WHERE parentctgrycode=").append(SIDBUtil.SQL2Str(lCtgryCode, " "));
    lSqlBuf.append("ORDER BY abbvCtgryName ");
    
    log.debug("getChildCollection:lSqlBuf=" + lSqlBuf.toString());
    SICtgry lCtgry = new SICtgry();
    
    try {
      lCtgry.setEncode(SIConfig.SIENCODE_NONE);
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlBuf.toString());
      
      // カテゴリリスト
      while (lResultSet.next()) {
        lCtgry = new SICtgry();
        lCtgry.setCtgryCode(lResultSet.getString("ctgryCode"));
        lCtgry.setCtgryName(lResultSet.getString("ctgryName"));
        lCtgry.setParentCtgryCode(lResultSet.getString("parentCtgryCode"));
        lCtgry.setAbbvCtgryName(lResultSet.getString("abbvCtgryName"));
        lCtgry.setCmdtyFlagTxt(lResultSet.getString("cmdtyFlag"));// 7.2.0 ST0301 追加
        
        lResColl.add(lCtgry);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      SIDBUtil.close(lStatement, lResultSet);
    }
    
    return lResColl;
  }
  
  /**
   * <b>getNameCollection</b> 全カテゴリの一覧を取得
   * 
   * @param Connection
   * @param custCode
   * @return Collection
   * @throws なし
   */
  public static Collection getNameCollection(Connection lConnection) {
    StringBuffer lSqlBuf = new StringBuffer("SELECT ctgrycode||'-'||ctgryname AS ctgryname,ctgrycode FROM ctgrymtbl ");
    lSqlBuf.append(" WHERE ctgrycode BETWEEN 'AA' AND 'ZZ' AND ctgrycode LIKE '__' ");
    lSqlBuf.append(" AND ctgrycode NOT IN (");
    for (int i=0;i<SIConfig.INVISIBLECTGRYS.length;i++) {
      if (i>0) lSqlBuf.append(",");
      lSqlBuf.append(SIDBUtil.SQL2Str(SIConfig.INVISIBLECTGRYS[i]));
    }
    lSqlBuf.append(",'UJ','SF','ZM') ");
    lSqlBuf.append(" ORDER BY abbvCtgryName ASC");
    
    Collection lResultColl = new ArrayList();
    
    try {
      lResultColl = SIDBUtil.getCollection(lConnection, lSqlBuf.toString(), false, true, 13);
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    
    return lResultColl;
  }
  
  public static Collection getNameCollectionFull(Connection lConnection) {
    StringBuffer lSqlBuf = new StringBuffer("SELECT ctgryname,ctgrycode FROM ctgrymtbl ");
    lSqlBuf.append(" WHERE ctgrycode != ").append(SIDBUtil.SQL2Str(SIConfig.SICATEGORY_ROOT_CODE));
    lSqlBuf.append(" ORDER BY abbvCtgryName ASC");
    
    Statement lStatement =null;
    ResultSet lResultSet =null;
    Collection lResultColl = new ArrayList();
    
    try {
      int limitLength = 10;
      lResultColl.add(SIFlagUtil.getBlank());
      
      SINameValue lNameValue=new SINameValue();
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      while (lResultSet.next()){
        String name = lResultSet.getString(1);
        String value = lResultSet.getString(2);
        if(limitLength>0&&name.length()>limitLength) name=name.substring(0,limitLength) + "("+value+")";
        else name=name + "("+value+")";
        
        lNameValue=new SINameValue(name,value);
        lResultColl.add(lNameValue);
      }
    } catch (SQLException e) {
      e.printStackTrace();
      SIDBUtil.close(lStatement,lResultSet);
    }
    return lResultColl;
  }
  
  /**
   * <b>getNameCollection</b> 親カテゴリの一覧を取得
   * 
   * @param Connection
   * @param custCode
   * @return Collection
   * @throws なし
   */
  public static Collection getParentNameCollection(Connection lConnection) {
    StringBuffer lSqlBuf = new StringBuffer("SELECT ctgryname,ctgrycode FROM ctgrymtbl ");
    lSqlBuf.append(" WHERE parentctgrycode = ").append(SIDBUtil.SQL2Str(SIConfig.SICATEGORY_ROOT_CODE));
    lSqlBuf.append(" AND ctgrycode !=  'category110'");
    lSqlBuf.append(" ORDER BY abbvctgryname ASC");
    
    Collection lResultColl = new ArrayList();
    
    try {
      lResultColl = SIDBUtil.getCollection(lConnection, lSqlBuf.toString(), false, true);
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    
    return lResultColl;
  }
  
  // 携帯の子カテ一覧表示用SQL
  public static String getChildListSql(String mother) {
    StringBuffer str = new StringBuffer();
    str.append("SELECT ctgryName,ctgryCode FROM ctgrymtbl WHERE parentCtgryCode=").append(SIDBUtil.SQL2Str(mother, " "));
    if(SIUtil.isNotNull(invisibleStr())) str.append("AND ctgryCode NOT IN(").append(invisibleStr()).append(") ");
    str.append("ORDER BY abbvCtgryName");
    return str.toString();
  }
  
  // 非表示設定カテゴリ
  public static String invisibleStr() {
    if(SIConfig.INVISIBLECTGRYS == null) return "";
    StringBuffer str = new StringBuffer();
    for (int i = 0; i < SIConfig.INVISIBLECTGRYS.length; i++) {
      str.append(SIDBUtil.SQL2Str(SIConfig.INVISIBLECTGRYS[i]));
      str.append(i < SIConfig.INVISIBLECTGRYS.length - 1 ? "," : "");
    }
    return str.toString();
  }
}