/////////////////////////////////////////////////////////////////
//Copyright (C) 2005 NTT CORPORATION.
//
//	ATCXV@ATCʂ̃O[vc[\ActionNX
//
//		ύX
//			2005.02.16  VK쐬   ΍茫Y
//
//	@\
//
//	ATCʂ̃O[vc[\ActionNXłB
//
/////////////////////////////////////////////////////////////////
package jp.co.ntt.lms.xmf.actions.xmo.Assignments;

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

import jp.co.ntt.lms.xmf.xmo.Assignments.AssignmentsConstants;
import jp.co.ntt.lms.xmo.Groups.GroupsClient;

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;

/**
 * ATCʂ̃O[vc[\ActionNXłB
 * 
 * @author Kentaro Ishizaki (UNITEC)
 * @version 1.0
 */
public class Assignmentsframeset_GroupAction extends Action
{
	/**
	 * strutst[[NĂ΂郁\bhłB
	 * 
	 * @param mapping }bsOIuWFNgB
	 * @param form ANVtH[IuWFNgB
	 * @param request NGXgIuWFNgB
	 * @param response X|XIuWFNgB
	 * @return ActionForward ANVtH[hIuWFNgB
	 * @throws Exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String strGroupRootName_o = "";//O[v[g

		String strLoginUserID = (String)request.getSession().getAttribute(AssignmentsConstants.USER_ID);

		// bZ[W\[X擾
		MessageResources resources = getResources(request, "msg.xmf.xmo.Assignments.MessageResources");
		
		// OC[UX[p[[Uʂ̊Ǘ҂Ń[gID؂蕪܂B
		String strTreeRoot_o = null;
		if( strLoginUserID == null ) {
			// ZbV؂Ăꍇ̓OCʂɃ_CNg
			request.setAttribute("message", "");
			request.setAttribute("detail", "");
			request.setAttribute("close", "on");
			request.setAttribute("errorurl", AssignmentsConstants.LOGIN_REDIRECT);
			return mapping.findForward("error");
		}
		else {
			// OC[UX[p[[Ȕꍇ
			if( strLoginUserID.equals(AssignmentsConstants.SUPER_USER) ) {
				strTreeRoot_o = AssignmentsConstants.GROUP_ROOT_o;
				//////////////////////////////////////////////////
				//O[ṽ[gf[^x[X擾B
				//////////////////////////////////////////////////
				GroupsClient objGroups = new GroupsClient();
				// ̐ݒ
				objGroups.setWhereGroupID( AssignmentsConstants.GROUP_ROOT_o );
				if( !objGroups.select() ) {
					// ܂Ȃꍇ
					if( objGroups.isErr() ) {
						// G[̏ꍇ
						request.setAttribute("message", "");
						request.setAttribute("detail", "");
						request.setAttribute("close", "on");
						request.setAttribute("errorurl", objGroups.getURL());
						return mapping.findForward("error");
					}
					else {
						String strMessage =  objGroups.getMessage();
						request.setAttribute("message", strMessage);
						request.setAttribute("detail", "");
						request.setAttribute("close", "on");
						request.setAttribute("errorurl", "");
						return mapping.findForward("error");
					}
				}

				// O[v̎擾
				if( objGroups.next() ) {
					strGroupRootName_o = objGroups.getGroupName();
				}
				else {
					//O[v݂ȂꍇG[o͂
					String strMessage = resources.getMessage("M022007");
					request.setAttribute("message", strMessage);
					request.setAttribute("detail", "");
					request.setAttribute("close", "on");
					request.setAttribute("errorurl", "");
					return mapping.findForward("error");
				}

			}
			else {
				strTreeRoot_o = AssignmentsConstants.GROUP_TREE_ROOT;
				strGroupRootName_o = AssignmentsConstants.TREE_ROOT_NAME;
			}
		}

		// c[f[^擾p̃Avbg
		String strServerName_o = request.getServerName();
		int intServerPort_o = request.getServerPort();
		String strContextPath_o = request.getContextPath();
		
		
		// NGXgɕϐZbg
		request.setAttribute("strServerName_o", strServerName_o);
		request.setAttribute("intServerPort_o", new Integer(intServerPort_o));
		request.setAttribute("strContextPath_o", strContextPath_o);
		// 
		request.setAttribute("strTreeRoot_o", strTreeRoot_o);
		request.setAttribute("strGroupRootName_o", strGroupRootName_o);
		
		return mapping.findForward("success");
	}

}
