/**
 * 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.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SICtgry;
import jp.co.sint.basic.SICtgryManager;
import jp.co.sint.basic.SINameValue;
import jp.co.sint.basic.SIPointRule;
import jp.co.sint.basic.SISrchCtgry;
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.database.SIDatabaseConnection;
import jp.co.sint.tools.SICheckDataConf;
import jp.co.sint.tools.SICheckUtil;
import jp.co.sint.tools.SICheckValid;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIDateTime;
import jp.co.sint.tools.SIStringUtil;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLParameter;// 7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @version $Id: UICtgryListCond.java,v 1.0 2003/09/25 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>
 * Jinwang Chen 2003/09/25 12:22:27 Original
 */
public class UICtgryListCond extends SICtgry {
  
  // ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  private String editModeRdo = this.editModeTxt;
  
  private String clickedCtgryCodeTxt = SIConfig.SICATEGORY_ROOT_CODE;
  private SISrchCtgry srchCtgry = new SISrchCtgry();
  
  private String bonusFromDate = "";
  private String bonusFromDateYear = "";
  private String bonusFromDateMonth = "";
  private String bonusFromDateDay = "";
  private String bonusToDate = "";
  private String bonusToDateYear = "";
  private String bonusToDateMonth = "";
  private String bonusToDateDay = "";
  private String bonusPointRate = "";
  private String bonusPointEditMode ="0";//0:new 1:mod
  
  public UICtgryListCond() throws SQLException, NamingException {
    SIDatabaseConnection lDatabaseConnection = new SIDatabaseConnection();
    this.setParentCtgryCode(this.getClickedCtgryCodeTxt());
    
    SICtgry lCtgry = new SICtgry();
    lCtgry.setCtgryCode(getParentCtgryCode());
    lCtgry.reset(lDatabaseConnection.getConnection());
    this.setEncode(SIConfig.SIENCODE_NONE);
    this.setParentCtgryName(lCtgry.getCtgryName());
    lDatabaseConnection.close();
    SIDateTime lDateTime = new SIDateTime();
    lDateTime.addDay(1);
    String Month = Integer.toString(lDateTime.getMonth());
    String Day = Integer.toString(lDateTime.getDay());
    
    if (Month.length() == 1) Month = "0" + Month;
    if (Day.length() == 1) Day = "0" + Day;
    
    // ボーナス適用期間の年(From)
    bonusFromDateYear = Integer.toString(lDateTime.getYear());
    // ボーナス適用期間の月(From)
    bonusFromDateMonth = Month;
    // ボーナス適用期間の日(From)
    bonusFromDateDay = Day;
    
    // 一ヶ月後の日付をゲットします
    lDateTime.addMonth(1);
    Month = Integer.toString(lDateTime.getMonth());
    Day = Integer.toString(lDateTime.getDay());
    if (Month.length() == 1) Month = "0" + Month;
    if (Day.length() == 1) Day = "0" + Day;
    
    // ボーナス適用期間の年(To)
    bonusToDateYear = Integer.toString(lDateTime.getYear());
    // ボーナス適用期間の月(To)
    bonusToDateMonth = Month;
    // ボーナス適用期間の日(To)
    bonusToDateDay = Day;
  }
  
  public void setEditModeRdo(String lEditModeRdo) {
    if (SIUtil.isNull(lEditModeRdo)) lEditModeRdo = SIConfig.SIEDIT_MODE_INSERT;
    editModeRdo = lEditModeRdo;
  }
  
  public void setSrchCtgry(SISrchCtgry lSrchCtgry) {
    if (lSrchCtgry == null) lSrchCtgry = new SISrchCtgry();
    this.srchCtgry = lSrchCtgry;
  }
  
  public void setClickedCtgryCodeTxt(String lClickedCtgryCodeTxt) {
    if (SIUtil.isNull(lClickedCtgryCodeTxt)) lClickedCtgryCodeTxt = SIConfig.SICATEGORY_ROOT_CODE;
    this.clickedCtgryCodeTxt = SIUtil.changeTo(lClickedCtgryCodeTxt.trim(), getEncode());
  }
  
  public String getClickedCtgryCodeTxt() {
    return this.clickedCtgryCodeTxt;
  }
  
  public SISrchCtgry getSrchCtgry() {
    return srchCtgry;
  }
  
  public String getEditModeRdo() {
    return this.editModeRdo;
  }
  
  /**
   * @return
   */
  public String getBonusFromDate() {
    return bonusFromDate;
  }
  
  /**
   * @return
   */
  public String getBonusPointRate() {
    return bonusPointRate;
  }
  
  /**
   * @return
   */
  public String getBonusToDate() {
    return bonusToDate;
  }
  
  /**
   * @return
   */
  public String getBonusFromDateDay() {
    return bonusFromDateDay;
  }
  
  /**
   * @return
   */
  public String getBonusFromDateMonth() {
    return bonusFromDateMonth;
  }
  
  /**
   * @return
   */
  public String getBonusFromDateYear() {
    return bonusFromDateYear;
  }
  
  /**
   * @param string
   */
  public void setBonusFromDateDay(String string) {
    bonusFromDateDay = string;
  }
  
  /**
   * @param string
   */
  public void setBonusFromDateMonth(String string) {
    bonusFromDateMonth = string;
  }
  
  /**
   * @param string
   */
  public void setBonusFromDateYear(String string) {
    bonusFromDateYear = string;
  }
  
  /**
   * @param category
   */
  public void setLog(Category category) {
    log = category;
  }
  
  /**
   * @return
   */
  public String getBonusToDateDay() {
    return bonusToDateDay;
  }
  
  /**
   * @return
   */
  public String getBonusToDateMonth() {
    return bonusToDateMonth;
  }
  
  /**
   * @return
   */
  public String getBonusToDateYear() {
    return bonusToDateYear;
  }
  
  /**
   * @param string
   */
  public void setBonusToDateDay(String string) {
    bonusToDateDay = string;
  }
  
  /**
   * @param string
   */
  public void setBonusToDateMonth(String string) {
    bonusToDateMonth = string;
  }
  
  /**
   * @param string
   */
  public void setBonusToDateYear(String string) {
    bonusToDateYear = string;
  }
  
  /**
   * @param string
   */
  public void setBonusFromDate(String string) {
    if (SIUtil.isNull(string)) string = "";
    this.bonusFromDate = SIUtil.changeTo(string.trim(), this.getEncode());
    if (SIUtil.isNotNull(string)) {
      String[] lDateList = SIStringUtil.split(string, "/");
      if (lDateList.length == 3) {
        this.setBonusFromDateYear(lDateList[0]);
        this.setBonusFromDateMonth((lDateList[1].length() == 2) ? lDateList[1] : "0" + lDateList[1]);
        this.setBonusFromDateDay((lDateList[2].length() == 2) ? lDateList[2] : "0" + lDateList[2]);
      }
      
    }
  }
  
  /**
   * @param string
   */
  public void setBonusPointRate(String string) {
    if (SIUtil.isNull(string)) string = "";
    this.bonusPointRate = SIUtil.changeTo(string.trim(), this.getEncode());
  }
  
  /**
   * @param string
   */
  public void setBonusToDate(String string) {
    if (SIUtil.isNull(string)) string = "";
    this.bonusToDate = SIUtil.changeTo(string.trim(), this.getEncode());
    if (SIUtil.isNotNull(string)) {
      String[] lDateList = SIStringUtil.split(string, "/");
      if (lDateList.length == 3) {
        this.setBonusToDateYear(lDateList[0]);
        this.setBonusToDateMonth((lDateList[1].length() == 2) ? lDateList[1] : "0" + lDateList[1]);
        this.setBonusToDateDay((lDateList[2].length() == 2) ? lDateList[2] : "0" + lDateList[2]);
      }
    }
  }
  
  //getter of bonusPointEditMode
  public String getBonusPointEditMode() {
    return this.bonusPointEditMode;
  }
  
  //setter of bonusPointEditMode
  public void setBonusPointEditMode(String bonusPointEditMode) {
    if (SIUtil.isNull(bonusPointEditMode)) bonusPointEditMode = "";
    this.bonusPointEditMode = SIUtil.changeTo(bonusPointEditMode.trim(), this.getEncode());
  }
  
  public void init(HttpServletRequest lRequest, Connection lConnection, SIURLParameter lUrlParam) {// 7.1.1 ST0236 修正
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    
    this.setActionNameTxt((String) lUrlParam.getParam("actionNameTxt"));// 7.1.1 ST0236 修正
    if (SIUtil.isNotNull(this.getActionNameTxt())) {
      this.setEditModeRdo((String) lUrlParam.getParam("editModeRdo"));// 7.1.1 ST0236 修正
    }
    this.setEditModeTxt(this.getEditModeRdo());
    this.setClickedCtgryCodeTxt((String) lUrlParam.getParam("clickedCtgryCodeTxt"));// 7.1.1 ST0236 修正
    if (this.getEditModeTxt().equalsIgnoreCase(SIConfig.SIEDIT_MODE_INSERT)) {// 新規レコードの作成
      this.setParentCtgryCode(this.getClickedCtgryCodeTxt());
      try {// 親カテゴリ名の設定
        String lSql = "SELECT CtgryName FROM CtgryMTbl WHERE CtgryCode=" + SIDBUtil.SQL2Str(getParentCtgryCode());
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setParentCtgryName(SIDBUtil.getFirstData(lConnection, lSql));
      } catch (SIDBAccessException e) {
        e.printStackTrace();
      }
    } else {// 修正
      this.setCtgryCode(this.getClickedCtgryCodeTxt());
      this.srchCtgry.setCtgryCode(this.getClickedCtgryCodeTxt());
      this.reset(lConnection);
      this.srchCtgry.reset(lConnection);
    }
  }
  
  public void initReg(HttpServletRequest lRequest, SIURLParameter lUrlParam) {// 7.1.1 ST0236 修正
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    // 7.1.1 ST0236 修正ここから
    this.setEditModeTxt((String) lUrlParam.getParam("editModeTxt"));
    this.setEditModeRdo((String) lUrlParam.getParam("editModeTxt"));
    this.setClickedCtgryCodeTxt((String) lUrlParam.getParam("clickedCtgryCodeTxt"));
    this.setParentCtgryCode((String) lUrlParam.getParam("parentCtgryCode"));
    this.setParentCtgryName((String) lUrlParam.getParam("parentCtgryName"));
    this.setCtgryCode((String) lUrlParam.getParam("ctgryCode"));
    this.setCtgryName((String) lUrlParam.getParam("ctgryName"));
    this.setCmdtyFlagTxt((String) lUrlParam.getParam("cmdtyFlagTxt"));// 7.2.0 ST0301 追加
    
    this.srchCtgry.setSrchKeyName1((String) lUrlParam.getParam("srchKeyName1"));
    this.srchCtgry.setSrchKeyName2((String) lUrlParam.getParam("srchKeyName2"));
    this.srchCtgry.setSrchKeyName3((String) lUrlParam.getParam("srchKeyName3"));
    this.srchCtgry.setDispType1((String) lUrlParam.getParam("dispType1"));
    this.srchCtgry.setDispType2((String) lUrlParam.getParam("dispType2"));
    this.srchCtgry.setDispType3((String) lUrlParam.getParam("dispType3"));
    // 7.1.1 ST0236 修正ここまで
    this.setBonusFromDate((String) lUrlParam.getParam("bonusFromDate"));
    this.setBonusFromDateYear((String) lUrlParam.getParam("bonusFromDateYear"));
    this.setBonusFromDateMonth((String) lUrlParam.getParam("bonusFromDateMonth"));
    this.setBonusFromDateDay((String) lUrlParam.getParam("bonusFromDateDay"));
    this.setBonusToDate((String) lUrlParam.getParam("bonusToDate"));
    this.setBonusToDateYear((String) lUrlParam.getParam("bonusToDateYear"));
    this.setBonusToDateMonth((String) lUrlParam.getParam("bonusToDateMonth"));
    this.setBonusToDateDay((String) lUrlParam.getParam("bonusToDateDay"));
    this.setBonusPointRate((String) lUrlParam.getParam("bonusPointRate"));
  }
  
  public void initReset(HttpServletRequest lRequest, Connection lConnection) {
    this.setEncode(SIConfig.SIENCODE_NONE);
    String parentCode = this.getParentCtgryCode();
    this.setClickedCtgryCodeTxt(parentCode);
    this.setCtgryCode(parentCode);
    this.srchCtgry.setCtgryCode(parentCode);
    this.reset(lConnection);
    this.srchCtgry.reset(lConnection);
  }
  
  public void initDetail(HttpServletRequest lRequest, Connection lConnection) {
    Statement lStatement = null;
    ResultSet lResultSet = null;
    StringBuffer lSqlBuf = new StringBuffer();
    
    lSqlBuf.append("SELECT * FROM bonuspointtbl ");
    lSqlBuf.append("WHERE CtgryCode=" + SIDBUtil.SQL2Str(this.getCtgryCode()," "));
    lSqlBuf.append("AND bonusfromdate <= current_date AND bonustodate >= current_date");
    
    try {
      this.setEncode(SIConfig.SIENCODE_NONE);
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlBuf.toString());
      // 執行中データの設定
      if (lResultSet.next()) {
        this.setBonusPointRate(lResultSet.getString("BonusPointRate"));
        this.setBonusFromDate(SIDBUtil.getDate(lResultSet.getTimestamp("BonusFromDate")));
        this.setBonusToDate(SIDBUtil.getDate(lResultSet.getTimestamp("BonusToDate")));
        this.setBonusPointEditMode("1");
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      SIDBUtil.close(lStatement, lResultSet);
    }
  }
  
  public boolean validateReg(HttpServletRequest lRequest, Connection lConnection) {
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    SICustomError err;
    StringBuffer lSqlBuf = new StringBuffer();
    lSqlBuf.append("SELECT CtgryCode FROM CtgryVW WHERE CtgryCode=");
    lSqlBuf.append(SIDBUtil.SQL2Str(getParentCtgryCode()));
    lSqlBuf.append(" AND branch LIKE '%").append(getCtgryCode()).append("~%'");
    
    if (SIUtil.isNotNull(this.getCtgryCode()) && SIConfig.SICATEGORY_ROOT_CODE.equalsIgnoreCase(this.getCtgryCode())) {} else {
      SICheckValid.checkValid(errors, "カテゴリコード", this.getCtgryCode(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
      SICheckValid.checkValid(errors, "カテゴリコード", this.getCtgryCode(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 16);
    }
    SICheckValid.checkValid(errors, "カテゴリ名称", this.getCtgryName(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);// 7.1.1 ST0162 修正
    SICheckValid.checkValid(errors, "検索条件名称1", this.srchCtgry.getSrchKeyName1(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 32);
    SICheckValid.checkValid(errors, "検索条件名称2", this.srchCtgry.getSrchKeyName2(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 32);
    SICheckValid.checkValid(errors, "検索条件名称3", this.srchCtgry.getSrchKeyName3(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 32);
    // 追加 postgres8.x対応 ここから
    if (SIConfig.SIEDIT_MODE_INSERT.equalsIgnoreCase(this.getEditModeTxt().trim())) {
      // 重複レコードの存在かどうかをチェックします
      String lSqlStatement = "SELECT CtgryCode FROM CtgryMTbl WHERE CtgryCode=" + SIDBUtil.SQL2Str(this.getCtgryCode(), " ");
      SICheckValid.checkDuplicate(errors, lConnection, "カテゴリコード", lSqlStatement);
    }
    // 追加 postgres8.x対応 ここまで
    try {
      log.debug("validateReg:lSqlBuf=" + lSqlBuf.toString());
      if (SIUtil.isNotNull(getCtgryCode()) && SIUtil.isNotNull(getParentCtgryCode()) && getCtgryCode().equalsIgnoreCase(getParentCtgryCode())) {
        errors.addError(new SICustomError("manager.app.selfcatgeory"));
      } else if (!getEditModeTxt().equals(SIConfig.SIEDIT_MODE_INSERT) && SIUtil.isNotNull(SIDBUtil.getFirstData(lConnection, lSqlBuf.toString()))) {
        errors.addError(new SICustomError("manager.app.selfcatgeory"));
      }
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    return errors.isEmpty();
  }
  
  public boolean validateDelete(HttpServletRequest lRequest, Connection lConnection) {
    SICustomErrors errors = new SICustomErrors();
    StringBuffer lSqlBuf = new StringBuffer();
    SICtgryManager man = new SICtgryManager(lConnection);
    String lConditionSQL = man.getConditionSQL(this.getCtgryCode());
    
    SICheckValid.checkValid(errors, "カテゴリコード", this.getCtgryCode(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    
    try {
      if (SIUtil.isNotNull(getCtgryCode())) {
        // 1.こどものカテゴリ
        lSqlBuf.append("SELECT CtgryCode FROM CtgryMTbl WHERE 1=2 AND " + lConditionSQL);
        
        log.debug("lSqlBuf=" + lSqlBuf.toString());
        
        if (SIDBUtil.hasData(lConnection, lSqlBuf.toString())) {
          errors.addError(new SICustomError("manager.app.ctgry.childctgry"));
        }
        
        // 2.商品カテゴリ関連テーブル
        if (errors.isEmpty()) {
          lSqlBuf = new StringBuffer();
          lSqlBuf.append("SELECT CtgryCode FROM CmdtyCtgryMTbl ");
          lSqlBuf.append("WHERE CtgryCode=" + SIDBUtil.SQL2Str(getCtgryCode()));
          log.debug("lSqlBuf=" + lSqlBuf.toString());
          if (SIDBUtil.hasData(lConnection, lSqlBuf.toString())) {
            errors.addError(new SICustomError("manager.app.ctgry.duplicate"));
          }
        }
      }
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    return errors.isEmpty();
  }
  
  public boolean validateInsertBonus(HttpServletRequest lRequest, Connection lConnection) {
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    SIDateTime lDateTime = new SIDateTime();
    
    SICheckValid.checkValid(errors, "ボーナスポイント付加率", this.getBonusPointRate(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "ボーナスポイント付加率", this.getBonusPointRate(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2);
    
    SICheckValid.checkValid(errors, "ボーナス設定期間FROM", this.getBonusFromDate(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DATE_TYPE);
    SICheckValid.checkValid(errors, "ボーナス設定期間TO", this.getBonusToDate(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DATE_TYPE);
    
    try {
      if (SIUtil.isNotNull(this.getBonusFromDate()) && SIUtil.isNotNull(this.getBonusToDate())) {
        SICheckValid.checkValid(errors, "ボーナス設定期間FROM", "ボーナス設定期間TO", this.getBonusFromDate(), this.getBonusToDate(), SICheckDataConf.SICHECK_DATA_DATE_LESS_EQUAL_TYPE);
        SICheckValid.checkValid(errors, "ボーナス設定期間FROM", "今日の日付", this.getBonusFromDate(), lDateTime.getFullDate(), SICheckDataConf.SICHECK_DATA_DATE_GREATER_TYPE);
      }
      StringBuffer sql1 = new StringBuffer();
      StringBuffer sql2 = new StringBuffer();
      sql1.append("SELECT * FROM bonuspointtbl WHERE ctgrycode = ").append(SIDBUtil.SQL2Str(this.getCtgryCode()));
      sql1.append(" AND bonustodate >= ").append(SIDBUtil.SQL2Str(this.getBonusFromDate()));
      sql2.append("SELECT * FROM bonuspointtbl WHERE ctgrycode = ").append(SIDBUtil.SQL2Str(this.getCtgryCode()));
      sql2.append(" AND bonusfromdate > current_date");
      if(errors.isEmpty()) {
        SICheckValid.checkNotExist(errors, lConnection, "ボーナス設定期間FROMに影響する既存設定", sql1.toString());
        SICheckValid.checkNotExist(errors, lConnection, "ボーナス予約設定", sql2.toString());
      }
    } catch (Exception e) {
      log.debug(e);
    }
    
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    
    return errors.isEmpty();
  }
  
  public boolean validateUpdateBonus(HttpServletRequest lRequest, Connection lConnection) {
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    SIDateTime lDateTime = new SIDateTime();
    
    SICheckValid.checkValid(errors, "ボーナス設定期間FROM", this.getBonusFromDate(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DATE_TYPE);
    SICheckValid.checkValid(errors, "ボーナス設定期間TO", this.getBonusToDate(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DATE_TYPE);
    
    try {
      if (SIUtil.isNotNull(this.getBonusFromDate()) && SIUtil.isNotNull(this.getBonusToDate())) {
        SICheckValid.checkValid(errors, "ボーナス設定期間TO", "今日の日付", this.getBonusToDate(), lDateTime.getFullDate(), SICheckDataConf.SICHECK_DATA_DATE_GREATER_EQUAL_TYPE);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM bonuspointtbl WHERE ctgrycode = ").append(SIDBUtil.SQL2Str(this.getCtgryCode()));
        sql.append(" AND bonusfromdate > ").append(SIDBUtil.SQL2Str(this.getBonusFromDate()));
        sql.append(" AND bonusfromdate <= ").append(SIDBUtil.SQL2Str(this.getBonusToDate()));
        SICheckValid.checkNotExist(errors, lConnection, "ボーナス設定期間TOに影響する予約設定", sql.toString());
      }
    } catch (Exception e) {
      log.debug(e);
    }
    
    if (!errors.isEmpty()){
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
      this.setBonusPointEditMode("1");
    }
    return errors.isEmpty();
  }
  
  public static Collection getEditModeColl() {
    Collection lEditModeColl = new ArrayList();
    lEditModeColl.add(new SINameValue("追加", SIConfig.SIEDIT_MODE_INSERT));
    lEditModeColl.add(new SINameValue("編集", SIConfig.SIEDIT_MODE_UPDATE));
    return lEditModeColl;
  }
  
  public boolean isRoot() {
    if (!this.getEditModeTxt().equalsIgnoreCase(SIConfig.SIEDIT_MODE_INSERT) && this.getCtgryCode().equalsIgnoreCase(SIConfig.SICATEGORY_ROOT_CODE)) return true;
    else return false;
  }
  
  public boolean isTop() {
    if (this.getParentCtgryCode().equalsIgnoreCase(SIConfig.SICATEGORY_ROOT_CODE)) return true;
    else return false;
  }
  
  public boolean enableBtn() {
    if (this.getEditModeTxt().equalsIgnoreCase(SIConfig.SIEDIT_MODE_INSERT)) return false;
    // else if (this.getCtgryCode().equalsIgnoreCase(SIConfig.SICATEGORY_ROOT_CODE)) return false;
    else return true;
  }
  
  public boolean isBonus() {
    if (!this.getEditModeTxt().equalsIgnoreCase(SIConfig.SIEDIT_MODE_INSERT) && !this.getCtgryCode().equalsIgnoreCase(SIConfig.SICATEGORY_ROOT_CODE) && !this.getParentCtgryCode().equalsIgnoreCase(SIConfig.SICATEGORY_ROOT_CODE)) return true;
    else return false;
  }
  
  public Collection getCollection(Connection lConnection) throws SIDBAccessException{
    Statement lStatement = null;
    ResultSet lResultSet = null;
    SIPointRule lPoint = null;
    Collection lPoints = new ArrayList();
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT bonusfromdate,bonustodate,bonuspointrate");
    lSqlBuf.append(",CASE WHEN bonusfromdate > current_date THEN 1 ");
    lSqlBuf.append(" WHEN bonustodate >= current_date THEN 2 ELSE 0 END AS delflg ");
    lSqlBuf.append("FROM bonuspointtbl WHERE ctgrycode =").append(SIDBUtil.SQL2Str(this.getCtgryCode()," "));
    lSqlBuf.append("ORDER BY bonusfromdate ASC");
    
    //実行
    try{
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      
      //ボーナスレコードのセットの作成
      while (lResultSet.next()){
        lPoint=new SIPointRule();
        lPoint.setEncode(SIConfig.SIENCODE_NONE);
        lPoint.setBonusFromDate(SIDBUtil.getDate(lResultSet.getTimestamp("bonusFromDate")));
        lPoint.setBonusToDate(SIDBUtil.getDate(lResultSet.getTimestamp("bonusToDate")));
        lPoint.setBonusPointRate(lResultSet.getString("bonusPointRate"));
        lPoint.setDelFlg(lResultSet.getString("delFlg"));
        lPoints.add(lPoint);
      }
    }catch(SQLException ex){
      ex.printStackTrace();
      throw new SIDBAccessException(ex);
    }finally{
      SIDBUtil.close(lResultSet,lStatement);
    }
    return lPoints;
  }
}
