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

import java.util.Hashtable;

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.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.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;

/**
 * qO[v擾ActionNXłB
 * 
 * @author Kentaro Ishizaki (UNITEC)
 * @version 1.0
 */
public class AssignmentsGetChildGroupsAction 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" );

		GroupAndPeopleClient objXMO = null;
		int intGPResult = 0;
		try {
			StringBuffer strGetLine = new StringBuffer();
			strGetLine.append( "<childlenlist>\n" );

			// IꂽeIDGROUP_TREE_ROOT̏ꍇ
			if( strParentID.equals(AssignmentsConstants.GROUP_TREE_ROOT) ) {

				// OCĂ郆[UɊǗATCĂLO擾܂B
				// OC[UID̎擾
				String strUserID = (String)request.getSession().getAttribute( AssignmentsConstants.USER_ID );
				if( strUserID == null ) {
					request.setAttribute("strChildGroups", "ERR");
					return mapping.findForward("success");
				}

				// ZbVXMOID擾
				String strXmoID = (String)request.getSession().getAttribute( AssignmentsConstants.ASSIGN_XMO_ID );
				if( strXmoID == null ) {
					request.setAttribute("strChildGroups", "ERR");
					return mapping.findForward("success");
				}
				XMSBaseClient objXms = new XMSBaseClient();

				// [gGroupXg(ATCǗ(LO)Ă̂擾)
				if( !objXms.searchRootGroupList(strUserID, strXmoID ) ) {
					request.setAttribute("strChildGroups", "ERR");
					return mapping.findForward("success");
				}

				// ܂ꍇ
				GroupsData[] objGroupsList = objXms.getRootGroupList();

				// [gMemberShipIDZbVɊi[邽߂HashtableCX^X
				Hashtable htbRootInfo = new Hashtable();
				// ZbVɊi[܂
				request.getSession().setAttribute( AssignmentsConstants.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() ) {
					request.setAttribute("strChildGroups", "ERR");
					return mapping.findForward("success");
				}else{
					intGPResult = objXMO.getResultCount();
				}
				// O[vǗXMO̎擾
				String strGroupflag = "";//lO[vf
				GroupsClient objGroups = new GroupsClient();
				PeopleClient objPeople = new PeopleClient();

				int intCounter = 0;
				while( objXMO.next() ) {
					intCounter++;
					// O[vID̎擾
					String strChildID = objXMO.getChildID();
					if(strChildID.substring( 0, 2 ).equals( "GR" ) ) {
						strGroupflag = "0";
					}
					else{
						strGroupflag = "1";
					}

					// O[vIDɃO[v̂擾
					if(strGroupflag.equals( "0" )){
						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(intCounter==intGPResult){
								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 {
							request.setAttribute("strChildGroups", "ERR");
							return mapping.findForward("success");
						}
					}
					else{
						objPeople.clear();
						objPeople.setWhereUserID( strChildID );
						if( objPeople.select() ) {
							String strUserName = "";
							String strType = "";
							if( objPeople.next() ) {
								strUserName = objPeople.getUserName();
								strType = objPeople.getSex() + "^" + objPeople.getUserType();
							}
							//Kw̍ŏIR[h̏ꍇ
							if(intCounter==intGPResult){
								strType = strType + "^L";
							}
							strGetLine.append( "<child title=\"" );
							strGetLine.append( HtmlEscape.escape(strUserName) );
							strGetLine.append( "\" iconid=\"people\" id=\"" );
							strGetLine.append( strChildID );
							strGetLine.append( "\" type=\"" );
							strGetLine.append( strType );
							strGetLine.append( "\" />\n" );
						}
						else {
							request.setAttribute("strChildGroups", "ERR");
							return mapping.findForward("success");
						}

					}
				}
			}
			strGetLine.append( "</childlenlist>\n" );

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