/////////////////////////////////////////////////////////////////
//Copyright (C) 2005 NTT CORPORATION.
//
//	ʃOQƉʕ\
//
//		ύX
//			2005.01.31  VK쐬  OGA
//
//	@\
//
//	ʃOQƉʂɃc[\܂B
//
/////////////////////////////////////////////////////////////////
/*
 * 쐬: 2005/02/20
 *
 * TODO ̐ꂽt@C̃ev[gύXɂ͎փWv:
 * EBhE - ݒ - Java - R[hEX^C - R[hEev[g
 */
package jp.co.ntt.lms.xmf.actions.lo.wbt.IndividualLog;

import java.util.Hashtable;

import jp.co.ntt.lms.xmf.actions.lo.wbt.IndividualLog.inc.*;

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 jp.co.ntt.lms.xmo.XMOException;
import jp.co.ntt.lms.xmo.Lo.LoClient;
import jp.co.ntt.lms.xmo.Environment;
import jp.co.ntt.lms.lo.*;
import jp.co.ntt.lms.lo.WBT.IndividualLog.IndividualLogStoreData;
import jp.co.ntt.lms.Common.RDF.*;
import jp.co.ntt.lms.xmo.MessageData;


/**
 * @author Administrator
 *
 * TODO ̐ꂽ^Rg̃ev[gύXɂ͎փWv:
 * EBhE - ݒ - Java - R[hEX^C - R[hEev[g
 */
public class IndividualLogMenu_TreeAction  extends Action{

	private IndividualLogMessga_inc IndividualLogMessga_inc = new IndividualLogMessga_inc();
	private IndividualLogCommon_inc IndividualLogCommon_inc = new IndividualLogCommon_inc();
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

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

		////////////////////////////////////////////////////////////////////
		// p[^擾
		// 	ELoID				LOID				K{
		// 	EAuID				AuID				C
		// 	ELoType			LoType				K{
		// 	EuserId			[UID			K{
		// 	EreturnURL			߂y[W			K{
		////////////////////////////////////////////////////////////////////
		String strShowChildlenURL  ="";
		String strTopURL = "";

		String strLoID = request.getParameter( "LoID" );
		String strAuID = request.getParameter( "AuID" );
		String strLoType = request.getParameter( "LoType" );
		String strUserID = request.getParameter( "userId" );
		// AknhcB
		String strRealLoID = "";
		///////////////////////////////////////////
		// p[^`FbN
		///////////////////////////////////////////
		if( strLoID == null || strLoType == null || strUserID == null ) {
			Exception e = new Exception();
			e.printStackTrace();
			request.setAttribute("message", IndividualLogMessga_inc.M012002);
			request.setAttribute("detail", IndividualLogMessga_inc.D012002);
			request.setAttribute("close", "on");

			return mapping.findForward("error");
		}

		/////////////////////////////////////////////
		// c[̃[gɕ\GIF̂ݒ
		/////////////////////////////////////////////
		String strRootGif = "";
		strRootGif = IndividualLogCommon_inc.IMG_ROOT_LO_CLOSE_o;

		String strprm = ""; //scriptpTREE\

		// |bvAbvj[\VM̐ݒێ܂B
		String strChildVM = "GetMenu.do";
		// qXg\JSP̐ݒێ܂B
		String strChildVM2 = "GetChildMatSt.do";

		String strDocument = "";
		String strMaterialID = "";

		try {
			IndividualLogStoreData onjData = new IndividualLogStoreData();
			strRealLoID = onjData.getRealLoID(strLoID);

			// ގʂLoTopClientCX^X
			// LOT[o̎擾
			Environment objEnv = new Environment();
			String strLoServerName = objEnv.getLoServerName();
			// LONXCX^X܂
			LoTopClient objLoTopClient = new LoTopClient( strLoServerName, strLoType );

			// LO狳ޏڍ׏擾
			if( objLoTopClient.getData(strRealLoID) ) {
				if( objLoTopClient.getDataLength() > 0 ) {
					LoComData objData = objLoTopClient.getDataInfo(0);
					// ID̎擾
					strMaterialID = objData.getMaterialID();
				}
			}

			// LO狳ޏڍ׏擾
			Hashtable objParams = new Hashtable();
			objParams.put( "MATID", strMaterialID );
			if( objLoTopClient.getMaterialStructFile(objParams) ) {
				strDocument = objLoTopClient.getMaterialFile();
			}
			else{
				//ލ\t@C̎擾Ɏs܂B;
				Exception e = new Exception();
				e.printStackTrace();
				return mapping.findForward("error");
			}

		}catch(Exception ex)
		{
			//VXeG[
			ex.printStackTrace();
			return mapping.findForward("error");
		}

		StringBuffer strIniNode = new StringBuffer();
		String strSectionTopID = null;
		String strSectionTopTitle = null;
		try
		{
			RDFDocument objDocument = new RDFDocument();
			RDFNode objRDFNode = objDocument.load( new StringBuffer(strDocument) );
			RDFNode objSectionTop = objRDFNode.getChildNodes().getNode(0);
			strSectionTopID = objSectionTop.getID();
			strSectionTopTitle = objSectionTop.getTitle();
			request.getSession().setAttribute( strLoID, objRDFNode );

			// WJNodeID擾
			strIniNode = getSectionNodeID( objRDFNode.getChildNodes().getNode(0), null );
		}
		catch(Exception ex)
		{
			//ލ\̓WJɎs܂B
			ex.printStackTrace();
			return mapping.findForward("error");
		}

		strprm = new String(strIniNode);

		// c[f[^擾p̃Avbg
		String strServerName = request.getServerName();
		int intServerPort = request.getServerPort();
		String strContextPath = request.getContextPath();

		request.setAttribute( "strServerName", strServerName);
		request.setAttribute( "intServerPort", new Integer(intServerPort));
		request.setAttribute( "strContextPath", strContextPath);
		request.setAttribute( "strSectionTopID", strSectionTopID);
		request.setAttribute( "strRootGif", strRootGif);
		request.setAttribute( "strSectionTopTitle", strSectionTopTitle);
		request.setAttribute( "strprm", strprm);

		request.setAttribute( "strChildVM", strChildVM);
		request.setAttribute( "strChildVM2", strChildVM2);

		request.setAttribute( "strShowChildlenURL", strShowChildlenURL);
		request.setAttribute( "strTopURL", strTopURL);
		request.setAttribute( "strLoID", strLoID);
		request.setAttribute( "strAuID", strAuID);
		request.setAttribute( "strLoType", strLoType);
		request.setAttribute( "strUserID", strUserID);
		request.setAttribute( "strRealLoID", strRealLoID);
		
		return mapping.findForward("success");
	}

	/*
	 * OQƉʃc[ɏ\鋳ލ\擾܂B
	 */
	private StringBuffer getSectionNodeID( RDFNode objNode, StringBuffer strIniNodeID ) {
		if( strIniNodeID == null ) {
			strIniNodeID = new StringBuffer();
		}

		RDFNodeList objList = objNode.getChildNodes();
		int intLength = objList.getLength();
		for( int i = 0; i < intLength; i++ ) {
			RDFNode objChildNode = objList.getNode( i );
			if( objChildNode.getNodeType() == RDFNode.BLOCK_NODE ) {
				if( strIniNodeID.length() > 0 ) {
					strIniNodeID.append( "," );
				}
				strIniNodeID.append( "'" + objNode.getID() + "'" );
				break;
			}
		}

		// qm[hċAĂяo
		for( int i = 0; i < intLength; i++ ) {
			RDFNode objChildNode = objList.getNode( i );
			if( objChildNode.getNodeType() == RDFNode.BLOCK_NODE ) {
				strIniNodeID = getSectionNodeID( objChildNode, strIniNodeID );
			}
		}

		return strIniNodeID;
	}
}
