/*
 * 쐬: 2005/02/13
 *
 * TODO ̐ꂽt@C̃ev[gύXɂ͎փWv:
 * EBhE - ݒ - Java - R[hEX^C - R[hEev[g
 */
package jp.co.ntt.lms.xmf.actions.lo.scorm.individuallog;

import java.util.Hashtable;

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

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;

import jp.co.ntt.lms.lo.LoComData;
import jp.co.ntt.lms.lo.LoTopClient;
import jp.co.ntt.lms.xmo.Lo.LoClient;
import jp.co.ntt.lms.xms.logsumup.scorm.ScormDocument;
import jp.co.ntt.lms.xmf.actions.lo.scorm.individuallog.inc.IndividualLogCommon_inc;

import jp.co.ntt.lms.xmo.*;

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

	/* ( Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO ꂽ\bhEX^u

		IndividualLogCommon_inc IndividualLogCommon_inc = new IndividualLogCommon_inc(); 

		// bZ[W\[X擾
		MessageResources resources = getResources(request);

		// 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" );
		String strRealLoID = "";	// Aknhc

		///////////////////////////////////////////
		// p[^`FbN
		///////////////////////////////////////////
		if( strLoID.equals("") || strLoType.equals("") || strUserID.equals("") ) {
			request.setAttribute("message", resources.getMessage("M012002"));
			request.setAttribute("detail", resources.getMessage("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\

		HttpSession session = request.getSession(true);

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

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

		try{
			// RealLoID̎擾
			LoClient onjData = new LoClient();
			if(onjData.getRealLOIDFromLOID(strLoID) != true){
				throw new Exception();
			}
			strRealLoID = onjData.getRealLOIDResult();

			// ގʂ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();
				throw e;
			}

		}catch(Exception ex){
			//VXeG[
			ex.printStackTrace();
			throw ex;
		}

		String strSectionTopID = null;
		String strSectionTopTitle = null;
		try{
			// Scormލ\`擾iScormm[h쐬j
			ScormDocument objSCONode = new ScormDocument();
			objSCONode.parseXML( strDocument );
			// [ghc̐ݒ
			strSectionTopID = objSCONode.getRootItemID();
			if(strSectionTopID.equals(null)){
				throw new Exception();
			}
			// [g^Cg̐ݒ
			strSectionTopTitle = objSCONode.getRootTitle();
			if(strSectionTopTitle.equals(null)){
				throw new Exception();
			}
			session.setAttribute( strLoID, objSCONode );

		}
		catch(Exception ex)
		{
			//ލ\̓WJɎs܂B
			ex.printStackTrace();
			throw ex;
		}
		// WJރXghcݒ肷
		strprm = strSectionTopID;

		// 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");
	}
}
