//===========================================================================
// 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.selections.*;
import jp.gr.java_conf.bugslife.user.*;
import jp.gr.java_conf.bugslife.bug.*;
import jp.gr.java_conf.bugslife.form.*;
import jp.gr.java_conf.bugslife.util.Converter;
/**
 *<PRE>
 * 
 * BugDependencyActionクラス
 *
 *</PRE>
 * @version	0.0
 * @author	鰐川 正寛
 */
public final class BugDependencyAction 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 {
		
		ActionForward forward = null;
		/*
		 * 初期処理
		 */
		BugDependencyForm myForm = (BugDependencyForm)form;	//formの取得
		HttpSession session = getSession(request);//sessioの取得
		UserInfo logonUser = (UserInfo)getAttribute(session,"UserInfo");	//ログインユーザの取得
		int logonUno = logonUser.getUno();
		
		String action = myForm.getAction();
		//debug
		log.debug("action =" + action);
		
		//セッションより取得
		Bug bug = (Bug)session.getAttribute("bug");
		
		if (bug == null) {
			//バグインスタンスの取得
			bug = new Bug(myForm.getBno());
			//バグインスタンスのセット
			session.setAttribute("bug", bug);
		} else {
			//故障番号が一致しない
			if (bug.getBno() != myForm.getBno()) {
				//バグインスタンスの取得
				bug = new Bug(myForm.getBno());
				//バグインスタンスのセット
				session.setAttribute("bug", bug);
			}
		}
		
		//バグ番号
		int bno = bug.getBno();
		
		//
		//アクション別の処理
		//
		//表示の場合
		if (action.equals("show")){
			
			forward = mapping.findForward("show");
			
		//更新の場合
		} else if (action.equals("update")) {
			
			//バリデータ
			ActionErrors errors = myForm.validate(mapping, request);
			if (!errors.empty()) {
				 saveErrors(request, errors);
				return (new ActionForward(mapping.getInput()));
			}
			
			//パラメータを取得
			List parentBnos = myForm.getParentBugsList();
			List childBnos = myForm.getChildBugsList();
			
			//
			//bnoをkeyにBugDependsDBを削除
			//
			//子障害依存を削除
			Collection childs = 
					BugDepends.getChildBugDepends(bno);
			if (childs != null) {
				Iterator childDelIte = childs.iterator();
				while (childDelIte.hasNext()) {
					((BugDepends)childDelIte.next()).delete();
				}
			}
			
			//親障害依存を削除
			Collection parents = 
					BugDepends.getBugDepends(bno);
			if (parents != null) {
				Iterator parentDelIte = parents.iterator();
				while (parentDelIte.hasNext()) {
					((BugDepends)parentDelIte.next()).delete();
				}
			}
			
			//
			//子障害依存を登録
			//
			BugDepends childDep = myForm.getBugDepends();
			
			if (childBnos.size() != 0) {
				Iterator childIte = childBnos.iterator();
				while (childIte.hasNext()) {
					childDep.setDependBno(((Integer)childIte.next()).intValue());
					//追加
					childDep.insert(logonUno);
				}
			}
			
			//
			//親障害依存を登録
			//
			BugDepends parentDep = new BugDepends();
			parentDep.setDependBno(bno);
			
			if (parentBnos.size() != 0) {
				Iterator parentIte = parentBnos.iterator();
				while (parentIte.hasNext()) {
					parentDep.setBno(((Integer)parentIte.next()).intValue());
					//追加
					parentDep.insert(logonUno);
				}
			}
			
			//フォームの内容をリセットする。
			myForm.reset(mapping, request);
			
			myForm.setMessageKey("success.depends.update");
			
			//バグインスタンスのセット
			session.setAttribute("bug", bug);
			
			forward = mapping.findForward("update");
			
		}
		
		//バグ依存インスタンスのセット
		BugDepends depends = new BugDepends();
		depends.setBno(bno);
		depends.load();
		
		//親子バグ番号文字列のセット
		myForm.setParentBugs(depends.getParentsString(bno));
		myForm.setChildBugs(depends.getChildsString(bno));
		
		//Treeのセット
		BugDependsTree tree = new BugDependsTree(bno);
		session.setAttribute("tree", tree);
		
		return forward;
	}




}
