package jp.co.sint.servlet.mallmgr;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.co.sint.basic.SILogin;
import jp.co.sint.beans.mallmgr.UICmdtyAllocationListCond;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SICSVConf;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.servlet.SIServlet;
import jp.co.sint.tools.SIErrorFactory;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SICSVWrite;

import org.apache.log4j.Category;
import jp.co.sint.tools.SIURLParameter;

public class SIRegCmdtyAllocationSrv extends SIServlet {
  // ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  /**
   * <b>doUpdate</b> HTTP リクエストの処理
   * 
   * @param request リクエスト
   * @param response
   * @return なし
   * @throws ServletException
   * @throws IOException
   */
  public void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    SILogin manLogin = SIHTMLUtil.getLogin(request);
    if (!manLogin.isLogin()) {
      forwardKey(request, response, "webshop.jsp.manager.login");
      return;
    }
    
    HttpSession session = request.getSession(true);// セッションの取得
    SIDatabaseConnection databaseConnection = new SIDatabaseConnection();// DBへのコネクションの作成
    SIURLParameter urlParam = new SIURLParameter(request);
    
    try {
      String actionName = this.getActionName(urlParam);// 画面からのアクション
      
      UICmdtyAllocationListCond cmdtyList = new UICmdtyAllocationListCond();
      
      if (SIConfig.SIACTION_LIST.equalsIgnoreCase(actionName)) {// 一覧と検索などの画面
        // データの取得とデータのチェック
        cmdtyList.init(request, urlParam);
        cmdtyList.validate(request, databaseConnection.getConnection());
        session.setAttribute(SIConfig.SISESSION_MAN_CMDTY_ALLOCATION_LIST_NAME, cmdtyList);
        forwardKey(request, response, "webshop.jsp.manager.cmdty.allocation");
      } else if (SIConfig.SIACTION_CSV.equalsIgnoreCase(actionName)) {// CSV出力
        cmdtyList = (UICmdtyAllocationListCond) session.getAttribute(SIConfig.SISESSION_MAN_CMDTY_ALLOCATION_LIST_NAME);
        if (cmdtyList == null) cmdtyList = new UICmdtyAllocationListCond();
        if (cmdtyList.validateCSV()){
          this.produceCSVFile(request, response, databaseConnection.getConnection(), cmdtyList);
        }
        if (!response.isCommitted()) {
          request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("database.query.notexist", "CSVデータ"));
          forwardKey(request, response, "webshop.jsp.manager.cmdty.allocation");
        }
      }
    } catch (SQLException e) {
      e.printStackTrace();
      throw new ServletException();
    } catch (NamingException e) {
      e.printStackTrace();
      throw new ServletException();
    } finally {
      databaseConnection.close();
    }
  }
  
  private void produceCSVFile(HttpServletRequest request, HttpServletResponse response, Connection lConnection, UICmdtyAllocationListCond cmdtyList) {
    // CSVファイル名の作成
    String lCsvFileName = SICSVConf.getCsvFileName(SICSVConf.SICSV_CMDTY_ALLOCATION_LIST_INX);
    SICSVWrite lCsv = new SICSVWrite(lCsvFileName);
    
    // SQL文の設定
    lCsv.setSqlStatement(cmdtyList.getSql(1));
    // タイトルとFieldの設定
    lCsv.setCsvTitleAndField(SICSVConf.getTitleAndFieldName(SICSVConf.SICSV_CMDTY_ALLOCATION_LIST_INX));
    // CSVファイルの出力
    lCsv.execute(lConnection, response);
  }
  
  public void destroy() {
  }
}
