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

import java.util.Hashtable;
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.util.HtmlEscape;

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

/**
 * [ATCqO[v擾ActionNXłB
 * 
 * @author Kentaro Ishizaki (UNITEC)
 * @version 1.0
 */
public class RoleAssignmentsGetChildGroupsAction 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[^擾
		String strParentID = request.getParameter( "ParentID" );

		// LbV
		response.setHeader( "cache-control", "no-cache" );

		GroupAndPeopleClient objXMO = null;
		int intGPResult = 0;	
		try {
			StringBuffer strGetLine = new StringBuffer();
			strGetLine.append( "<childlenlist>\n" );
			
			if(strParentID.equals(RoleAssignmentsConstants.GROUP_TREE_ROOT)){
				// OCĂ郆[UɊǗATCĂLO擾܂B
				// OC[UID̎擾
				String strUserID = (String)request.getSession().getAttribute(RoleAssignmentsConstants.USER_ID);
				if(strUserID == null){
					request.setAttribute("strChildGroups", "ERR1");
					return mapping.findForward("success");
				}
				
				// ZbVXMOID擾
				String strXmoID = (String)request.getSession().getAttribute(RoleAssignmentsConstants.ASSIGN_XMO_ID);
				if(strXmoID == null){
					request.setAttribute("strChildGroups", "ERR2");
					return mapping.findForward("success");
				}
				XMSBaseClient objXms = new XMSBaseClient();
				
				// [gGroupXg(ATCǗ(LO)Ă̂擾)
				if(!objXms.searchRootGroupList(strUserID, strXmoID)){
					request.setAttribute("strChildGroups", "ERR3");
					return mapping.findForward("success");
				}
				
				// ܂ꍇ
				GroupsData[] objGroupsList = objXms.getRootGroupList();
				
				// [gMemberShipIDZbVɊi[邽߂HashtableCX^X
				Hashtable htbRootInfo = new Hashtable();
				// ZbVɊi[܂
				request.getSession().setAttribute(RoleAssignmentsConstants.ASSIGN_ROOT_INFO, htbRootInfo);
				for( int i = 0; i < objGroupsList.length; i++ ) {
					GroupsData objGroupsData = objGroupsList[i];
					
					String strGroupID = objGroupsData.getGroupID();
					String strParentGroupID = objGroupsData.getParentID();
					// ZbVLOIDL[ɂāAMemberShipIDi[܂
					htbRootInfo.put(strGroupID, strParentGroupID);
					
					// o͕̍쐬
					strGetLine.append( "<child title=\"" );
					strGetLine.append( HtmlEscape.escape(objGroupsData.getGroupName()) );
					strGetLine.append( "\" iconid=\"group\" id=\"" );
					strGetLine.append( strGroupID );
					strGetLine.append( "\" type=\"" );
					if(i == objGroupsList.length - 1){
						strGetLine.append( objGroupsData.getGroupType() + "^L");
					}
					else{
						strGetLine.append(objGroupsData.getGroupType());
					}
					strGetLine.append( "\" />\n" );
				}
			}
			else{
			
				// O[vƐlǗXMO̎擾
				objXMO = new GroupAndPeopleClient();
				objXMO.setParentID( strParentID );
				
				if( !objXMO.select() ) {
					// ܂Ȃꍇ
					if( objXMO.isErr() ) {
						// G[̏ꍇ
						request.setAttribute("message", "");
						request.setAttribute("detail", "");
						request.setAttribute("close", "on");
						request.setAttribute("errorurl", objXMO.getURL());
						return mapping.findForward("error");
					}
					else {
						String strMessage =  objXMO.getMessage();
						request.setAttribute("message", strMessage);
						request.setAttribute("detail", "");
						request.setAttribute("close", "on");
						request.setAttribute("errorurl", "");
						return mapping.findForward("error");
					}
				}else{
					intGPResult = objXMO.getResultCount();
				}
				// O[vǗXMO̎擾
				String strGroupflag = "";//lO[vf
				GroupsClient objGroups = new GroupsClient();

				Vector vecChildList = new Vector();
				while( objXMO.next() ) {
					
					// O[vID̎擾
					String strChildID = objXMO.getChildID();
					if(strChildID.substring( 0, 2 ).equals( "GR" ) ) {
						vecChildList.add( strChildID );
					}
				}
				
				for( int i = 0; i < vecChildList.size(); i++ ) {
					String strChildID = (String)vecChildList.get(i);
					// O[vIDɃO[v̂擾
					objGroups.clear();
					objGroups.setWhereGroupID( strChildID );
					if( objGroups.select() ) {
						String strGroupName = "";
						String strGroupType = "";
						if( objGroups.next() ) {
							strGroupName = objGroups.getGroupName();
							strGroupType = objGroups.getGroupType();
						}
						//Kw̍ŏIR[h̏ꍇ
						if(i==vecChildList.size()-1){
							strGroupType = strGroupType + "^L";
						}
						strGetLine.append( "<child title=\"" );
						strGetLine.append( HtmlEscape.escape(strGroupName) );
						strGetLine.append( "\" iconid=\"group\" id=\"" );
						strGetLine.append( strChildID );
						strGetLine.append( "\" type=\"" );
						strGetLine.append( strGroupType );
						strGetLine.append( "\" />\n" );
					}
					else {
						// ܂Ȃꍇ
						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");
						}
					}
				}
			}
			strGetLine.append( "</childlenlist>\n" );

			request.setAttribute("strChildGroups", strGetLine.toString());
			return mapping.findForward("success");
		}
		catch( Exception e ) {
			e.printStackTrace();
			request.setAttribute("strChildGroups", "ERR_EXCEPTION");
			return mapping.findForward("success");
		}
		
	}
}
