/**
 * Created on 2003/11/13
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
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.SIDeliveryTime;
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 jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @author arai
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class UIDeliveryTimetableListCond extends SIDeliveryTime {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  private String deliveryTypeNameTxt = "";
  
  public UIDeliveryTimetableListCond() {}
  
  //7.1.1 ST0236 修正 ここから
  public void init(SIURLParameter lUrlParam) {
    this.setMallShopCode((String)lUrlParam.getParam("regMallShopCodeTxt"));
    this.setDeliveryTypeCode((String)lUrlParam.getParam("regDeliveryTypeCodeTxt"));
  }
  //7.1.1 ST0236 修正 ここまで
  
  public Collection getCollection(Connection lConnection) throws SIDBAccessException {
    Statement statement = null;
    ResultSet resultSet = null;
    Collection deliColl = new ArrayList();
    StringBuffer strBuf = new StringBuffer();
    SIDeliveryTime deliTime = new SIDeliveryTime();
    
    strBuf = new StringBuffer();
    strBuf.append("SELECT * FROM DeliveryTimeMtbl WHERE 1=1 ");
    
    SITableConditionManager lConditionMan = new SITableConditionManager();
    SITableCondition DD = new SITableCondition("", "mallshopcode", this.getMallShopCode(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(true);
    lConditionMan.add(DD);
    
    DD = new SITableCondition("", "deliveryTypeCode", this.getDeliveryTypeCode(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(true);
    lConditionMan.add(DD);
    
    strBuf.append(lConditionMan.getCondtionSQL());
    strBuf.append(" ORDER BY deliveryTimeFrom ASC, deliveryTimeTo ASC ");
    
    log.debug("UIDeliveryTimetableListCond sqlStatement=" + strBuf.toString());
    //検索の条件
    //出力順
    //実行
    try {
      statement = lConnection.createStatement();
      resultSet = statement.executeQuery(strBuf.toString());
      
      while (resultSet.next()) {
        deliTime = new SIDeliveryTime();
        deliTime.setEncode(SIConfig.SIENCODE_NONE);
        deliTime.setMallShopCode(resultSet.getString("mallShopCode"));
        deliTime.setDeliveryTypeCode(resultSet.getString("deliveryTypeCode"));
        deliTime.setDeliveryTimeFrom(resultSet.getString("deliveryTimeFrom"));
        deliTime.setDeliveryTimeTo(resultSet.getString("deliveryTimeTo"));
        
        deliColl.add(deliTime);
      }
    } catch (Exception ex) {
      log.debug("exception sql = " + strBuf.toString());
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(resultSet, statement);
    }
    return deliColl;
  }
  
  public String getDeliveryName(Connection lConnection) throws SIDBAccessException {
    Statement statement = null;
    ResultSet resultSet = null;
    StringBuffer strBuf = new StringBuffer();
    
    String deliveryName = "";
    
    strBuf = new StringBuffer();
    strBuf.append("SELECT deliveryTypeName FROM DeliveryTypeMtbl WHERE 1=1 ");
    
    SITableConditionManager lConditionMan = new SITableConditionManager();
    SITableCondition DD = new SITableCondition("", "mallshopcode", this.getMallShopCode(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(true);
    lConditionMan.add(DD);
    
    DD = new SITableCondition("", "deliveryTypeCode", this.getDeliveryTypeCode(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(true);
    lConditionMan.add(DD);
    
    strBuf.append(lConditionMan.getCondtionSQL());
    //検索の条件
    //出力順
    //実行
    try {
      statement = lConnection.createStatement();
      resultSet = statement.executeQuery(strBuf.toString());
      
      if (resultSet.next()) {
        deliveryName = resultSet.getString("deliveryTypeName");
      }
    } catch (Exception ex) {
      log.debug("exception sql = " + strBuf.toString());
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(resultSet, statement);
    }
    
    return deliveryName;
  }
  
  /**
   * @return
   */
  public String getDeliveryTypeNameTxt() {
    return deliveryTypeNameTxt;
  }
  
  /**
   * @param string
   */
  public void setDeliveryTypeNameTxt(String string) {
    if (string == null) string = "";
    deliveryTypeNameTxt = SIUtil.changeTo(string, this.encode);
  }
}
