/**
 * @version $Id : UIAddressListCond.java,v 1.0 Exp $
 * @author      : XXXX　
 * <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>
 * 							  2003/08/05  Original
 *
 * 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 jp.co.sint.basic.SICustAddress;
import jp.co.sint.basic.SIBasic;
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 org.apache.log4j.Category;

public class UIAddressListCond extends SIBasic {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  //全ての条件を保存する用
  SITableConditionManager tableConditionMan = new SITableConditionManager();

  private String custCode = "";

  public UIAddressListCond() {
  }

  public Collection getCollection(Connection dbConnection) throws SIDBAccessException {
    Statement statement = null;
    ResultSet resultSet = null;
    SICustAddress currAddress = new SICustAddress();
    StringBuffer lSqlBuf = new StringBuffer();
    Collection address = new ArrayList();

    if (this.getCustCode() == null) {
      return null;
    }

    SITableConditionManager lConditionMan = new SITableConditionManager();
    SITableCondition DD = new SITableCondition();

    //出力項目
    lSqlBuf.append(" SELECT * FROM custAddressTbl WHERE 1=1 ");
    DD = new SITableCondition("", "custCode", this.getCustCode(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(true);
    lConditionMan.add(DD);
    lSqlBuf.append(lConditionMan.getCondtionSQL());

    //検索の条件
    //出力順
    //実行
    try {
      statement = dbConnection.createStatement();
      resultSet = statement.executeQuery(lSqlBuf.toString());

      //商品レコードのセットの作成
      while (resultSet.next()) {
        currAddress = new SICustAddress();
        currAddress.setEncode(SIConfig.SIENCODE_NONE);

        currAddress.setDeliveryName(resultSet.getString("DeliveryName"));
        currAddress.setDeliveryAddressee(resultSet.getString("DeliveryAddressee"));
        currAddress.setCompanyName(resultSet.getString("CompanyName"));
        currAddress.setCustCompanyFlg(resultSet.getString("CustCompanyFlg"));
        currAddress.setPostCode1(resultSet.getString("postCode1"));
        currAddress.setPostCode2(resultSet.getString("postCode2"));
        currAddress.setAddress1(resultSet.getString("address1"));
        currAddress.setAddress2(resultSet.getString("address2"));
        currAddress.setAddress3(resultSet.getString("address3"));

        currAddress.setLastDeliveryDate(SIDBUtil.getDate(resultSet.getTimestamp("lastDeliveryDate")));
        address.add(currAddress);
      }
    } catch (Exception ex) {
      log.debug("exception lSqlBuf = " + lSqlBuf.toString());
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(resultSet, statement);
    }
    return address;
  }

  /**
   * @return
   */
  public String getCustCode() {
    return custCode;
  }

  /**
   * @param string
   */
  public void setCustCode(String string) {
    custCode = string;
  }

}