//===========================================================================
// package
//===========================================================================
package jp.gr.java_conf.bugslife.action;

//===========================================================================
// import
//===========================================================================
import java.io.IOException;
import java.util.Locale;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import org.apache.commons.beanutils.*;

import java.util.*;

import jp.gr.java_conf.bugslife.form.*;
import jp.gr.java_conf.bugslife.bug.*;
import jp.gr.java_conf.bugslife.user.*;
/**
 *<PRE>
 * 
 * SerchActionクラス
 *
 *</PRE>
 * @version	0.0
 * @author	鰐川 正寛
 */
public final class SerchAction extends BaseAction 
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 * Log
	 */
	private Log log = LogFactory.getLog(this.getClass().getName());
//===========================================================================
// methods
//===========================================================================
	/**
	 * exevute
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param actionForm The optional ActionForm bean for this request (if any)
	 * @param request The HTTP request we are processing
	 * @param response The HTTP response we are creating
	 *
	 * @exception Exception if business logic throws an exception
	 */
	public ActionForward doAction(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
				throws Exception {
		/*  初期処理
		 */
		HttpSession session = getSession(request);	//sessioの取得
		UserInfo user = (UserInfo)getAttribute(session,"UserInfo");	//ログインユーザの取得
		ActionForward forward = null;
		SerchForm myForm = (SerchForm)form;
		
		String action = myForm.getAction();
		//debug
		log.debug("action = " + action);
		
		//検索画面表示
		if (action.equals("show")) {
			
			//エラーも一覧画面を出すため
			forward = mapping.findForward("init");
			
		//検索処理
		} else {
			
			Collection bugNumbers = null;			//バグ番号リスト
			Collection bugs = null;					//バグリスト
			
			//データチェック
			ActionErrors errors = myForm.validate(mapping, request);
			if (!errors.empty()) {
				saveErrors(request, errors);
				
				if (action.equals("quick")) {
					//quickの場合エラーを一覧画面を出すため
					bugs = new ArrayList();
					
					forward = mapping.findForward("failure");
				} else {
					//詳細検索の場合は検索画面に戻る
					forward = new ActionForward(mapping.getInput());
				}
				
			} else {
/*----------------------------------
				//バグ番号指定検索
				if (action.equals("quick")) {
					String where = new String(myForm.getWhere());
					bugs = BugFinder.findChoiceBug(where, myForm.getParams());
					
					forward = mapping.findForward("success");
					
				} else {
----------------------------------*/
				//詳細検索
					//対象となるバグ番号リストを求める
					String where = new String(myForm.getWhere());
					bugNumbers = BugFinder.findSerchBug(where, myForm.getParams());
					
					if (bugNumbers != null) {
						//バグリストを求める
						bugs = BugFinder.getBugs(bugNumbers);
						//debug
//						display(bugs);
						
					}
					
					forward = mapping.findForward("success");
					
				}
//			}
			//検索後の処理
			if (bugNumbers == null) {
				bugNumbers = new ArrayList();
				bugs = new ArrayList();
			}
			//カウントセット
			myForm.setCount(bugs.size());
			session.setAttribute("bugList", bugs);
			session.setAttribute("bugNUmberList", bugNumbers);
			
		}
		// Forward control to the specified success URI
		return forward;
	}
	//debug
	private void display(Collection list) {
		
		Iterator ite = list.iterator();
		while(ite.hasNext()) {
			
			ChoiceBug bug = (ChoiceBug)ite.next();
//			Bug bug = (Bug)ite.next();
			log.debug(bug.toString());
			
		}
		
	}
}
