//===========================================================================
// 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.selections.*;
import jp.gr.java_conf.bugslife.user.*;
import jp.gr.java_conf.bugslife.util.*;
/**
 *<PRE>
 * 
 * ReportActionクラス
 *
 *</PRE>
 * @version	0.0
 * @author	鰐川 正寛
 */
public final class ReportAction 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;
		ReportForm myForm = (ReportForm)form;
		
		String action = myForm.getAction();
		String reportType = myForm.getReportType();
		//debug
		log.debug("action = " + action + " reportType = " + reportType);
		
		//検索画面表示
		if (action.equals("show")) {
			
			//カレント工程をデフォルトの工程にセットする。
			String[] pno = new String[1];
			pno[0] = Sysconf.getSysconf().getCurrentPno();
			myForm.setProcessName(pno);
			
			//条件入力画面表示
			forward = mapping.findForward("init");
			
		//検索処理
		} else {
			
			Collection bugNumbers = null;			//バグ番号リスト
			Collection bugs = null;					//バグリスト
			
			//データチェック
			ActionErrors errors = myForm.validate(mapping, request);
			if (!errors.empty()) {
				saveErrors(request, errors);
				//条件入力画面に戻る
				forward = new ActionForward(mapping.getInput());
				
			} else {
				//発生件数集計(週計)
				if (reportType.equals("1")) {
					//未サポート
					Varidater.addErrors("error.unsuport.type", "reportType", errors);
					saveErrors(request, errors);
					forward = new ActionForward(mapping.getInput());	//条件入力画面に戻る
					
				//発生件数集計(日計)
				} else if (reportType.equals("2")) {
					//未サポート
					Varidater.addErrors("error.unsuport.type", "reportType", errors);
					saveErrors(request, errors);
					forward = new ActionForward(mapping.getInput());	//条件入力画面に戻る
					
				//担当者別故障一覧
				} else if (reportType.equals("3")) {
					performStatistics3(myForm,session);
					
					//区分一覧へ遷移する
					forward = mapping.findForward("report3");
					
				//発行者別故障一覧
				} else if (reportType.equals("4")) {
					
					performStatistics4(myForm,session);
					
					//区分一覧へ遷移する
					forward = mapping.findForward("report4");
					
				//区分別集計
				} else if (reportType.equals("5")) {
					
					performStatistics5(myForm,session);
					
					//区分一覧へ遷移する
					forward = mapping.findForward("report5");
					
				//エラー機能別集計（弱点集計）
				} else if (reportType.equals("6")) {
					
					performStatistics6(myForm,session);
					
					//区分一覧へ遷移する
					forward = mapping.findForward("report6");
					
				//サブシステム別障害件数
				} else if (reportType.equals("7")) {
					//未サポート
					Varidater.addErrors("error.unsuport.type", "reportType", errors);
					saveErrors(request, errors);
					forward = new ActionForward(mapping.getInput());	//条件入力画面に戻る
					
				//ＴＡＴ集計
				} else if (reportType.equals("8")) {
					performStatistics8(myForm,session);
					
					//区分一覧へ遷移する
					forward = mapping.findForward("report8");
					
				//バグ集計（品質判定）
				} else if (reportType.equals("9")) {
					//未サポート
					Varidater.addErrors("error.unsuport.type", "reportType", errors);
					saveErrors(request, errors);
					forward = new ActionForward(mapping.getInput());	//条件入力画面に戻る
					
				} else {
					
					Assert.check(false, "invalid reportType type = " + reportType);
					
				}
				
			}
		}
		// Forward control to the specified success URI
		return forward;
	}
	/**
	 * 担当者別集計
	 */
	private void performStatistics3(ReportForm myForm, HttpSession session) {
		//
		String where = new String(myForm.getWhere());
		
		//担当者別集計
		Collection dispatchs = BugFinder.getDispatchStatistics(
						where, myForm.getParams());
		
		//セッションにセットする
		session.setAttribute("stDispatchs", dispatchs);
		
	}
	/**
	 * 発行者別集計
	 */
	private void performStatistics4(ReportForm myForm, HttpSession session) {
		//
		String where = new String(myForm.getWhere());
		
		//発行者別集計
		Collection issuers = BugFinder.getIssuerStatistics(
						where, myForm.getParams());
		
		//debug
/*-----------------------------
		Iterator ite = issuers.iterator();
		while(ite.hasNext()) {
			BugListStatistics bug = (BugListStatistics)ite.next();
			
			log.debug(bug.toString());
		}
-----------------------------*/
		
		//セッションにセットする
		session.setAttribute("stIssuers", issuers);
		
	}
	/**
	 * 区分集計
	 */
	private void performStatistics5(ReportForm myForm, HttpSession session) {
		//
		String where = new String(myForm.getWhere());
		
		//エラータイプ集計
		Collection errorTypes = BugFinder.getSelectionStatistics(
						BugFinder.STYPE_ERROR_TYPE,		//エラータイプ集計
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stErrorTypes", errorTypes);
		
		//エラー原因集計
		Collection errorCause = BugFinder.getSelectionStatistics(
						BugFinder.STYPE_ERROR_CAUSE,		//エラー原因集計
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stErrorCauses", errorCause);
		
		//障害発生工程集計
		Collection issuPnos = BugFinder.getSelectionStatistics(
						BugFinder.STYPE_ISSU_PNO,		//発生工程集計
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stIssuPnos", issuPnos);
		
		//障害混入工程集計
		Collection trainPnos = BugFinder.getSelectionStatistics(
						BugFinder.STYPE_TRAIN_PNO,		//混入工程集計
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stTrainPnos", trainPnos);
		
		//摘出すべき工程集計
		Collection shouldFindPnos = BugFinder.getSelectionStatistics(
						BugFinder.STYPE_SHOULD_FIND_PNO,		//摘出すべき工程集計
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stShouldFindPnos", shouldFindPnos);
		
		//摘出遅延理由集計
		Collection errorDelayReason = BugFinder.getSelectionStatistics(
						BugFinder.STYPE_ERROR_DELAY_REASON,		//摘出遅延理由集計
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stErrorDelayReasons", errorDelayReason);
		
	}
	/**
	 * エラー機能集計
	 */
	private void performStatistics6(ReportForm myForm, HttpSession session) {
		//
		String where = new String(myForm.getWhere());
		
		//エラータイプ集計
		Collection errorPoints = BugFinder.getSelectionStatistics(
						BugFinder.STYPE_ERROR_POINT,		//エラー機能集計
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stErrorPoints", errorPoints);
		
	}
	/**
	 * ＴＡＴ集計
	 */
	private void performStatistics8(ReportForm myForm, HttpSession session) {
		//
		String where = new String(myForm.getWhere());
		
		//ＴＡＴ集計
		Collection tats = BugFinder.getTatStatistics(
						where, myForm.getParams());
		//セッションにセットする
		session.setAttribute("stTats", tats);
		
	}
	/**
	 * display(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());
			
		}
		
	}
}
