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

import java.util.*;
import java.sql.*;
import org.apache.log4j.Category;
import jp.co.sint.database.*;
import jp.co.sint.tools.*;
import jp.co.sint.config.*;

/**
 * @version $Id: SICmdtyManager.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 SICmdtyManager {
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  //カテゴリセット
  private Collection cmdties = new ArrayList();
  private String categoryCon="";
  //検索条件
  private Collection conditionColl=new java.util.ArrayList();
  //出力順
  private String outputOrder="";
  //検索用の項目に対するデータ
  private String searchShopCode="";
  private boolean searchNewCmdty=false;
  private boolean searchAdvice=false;
  private boolean searchSales=false;

  public SICmdtyManager(){
  }

  public void setCategorySQL(String categoryCon){
    this.categoryCon=categoryCon;
  }

  public void addCondtion(String condition){
    conditionColl.add(condition) ;
  }

  /**
   * <b>addCondition</b>
   * 検索条件を作成します。
   * @param  Collection　
   * @return なし
   * @throws なし
   */
  public void addCondition(Collection condition){
    SITableCondition tableCondition=new SITableCondition();
    tableCondition.setItemName("connectionName");
    tableCondition.setItemValue(condition);
    tableCondition.setCompareType(SIConfig.SICONDITION_TYPE_EQUAL);
    tableCondition.setAndOrType(SIConfig.SICONDITION_TYPE_AND);
    conditionColl.add(tableCondition);
  }

  public void addCondition(SITableCondition condition){
    conditionColl.add(condition);
  }

  public void setSearchShopCode(String searchShopCode){
    this.searchShopCode=searchShopCode;
  }

  public void setSearchNewCmdty(boolean searchNewCmdty){
    this.searchNewCmdty=searchNewCmdty;
  }

  public void setSearchAdvice(boolean searchAdvice){
    this.searchAdvice=searchAdvice;
  }

  public void setSearchSales(boolean searchSales){
    this.searchSales=searchSales;
  }

  public Collection getCollection(Connection lConnection) throws SIDBAccessException {
    Statement statement=null;
    ResultSet resultSet=null;
    SICmdty currCmdty=new SICmdty();

    StringBuffer lSqlBuf=new StringBuffer();
    cmdties=new ArrayList();
    //出力項目
    lSqlBuf.append("SELECT CmdtyMTbl.* FROM CmdtyMTbl,CmdtyCtgryMTbl ");
    //結合の条件
    lSqlBuf.append("WHERE CmdtyMTbl.ShopCode=CmdtyCtgryMTbl.ShopCode ");
    lSqlBuf.append("AND CmdtyMTbl.CmdtyCode=CmdtyCtgryMTbl.CmdtyCode ");
    //検索の条件
    lSqlBuf.append(SIDBUtil.getCondition(conditionColl));
    //出力順
    if (SIUtil.isNotNull(outputOrder)) {
      lSqlBuf.append(outputOrder);
    }
    //実行
    try{
      statement=lConnection.createStatement();
      log.debug("lSqlBuf="+lSqlBuf.toString());
      resultSet=statement.executeQuery(lSqlBuf.toString());

      //商品レコードのセットの作成
      while (resultSet.next()){
        currCmdty=new SICmdty();
        currCmdty.setShopCode(resultSet.getString("ShopCode"));
        currCmdty.setCmdtyCode(resultSet.getString("CmdtyCode"));
        currCmdty.setCmdtyName(resultSet.getString("CmdtyName"));
        currCmdty.setDescription(resultSet.getString("Description"));
        currCmdty.setUnitPrice(resultSet.getString("UnitPrice"));
        currCmdty.setSrchKeyName1(resultSet.getString("SrchKeyName1"));
        currCmdty.setSrchKeyName2(resultSet.getString("SrchKeyName2"));
        currCmdty.setSrchKeyName3(resultSet.getString("SrchKeyName3"));
        cmdties.add(currCmdty);
      }
    }catch(Exception ex){
      throw new SIDBAccessException(ex);
    }finally{
      SIDBUtil.close(resultSet,statement);
    }
    return cmdties;
  }

  /**
   * 出力順のSQL文の設定
   * @param outputOrder 出力順の文
   * @return なし
   * @throw なし
   */
  public void setOutputOrder(String outputOrder){
    this.outputOrder =outputOrder;
  }

  public void addCondition(){

  }
}