/**
 * Copyright (c) 2003-2006 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.servlet.mobile.vod;

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

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

import org.apache.log4j.Category;

import jp.co.sint.basic.SIUserInfo;
import jp.co.sint.beans.front.UISearch;
import jp.co.sint.beans.mallmgr.UIRegSearch;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.servlet.SIServlet;
import jp.co.sint.servlet.front.SISearchSrv;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIURLMap;
import jp.co.sint.tools.SIURLParameter;
import jp.co.sint.tools.SIUtil;

/**
 * @version $Id: SIISearchSrv.java,v 1.0 2006/12/14 Exp $
 * @author  Hong
 * <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>
 * Hong   2006/12/14 18:59:45  Original
 */
public class SIVSearchSrv 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 {
      HttpSession session=request.getSession(true);//セッションの取得
      SIDatabaseConnection databaseConnection=new SIDatabaseConnection();//DBへのコネクションの作成
      SIURLParameter urlParam = new SIURLParameter(request);
      SIUserInfo userLogin=SIHTMLUtil.getUserInfo(request);
      try {
        Connection lConnection=databaseConnection.getConnection();
        String actionName=this.getActionName(urlParam);//画面からのアクション
        if (this.getServletPath(request).equals("/mobile/v/search")){//探して欲しい入力シート
	        if (SIUtil.isNull(actionName)){
	            session.removeAttribute(SIConfig.SISESSION_SEARCH_EDIT_NAME);
	            forwardKey(request,response,"webshop.jsp.front.vod.other.search");            
	        }else if(actionName.equalsIgnoreCase(SIConfig.SIACTION_REGIST)){     
	            if (userLogin.isNotLogin(request)){//ログインしなければ、ログインへ
	                UISearch search=new UISearch();
	                search.init(request,urlParam);
	                session.setAttribute(SIConfig.SISESSION_SEARCH_EDIT_NAME,search);
	                if (search.validateMobile(request,lConnection)){
	                    String lUrl=SIURLMap.getUrl("webshop.servlet.front.vod.login")+"?nid="+SIHTMLUtil.URLEncode(SIURLMap.getUrl("webshop.servlet.front.vod.search.con"));
	    	            redirectHttps(request,response,lUrl);
	                }else{
	                    forwardKey(request,response,"webshop.jsp.front.vod.other.search");  
	                }               
	            }else{
	                UISearch search=new UISearch();
	                search.init(request,urlParam);
	                session.setAttribute(SIConfig.SISESSION_SEARCH_EDIT_NAME,search);
	              
	                search.getRegSearch().setCustCode(SIHTMLUtil.getUserInfo(request).getCustCode());
	                search.reset(lConnection);
	                session.setAttribute(SIConfig.SISESSION_SEARCH_ADDRESS_NAME,search);
	              
	                if (search.validateMobile(request,lConnection)){
	                    forwardKey(request,response,"webshop.jsp.front.vod.other.search.con");           
	                }else{
	                    forwardKey(request,response,"webshop.jsp.front.vod.other.search");           
	                }
	            }
	        }
        }else if (this.getServletPath(request).equals("/mobile/v/searchCon")){
            if (SIUtil.isNull(actionName)){
                UISearch search= (UISearch)session.getAttribute(SIConfig.SISESSION_SEARCH_EDIT_NAME);
              
                search.getRegSearch().setCustCode(SIHTMLUtil.getUserInfo(request).getCustCode());
                search.reset(lConnection);
                session.setAttribute(SIConfig.SISESSION_SEARCH_ADDRESS_NAME,search);
                
                if (search.validateMobile(request,lConnection)){
                    forwardKey(request,response,"webshop.jsp.front.vod.other.search.con");           
                }else{
                    forwardKey(request,response,"webshop.jsp.front.vod.other.search");           
                }
            }else if(actionName.equalsIgnoreCase(SIConfig.SIACTION_REMODIFY)){
                forwardKey(request,response,"webshop.jsp.front.vod.other.search");
            }else if(actionName.equalsIgnoreCase(SIConfig.SIACTION_CONFIRM)){
                UISearch search = (UISearch)session.getAttribute(SIConfig.SISESSION_SEARCH_EDIT_NAME);
	            UISearch searchAddress = (UISearch)session.getAttribute(SIConfig.SISESSION_SEARCH_ADDRESS_NAME);
	              
	            UIRegSearch regSearch=search.getRegSearch();
	            UIRegSearch regSearchAddress=searchAddress.getRegSearch();
	            
	            SISearchSrv frontSrv = new SISearchSrv();
	            frontSrv.sendMail(request,lConnection,regSearch,regSearchAddress);
	            frontSrv.insertUpdateLookForTableData(lConnection,regSearch,regSearchAddress);
	            forwardKey(request,response,"webshop.servlet.front.vod.main");
            }            
        }
      }catch(Exception e){
        e.printStackTrace();
        throw new ServletException();        
      }finally{
        databaseConnection.close();    
      }
    }
}
