/*
̃t@C̒ǵANTT I[v\[XCZX@o[W 1.0iu{
_vƂj̓Kp󂯂܂B
{_炵ȂÃt@CgpĂ͂Ȃ܂B
{_̃Rs[́Âtqkł܂B
yzzTCgURLz http://www.oss.ecl.ntt.co.jp/lms/

{_ɊÂЕz\tgEFÁÂ܂܁Âَ͖
ނ̕ۏ؂ȂŁAЕz܂B{_ɊÂyѐ𗥂
̕ɂẮA{_QƂĂB

uIWiR[hv́A NTT Cyber Space Laboratories Code łB 
uIWiR[hv́uJҁv́A{dMdbЂłB  
{dMdbЂɂn삳ꂽ́ACopyright (C) 2004 
{dMdb łB
SĂ̌ۂ܂B 
uRgr[^vF_____________________________________ 


The contents of this file are subject to the NTT Opensource License
Version 1.0 (the License); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
yzzTCgURLz http://www.oss.ecl.ntt.co.jp/lms/

Software distributed under the License is distributed on an AS IS
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.

The Original Code is NTT Cyber Space Laboratories Code .

The Initial Developer of the Original Code is NIPPON TELEGRAPH AND 
TELEPHONE CORPORATION.
Portions created by the NIPPON TELEGRAPH AND TELEPHONE CORPORATION 
are Copyright (C) 2004 NIPPON TELEGRAPH AND TELEPHONE CORPORATION. 
All Rights Reserved.

Contributor(s) ______________________________________.
*/

//
//	Offline K񓚏󋵃f[^XgANX
//			ύX
//				2004.01.08	VK쐬	꓇@pK
//
package jp.co.ntt.lms.lo.Offline.IndividualLog;

// Java API
import java.util.Vector;

import jp.co.ntt.lms.Common.RDF.RDFNode;

/**
 * K񓚏󋵃f[^XgA<BR>
 * 
 * @author H.Takishima
 * @version 2.0.4.0.0 2004/01/08
 */
public class IndividualLogReplyStatusStoreData extends IndividualLogStoreData {

	/**LOID **/
	private String mstrLOID;
	
	/**GroupID **/
	private String mstrGroupID;
	
	/**LoType **/
	private String mstrLoType;
	
	/**ZbV擾ڎ**/
	private RDFNode mobjNode;

	/**AUPʐݖMAX SQL擾f[^i[̈**/
	private String mstrSqlMAXData[ ][ ];

	/**AUPʐݖMAX SQLvZ i[̈**/
	Vector mvecMax;

	/**SQL擾f[^i[̈**/
	private String mstrSqlData[ ][ ];

	/**Kѕ̓f[^iSj**/
	private Vector mvecStoreData;
	
	/**
	 * RXgN^
	 * @param LOID
	 * @param GroupIDi[Uw莞̓[UIDAO[vw莞̓O[vIDj
	 * @param LoType
	 * @param objNode
	 **/
	public IndividualLogReplyStatusStoreData(String strLOID,String strGroupID,String strLoType,RDFNode objNode ) {
		this.mstrLOID = strLOID;
		this.mstrGroupID = strGroupID;
		this.mstrLoType = strLoType;
		this.mobjNode = objNode;
	}

	/**
	 * Kѕ͕\f[^IuWFNg쐬
	 * @return Aُ
	 **/
	public boolean makeStoreData( ) {
	
		try{
			//AUPʐݖMAX擾
			makeMaxSql( );
			
			//AUPʐݖMAXvZAꎞi[
			setMaxCount( );

			//K͕\쐬
			makeReplySql( );

			//f[^݂ꍇ
			if( mstrSqlData.length != 0 && mstrSqlData != null){
				//RecordSetf[^Obj
				setStoreData( );

				//IndividualLogStoreDataɍ쐬ꗗ\ObjZbg
				setStoreDataObj(mvecStoreData);
			}
			return true;

		}catch(Exception e){
			return false;
		}
	}

	/**
	 * AUPʂ̐ݖMAXm
	 * mvecMax[UID(String)][MAX(Integer)]i[
	 * @param UID
	 **/
	private void setMaxCount(  ){

		int intRowCnt;
		int intMaxCnt;
		Integer IntegerMaxCnt;

		String strUID = new String( );
		String strOldUID = new String( );
		Vector vecMaxRow = new Vector( );
		mvecMax = new Vector( );
		
		//isj
		intRowCnt = mstrSqlMAXData.length;
		
		intMaxCnt = 0;
		for(int i=0; i<intRowCnt; i++){
			strUID = mstrSqlMAXData[ i ][ REPLY_MAX_UID ];
			if( strUID.equals( strOldUID )){
				intMaxCnt = intMaxCnt + 1;
			}else if( strOldUID.equals( "" ) ){
				intMaxCnt = intMaxCnt + 1;
				strOldUID = strUID;
			}else{
				vecMaxRow = new Vector( );
				vecMaxRow.addElement( strOldUID );
				IntegerMaxCnt = new Integer( intMaxCnt );
				vecMaxRow.addElement( IntegerMaxCnt );
				mvecMax.addElement( vecMaxRow );
				//
				intMaxCnt = 1;
				strOldUID = strUID;
			}
			if ( i == intRowCnt-1){
				vecMaxRow = new Vector( );
				vecMaxRow.addElement( strOldUID );
				IntegerMaxCnt = new Integer( intMaxCnt );
				vecMaxRow.addElement( IntegerMaxCnt );
				mvecMax.addElement( vecMaxRow );
			}
		}
	}

	/**
	 * AUIDw肵AAUPʂ̐ݖ(interaction_id)MAXԂB
	 * @param AUID
	 * @return MAX
	 **/
	public int getInteractionIDMAX(String strPrmAUID ) {
		
		String strUID = new String( );
		Vector vecMaxRow = new Vector( );
		Integer IntegerMax;

		int intMax = 0;
		
		//f[^0̏ꍇ
		if( mvecMax == null ){
			return intMax;
		}
		for(int i=0; i < mvecMax.size( ); i++ ){
			vecMaxRow = ( Vector )mvecMax.get( i );
			strUID = vecMaxRow.get( REPLY_MAX_OBJ_UID ).toString( );
			//AUID
			if( strPrmAUID.equals( strUID ) ){
				//objint
				IntegerMax = ( Integer) vecMaxRow.get( REPLY_MAX_OBJ_MAX );
				intMax = IntegerMax.intValue( );
				return intMax;
			}
		}
		return intMax;
	}

	/**
	 * UIDwł̐ݖƐ𓚂ԂB
	 * @param UID
	 * @return [ݖ][]
	 */
	public Vector getCorrectResponse( String strPrmAUID ){
	
		int intRowCnt;
		int intFlg;
		Integer IntegerMaxCnt;
		Vector vecWrkCorrctRow;
		Vector vecCorrctResponseData;

		//isj
		intRowCnt = mstrSqlMAXData.length;
		
		vecCorrctResponseData = new Vector( );
		intFlg = 0;
		for(int  i=0; i < intRowCnt; i++){
			if( strPrmAUID.equals(mstrSqlMAXData[ i ][ REPLY_MAX_UID ])){
				vecWrkCorrctRow = new Vector( );
				vecWrkCorrctRow.addElement( mstrSqlMAXData[ i ][ REPLY_MAX_INTERACTION_ID ] );
				vecWrkCorrctRow.addElement( mstrSqlMAXData[ i ][ REPLY_MAX_CORRECT_RESPONSE ] );
				vecCorrctResponseData.addElement( vecWrkCorrctRow );
				intFlg = 1;
			//mstrSqlMAXDataAUɃ\[gĂׁAԂlZbgn߁AAUς΃Zbg
			}else if ( intFlg == 1 ) {
				break;
			}
		}
		return vecCorrctResponseData;
	}

	/**
	 * AUPʐݖMAXDB擾
	 * SQL𔭍sADBf[^擾
	 **/
	private void makeMaxSql() throws Exception{
	
		Vector vecParam = new Vector();		//i[̈
		String strSQLID = new String( );			//SQLID
		
		// ALOID擾B
		String strRealLoID = new IndividualLogStoreData().getRealLoID(mstrLOID);

		//[Uw
		if( mstrGroupID.substring(0, 2).equals( USERID_TOP ) ){
			//Zbg
			vecParam.addElement(strRealLoID);
			vecParam.addElement(mstrGroupID);
			strSQLID = SQLID_REPLY_MAX_USER;
		//O[vww
		}else{
			//Zbg
			vecParam.addElement(strRealLoID);
			vecParam.addElement(mstrGroupID);
			strSQLID = SQLID_REPLY_MAX_GROUP;
		}
		mstrSqlMAXData = execute( mstrLoType, strSQLID,  vecParam);
		
	}

	/**
	 * K񓚏󋵂̈ꗗf[^DB擾
	 * SQL𔭍sADBf[^擾
	 **/
	private void makeReplySql() throws Exception{
	
		Vector vecParam = new Vector();		//i[̈
		String strSQLID = new String( );			//SQLID
		
		// ALOID擾B
		String strRealLoID = new IndividualLogStoreData().getRealLoID(mstrLOID);

		//[Uw
		if( mstrGroupID.substring(0, 2).equals( USERID_TOP ) ){
			//Zbg
			vecParam.addElement(mstrGroupID);
			vecParam.addElement(strRealLoID);
			strSQLID = SQLID_REPLY_USER;

		//O[vww
		}else{
			//Zbg
			vecParam.addElement(strRealLoID);
			vecParam.addElement(mstrGroupID);
			strSQLID = SQLID_REPLY_GROUP;
		}
		mstrSqlData = execute( mstrLoType, strSQLID,  vecParam);
		
	}

	/**
	 * DB擾f[^ҏWA\AUɃf[^ObjɊi[
	 * ݒF	UID
	 *			ڎꗗf[^YRowԍ
	 *			UserID
	 *			[U
	 *			SessionID
	 *			ZbVt
	 *			񓚉
	 *			_
	 *			
	 *			ݖiInteractionIDj
	 *			l
	 *			딻
	 * ݒ菇Fڎf[^AUAUserIDAZbVti~jA񓚉񐔂̏Ɋi[
	 */
	private void setStoreData( ){
		int intRowCnt;								//isj
		int intColCnt;									//ij
		
		String strNodeUID = new String( );			//ڎ擾UID
		String strUID = new String( );				//DB擾UID
		String strSessionID = new String( );			//DB擾ZbVID
		String strUserID = new String( );			//DB擾[UID
		String strOldSessionID = new String( );		//ҏẄꎞi[ZbVID
		String strOldUserID = new String( );			//ҏẄꎞi[[UID
		String strInteraction_id = new String( );		//DB擾Interaction_id
		String strOldInteraction_id = new String( );	//ҏẄꎞi[Interaction_id
		int intAnsCnt;									//񓚉

		//isj
		intRowCnt = mstrSqlData.length;
		
		//ij
		intColCnt = mstrSqlData[0].length;
		
		//ڎ
		createRDFStoreData(mobjNode);
		
		int intStartRow=0;						//UserIDASessionIDRowStart
		int intEndRow=-1;						//UserIDASessionIDRowEnd
		
		int intRow = 0;							//mvecStoreDataɃZbgRowʒu
		
		mvecStoreData = new Vector( );		//f[^Obj

		//ڎɃ[v
		for(int i=0; i < getRDFMaxRow( ); i++){
			
			//ZNV
			if(getRDFType(i).equals(IndividualLogRDFNode.STATUS_S)){
				continue;
			}

			strNodeUID = getRDFAUID( i );					//ڎUID

			strOldUserID = "";
			strOldSessionID = "";
			strOldInteraction_id = "";
				
			//ڎAUŁASQL擾f[^
			for(int j=0; j<intRowCnt; j++){							//ڎRowLoop
				strUID = mstrSqlData[ j ][ REPLY_REC_UID ];

				//AUIDv
				if(strNodeUID.equals(strUID)){
					strUserID = mstrSqlData[ j ][ REPLY_REC_USER_ID ];				//UserID
					strSessionID = mstrSqlData[ j ][ REPLY_REC_SESSIONID ];			//SessionID
					strInteraction_id = mstrSqlData[ j ][ REPLY_REC_INTERACTION_ID ];		//Interaction_id

					//UserIDAZbVtPʁiUserIDASessionIDςj
					if( strUserID.equals(strOldUserID) && strSessionID.equals(strOldSessionID) ){
					
						//interaction_idJԂĂ
						if(strInteraction_id.equals( strOldInteraction_id )){
							intRow = intRow + 1;						//interaction_idς΁AJno^s
							if( intRow >intEndRow ){
								intEndRow = intRow;				//o^sŏIo^s傫΁AŏIo^s𒲐
							}
							//񓚉񐔂̌vZ
							intAnsCnt = intEndRow - intStartRow ;

							setmvecStoreData( i, j, intRow, intAnsCnt);

						//interaction_idς
						}else{
							intRow = intStartRow;					//interaction_idς΁AJno^s
							
							setmvecStoreData( i, j, intRow, 1);

						}

					//UserIDAZbVtPʁiUserIDASessionIDςjBreak	
					}else{
						intRow = intEndRow + 1;						//mvecStoreDataɓo^ĂŏIs܂ňړ
						intStartRow = intEndRow + 1;				//BrakeׁAJnʒu炷
						intEndRow = intEndRow+1;					//BrakeׁAIʒu炷

						setmvecStoreData( i, j, intRow ,1);

						strOldUserID = strUserID;
						strOldSessionID = strSessionID;
						
					}
					strOldInteraction_id = strInteraction_id;

				}
			}
		}
	}


	/**
	 * mvecStoreDataɗvfԍ݂ΕύXAȂΒǉ
	 * @param ڎf[^Row
	 * @param mstrSqlData̔zRow
	 * @param mvecStoreDatavfԍ
	 * @param 񓚉
	 */
	private void setmvecStoreData(int intNodeRow, int intSqlRow, int intParamRow, int intAnsCnt ){
		
		Vector vecWrkRow;								//AUIPʂ̃f[^
		String strCorrectCnt = new String( );			//
		String strAnsJudge = new String( );				//딻
		String strAnsCnt = new String( );				//񓚉
		int intMaxCnt;
		int intSetNo;
		
		String strDecimalFlg = "0";					// l𓚃tO

		//딻
		strAnsJudge = judgeResult(mstrSqlData[ intSqlRow ][ REPLY_REC_RESULT ]);

		// resultDecimall܂ޏꍇAl𓚃tO𗧂Ă܂B
		if(!strAnsJudge.equals(DEF_CORREC) && !strAnsJudge.equals(DEF_NO_ANSWER) && !strAnsJudge.equals(DEF_WRONG)){
			strDecimalFlg = "1";
		}

		//񓚉񐔕ύX 
		//intString
		Integer IntegerAnsCnt = new Integer(intAnsCnt);
		strAnsCnt = IntegerAnsCnt.toString( );
		
		//o^vfԍintEndCntmvecStoreData傫
		if( mvecStoreData.size( ) == 0 || mvecStoreData.size( ) <= intParamRow ){
			vecWrkRow = new Vector( );	
			//UIDڎRow intString
			Integer IntegerNodeRow = new Integer(intNodeRow);
			String strNodeRow =  new String( );
			strNodeRow = IntegerNodeRow.toString( );
			//o^
			vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_UID ] );					//UID
			vecWrkRow.addElement( strNodeRow );														//UIDڎRow
			vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_USER_ID ] );			//UserID
			vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_USER_NAME ] );		//wKҖ
			vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_SESSIONID ] );			//SessionID
			vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_STUDY_START_DATE ] );			//ZbVt
			vecWrkRow.addElement( strAnsCnt );														//񓚉
			vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_RAWSCORE ] );		//_
			if( strAnsJudge.equals(DEF_CORREC)){
				vecWrkRow.addElement( new String("1") );												//
			}else{
				vecWrkRow.addElement( new String("0") );												//
			}
			
			//JԂiinteraction_idAl𓚁A딻jǉ
			intMaxCnt = getInteractionIDMAX( mstrSqlData[ intSqlRow ][ REPLY_REC_UID ] );				//ݖMAX擾
			//̃f[^interaction_idAl𓚁A딻Zbgʒu
			intSetNo = getInteractionIdCol( mstrSqlData[ intSqlRow ][ REPLY_REC_UID ] ,mstrSqlData[ intSqlRow ][ REPLY_REC_INTERACTION_ID ] );
			//MAXhhZbg
			for( int i = 0; i<intMaxCnt; i++ ){
				if( i == intSetNo){
					vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_INTERACTION_ID ] );		//ݖ
					vecWrkRow.addElement( mstrSqlData[ intSqlRow ][ REPLY_REC_STUDENT_RESPONSE ] );		//l
					vecWrkRow.addElement( strAnsJudge );												//딻
				}else{
					vecWrkRow.addElement( new String(" ") );											//ݖ
					vecWrkRow.addElement( new String(" ") );											//l
					vecWrkRow.addElement( new String(" ") );											//딻
				}
			}
			// l𓚃tOVector̍ŌɊi[܂B
			vecWrkRow.addElement( new String(strDecimalFlg) );											// l𓚃tO
			mvecStoreData.addElement( vecWrkRow );
		}else{
			vecWrkRow = new Vector( );	

			//mvecStoreDataintEndCntvfڂύX
			vecWrkRow = (Vector)mvecStoreData.get(intParamRow);
			
			//𐔕ύX
			if( strAnsJudge.equals(DEF_CORREC)){
				Integer IntegerCorrectCnt = new Integer( vecWrkRow.get(REPLY_OBJ_CORRECT_COL).toString( ) );
				//StringintāAP
				int intCorrectCnt =  IntegerCorrectCnt.intValue( ) ;
				intCorrectCnt = intCorrectCnt + 1;
				IntegerCorrectCnt = null;
				//intString
				IntegerCorrectCnt = new Integer(intCorrectCnt);
				strCorrectCnt = IntegerCorrectCnt.toString( );
				vecWrkRow.set( REPLY_OBJ_CORRECT_COL, strCorrectCnt );
			}			
			//JԂiinteraction_idAl𓚁A딻ĵݒǉ
			//̃f[^interaction_idAl𓚁A딻Zbgʒu
			intSetNo = getInteractionIdCol( mstrSqlData[ intSqlRow ][ REPLY_REC_UID ] ,mstrSqlData[ intSqlRow ][ REPLY_REC_INTERACTION_ID ] );
			//JԂf[^PڃZbgʒu{intSetNoiݖPʂ̃\[g̔ԍj*JԂf[^COL
			vecWrkRow.set( REPLY_OBJ_INTERACTION_ID_COL + ( intSetNo * REPLY_OBJ_REPEAT_COL) ,
							 mstrSqlData[ intSqlRow ][ REPLY_REC_INTERACTION_ID ]);						//ݖ
			vecWrkRow.set( REPLY_OBJ_RESPONSE_COL + ( intSetNo * REPLY_OBJ_REPEAT_COL) ,
							 mstrSqlData[ intSqlRow ][ REPLY_REC_STUDENT_RESPONSE ]);					//l
			vecWrkRow.set( REPLY_OBJ_RESULT_COL + ( intSetNo * REPLY_OBJ_REPEAT_COL) ,
							 strAnsJudge);																//딻
			// l𓚃tOVector̍ŌɊi[܂B
			vecWrkRow.addElement( new String(strDecimalFlg) );											// l𓚃tO
			mvecStoreData.set( intParamRow ,vecWrkRow );
		}

	}

	/**
	 * AUiUIDjAݖiInteraction_idjw
	 * AUPʂł̃AXL[̐ݖ̏ԂԂ
	 * @param AUID
	 * @param Interaction_id
	 * @return w肵ݖ̏ԍ
	 */
	public int getInteractionIdCol(String strPrmAUID ,String strParamInteraction_id ) {
		
		int intRowCnt;
		int intCnt;
		
		//isj
		intRowCnt = mstrSqlMAXData.length;
		
		intCnt = -1;
		for( int i=0; i < intRowCnt; i++){
			if( strPrmAUID.equals(mstrSqlMAXData[ i ][ REPLY_MAX_UID ])){
				intCnt = intCnt +1;
				if( strParamInteraction_id.equals(mstrSqlMAXData[ i ][ REPLY_MAX_INTERACTION_ID ] ) ){
					break;
				}
			}
		}
		return intCnt;
	}

}