/**
 * Created on 2003/08/07
 *
 * 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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIBasic;
import jp.co.sint.basic.SITax;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SITableConditionManager;
import jp.co.sint.tools.SICheckDataConf;
import jp.co.sint.tools.SICheckValid;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
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 UITaxList extends SIBasic {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //全ての条件を保存する用の
  SITableConditionManager tableConditionMan = new SITableConditionManager();
  
  private String startDate;
  private String tax = "0";
  
  /**
   * init
   * 入力したデータから、このbeansを設定します。
   * @param SIURLParameter
   * @return なし
   * @throws なし
   */
  //7.1.1 ST0236 修正 ここから
  public void init(SIURLParameter lUrlParam) {
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    this.setStartDate((String)lUrlParam.getParam("startDate"));
    this.setTax((String)lUrlParam.getParam("tax"));
  }
  //7.1.1 ST0236 修正 ここまで
  
  /**
   * validate
   * 入力したデータをチェックします。
   * 不正なデータがある場合、エラーをオブジェクトに格納します。
   * そのオブジェクトは、エラーメッセージとして画面に表示されます。
   * @param HttpServletRequest ，Connection , SIURLParameter
   * @return true:エラーがない false:エラーが１つ以上ある
   * @throws なし
   */
  public boolean validate(HttpServletRequest request,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SICustomErrors errors = new SICustomErrors();
    
    SICheckValid.checkValid(errors, "適用開始日", this.getStartDate(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DATE_TYPE);//7.1.1 ST0162 修正
    
    if (!((String)lUrlParam.getParam("editModeTxt")).equals(SIConfig.SIEDIT_MODE_DELETE)) {//7.1.1 ST0236 修正
      //7.1.1 ST0162 修正 ここから
      SICheckValid.checkValid(errors, "消費税率", this.getTax(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
      SICheckValid.checkValid(errors, "消費税率", this.getTax(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 3);
      //7.1.1 ST0162 修正 ここまで
    }
    
    request.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    if (!errors.isEmpty())
      request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    return errors.isEmpty();
  }
  
  /**
   * getCollection
   * 条件に合ったレコードを検索して、結果のコネクションを作成して、戻します。
   * @param Connection
   * @return Collection
   * @throws SIDBAccessException
   */
  public Collection getCollection(Connection dbConnection) throws SIDBAccessException {
    Statement statement = null;
    ResultSet resultSet = null;
    SITax currTax = new SITax();
    StringBuffer lSqlBuf = new StringBuffer();
    Collection arytax = new ArrayList();
    
    lSqlBuf.append(" select startdate, tax from taxmtbl order by startdate asc");
    //実行
    try {
      statement = dbConnection.createStatement();
      resultSet = statement.executeQuery(lSqlBuf.toString());
      
      //商品レコードのセットの作成
      while (resultSet.next()) {
        currTax = new SITax();
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        currTax.setStartDate(df.format(resultSet.getTimestamp("startdate")));
        currTax.setTax(resultSet.getString("tax"));
        arytax.add(currTax);
      }
    } catch (Exception ex) {
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(resultSet, statement);
    }
    return arytax;
  }
  
  /**
   * @return
   */
  public SITableConditionManager getTableConditionMan() {
    return tableConditionMan;
  }
  
  /**
   * @param manager
   */
  public void setTableConditionMan(SITableConditionManager manager) {
    tableConditionMan = manager;
  }
  
  /**
   * @return
   */
  public String getStartDate() {
    return startDate;
  }
  
  /**
   * @param string
   */
  public void setStartDate(String string) {
    if (string == null) string = "";
    startDate = SIUtil.changeTo(string.trim(), this.encode);
  }
  
  /**
   * @return
   */
  public String getTax() {
    return tax;
  }
  
  /**
   * @param string
   */
  public void setTax(String string) {
    if (string == null) string = "";
    tax = SIUtil.changeTo(string.trim(), this.encode);
  }
}
