/////////////////////////////////////////////////////////////////
//Copyright (C) 2005 NTT CORPORATION.
//
//	OfficeLOツールクラス
//
//		変更履歴
//			2005.02.17 新規作成  梅原 浩司
//
//	機能
//
//	このクラスはOfficeLOツールのクラスです。
//
////////////////////////////////////////////////////////////////

package jp.co.ntt.lms.xmf.tools.lo.office;

import jp.co.ntt.lms.xmf.tools.ToolBase;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

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

import jp.co.ntt.lms.MultipartParser;
import jp.co.ntt.lms.Part;
import jp.co.ntt.lms.lo.Office.OfficeLoEnv;
import jp.co.ntt.lms.xmf.common.CommonFunction;

/**
 *  OfficeLOツールクラス
 * 
 *  このクラスはOfficeLOツールのクラスです。
 * 
 *  @author 梅原 浩司
 *  @version 1.0
 */
public class OfficeLoTool extends ToolBase {
//	ADD 2005/02/23 START Tsuboi(UNITEC) [キーワードリスト数]
	private final int NUM_LOSEARCH_KEYWORD_COUNT = 5;
//ADD 2005/02/23 END Tsuboi(UNITEC) [キーワードリスト数] 
	
	/**
	 * LOファイルをアップロードする。
	 * @param request リクエストオブジェクト
	 * @param response レスポンスオブジェクト
	 * @throws Exception
	 */
	public void uploadLoFile(HttpServletRequest request, HttpServletResponse response)
	throws Exception
	{
		String strFileName        = "";												// Lo登録ファイル名
		String strParentID        = "";												// 親ID
		String strReturnURL       = "";												// 戻りURL
		String strLoType          = "";												// Loタイプ
		String strAction          = "";												// 登録フラグ
		String strNextURL         = "";												// 次画面URL
		String strLoTitle         = "";												// Loタイトル
		String strForeignID       = "";												// 外部ID
		String strLoExplanation   = "";												// Loの説明
		String strKeyWord         = "";												// キーワード
		String strVersion         = "";												// バージョン
		String strCreateDate      = "";												// 作成日
		String strConclusion      = "";												// 完結要件
		String strWinHeight		  = "";												// Windowサイズ(縦)
		String strWinWidth		  = "";												// Windowサイズ(横)
		String strSessionID = "";													// SessionID

		String strOfficeURL       = "";												// Office教材URL
		String strOutDir          = "D:/LoUpload";									// Lo登録フォルダ
		int i               	= 0;
		String strLoID 			= "";
		String strRealLoID		= "";
		String strOldFileName	= "";

		String strEntryDate		= "";		// 登録日
		String strEntryName		= "";		// 登録者
		String strEntryID		= "";		// 登録ログインID
		String strLastDate		= "";		// 最終更新日
		String strLastName		= "";		// 最終更新者
		String strLastID		= "";		// 最終登録ログインID
		String strRenewCnt		= "";		// 更新回数
		String strPrere		 	= "";		// 前提条件
// ADD 2005/02/13 START Katsuhiko Kagami(UNITEC) [LO購入項目追加]
		String strBuyStatus 	= "";		// 購入ステータス
// ADD 2005/02/13 END Katsuhiko Kagami(UNITEC) [LO購入項目追加]
// ADD 2005/02/14 START s-mori [推奨LO検索キーワード追加]
		String[] strLoSearchKeyWord = new String[NUM_LOSEARCH_KEYWORD_COUNT];
		for(int k = 0; k < NUM_LOSEARCH_KEYWORD_COUNT; k++){
			strLoSearchKeyWord[k] = "";
		}
		Vector vecLoSearchKeyWordList = new Vector();	
// ADD 2005/02/14 END
//		 ADD 2005/02/25 START Katsuhiko Kagami(UNITEC) [LOM詳細情報]
		String strLomDetail 	= "";		// LOM詳細情報
//	 ADD 2005/02/25 END Katsuhiko Kagami(UNITEC) [LOM詳細情報]

		try
		{

			Hashtable htbParam        = new Hashtable();								// アップロードパラメータリスト
			ServletInputStream objSis = (ServletInputStream)request.getInputStream();	// Postファイルデータ取得
			String strCt              = request.getContentType();						// コンテントタイプ取得
			int intClen               = request.getContentLength();						// コンテント長取得
			//String strFileSeparator   = System.getProperty("file.separator");
			String strFileSeparator   = "\\";
			OfficeLoEnv objOfficeEnv;
			String strBaseDir;
			try{
				objOfficeEnv = new OfficeLoEnv();
			}catch(Exception e){
				response.sendRedirect( CommonFunction.getParamErrURL( getContextString("STR_FILE_ACCESS_ERR"), null, true ) );
				return;
			}
			try{
				strBaseDir = objOfficeEnv.getMaterialPath();
			}catch(Exception e){
				response.sendRedirect( CommonFunction.getParamErrURL( getContextString("STR_ENV_ERR"), null, true ) );
				return;
			}
			if(strBaseDir == null || strBaseDir.equals(""))
			{
				strBaseDir = strOutDir;
			}
			// 編集アップロードクラス生成
			MultipartParser objMp     = new MultipartParser(strCt,
															intClen,
															objSis,
															strBaseDir);				
			// ファイル内容０バイトはアップロードエラーとする
			if(objMp.getFileSize() == 0)
			{
				Exception e = new Exception(getContextString("MSG_UPLOAD_ERR"));
				e.printStackTrace();
				response.sendRedirect(CommonFunction.getParamErrURL(getContextString("MSG_UPLOAD_ERR"), null, true));
				return;											
			}
			// アップロードパラメータクラスリスト取得
			Vector vecList            = objMp.getEachParts();
			// Postパラメータ設定
			StringBuffer strBuf = new StringBuffer();
			for(i = 0; i < vecList.size(); i++)
			{
				Part objPart = (Part)vecList.get(i);
				htbParam     = (Hashtable)objPart.getParams();
				Enumeration objEnum = htbParam.keys();
				while(objEnum.hasMoreElements())
				{
					String strKey = (String)objEnum.nextElement();
					if(strKey.equals("fileName"))
					{
						strFileName   = (String)htbParam.get(strKey);
						if( strFileName != null && !strFileName.trim().equals( "" ) ) {
							int intIndex  = strFileName.lastIndexOf(strFileSeparator);
							strFileName   = strFileName.substring(intIndex + 1);
							intIndex      = strFileName.lastIndexOf(".");
						}
						else {
							strFileName = "";
						}
					}
					// hidden変数
					if(strKey.equals("parentID"))
					{
						strParentID = (String)htbParam.get(strKey);
					}
					if(strKey.equals("LoType"))
					{
						strLoType = (String)htbParam.get(strKey);
					}
					if(strKey.equals("returnURL"))
					{
						strReturnURL = (String)htbParam.get(strKey);
					}
					if(strKey.equals("action"))
					{
						strAction = (String)htbParam.get(strKey);
					}
					if(strKey.equals("nextURL"))
					{
						strNextURL = (String)htbParam.get(strKey);
					}
					// 入力変数
					if(strKey.equals("LoTytle"))
					{
						strLoTitle = (String)htbParam.get(strKey);
					}
					if(strKey.equals("ForeignID"))
					{
						strForeignID = (String)htbParam.get(strKey);
					}
					if(strKey.equals("SessionID"))
					{
						strSessionID = (String)htbParam.get(strKey);
					}
					if(strKey.equals("KeyWord"))
					{
						strKeyWord = (String)htbParam.get(strKey);
						if( strKeyWord.equals("KeyWord") )
						{
							strKeyWord = "";
						}
					}
					if(strKey.equals("Loexplanation"))
					{
						strLoExplanation = (String)htbParam.get(strKey);
						if( strLoExplanation.equals("Loexplanation") )
						{
							strLoExplanation = "";
						}
					}
					if(strKey.equals("Version"))
					{
						strVersion = (String)htbParam.get(strKey);
						if( strVersion.equals("Version") )
						{
							strVersion = "";
						}
					}
					if(strKey.equals("LocreateDate"))
					{
						strCreateDate = (String)htbParam.get(strKey);
						if( strCreateDate.equals("LocreateDate") )
						{
							strCreateDate = "";
						}
					}

					// 完結要件
					if(strKey.equals("hdnConclusion"))
					{
						strConclusion = (String)htbParam.get(strKey);

						if( strConclusion.equals("hdnConclusion") )
						{
							strConclusion = "";
						}
					}

					// Windowサイズ(縦)
					if(strKey.equals("txtWinHeight"))
					{
						strWinHeight = (String)htbParam.get(strKey);
						if( strWinHeight.equals("txtWinHeight") )
						{
							strWinHeight = "";
						}
					}

					// Windowサイズ(横)
					if(strKey.equals("txtWinWidth"))
					{
						strWinWidth = (String)htbParam.get(strKey);

						if( strWinWidth.equals("txtWinWidth") )
						{
							strWinWidth = "";
						}
					}
					if(strKey.equals("LoID"))
					{
						strLoID = (String)htbParam.get(strKey);

						if( strLoID.equals("LoID") )
						{
							strLoID = "";
						}
					}
					if(strKey.equals("RealLoID"))
					{
						strRealLoID = (String)htbParam.get(strKey);

						if( strRealLoID.equals("RealLoID") )
						{
							strRealLoID = "";
						}
					}
					if(strKey.equals("OldFileName"))
					{
						strOldFileName = (String)htbParam.get(strKey);

						if( strOldFileName.equals("OldFileName") )
						{
							strOldFileName = "";
						}
					}
					if( strKey.equals("EntryDate") )
					{
						strEntryDate = (String)htbParam.get("EntryDate");

						if( strEntryDate.equals("EntryDate") ) {
							strEntryDate = "";
						}
					}
					if( strKey.equals("EntryName") )
					{
						strEntryName		= (String)htbParam.get("EntryName");

						if( strEntryName.equals("EntryName") ) {
							strEntryName = "";
						}
					}
					if( strKey.equals("EntryID") )
					{
						strEntryID		= (String)htbParam.get("EntryID");

						if( strEntryID.equals("EntryID") ) {
							strEntryID = "";
						}
					}
					if( strKey.equals("LastDate") )
					{
						strLastDate		= (String)htbParam.get("LastDate");

						if( strLastDate.equals("LastDate") ) {
							strLastDate = "";
						}
					}
					if( strKey.equals("LastName") )
					{
						strLastName		= (String)htbParam.get("LastName");

						if( strLastName.equals("LastName") ) {
							strLastName = "";
						}
					}
					if( strKey.equals("LastID") )
					{
						strLastID		= (String)htbParam.get("LastID");

						if( strLastID.equals("LastID") ) {
							strLastID = "";
						}
					}
					if( strKey.equals("RenewCnt") )
					{
						strRenewCnt		= (String)htbParam.get("RenewCnt");

						if( strRenewCnt.equals("RenewCnt") ) {
							strRenewCnt = "";
						}
					}
					if( strKey.equals( "hdnPrere" ) )
					{
						strPrere		= (String)htbParam.get( "hdnPrere" );

						if( strPrere.equals( "hdnPrere" ) ) {
							strPrere = "";
						}
					}
// ADD 2005/02/13 START Katsuhiko Kagami(UNITEC) [LO購入項目追加]
					if( strKey.equals( "BuyStatus" ) )
					{
						strBuyStatus	= (String)htbParam.get( "BuyStatus" );

						if( strBuyStatus.equals( "BuyStatus" ) ) {
							strBuyStatus = "";
						}
					}
// ADD 2005/02/13 END Katsuhiko Kagami(UNITEC) [LO購入項目追加]

// ADD 2005/02/23 START Tsuboi [strLoSearchKeyWord挿入]
					for(int k = 0; k < NUM_LOSEARCH_KEYWORD_COUNT; k++){
						if( strKey.equals( "LoSearchKeyWord_" + k ) )
						{
							strLoSearchKeyWord[k]	= (String)htbParam.get( "LoSearchKeyWord_" + k );

							if( strLoSearchKeyWord[k].equals( "LoSearchKeyWord_" + k ) ) {
								strLoSearchKeyWord[k] = "";
							}
						}
					}
// ADD 2005/02/23 END Tsuboi [strLoSearchKeyWord挿入]
// ADD 2005/02/25 START Katsuhiko Kagami(UNITEC) [LOM詳細情報]
					if( strKey.equals( "LomDetail" ) )
					{
						strLomDetail	= (String)htbParam.get( "LomDetail" );

						if( strLomDetail.equals( "LomDetail" ) ) {
							strLomDetail = "";
						}
					}
// ADD 2005/02/25 END Katsuhiko Kagami(UNITEC) [LOM詳細情報]
				}

			}
			strOfficeURL = objOfficeEnv.getOfficeURL() +
						   "/" + strLoType +
						   "/" + strSessionID +
						   "/" + strFileName;
		}
		catch(IOException ie)
		{
			ie.printStackTrace();
			response.sendRedirect(CommonFunction.getParamErrURL(getContextString("MSG_UPLOAD_ERR"), null, true));
			return;											
		}
		catch(Exception e)
		{
			response.sendRedirect(CommonFunction.getParamErrURL(getContextString("MSG_UPLOAD_ERR"), null, true));
			return;
		}

		
		for(int k = 0; k < NUM_LOSEARCH_KEYWORD_COUNT; k++){
				vecLoSearchKeyWordList.add(strLoSearchKeyWord[k]) ;
		}

		context.put("strNextURL", strNextURL);
		context.put("strParentID", strParentID);
		context.put("strLoType", strLoType);
		context.put("strReturnURL", strReturnURL);
		context.put("strAction", strAction);
		context.put("strLoTitle", strLoTitle);
		context.put("strForeignID", strForeignID);
		context.put("strKeyWord", strKeyWord);
		context.put("strLoExplanation", strLoExplanation);
		context.put("strVersion", strVersion);
		context.put("strCreateDate", strCreateDate);
		context.put("strSessionID", strSessionID);
		context.put("strConclusion", strConclusion);
		context.put("strWinHeight", strWinHeight);
		context.put("strWinWidth", strWinWidth);
		context.put("strFileName", strFileName);
		context.put("strOfficeURL", strOfficeURL);
		context.put("strLoID", strLoID);
		context.put("strRealLoID", strRealLoID);
		context.put("strOldFileName", strOldFileName);
		context.put("strEntryDate", strEntryDate);
		context.put("strEntryName", strEntryName);
		context.put("strEntryID", strEntryID);
		context.put("strLastDate", strLastDate);
		context.put("strLastName", strLastName);
		context.put("strLastID", strLastID);
		context.put("strRenewCnt", strRenewCnt);
		context.put("strPrere", strPrere);
//ADD 2005/02/13 START Katsuhiko Kagami(UNITEC) [LO購入項目追加]
		context.put("BuyStatus", strBuyStatus);
// ADD 2005/02/13 END Katsuhiko Kagami(UNITEC) [LO購入項目追加]
// ADD 2005/02/14 START s-mori [推奨LO検索キーワード追加]
		context.put("vecLoSearchKeyWord", vecLoSearchKeyWordList);
// ADD 2005/02/14 END
//ADD 2005/02/25 START Katsuhiko Kagami(UNITEC) [LOM詳細情報]
		context.put("LomDetail", strLomDetail);
//ADD 2005/02/25 END Katsuhiko Kagami(UNITEC) [LOM詳細情報]

		return;
	}	
}
