/*
 * 作成日: 2005/02/17
 */
package jp.co.ntt.lms.xmf.actions.xmo.evaluate;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import java.net.URLEncoder;
import java.util.Date;
import java.text.SimpleDateFormat;

import jp.co.ntt.lms.xmo.Evaluate.EvaluateClient;
import jp.co.ntt.lms.xmo.Lo.LoClient;
import jp.co.ntt.lms.xmf.common.CommonFunction;

/**
 * @author 梅原 浩司
 */
public class EvaluateAction extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// メッセージリソース取得
		MessageResources resCnt = getResources(request, "msg.xmf.xmo.Evaluate.MessageResources");

		// キャッシュ無効処理
		response.setHeader("cache-control", "no-cache");

		/////////////////////////////////////////////////////////////
		// 各パラメータ取得処理
		/////////////////////////////////////////////////////////////
		String strAction       = (String)request.getParameter("ACTION");

		// SystemURL
		String strSystemURL = (String)request.getAttribute("SYSTEM_URL");
		if(strSystemURL == null)
		{
			strSystemURL = (String)request.getParameter("SYSTEM_URL");
			if(strSystemURL == null)
			{
				String strMessage = "";
		
				strMessage = URLEncoder.encode("システムURLが指定されていません。", "UTF-8");

				response.sendRedirect("../Message/Messageerror.vm?message=" + strMessage);
			}
		}

		// LoID
		String strLoID = (String)request.getAttribute("LO_ID");
		if(strLoID == null)
		{
			strLoID = (String)request.getParameter("LO_ID");
			if(strLoID == null)
			{
				// エラーページを出力
				String strMessage = "";
		
				strMessage = URLEncoder.encode(resCnt.getMessage("STR_ERRMSG_LOID"), "UTF-8");

				response.sendRedirect("../Message/Messageerror.vm?message=" + strMessage);
			}
		}

		// UserID
		String strUserID = (String)request.getAttribute("USER_ID");
		if(strUserID == null)
		{
			strUserID = (String)request.getParameter("USER_ID");
			if(strUserID == null)
			{
				String strMessage = "";
			
				strMessage = URLEncoder.encode(resCnt.getMessage("STR_ERRMSG_USERID"), "UTF-8");

				response.sendRedirect("../Message/Messageerror.vm?message=" + strMessage);
			}
		}

		// Score
		String strScore        = (String)request.getParameter("SCORE");

		// Comment
		String strCustomMsgDir = (String)request.getParameter("CUSTOM_MSG_DIR");
		if(strCustomMsgDir == null)
		{
			strCustomMsgDir = "";
		}



		/////////////////////////////////////////////////////////////
		// 評価テーブル追加処理
		/////////////////////////////////////////////////////////////
		if(strAction != null && strAction.equals("on"))
		{
			// RealLOID取得
			String strRealLOID = "";
			LoClient objLoClient = new LoClient();
			if(objLoClient.getRealLOIDFromLOID(strLoID)){
				strRealLOID = objLoClient.getRealLOIDResult();
			}
			else{
				// 検索がうまくいかなかった場合
				if( objLoClient.isErr() ) {
					// エラーの場合
					response.sendRedirect( objLoClient.getURL() );
				}
				else {
					String strMessage =  objLoClient.getMessage();
					response.sendRedirect( CommonFunction.getParamErrURL( strMessage, null, true ) );
				}
			}

			EvaluateClient objEc = new EvaluateClient();

			objEc.setLoID(strRealLOID);
			objEc.setUserID(strUserID);
			Date objDate = new Date();
			SimpleDateFormat objDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String strDate = objDateFormat.format(objDate);
			objEc.setScoreDate(strDate);
			objEc.setScore(strScore);
			objEc.setCustomMsgDir(strCustomMsgDir);

			// 登録メソッド実行
			if(!objEc.insert())
			{
				// 登録がうまくいかなかった場合
				if(objEc.isErr())
				{
					// エラーの場合
					response.sendRedirect( objEc.getURL() );
				}
				else
				{
					String strMessage =  objEc.getMessage();
					response.sendRedirect(CommonFunction.getParamErrURL(strMessage, null, true));
				}
			}
		}
		
		request.setAttribute("strAction", strAction);
		request.setAttribute("strLoID", strLoID);
		request.setAttribute("strUserID", strUserID);
		request.setAttribute("strSystemURL", strSystemURL);
		request.setAttribute("strCustomMsgDir", strCustomMsgDir);

		return mapping.findForward("success");
	}
}
