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

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

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.beans.mallmgr.UICmdtyUploadImgList;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.tools.SIErrorFactory;
import jp.co.sint.tools.SIFileExistException;
import jp.co.sint.tools.SIUtil;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Category;

/**
 * @version $Id: SICmdtyUploadImgSrv.java,v 1.0 2003/12/04 Exp $
 * @author Tsuyoshi Yagi <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>
 *         Tsuyoshi Yagi 2005/08/23 Original
 */
public class SIBGUploadSrv extends SIUploadSrv {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  /**
   * <b>doUpdate </b> HTTP リクエストの処理
   * 
   * @param request
   *          リクエスト
   * @param response
   * @return なし
   * @throws ServletException
   * @throws IOException
   * @throws FileNotFoundException
   */
  public void doUpdate(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, FileNotFoundException {
    SIDatabaseConnection databaseConnection = new SIDatabaseConnection();//DBへのコネクションの作成
    HttpSession session = request.getSession();
    try {
      
      super.doUploadBG(request, response, new HashMap(), databaseConnection.getConnection());

      UICmdtyUploadImgList uploadImgList = new UICmdtyUploadImgList();
      uploadImgList.setDestPath(getMultiParameter(request, "destPath"));
      uploadImgList.setShopCode(getMultiParameter(request, "subDir"));
      session.setAttribute(SIConfig.SISESSION_MAN_UPLOAD_IMG_NAME, uploadImgList);
      String lUploadMsg = (String) request.getAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME);

      if (SIUtil.isNull(lUploadMsg)) {//アップロード成功
        request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory
            .getErrorMsg("manager.message.success.imaegctrl"));
      }
    } catch (FileNotFoundException e) {
      request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg(
          "manager.message.failure.exist", "画像"));
      
      log.warn(e);
    } catch (FileUploadException e) {
      request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory
          .getErrorMsg("manager.message.failure.upload"));
      log.warn(e);
    } catch (SIFileExistException e) {
      request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, e.toString());
      log.warn(e);
    } catch (SQLException e) {
      e.printStackTrace();
      throw new ServletException();
    } catch (NamingException e) {
      e.printStackTrace();
      throw new ServletException();
    } finally {
      databaseConnection.close();
    }
    
    // EDBTG003-00 elecs-matsushima mod start imageUploadFlg
//    forwardKey(request, response, "webshop.jsp.manager.cmdty.indivdual");
    String imageUploadFlg = getMultiParameter(request, "imageUploadFlg");
    if (SIUtil.isNotNull(imageUploadFlg) && imageUploadFlg.equals(SIConfig.BGUPLOAD_IMAGE_UPLOAD_FLG_SETCMDTY)){
      forwardKey(request, response, "webshop.jsp.manager.cmdty.set.edit");
    } else {
      forwardKey(request, response, "webshop.jsp.manager.cmdty.indivdual");
    }
    // EDBTG003-00 elecs-matsushima mod end
  }

}