Log.java 12.1 KB
package th.co.ais.ssbsrfc.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import th.co.ais.ssbsrfc.config.Constant;
import th.co.ais.ssbsrfc.config.Invoke;
import th.co.ais.ssbsrfc.config.State;
import th.co.ais.ssbsrfc.config.StateConfig;
import th.co.ais.ssbsrfc.instance.EC02Instance;
import th.co.ais.ssbsrfc.instance.Stat;
import th.co.ais.ssbsrfc.interfaces.EQXMsg;
import th.co.ais.ssbsrfc.message.MessageParser;
import ais.mmt.sand.comlog.DetailsLogPrototype;
import ais.mmt.sand.comlog.SummaryLogPrototype;
import ais.mmt.sand.comlog.exception.CommonLogException;
import ec02.af.abstracts.AbstractAF;
import ec02.af.data.EquinoxRawData;
import ec02.utils.AppLog;

public class Log {
	
	public static boolean debug = false;
	
	public static void results(EC02Instance ec02Instance, String currentSubState, List<EquinoxRawData> equinoxRawDataList, int type, boolean isStat) {
		int idx = StateConfig.getSubStateIndex(currentSubState);
		State state = StateConfig.subStateList[idx];
		
		int msgType = state.msgType;
		results(ec02Instance, currentSubState, equinoxRawDataList, type, Constant.TYPE_RECEIVE, msgType, null, isStat);
	}
	
	public static void results(EC02Instance ec02Instance, String currentSubState, List<EquinoxRawData> equinoxRawDataList, int type, String cmd, boolean isStat) {
		int idx = StateConfig.getSubStateIndex(currentSubState);
		State state = StateConfig.subStateList[idx];
		
		int msgType = state.msgType;
		results(ec02Instance, currentSubState, equinoxRawDataList, type, Constant.TYPE_RECEIVE, msgType, cmd, isStat);
	}
	
	public static void results(EC02Instance ec02Instance, String currentSubState, List<EquinoxRawData> equinoxRawDataList, int type, int direction, int msgType, String _cmd, boolean isStat) 
	{
		String errMsg = "";
		if (Global.isStat) 
		{
			int flow =  ec02Instance.getAFInstance().getFlow();
			switch (flow) {
				default:
					StateConfig.PROJECT_STAT_NAME = StateConfig.PROJECT_NAME;
					break;
			}
			
			AbstractAF abstractAF = ec02Instance.getAbstractAF();
			String name = EQXMsg.EMPTY;
			if (Log.debug) {
				AppLog.d("## RECEIVE WRITE STAT FOR SUBSTATE");
			}
			if (isStat) {
				// RECEIVE
				name = StateConfig.getLASEName(currentSubState, direction, msgType, type, errMsg, _cmd);
				abstractAF.getUtils().incrementStats(name);
				if (Log.debug) {
					AppLog.d("## CST_(" + flow + ":" + currentSubState + "): " + name);
				}
			}
			if (Log.debug) {
				AppLog.d("## RECEIVE WRITE STAT FOR SUBSTATE END");
			}
			
			// SEND
			if (Log.debug) {
				AppLog.d("## SEND WRITE STAT FOR EQUINOXRAWDATA");
			}
			for(EquinoxRawData eqx : equinoxRawDataList) {
				if (isStat) {
					String _type = eqx.getType();
					if (_type.equals("request")) {
						String invoke = eqx.getInvoke();
						currentSubState = ec02Instance.getAFInstance().getCurrentState();
						if (invoke != null && invoke.contains(".")) {
							String cmd = new Invoke(invoke).cmd;
							name = StateConfig.getLASEName(currentSubState, Constant.TYPE_SEND, Constant.TYPE_REQUEST, Constant.TYPE_SUCCESS, "", cmd);
							abstractAF.getUtils().incrementStats(name);
							if (Log.debug) {
								AppLog.d("## CST_(" + flow + ":" + currentSubState + "): " + name);
							}
						} else {
							name = StateConfig.getLASEName(currentSubState, Constant.TYPE_SEND, Constant.TYPE_REQUEST, Constant.TYPE_SUCCESS, "");
							abstractAF.getUtils().incrementStats(name);
							if (Log.debug) {
								AppLog.d("## CST_(" + flow + ":" + currentSubState + "): " + name);
							}
						}
					} else if (_type.equals("response")) {
						int x = type;
						try {
							String subState = eqx.getRawDataAttributes().get("subState");
//							AppLog.d("subState :" + subState);
							String[] arr = subState.split(",");
							currentSubState = arr[0];
							x = Integer.parseInt(arr[1]);
						} 
						catch (Exception e) {
							if (Log.debug) {
								AppLog.d("## ERROR STAT:" + e.getMessage());
							}
						}
						
//						AppLog.d("## currentSubState : " + currentSubState);
						eqx.getRawDataAttributes().remove("subState");
						name = StateConfig.getLASEName(currentSubState, Constant.TYPE_SEND, Constant.TYPE_RESPONSE, x, "");
						abstractAF.getUtils().incrementStats(name);
						if (Log.debug) {
							AppLog.d("## CST_(" + flow + ":" + currentSubState + "): " + name);
						}
					}
				}
			}
			if (Log.debug) {
				AppLog.d("## SEND WRITE STAT FOR EQUINOXRAWDATA END");
			}
			
			// etc
			if (Log.debug) {
				AppLog.d("## STAT WRITE STAT FOR LISTSTAT");
			}
			ArrayList<Stat> listStat = ec02Instance.getAFInstance().getListStat();
			if (listStat.size() > 0) {
				for(Stat stat : listStat) {
					String nameLog = stat.getName();
					if (nameLog == null) {
						name = StateConfig.getLASEName(stat.getCurrentSubState(), stat.getSendOrReceive(), stat.getResponseOrRequest(), stat.getType(), stat.getMessage(), stat.getCommand());
						abstractAF.getUtils().incrementStats(name);
						if (Log.debug) {
							AppLog.d("## CST_(" + flow + ":LISTSTAT): " + name);
						}
					} else {
						name = nameLog;
						abstractAF.getUtils().incrementStats(name);
						if (Log.debug) {
							AppLog.d("## CST_(" + flow + ":LISTSTAT): " + name);
						}
					}
				}
			}
			ec02Instance.getAFInstance().setListStat(null);
			// etc
			if (Log.debug) {
				AppLog.d("## STAT WRITE STAT FOR LISTSTAT END");
			}
		}
	}
	
	public static void summaryLog(EC02Instance ec02Instance, AbstractAF abstractAF)
	{
		Log.dev("" + Global.isWriteSummary);
		if (Global.isSummary && (ec02Instance.getAFInstance().getCurrentState().equals(StateConfig.SSTATE_END) || Global.isWriteSummary)) 
		{
			int flow =  ec02Instance.getAFInstance().getFlow();
			long requestTimestamp = Global.currentInputDateTime;
			String session = abstractAF.getEquinoxProperties().getSession();
			String commandName = EQXMsg.EMPTY;
			String initialInvoke = ec02Instance.getAFInstance().getInitialInvoke();
			String identity = Log.getIdentity(ec02Instance, abstractAF);
			String resultCode = ec02Instance.getAFInstance().getResultCode();
			String resultDescription = ec02Instance.getAFInstance().getResultDescription();
			long responseTimestamp = System.currentTimeMillis();
			try {
				String subState = StateConfig.arrFirstStateFlow[flow-1];
				commandName = StateConfig.getCommandFromState(subState);
			} catch (Exception e) { }
			
			SummaryLogPrototype sumLog = ec02Instance.getAFInstance().getSumlog();
			sumLog.setSession(session);
			sumLog.setIdentity(identity);
			sumLog.setInitInvoke(initialInvoke);
			sumLog.setCmdName(commandName);
			sumLog.setResultCode(resultCode);
			sumLog.setResultDesc(resultDescription);
			sumLog.setReqTimeStamp(requestTimestamp);
			sumLog.setRespTimeStamp(responseTimestamp);
			String message = "";
			try {
				message = sumLog.print();
			} catch (CommonLogException e) {
				AppLog.e("## COMMONLOG EXCEPTION:" + e);
				AppLog.e("## COMMONLOG EXCEPTION:" + e.getStackTrace());
			}
			abstractAF.getUtils().writeLog(Global.summaryNameLog, message);
			if (Log.debug) {
				AppLog.d("## SUMMARY :" + message);
			}
			
			if (Global.isWriteSummary) {
				Global.isWriteSummary = false;
				ec02Instance.getAFInstance().getBackupSummary().restore(ec02Instance);
			}
		}
	}
	
	private static String getIdentity(EC02Instance ec02Instance, AbstractAF abstractAF)
	{
		String identity = "";
		int flow =  ec02Instance.getAFInstance().getFlow();
		switch (flow) {
			case Constant.FLOW_TEST_A:
				break;
		}
		 
		return identity;
	}
	
//	public static void errorLog(EC02Instance ec02Instance, AbstractAF abstractAF) 
//	{
//		AppLog.d("## isErrorLog : " + Global.isErrorLog);
//		if(Global.isErrorLog) {
//			AppLog.d("## WRITE ERROR LOG");
//			
//			DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss.S");
//			Calendar cal = Calendar.getInstance();
//			String timestamp = dateFormat.format(cal.getTime());
//			String logLevel = "ERROR";
//			String identity = Log.getIdentity(ec02Instance, abstractAF);
//			String rawInstance = Global.encodeInstance(ec02Instance.getAFInstance());
//			String errorCode = ec02Instance.getAFInstance().getResultCode();
//			String errorDescription = ec02Instance.getAFInstance().getResultDescription();
//			
//			String message = timestamp + "|" + logLevel + "|" + identity + "|" + rawInstance  + "|" + errorCode + "|" + errorDescription;
//			abstractAF.getUtils().writeLog(Global.errorNameLog, message);
//			if (Log.debug) {
//				AppLog.d("## ERROR LOG :" + message);
//			}
//		}
//	}
	
	public static void detailLog(EC02Instance ec02Instance, AbstractAF abstractAF,String currentState, String nextState, List<EquinoxRawData> eqxRawDataListInput, List<EquinoxRawData> eqxRawDataListOutput)
	{
		if (Global.isDetail) 
		{
			String session = abstractAF.getEquinoxProperties().getSession();
			String initialInvoke = ec02Instance.getAFInstance().getInitialInvoke();
			String scenario = "Unexpected Event Handling";
//			String msisdn = ec02Instance.getAFInstance().getMsisdn();
			String strInputTimeStamp = Global.inputTimeStamp;
			java.sql.Date dateInput = DateUtil.stringToDate(strInputTimeStamp, "yyyyMMdd HH:mm:ss.S");
			Calendar c = Calendar.getInstance();
			c.setTime(dateInput);
			long lInputTimeStamp = c.getTimeInMillis();
			long lOutputTimeStamp = System.currentTimeMillis();
		    String outputTimeStamp = Global.getTimeStampLog(lOutputTimeStamp);
//		    Global.setOutputTimeStamp(outputTimeStamp);
		    if (!Global.isRejectByApplication) {
		    	ec02Instance.getAFInstance().setOutgoingStateTimeStamp(outputTimeStamp);
		    } else {
		    	Global.setRejectByApplication(false);
		    }
//			long totalProcessingTime = 0;
//			try {
//				totalProcessingTime = Global.diffTimeStampLog(inputTimeStamp, outputTimeStamp);
//			} catch (Exception e) {
//				totalProcessingTime = -1;
//			}
			
			for(EquinoxRawData eqx : eqxRawDataListOutput) {
				String type = eqx.getType();
				String invoke = eqx.getInvoke();
				if (type.equals("request") && (invoke.length() > 0 || invoke != null)) {
//					ec02Instance.getAFInstance().putTimeRequest(invoke, outputTimeStamp);
				}
			}
			
			int flow =  ec02Instance.getAFInstance().getFlow();
			scenario = Global.getFlowName(flow);
			
			ArrayList<Object> arrInput = ec02Instance.getAFInstance().getInput();
			ArrayList<Object> arrOutput = ec02Instance.getAFInstance().getOutput();
			
			if (arrInput == null || (arrInput != null && arrInput.size() == 0)) {
				arrInput = null;
			}
			if (arrOutput == null || (arrOutput != null && arrOutput.size() == 0)) {
				arrOutput = null;
				outputTimeStamp = null;
			} else {
				Log.dev(" arrOutput count:" + arrOutput.size());
			}
			
			String identity = session;
			switch (flow) {
				case Constant.FLOW_TEST_A:
					identity = "MIS";
					if(session.matches("66\\d{9}")) {
						identity = session;
					}
					break;
				default:
					identity = session;
					break;
			}
			
			String message = "";
			DetailsLogPrototype detLog = ec02Instance.getAFInstance().getDetaillog();
			try {
				detLog.setSession(session);
				detLog.setIdentity(identity);
				detLog.setInputTimestamp(lInputTimeStamp);
				detLog.addScenario(scenario, initialInvoke, currentState + "." + ec02Instance.getAFInstance().getPreviousState(), nextState + "." + ec02Instance.getAFInstance().getCurrentState());
				detLog.setOutputTimestamp(lOutputTimeStamp);

				detLog.setEnableData(Global.isDetailData);
				detLog.setEnableRawData(Global.isDetailRawData);
				detLog.setEnableScenario(Global.isDetailScenario);
				detLog.setEnableCurrentState(Global.isDetailCurrentState);
				detLog.setEnableNextState(Global.isDetailNextState);
				
//				AppLog.d("## PARAMETERS : " + MessageParser.toJson(detLog));
				message = detLog.print();
			} catch (CommonLogException e) {
				if (Log.debug) {
					AppLog.d("## DETAIL LOG:" + e.getMessage());
				}
			}
			
			ec02Instance.getAFInstance().setDetaillog(null);
			abstractAF.getUtils().writeLog(Global.detailNameLog, message);
			if (Log.debug) {
				message = "## DETAIL LOG:" + message;
				AppLog.d(message);
			}
			ec02Instance.getAFInstance().setInput(null);
			ec02Instance.getAFInstance().setOutput(null);
		}
	}
	
	public static void dev(String message) {
		if (debug) {
			AppLog.d("#dev# :" + message);
		}
	}
}