//===========================================================================
// 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;
import jp.gr.java_conf.bugslife.util.Assert;
/**
 *<PRE>
 * 
 * BugDetail2Actionクラス
 *
 *</PRE>
 * @version	0.0
 * @author	鰐川 正寛
 */
public final class BugAssignAction extends BaseAction 
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 * Form
	 */
	BugAssignForm m_form;
	/**
	 * session
	 */
	HttpSession m_session;
	/**
	 * Bug
	 */
	Bug m_bug = null;
	/**
	 * Bug
	 */
	UserInfo m_logonUser;
	/**
	 * 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;
		/*
		 * 初期処理
		 */
		m_form = (BugAssignForm)form;	//formの取得
		m_session = getSession(request);//sessioの取得
		m_logonUser = (UserInfo)getAttribute(m_session,"UserInfo");	//ログインユーザの取得
		m_bug = (Bug)m_session.getAttribute("bug");	//バグの取得
		
		String action = m_form.getAction();
		//debug
		log.debug(	"action =" + action + 
					" bno = " + m_form.getBno() +
					" bno(s) = " + (m_bug == null ? "null" : ""+m_bug.getBno()));
		log.debug("ccRequest = " + m_form.getCcRequest());
		
		/*
		 * メイン処理
		 */
		//表示の場合
		if (action.equals("show")){
			//=========================
			//bugインスタンスの取得判定
			//=========================
			if ((m_bug == null) || (m_bug.getBno() != m_form.getBno())) {
				//バグインスタンスの取得
				m_bug = new Bug(m_form.getBno());
				//バグインスタンスのセット
				m_session.setAttribute("bug", m_bug);
			}
			
			//入力情報セット
			PropertyUtils.copyProperties(m_form, m_bug);
			//ＣＣのセット
			m_session.setAttribute("ccs", m_bug.getCc());
			//showへ遷移
			forward = mapping.findForward("success");
			
		//submitの場合
		} else {
			//=========================
			//bugインスタンスの取得判定
			//=========================
			if ((m_bug == null) || (m_bug.getBno() != m_form.getBno())) {
				//警告！
				log.warn("unmatch bno ");
				//バグインスタンスの取得
				m_bug = new Bug(m_form.getBno());
				//バグインスタンスの再セット
				m_session.setAttribute("bug", m_bug);
			}
			//チェック
			ActionErrors errors = m_form.validate(mapping, request);
			if (!errors.empty()) {
				 saveErrors(request, errors);
				return (new ActionForward(mapping.getInput()));
			}
			
			//CC追加か？
			if (m_form.getCcRequest().equals("add")) {
				//ＣＣの追加
				addCc();
				
				forward = mapping.findForward("success");
			//CCの削除か？
			} else if (m_form.getCcRequest().equals("del")) {
				//ＣＣの削除
				delCc();
				
				forward = mapping.findForward("success");
			//変更の確定
			} else {
				
				//ccのセット&更新
				Collection ccs = (Collection)m_session.getAttribute("ccs");
				m_bug.setCc(ccs);
				m_bug.updateCc(m_logonUser.getUno());
				
				/*-----------------------------------------------------
 				ドロップダウンでななくなっためコメントアウトします。
                                //====================
				//担当者変更
				//====================
				//差分取得開始
				m_bug.startActivity();
				
				m_bug.setIssuer(m_form.getIssuer());
				m_bug.setAuther(m_form.getAuther());
				m_bug.setAnalyst(m_form.getAnalyst());
				m_bug.setUpdater(m_form.getUpdater());
				m_bug.setComfirmer(m_form.getComfirmer());
				
				//差分の取得終了
				m_bug.endActivity();
				
				//差分を取ってくる
				Activitys act = m_bug.getActivitys();
				//履歴の出力
				act.addActivity(m_logonUser);
				
				m_bug.update(m_logonUser.getUno());		//更新
				-------------------------------------------------------*/
				
				//ここまで来れば成功
				m_form.setMessage("更新が完了しました");
				
				//バグインスタンスを取り直す
				m_bug = new Bug(m_form.getBno());
				//バグインスタンスのセット
				m_session.setAttribute("bug", m_bug);
				
				
				forward = mapping.findForward("success");
			}
			
		}
		// Forward control to the specified success URI
		return forward;
	}
	/**
	 * CCの追加
	 */
	private void addCc() {
		
		String[] unos = m_form.getAddCcUno();
		Collection ccs = (Collection)m_session.getAttribute("ccs");
		
		for (int i = 0; i < unos.length; i++) {
			int uno = Integer.parseInt(unos[i]);
			//debug
			//log.debug("addCC["+i+"]"+unos[i]);
			
			//unoが一致したらコレクションに追加しない
			//ための一致チェック
			boolean match = false;
			Iterator ite = ccs.iterator();
			while(ite.hasNext()) {
				Cc cc = (Cc)ite.next();
				if (cc.getUno() == uno) {
					match = true;
					break;
				}
			}
			if (! match) {
				Cc cc = Cc.createCc(m_bug.getBno(), uno);
				ccs.add(cc);
			}
		}
		//formに残った場合に反転してしまうので、初期化する
		m_form.setAddCcUno(null);
		
	}
	/**
	 * CCの削除
	 */
	private void delCc() {
		
		String[] unos = m_form.getDelCcUno();
		Collection ccs = (Collection)m_session.getAttribute("ccs");
		Iterator ite = ccs.iterator();
		
		while(ite.hasNext()) {
			Cc cc = (Cc)ite.next();
			for (int i = 0; i < unos.length; i++) {
				//unoが一致したらコレクションから削除する
				if (cc.getUno() == Integer.parseInt(unos[i])) {
					ite.remove();
				}
			}
		}
		//formに残った場合に反転してしまうので、初期化する
		m_form.setDelCcUno(null);
	}
}
