package jp.co.sint.servlet.mallmgr;

import java.io.IOException;
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 java.util.Iterator;

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.basic.SIMailTemp;
import jp.co.sint.basic.SIRankingAnalysis;
import jp.co.sint.beans.mallmgr.UIRankingAnalysisListCond;
import jp.co.sint.beans.mallmgr.UIStockDecide;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SICSVConf;
import jp.co.sint.config.SIDBMultiConf;
import jp.co.sint.config.SIFlagConf;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.database.SIDateType;
import jp.co.sint.database.SIDeleteRec;
import jp.co.sint.database.SIDuplicateKeyException;
import jp.co.sint.database.SIInsertRec;
import jp.co.sint.database.SIModifyRec;
import jp.co.sint.database.SISpcType;
import jp.co.sint.mail.SISendMail;
import jp.co.sint.servlet.SIServlet;
import jp.co.sint.tools.SICheckUtil;
import jp.co.sint.tools.SICSVWrite;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIDateTime;
import jp.co.sint.tools.SIErrorFactory;
import jp.co.sint.tools.SIFatalException;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIBGUtil;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIFlagUtil;

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

public class SIRankingAnalysisSrv 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);// 画面からのアクション
      String editMode = this.getEditMode(urlParam);// DBへの編集モード
      Connection lConnection = databaseConnection.getConnection();
      
      UIRankingAnalysisListCond rankingList = new UIRankingAnalysisListCond();
      
      if (SIUtil.isNull(actionName)) {
        forwardKey(request, response, "webshop.jsp.manager.rankinganalysis");
      }else if (SIConfig.SIACTION_LIST.equalsIgnoreCase(actionName)) {// 一覧と検索などの画面（入庫管理）
        // データの取得とデータのチェック
        rankingList.init(request, urlParam);
        rankingList.validate(request);
        rankingList.getCollection(lConnection);
        session.setAttribute(SIConfig.SISESSION_MAN_RANKING_ANALYSIS_NAME, rankingList);
        forwardKey(request, response, "webshop.jsp.manager.rankinganalysis");
      } else if (SIConfig.SIACTION_CSV.equalsIgnoreCase(actionName)) {//CSV出力
        // 取得するCSVデータの取得
         rankingList=(UIRankingAnalysisListCond)session.getAttribute(SIConfig.SISESSION_MAN_RANKING_ANALYSIS_NAME);
        if (rankingList==null) rankingList=new UIRankingAnalysisListCond();
        this.produceCSVFile(response, rankingList);
        if (!response.isCommitted()) {
          request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("database.query.notexist", "CSVデータ"));
          forwardKey(request, response, "webshop.jsp.manager.rankinganalysis");
        }
      }
      SIDBUtil.close(lConnection);
    } catch (SQLException e) {
      e.printStackTrace();
      throw new ServletException();
    } catch (NamingException e) {
      e.printStackTrace();
      throw new ServletException();
    }
  }
  
  private void produceCSVFile(HttpServletResponse response, UIRankingAnalysisListCond lListCond) {
    // CSVファイル名の作成
    String lCsvFileName = SICSVConf.getCsvFileName(SICSVConf.SICSV_RANKING_ANALYSIS_LIST_INX);
    SICSVWrite lCsv = new SICSVWrite(lCsvFileName);
    
    // タイトルの設定
    lCsv.setCsvTitleAndField(SICSVConf.getTitleAndFieldName(SICSVConf.SICSV_RANKING_ANALYSIS_LIST_INX));
    if (SIDBMultiConf.SIDB_CURRENT_INX == SIDBMultiConf.SIDB_POSTGRESQL_INX) {} else {
      lCsv.setFieldType(SICSVConf.getFieldType(SICSVConf.SICSV_RANKING_ANALYSIS_LIST_INX));
    }
    // CSVファイルの出力
    lCsv.executeCollection(lListCond.getResultCSV(), response);
  }
}