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

import java.net.URLEncoder;
import java.util.Vector;

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

import jp.co.ntt.lms.xmf.xmo.RoleAssignments.RoleAssignmentsConstants;
import jp.co.ntt.lms.xmo.Base.XMSBaseClient;
import jp.co.ntt.lms.xmo.GroupAndPeople.GroupAndPeopleClient;
import jp.co.ntt.lms.xmo.Groups.GroupsClient;
import jp.co.ntt.lms.xmo.Groups.GroupsData;
import jp.co.ntt.lms.xmo.People.PeopleClient;
import jp.co.ntt.lms.xmo.RoleAssignments.RoleAssignmentsClient;
import jp.co.ntt.lms.xmo.Roles.RolesClient;

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ʂ̃ATCc[\ActionNXłB
 * 
 * @author Kentaro Ishizaki (UNITEC)
 * @version 1.0
 */
public class RoleAssignmentsframe_assignAction 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
	{
		///////////////////////////////////////////
		// p[^擾
		// 	EparentID	ehc	K{
		// 	EselectID	qhc	K{
		// 	EreturnURL	߂y[W		K{
		///////////////////////////////////////////
		String strParentID = request.getParameter("parentID");
		String strSelectID = request.getParameter("selectID");
		String strReturnURL = request.getParameter("returnURL");		// ߂y[W

		// bZ[W\[X擾
		MessageResources resources = getResources(request, "msg.xmf.xmo.RoleAssignments.MessageResources");
		
		String strLoginUserID_o = (String)request.getSession().getAttribute(RoleAssignmentsConstants.USER_ID);	// OC[U[ID
		
		// OC[UX[p[[Uʂ̊Ǘ҂Ń[gID؂蕪܂B
		String strTreeRoot_o = null;
		
		if(strLoginUserID_o == null){
			// ZbV؂Ăꍇ̓OCʂɃ_CNg
			request.setAttribute("message", "");
			request.setAttribute("detail", "");
			request.setAttribute("close", "on");
			request.setAttribute("errorurl", "../Base/LoginRedirector.vm");
			return mapping.findForward("error");
		}
		else{
			if(strLoginUserID_o.equals(RoleAssignmentsConstants.SUPER_USER)){
				strTreeRoot_o = RoleAssignmentsConstants.GROUP_ROOT_o;
			}else{
				strTreeRoot_o = RoleAssignmentsConstants.GROUP_TREE_ROOT;
			}
		}


		///////////////////////////////////////////
		// p[^`FbN
		///////////////////////////////////////////
		if( strParentID == null || strParentID.equals( "" ) ) {
			Exception e = new Exception();
			e.printStackTrace();
			String strMessage = resources.getMessage("M092001");
			String strDetail = resources.getMessage("D092001");
			request.setAttribute("message", strMessage);
			request.setAttribute("detail", strDetail);
			request.setAttribute("close", "on");
			request.setAttribute("errorurl", "");
			return mapping.findForward("error");
		}
		if( strSelectID == null || strSelectID.equals( "" ) ) {
			Exception e = new Exception();
			e.printStackTrace();
			String strMessage = resources.getMessage("M092002");
			String strDetail = resources.getMessage("D092002");
			request.setAttribute("message", strMessage);
			request.setAttribute("detail", strDetail);
			request.setAttribute("close", "on");
			request.setAttribute("errorurl", "");
			return mapping.findForward("error");
		}
		if( strReturnURL == null || strReturnURL.equals( "" ) ) {
			Exception e = new Exception();
			e.printStackTrace();
			request.setAttribute("message", "");
			request.setAttribute("detail", "");
			request.setAttribute("close", "on");
			request.setAttribute("errorurl", "../Com/ComSystemErr.vm");
			return mapping.findForward("error");
		}

		strReturnURL = URLEncoder.encode( strReturnURL, "UTF-8" );

		String strUserName_o = "";
		String strGroupName_o = "";

		//////////////////////////////////
		// l܂̓O[v̑݃`FbN
		//////////////////////////////////
		// O[vf[^擾
		if( strSelectID.substring( 0,2 ).equals( "GR" ) )
		{
			// O[vIuWFNg쐬

			GroupsClient objGroups = new GroupsClient();

			//̐ݒ
			objGroups.setWhereGroupID( strSelectID );
			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");
				}
			}

			// f[^݃`FbN
			if( !objGroups.next() )
			{
				//O[v݂ȂꍇG[o͂
				String strMessage = resources.getMessage("M092007");
				String strDetail = resources.getMessage("D092007");
				request.setAttribute("message", strMessage);
				request.setAttribute("detail", strDetail);
				request.setAttribute("close", "on");
				request.setAttribute("errorurl", "");
				return mapping.findForward("error");
			}
			//O[v
			strGroupName_o = objGroups.getGroupName();

		}
		// [Uf[^擾
		else if( strSelectID.substring( 0,2 ).equals( "PE" ) )
		{
			// lIuWFNg쐬

			PeopleClient objPeople = new PeopleClient();

			//̐ݒ
			objPeople.setWhereUserID( strSelectID );
			if( !objPeople.select() )
			{
				// ܂Ȃꍇ
				if( objPeople.isErr() )
				{
					// G[̏ꍇ
					request.setAttribute("message", "");
					request.setAttribute("detail", "");
					request.setAttribute("close", "on");
					request.setAttribute("errorurl", objPeople.getURL());
					return mapping.findForward("error");
				}
				else
				{
					String strMessage =  objPeople.getMessage();
					request.setAttribute("message", strMessage);
					request.setAttribute("detail", "");
					request.setAttribute("close", "on");
					request.setAttribute("errorurl", "");
					return mapping.findForward("error");
				}
			}

			// f[^݃`FbN
			if( !objPeople.next() )
			{
				//[U[݂ȂꍇG[o͂
				String strMessage = resources.getMessage("M092004");
				String strDetail = resources.getMessage("D092004");
				request.setAttribute("message", strMessage);
				request.setAttribute("detail", strDetail);
				request.setAttribute("close", "on");
				request.setAttribute("errorurl", "");
				return mapping.findForward("error");
			}
			//[U[
			strUserName_o = objPeople.getUserName();

			
		}

		// ZbVXMOID擾
		String strXmoID = (String)request.getSession().getAttribute(RoleAssignmentsConstants.ASSIGN_XMO_ID);
		if(strXmoID == null){
			String strMessage = resources.getMessage("M092009");
			String strDetail = resources.getMessage("D092009");
			request.setAttribute("message", strMessage);
			request.setAttribute("detail", strDetail);
			request.setAttribute("close", "on");
			request.setAttribute("errorurl", "");
			return mapping.findForward("error");
		}
		XMSBaseClient objXms = new XMSBaseClient();
		
		// [gGroupXg(ATCǗ(LO)Ă̂擾)
		if(!objXms.searchRootGroupList(strLoginUserID_o, strXmoID)){
			String strMessage = resources.getMessage("M093001");
			String strDetail = resources.getMessage("D093001");
			request.setAttribute("message", strMessage);
			request.setAttribute("detail", strDetail);
			request.setAttribute("close", "on");
			request.setAttribute("errorurl", "");
			return mapping.findForward("error");
		}
		
		// ܂ꍇ
		GroupsData[] objGroupsList = objXms.getRootGroupList();


		// Xgf[^擾
		// Assignmentse[u擾
		RoleAssignmentsClient objRoleAssignmentsXMO = null;
		RolesClient objRolesXMO = null;

		// RoleAssignmentsRoleId̎擾
		objRoleAssignmentsXMO = new RoleAssignmentsClient();
		objRoleAssignmentsXMO.setWhereUserID( strSelectID );
		objRoleAssignmentsXMO.setOrderByRoleAssID();
		if( !objRoleAssignmentsXMO.select() ) {
			// ܂Ȃꍇ
			if( objRoleAssignmentsXMO.isErr() ) {
				// G[̏ꍇ
				request.setAttribute("message", "");
				request.setAttribute("detail", "");
				request.setAttribute("close", "on");
				request.setAttribute("errorurl", objRoleAssignmentsXMO.getURL());
				return mapping.findForward("error");
			}
			else {
				String strMessage =  objRoleAssignmentsXMO.getMessage();
				request.setAttribute("message", strMessage);
				request.setAttribute("detail", "");
				request.setAttribute("close", "on");
				request.setAttribute("errorurl", "");
				return mapping.findForward("error");
			}
		}

		Vector vecRolesList = new Vector();
		while( objRoleAssignmentsXMO.next() ) {
			// [hc̎擾
			String strRoleID = objRoleAssignmentsXMO.getRoleID();
			// O[vhc̎擾
			String strGroupID = objRoleAssignmentsXMO.getGroupID();

			
			// O[v擾
			GroupsClient objGroupsXMO = new GroupsClient();
			// ̐ݒ
			objGroupsXMO.setWhereGroupID( strGroupID );
			if( !objGroupsXMO.select() ) {
				// ܂Ȃꍇ
				if( objGroupsXMO.isErr() ) {
					// G[̏ꍇ
					request.setAttribute("message", "");
					request.setAttribute("detail", "");
					request.setAttribute("close", "on");
					request.setAttribute("errorurl", objGroupsXMO.getURL());
					return mapping.findForward("error");
				}
				else {
					String strMessage =  objGroupsXMO.getMessage();
					request.setAttribute("message", strMessage);
					request.setAttribute("detail", "");
					request.setAttribute("close", "on");
					request.setAttribute("errorurl", "");
					return mapping.findForward("error");
				}
			}
			// O[v̎擾
			String strGroupName = "";
			if( objGroupsXMO.next() ) {
				strGroupName = objGroupsXMO.getGroupName();
			}
			else {
				request.setAttribute("message", "");
				request.setAttribute("detail", "");
				request.setAttribute("close", "on");
				request.setAttribute("errorurl", objGroupsXMO.getURL());
				return mapping.findForward("error");
			}

			// [擾
			objRolesXMO = new RolesClient();
			// ̐ݒ
			objRolesXMO.setWhereRoleID( strRoleID );
			if( !objRolesXMO.select() ) {
				// ܂Ȃꍇ
				if( objRolesXMO.isErr() ) {
					// G[̏ꍇ
					request.setAttribute("message", "");
					request.setAttribute("detail", "");
					request.setAttribute("close", "on");
					request.setAttribute("errorurl", objRolesXMO.getURL());
					return mapping.findForward("error");
				}
				else {
					String strMessage =  objRolesXMO.getMessage();
					request.setAttribute("message", strMessage);
					request.setAttribute("detail", "");
					request.setAttribute("close", "on");
					request.setAttribute("errorurl", "");
					return mapping.findForward("error");
				}
			}
			// [̎擾
			String strRoleName = "";
			String[] strRolesInfo = new String[5];
			if( objRolesXMO.next() ) {
				strRolesInfo[0] = objRolesXMO.getRoleID();			// hc
				strRolesInfo[1] = objRolesXMO.getRoleName();		// 
				strRolesInfo[2] = strGroupID;						// O[vhc
				strRolesInfo[3] = strGroupName;						// O[v
			}else{
				//[݂ȂꍇG[o͂
				String strMessage =  "[݂܂B";
				request.setAttribute("message", strMessage);
				request.setAttribute("detail", "");
				request.setAttribute("close", "on");
				request.setAttribute("errorurl", "");
				return mapping.findForward("error");
			}

////////////////////////////////////////////////////////////////////////////////////	//
			// O[vƐlǗXMONCAg̃CX^X
			GroupAndPeopleClient objGroupAndPeople = new GroupAndPeopleClient();

			// O[vhc
			String strID = strGroupID;

			// pX̎擾
			objGroupAndPeople.searchPath(strID);

			String strFullPath = "";
			if ( strLoginUserID_o.equals(RoleAssignmentsConstants.SUPER_USER) ){
				// pX̎擾܂ꍇ
				if( objGroupAndPeople.next() ) {
					String[] strPath = objGroupAndPeople.getPath();
					for( int j = 0; j < strPath.length; j++ ) {
						strFullPath += "/" + strPath[j];
					}
					strRolesInfo[4] = strFullPath;
				}
			}
			else{
				strFullPath = "/" + RoleAssignmentsConstants.GROUP_TREE_ROOT;
				// pX̎擾܂ꍇ
				if( objGroupAndPeople.next() ) {
					String[] strPath = objGroupAndPeople.getPath();
					boolean bflg = false;
					
					for( int j = 0; j < strPath.length; j++ ) {
						for( int i=0; i<objGroupsList.length; i++ ){
							if ( objGroupsList[i].getGroupID().equals(strPath[j]) ){
								bflg = true;
							}
						}
							
						if ( bflg ){
							strFullPath += "/" + strPath[j];
						}
					}
					strRolesInfo[4] = strFullPath;
				}
				//pXgɑΏۂ̃O[vID܂܂Ă邩`FbNB
				boolean bcontainflg = false;
				for( int i=0; i<objGroupsList.length; i++ ){
					if ( strFullPath.indexOf(objGroupsList[i].getGroupID()) != -1 ){
						bcontainflg = true;
						break;
					}
				}
				//܂܂ĂȂꍇ̓XLbv
				if ( bcontainflg != true ){
					continue;
				}
			}
			// ̃NA
			objGroupAndPeople.clear();
			vecRolesList.add(strRolesInfo);
////////////////////////////////////////////////////////////////////////////////////	//

		}

		//[񃊃XgxN^[zɃRs[܂B
		String[][] strRolesCopy_o = new String[vecRolesList.size()][5];
		vecRolesList.copyInto( strRolesCopy_o );
		
		
		// NGXgɕϐZbg
		request.setAttribute("strSelectID", strSelectID);
		request.setAttribute("strGroupName_o", strGroupName_o);
		request.setAttribute("strUserName_o", strUserName_o);
		// f[^Xg
		request.setAttribute("strRolesCopy_o", strRolesCopy_o);
		
		return mapping.findForward("success");
	}
}
