/**
 * Created on 2003/08/14
 *
 * 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 javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIBasic;
import jp.co.sint.basic.SIOpaque;
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.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 : UITelopList.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>
 * Jinwang Chen   2003/08/05  Original
 */
public class UIOpaqueList extends SIBasic {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  private String opaqueCode = "";
  private String dispOrder = "";
  private String description = "";
  
  private String startDateTime = "";
  private String endDateTime = "";
  
  private String telopFromDateYear = "";
  private String telopFromDateMonth = "";
  private String telopFromDateDay = "";
  private String telopFromDateHour = "";
  
  private String telopToDateYear = "";
  private String telopToDateMonth = "";
  private String telopToDateDay = "";
  private String telopToDateHour = "";
  //SQL検索のばあいに、条件文
  private String conditionSQL = "";
  //並び順の文
  private String orderBySQL = "";
  
  public UIOpaqueList() {}
  
  /**
   * init
   * 入力したデータから、このbeansを設定します。
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  //7.1.1 ST0236 修正 ここから
  public void init(SIURLParameter lUrlParam) {
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    this.setOpaqueCode((String)lUrlParam.getParam("opaqueCode"));
    this.setDispOrder((String)lUrlParam.getParam("dispOrder"));
    this.setDescription((String)lUrlParam.getParam("description"));
    this.setTelopFromDateYear((String)lUrlParam.getParam("telopFromDateYear"));
    this.setTelopFromDateMonth((String)lUrlParam.getParam("telopFromDateMonth"));
    this.setTelopFromDateDay((String)lUrlParam.getParam("telopFromDateDay"));
    this.setTelopFromDateHour((String)lUrlParam.getParam("telopFromDateHour"));
    this.setStartDateTime(this.getStartDateTime());
    this.setTelopToDateYear((String)lUrlParam.getParam("telopToDateYear"));
    this.setTelopToDateMonth((String)lUrlParam.getParam("telopToDateMonth"));
    this.setTelopToDateDay((String)lUrlParam.getParam("telopToDateDay"));
    this.setTelopToDateHour((String)lUrlParam.getParam("telopToDateHour"));
    this.setEndDateTime(this.getEndDateTime());
    this.setActionNameTxt((String)lUrlParam.getParam("actionNameTxt"));
    this.setEditModeTxt((String)lUrlParam.getParam("editModeTxt"));
  }
  //7.1.1 ST0236 修正 ここまで
  
  /**
   * validate
   * 入力したデータをチェックします。
   * 不正なデータがある場合、エラーをオブジェクトに格納します。
   * そのオブジェクトは、エラーメッセージとして画面に表示されます。
   * @param HttpServletRequest ，Connection
   * @return true:エラーがない false:エラーが１つ以上ある
   * @throws
   */
  public boolean validate(HttpServletRequest lRequest, Connection lConnection) {
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    
    if (!editModeTxt.equals(SIConfig.SIEDIT_MODE_INSERT)) {
      SICheckValid.checkValid(errors, "テロップ番号", this.getOpaqueCode(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
      SICheckValid.checkValid(errors, "テロップ番号", this.getOpaqueCode(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 6);
    }
    
    if (!editModeTxt.equals(SIConfig.SIEDIT_MODE_DELETE)) {
      //7.1.1 ST0162 修正 ここから
      SICheckValid.checkValid(errors, "表示順", this.getDispOrder(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
      SICheckValid.checkValid(errors, "表示順", this.getDispOrder(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 3);
      //7.1.1 ST0162 修正 ここまで
      
      boolean startflg = false;
      if (SIUtil.isNotNull(this.getStartDateTime())) {
        if (!SICheckUtil.isDateTime(this.getStartDateTime(), "yyyy/MM/dd HH")) {
          errors.addError(new SICustomError("input.data.date", "表示開始日時"));
        }else{
          startflg = true;
        }
      }
      
      boolean endflg = false;
      if (SIUtil.isNotNull(this.getEndDateTime())) {
        if (!SICheckUtil.isDateTime(this.getEndDateTime(), "yyyy/MM/dd HH")) {
          errors.addError(new SICustomError("input.data.date", "表示終了日時"));
        }else{
          endflg = true;
        }
      }
      
      if (SIUtil.isNull(this.getStartDateTime()) && SIUtil.isNull(this.getEndDateTime())) {
        errors.addError(new SICustomError("input.data.require.one", "表示開始日時", "表示終了日時"));
      }
      
      if(startflg && endflg){
        SICheckValid.checkValid(errors,"表示開始日時","表示終了日時",this.getStartDateTime(),this.getEndDateTime(),SICheckDataConf.SICHECK_DATA_DATETIME_LESS_TYPE);
      }
      SICheckValid.checkValid(errors, "本文", this.getDescription(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
      SICheckValid.checkValid(errors, "本文", this.getDescription(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 400);
    }
    
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    return errors.isEmpty();
  }
  
  /**
   * getFirstColl
   * 編集対象のデータを取得する。
   * @param Connection , String
   * @return SIOpaque
   * @throws SIDBAccessException
   */
  public SIOpaque getFirstColl(Connection lConnection, String pOpaqueCode) throws SIDBAccessException {
    //初期表示時(該当レコードがない場合)
    if (getCollection(lConnection, pOpaqueCode).isEmpty() || pOpaqueCode.trim().length() == 0) {
      SIOpaque opaque = new SIOpaque();
      return opaque;
      //選択表示時(該当レコードがある場合)
    } else {
      return (SIOpaque) getCollection(lConnection, pOpaqueCode).iterator().next();
    }
  }
  
  /**
   * getFirstColl
   * 編集対象のデータを取得する。
   * @param
   * @return SIOpaque
   * @throws
   */
  public SIOpaque getFirstColl() {
    SIOpaque lopaque = new SIOpaque();
    lopaque.setEncode(SIConfig.SIENCODE_NONE);
    lopaque.setOpaqueCode(this.getOpaqueCode());
    lopaque.setDispOrder(this.getDispOrder());
    lopaque.setDescription(this.getDescription());
    lopaque.setTelopFromDateYear(this.getTelopFromDateYear());
    lopaque.setTelopFromDateMonth(this.getTelopFromDateMonth());
    lopaque.setTelopFromDateDay(this.getTelopFromDateDay());
    lopaque.setTelopFromDateHour(this.getTelopFromDateHour());
    lopaque.setStartDateTime(this.getStartDateTime());
    lopaque.setTelopToDateYear(this.getTelopToDateYear());
    lopaque.setTelopToDateMonth(this.getTelopToDateMonth());
    lopaque.setTelopToDateDay(this.getTelopToDateDay());
    lopaque.setTelopToDateHour(this.getTelopToDateHour());
    lopaque.setEndDateTime(this.getEndDateTime());
    return lopaque;
  }
  
  /**
   * getCollection
   * 条件に合ったレコードを検索して、結果のコネクションを作成して、戻します。
   * @param Connection , String
   * @return Collection
   * @throws SIDBAccessException
   */
  public Collection getCollection(Connection lConnection, String pOpaqueCode) throws SIDBAccessException {
    Statement lStatement = null;
    ResultSet lResultSet = null;
    SIOpaque lTelop = new SIOpaque();
    StringBuffer lSqlStatement = new StringBuffer();
    Collection telops = new ArrayList();
    SIDateTime mydatefrom;
    SIDateTime mydateto;
    
    //基本のSQL
    lSqlStatement.append(" SELECT * FROM opaquemtbl where 1=1");
    SITableConditionManager lConditionMan = new SITableConditionManager();
    SITableCondition DD = new SITableCondition("", "opaquecode", pOpaqueCode, SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(false);
    lConditionMan.add(DD);
    
    lSqlStatement.append(lConditionMan.getCondtionSQL());
    lSqlStatement.append(" ORDER BY disporder ASC , enddatetime DESC");
    
    log.debug("sqlStatement=" + lSqlStatement.toString());
    
    //実行
    try {
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlStatement.toString());
      
      //商品レコードのセットの作成
      while (lResultSet.next()) {
        lTelop = new SIOpaque();
        lTelop.setEncode(SIConfig.SIENCODE_NONE);
        lTelop.setOpaqueCode(lResultSet.getString("opaqueCode"));
        lTelop.setDispOrder(lResultSet.getString("dispOrder"));
        lTelop.setDescription(lResultSet.getString("description"));
        lTelop.setStartDateTime(SIDBUtil.getDateTime(lResultSet.getTimestamp("startdatetime")));
        lTelop.setEndDateTime(SIDBUtil.getDateTime(lResultSet.getTimestamp("enddatetime")));
        lTelop.setUpdateDateTime(SIDBUtil.getDateTime(lResultSet.getTimestamp("updatedatetime")));
        if (lResultSet.getTimestamp("startdatetime") != null) {
          mydatefrom = new SIDateTime(lResultSet.getTimestamp("startdatetime"));
          lTelop.setTelopFromDateYear(String.valueOf(mydatefrom.getYear()));
          lTelop.setTelopFromDateMonth(String.valueOf(mydatefrom.getMonth()));
          lTelop.setTelopFromDateDay(String.valueOf(mydatefrom.getDay()));
          lTelop.setTelopFromDateHour(String.valueOf(mydatefrom.getHourStr()));
        }
        if (lResultSet.getTimestamp("enddatetime") != null) {
          mydateto = new SIDateTime(lResultSet.getTimestamp("enddatetime"));
          lTelop.setTelopToDateYear(String.valueOf(mydateto.getYear()));
          lTelop.setTelopToDateMonth(String.valueOf(mydateto.getMonth()));
          lTelop.setTelopToDateDay(String.valueOf(mydateto.getDay()));
          lTelop.setTelopToDateHour(String.valueOf(mydateto.getHourStr()));
        }
        telops.add(lTelop);
      }
    } catch (Exception ex) {
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(lResultSet, lStatement);
    }
    return telops;
  }
  
  /**
   * @param string
   */
  private void setStartDateTime(String string) {
    startDateTime = string;
  }
  
  public String getStartDateTime() {
    if (SIUtil.isNull(getTelopFromDateYear()) && SIUtil.isNull(getTelopFromDateMonth()) && SIUtil.isNull(getTelopFromDateDay()) && SIUtil.isNull(getTelopFromDateHour())) {
      return "";
    }
    log.debug("getSaleFromDate:" + getTelopFromDateYear() + "/" + getTelopFromDateMonth() + "/" + getTelopFromDateDay() + " " + getTelopFromDateHour());
    return this.getTelopFromDateYear() + "/" + this.getTelopFromDateMonth() + "/" + this.getTelopFromDateDay() + " " + getTelopFromDateHour() + ":00:00";
  }
  
  /**
   * @return
   */
  public String getEndDateTime() {
    if (SIUtil.isNull(this.getTelopToDateYear())
      && SIUtil.isNull(this.getTelopToDateMonth())
      && SIUtil.isNull(this.getTelopToDateDay())
      && SIUtil.isNull(this.getTelopToDateHour())) {
      return "";
    }
    
    log.debug("getTelopToDate:" + this.getTelopToDateYear() + "/" + this.getTelopToDateMonth() + "/" + this.getTelopToDateDay() + " " + this.getTelopToDateHour());
    return this.getTelopToDateYear() + "/" + this.getTelopToDateMonth() + "/" + this.getTelopToDateDay() + " " + this.getTelopToDateHour() + ":00:00";
  }
  
  /**
   * @return
   */
  public String getDispOrder() {
    return dispOrder;
  }
  
  /**
   * @return
   */
  public Category getLog() {
    return log;
  }
  
  /**
   * @return
   */
  public String getOpaqueCode() {
    return opaqueCode;
  }
  
  /**
  * @param string
  */
  public void setEndDateTime(String string) {
    endDateTime = string;
  }
  
  /**
   * @param category
   */
  public void setLog(Category category) {
    log = category;
  }
  
  /**
   * @param i
   */
  public void setOpaqueCode(String string) {
    if (SIUtil.isNull(string))
      string = "";
    this.opaqueCode = SIUtil.changeTo(string.trim(), this.encode);
  }
  
  /**
   * @return
   */
  public String getConditionSQL() {
    return conditionSQL;
  }
  
  /**
   * @return
   */
  public String getOrderBySQL() {
    return orderBySQL;
  }
  
  /**
   * @param string
   */
  public void setConditionSQL(String string) {
    conditionSQL = string;
  }
  
  /**
   * @param string
   */
  public void setOrderBySQL(String string) {
    orderBySQL = string;
  }
  
  /**
   * @return
   */
  public String getDescription() {
    return description;
  }
  
  /**
   * @param string
   */
  public void setDescription(String string) {
    if (SIUtil.isNull(string)) string = "";
    string = SIStringUtil.replace(string, "\r\n", "");
    this.description = SIUtil.changeTo(string.trim(), this.encode);
  }
  
  /**
   * @return
   */
  public String getTelopFromDateDay() {
    return telopFromDateDay;
  }
  
  /**
   * @return
   */
  public String getTelopFromDateHour() {
    return telopFromDateHour;
  }
  
  /**
   * @return
   */
  public String getTelopFromDateMonth() {
    return telopFromDateMonth;
  }
  
  /**
   * @return
   */
  public String getTelopFromDateYear() {
    return telopFromDateYear;
  }
  
  /**
   * @return
   */
  public String getTelopToDateDay() {
    return telopToDateDay;
  }
  
  /**
   * @return
   */
  public String getTelopToDateHour() {
    return telopToDateHour;
  }
  
  /**
   * @return
   */
  public String getTelopToDateMonth() {
    return telopToDateMonth;
  }
  
  /**
   * @return
   */
  public String getTelopToDateYear() {
    return telopToDateYear;
  }
  
  /**
   * @param string
   */
  public void setTelopFromDateDay(String string) {
    if (string == null) string = "";
    telopFromDateDay = string;
  }
  
  /**
   * @param string
   */
  public void setTelopFromDateHour(String string) {
    if (string == null) string = "";
    telopFromDateHour = string;
  }
  
  /**
   * @param string
   */
  public void setTelopFromDateMonth(String string) {
    if (string == null) string = "";
    telopFromDateMonth = string;
  }
  
  /**
   * @param string
   */
  public void setTelopFromDateYear(String string) {
    if (string == null) string = "";
    telopFromDateYear = string;
  }
  
  /**
   * @param string
   */
  public void setTelopToDateDay(String string) {
    if (string == null) string = "";
    telopToDateDay = string;
  }
  
  /**
   * @param string
   */
  public void setTelopToDateHour(String string) {
    if (string == null) string = "";
    telopToDateHour = string;
  }
  
  /**
   * @param string
   */
  public void setTelopToDateMonth(String string) {
    if (string == null) string = "";
    telopToDateMonth = string;
  }
  
  /**
   * @param string
   */
  public void setTelopToDateYear(String string) {
    if (string == null)
      string = "";
    telopToDateYear = string;
  }

  /**
   * @param string
   */
  public void setDispOrder(String string) {
    if (SIUtil.isNull(string))
      string = "";
    this.dispOrder = SIUtil.changeTo(string.trim(), this.encode);
  }
}
