/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.beans.front;

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.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIFrontBasic;
import jp.co.sint.basic.SIUserInfo;
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.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.SIHTMLUtil;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;


/**
 * @version $Id: UIUserCustqaDetail.java,v 1.0 2003/12/25 Exp $
 * @author  yamauchi
 * <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>
 * yamauchi        2003/12/25 16:26:10  Original
 */
public class UIUserCustqaDetail extends SIFrontBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //ショップコード
  private String shopCode ="";
  
  //問い合わせ番号
  private String custQACode= "";
  
  //顧客コード
  private String custCode="";
  
  //管理コード
  private String mallShopCode = "";
  
  //質問タイトル
  private String qTile = "";
  
  //質問本文
  private String qDescription = "";
  
  //
  private String qSignature = "";
  
  private String qInitDateTime = "";
  
  private String aTile = "";
  
  private String aDescription = "";
  
  private String aSignature = "";
  
  private String aInitDateTime = "";
  
  //ショップ名
  private String frontShopName="";
  
  public UIUserCustqaDetail(){}
  
  public UIUserCustqaDetail(HttpServletRequest lRequest,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    init(lRequest,lUrlParam);//7.1.1 ST0236 修正
  }
  
  //setter of ショップコード
  public void setShopCode(String lShopCode){
    if (SIUtil.isNull(lShopCode)) lShopCode="";
    this.shopCode=SIUtil.changeTo(lShopCode.trim(),this.encode);
  }
  
  //setter of 問合せ番号(詳細検索用)
  public void setCustQACode(String lCustqaCode){
    if (SIUtil.isNull(lCustqaCode)) lCustqaCode="";
    this.custQACode=SIUtil.changeTo(lCustqaCode.trim(),this.encode);
  }
  
  //setter of 顧客コード
  public void setCustCode(String CustCode){
    if (SIUtil.isNull(CustCode)) CustCode="";
    this.custCode=SIUtil.changeTo(CustCode.trim(),this.encode);
  }
  
  public void setMallShopCode(String string) {
    if(string==null)string="";
    mallShopCode = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setQDescription(String string) {
    if(string==null)string="";
    qDescription = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setQTile(String string) {
    if(string==null)string="";
    qTile = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setQInitDateTime(String string) {
    if(string==null)string="";
    qInitDateTime = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setFrontShopName(String string) {
    if(string==null)string="";
    frontShopName = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setATile(String string) {
    if(string==null)string="";
    aTile = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setADescription(String string) {
    if(string==null)string="";
    aDescription = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setAInitDateTime(String string) {
    if(string==null)string="";
    aInitDateTime = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setASignature(String string) {
    if(string==null)string="";
    aSignature = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  //getter of ショップコード
  public String getShopCode(){
    return this.shopCode;
  }
  
  //getter of 問合せ番号(詳細検索用)
  public String getCustQACode(){
    return this.custQACode;
  }
  
  //getter of 顧客コード
  public String getCustCode(){
    return this.custCode;
  }
  
  public String getMallShopCode() {
    return mallShopCode;
  }
  
  public String getFrontShopName() {
    return frontShopName;
  }
  
  public String getQTile() {
    return qTile;
  }
  
  public String getQDescription() {
    return qDescription;
  }
  
  public String getQInitDateTime(){
    return qInitDateTime;
  }
  
  public String getATile() {
    return aTile;
  }
  
  public String getADescription() {
    return aDescription;
  }
  
  public String getAInitDateTime() {
    return aInitDateTime;
  }
  
  public String getASignature() {
    return aSignature;
  }
  
  /**
   * <b>init</b>
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request クライアントからリクエスト
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    //7.1.1 ST0236 修正 ここから
    super.init(lRequest,lUrlParam);
    this.setCustQACode((String)lUrlParam.getParam("custQACode"));
    this.setMallShopCode((String)lUrlParam.getParam("mallShopCode"));
    this.setQTile((String)lUrlParam.getParam("qtitleTxt"));
    this.setQDescription((String)lUrlParam.getParam("qdescriptionTxt"));
    //7.1.1 ST0236 修正 ここまで
  }
  
  public boolean validate(HttpServletRequest lRequest, Connection lConnection) {
     lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
     SICustomErrors errors = new SICustomErrors();
     
     SICheckValid.checkValid(errors, "お問合せ先", this.getMallShopCode(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
     SICheckValid.checkValid(errors, "タイトル", this.getQTile(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
     SICheckValid.checkValid(errors, "タイトル", this.getQTile(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 100);
     SICheckValid.checkValid(errors, "内容", this.getQDescription(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 2000);//7.1.1 ST0162 修正
     
     if (!errors.isEmpty())
     lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
     return errors.isEmpty();
   }
  
  /**
   * <b>validateCustCode</b>
   * 本人かどうかのセキュリティのチェック
   * @param lRequest クライアントからのリクエスト
   * @param lConnection データベースへの接続コネクション
   * @return
   * @throws なし
   */
  public boolean validateCustCode(HttpServletRequest lRequest, Connection lConnection){
    SIUserInfo lUserInfo=SIHTMLUtil.getUserInfo(lRequest);//ログイン情報の取得
    SICustomErrors errors = new SICustomErrors();
    if (SIUtil.isNotNull(getCustQACode())){
      StringBuffer lSqlBuf=new StringBuffer("SELECT custQACode FROM CustQATbl WHERE custQACode=");
      lSqlBuf.append(SIDBUtil.SQL2Str(getCustQACode()));
      lSqlBuf.append(" AND CustCode=").append(SIDBUtil.SQL2Str(lUserInfo.getCustCode()));
      log.debug("validateCustCode:lSqlBuf="+lSqlBuf.toString());
      
      try {
        if (SIDBUtil.hasData(lConnection,lSqlBuf.toString())){
        }else{
          errors.addError(new SICustomError("manager.app.access.disable"));
        }
      } catch (SIDBAccessException e) {
        e.printStackTrace();
        errors.addError(new SICustomError("manager.app.access.disable"));
      }
    }
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY,errors);
    return errors.isEmpty();
  }
  
  /**
   * <b>getDetail</b>
   * 条件に合ったレコードを検索して、結果を得られるかどうか返します
   * @param lConnection データベースへの接続コネクション
   * @return
   * @throws なし
   */
  public boolean reset(Connection dbConnection){
    boolean lResult = false;
    Statement statement=null;
    ResultSet resultSet=null;
    StringBuffer lSqlBuf=new StringBuffer();
    
    //受注番号が無いとき
    if(SIUtil.isNull(this.getCustQACode())){
      return lResult;
    }
    
    //基本のSQL
    lSqlBuf.append("SELECT aa.*, bb.FrontShopName ");
    lSqlBuf.append("FROM CustQATbl aa,MallShopMTbl bb ");
    lSqlBuf.append("WHERE aa.MallShopCode=bb.MallShopCode ");
    lSqlBuf.append("AND aa.CustQACode=").append(SIDBUtil.SQL2Str(this.getCustQACode()));
    
    log.debug("lSqlBuf="+lSqlBuf.toString());
    //実行
    try {
      statement = dbConnection.createStatement();
      resultSet = statement.executeQuery(lSqlBuf.toString());
      
      //商品レコードのセットの作成
      if (resultSet.next()) {
        this.setEncode(SIConfig.SIENCODE_NONE);
        
        this.setCustQACode(resultSet.getString("CustQACode"));       //問い合わせID
        this.setMallShopCode(resultSet.getString("mallShopCode"));   //管理コード
        this.setCustCode(resultSet.getString("CustCode"));           //ショップコード
        this.setFrontShopName(resultSet.getString("FrontShopName"));//ショップ名
        this.setQTile(resultSet.getString("qTile"));
        this.setQDescription(resultSet.getString("qDescription"));
        if(SIUtil.isNotNull(resultSet.getString("qInitDateTime"))){
          this.setQInitDateTime(SIDBUtil.getDateTime(resultSet.getTimestamp("qInitDateTime")));
        }
        this.setATile(resultSet.getString("aTile"));
        this.setADescription(resultSet.getString("aDescription"));
        this.setASignature(resultSet.getString("aSignature"));
        if(SIUtil.isNotNull(resultSet.getString("aInitDateTime"))){
          this.setAInitDateTime(SIDBUtil.getDateTime(resultSet.getTimestamp("aInitDateTime")));
        }
        lResult = true;
      }
    } catch (SQLException sqle) {
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(statement, resultSet);
    }
    return lResult;
  }
  
  public static Collection getMallShopCollection(Connection lConnection) throws SIDBAccessException {
    String lSql="SELECT FrontShopName,MallShopCode FROM mallShopMTbl WHERE STATUS = '1' ORDER BY FrontShopName";
    try {
      return SIDBUtil.getCollection(lConnection,lSql,true);
    } catch (SIDBAccessException e) {
      e.printStackTrace();
      return new ArrayList();
    }
  }
}
