/**
 * 
 */
package com.cim.idm.wmsextend.generic.errorHandler;

import com.cim.idm.wmsextend.generic.GenericServiceProxy;
import com.cim.idm.wmsextend.generic.object.ErrorDef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.MessageFormat;

/**
 * @author sjlee
 *
 */
public class CustomException extends Exception 
{
	
	/**
	 * @uml.property  name="errorDef"
	 * @uml.associationEnd  
	 */
	public ErrorDef  errorDef;
	
	private static Log 	log = LogFactory.getLog(CustomException.class);

	/**
	 * 
	 */
	public CustomException() {
		// TODO Auto-generated constructor stub
	}

	public CustomException(String errorCode, Object... args)
	{
		
		if(log.isInfoEnabled()){
			log.error("errorCode = " + errorCode);
		}
		
		ErrorDef tempErrorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
		if ( tempErrorDef == null )
		{
			tempErrorDef = GenericServiceProxy.getErrorDefMap().getErrorDef("UndefinedCode");
		}
		errorDef = new ErrorDef();
		
		errorDef.setErrorCode(tempErrorDef.getErrorCode());

		String korTempMsg = (tempErrorDef.getKor_errorMessage() != null) ? tempErrorDef.getKor_errorMessage() : "Default Korean Message";
		String engTempMsg = (tempErrorDef.getEng_errorMessage() != null) ? tempErrorDef.getEng_errorMessage() : "Default English Message";
		String chaTempMsg = (tempErrorDef.getCha_errorMessage() != null) ? tempErrorDef.getCha_errorMessage() : "Default Chinese Message";
		String locTempMsg = (tempErrorDef.getLoc_errorMessage() != null) ? tempErrorDef.getLoc_errorMessage() : "Default Localized Message";


		errorDef.setKor_errorMessage(
				MessageFormat.format(korTempMsg, args));
		errorDef.setEng_errorMessage(
				MessageFormat.format(engTempMsg, args));
		errorDef.setCha_errorMessage(
				MessageFormat.format(chaTempMsg, args));
		errorDef.setLoc_errorMessage(
				MessageFormat.format(locTempMsg, args));
		
		log.error("errorDescription = " + errorDef.getEng_errorMessage());

		// Temporary Update Use Flag
//		try {
//			// 2018.03.13 parkkw - Update UseTimekey once a day
//			String lastUseTimeKey = "";
//			String newTimekey = TimeUtils.getCurrentEventTimeKey();
//			String sqlString = "SELECT errorCode, kor_errorMsg, eng_errorMsg, cha_errorMsg, loc_errorMsg, useTimekey FROM BSERRORDEFINITION WHERE errorCode = :ERRORCODE ";
//			List<Map<String, Object>> resultList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlString, errorCode);
//			
//			if (resultList != null && resultList.size() > 0) {
//				ListOrderedMap orderMap = (ListOrderedMap)resultList.get(0);
//				if (orderMap.get("useTimekey") != null) {
//					lastUseTimeKey = orderMap.get("useTimekey").toString();
//				}
//			}
//			
//			if(StringUtils.isEmpty(lastUseTimeKey) || StringUtils.equals(StringUtils.substring(lastUseTimeKey, 0,8), StringUtils.substring(newTimekey, 0,8)) == false ){
//				String tempSql = "UPDATE BSERRORDEFINITION SET USEFLAG = :FLAG, USETIMEKEY = :TIMEKEY WHERE ERRORCODE = :ERRORCODE ";
//				Map<String, String> tempMap = new HashMap<String, String>();
//				tempMap.put("FLAG", GenericServiceProxy.getConstantMap().FLAG_Y);
//				tempMap.put("TIMEKEY", TimeStampUtils.getLocalCurrentEventTimeKey());
//				tempMap.put("ERRORCODE", errorCode);
//				IDMFrameServiceProxy.getSqlTemplate().update(tempSql, tempMap);
//			}
//		} catch (Exception te) {
//			log.info("UPDATE BSERRORDEFINITION SET USEFLAG = 'Y' WHERE ERRORCODE = " + errorCode);
//		}
		// Temporary Update Use Flag
	}
	/**
	 * @param errorCode
	 */
	public CustomException(String errorCode) {
		
		if(log.isInfoEnabled()){
			log.info("errorCode = " + errorCode);
		}
		
		ErrorDef tempErrorDef = GenericServiceProxy.getErrorDefMap().getErrorDef(errorCode);
		
		if ( tempErrorDef == null )
		{
			tempErrorDef = GenericServiceProxy.getErrorDefMap().getErrorDef("UndefinedCode");
		}
		errorDef = new ErrorDef();
		
		errorDef.setErrorCode(tempErrorDef.getErrorCode());
		
		String korTempMsg = tempErrorDef.getKor_errorMessage();
		String engTempMsg = tempErrorDef.getEng_errorMessage();
		String chaTempMsg = tempErrorDef.getCha_errorMessage();
		String locTempMsg = tempErrorDef.getLoc_errorMessage();
		
		errorDef.setKor_errorMessage(korTempMsg);
		errorDef.setEng_errorMessage(engTempMsg);
		errorDef.setCha_errorMessage(chaTempMsg);
		errorDef.setLoc_errorMessage(locTempMsg);
		//super(message);
		// TODO Auto-generated constructor stub
		
		// Temporary Update Use Flag
//		try {
//			// 2018.03.13 parkkw - Update UseTimekey once a day
//			String lastUseTimeKey = "";
//			String newTimekey = TimeUtils.getCurrentEventTimeKey();
//			String sqlString = "SELECT errorCode, kor_errorMsg, eng_errorMsg, cha_errorMsg, loc_errorMsg, useTimekey FROM BSERRORDEFINITION WHERE errorCode = :ERRORCODE ";
//			List<Map<String, Object>> resultList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlString, errorCode);
//			
//			if (resultList != null && resultList.size() > 0) {
//				ListOrderedMap orderMap = (ListOrderedMap)resultList.get(0);
//				if (orderMap.get("useTimekey") != null) {
//					lastUseTimeKey = orderMap.get("useTimekey").toString();
//				}
//			}
//			
//			if(StringUtils.isEmpty(lastUseTimeKey) || StringUtils.equals(StringUtils.substring(lastUseTimeKey, 0,8), StringUtils.substring(newTimekey, 0,8)) == false ){
//				String tempSql = "UPDATE BSERRORDEFINITION SET USEFLAG = :FLAG, USETIMEKEY = :TIMEKEY WHERE ERRORCODE = :ERRORCODE ";
//				Map<String, String> tempMap = new HashMap<String, String>();
//				tempMap.put("FLAG", GenericServiceProxy.getConstantMap().FLAG_Y);
//				tempMap.put("TIMEKEY", TimeStampUtils.getLocalCurrentEventTimeKey());
//				tempMap.put("ERRORCODE", errorCode);
//				IDMFrameServiceProxy.getSqlTemplate().update(tempSql, tempMap);	
//			}
//		} catch (Exception te) {
//			log.info("UPDATE BSERRORDEFINITION SET USEFLAG = 'Y' WHERE ERRORCODE = " + errorCode);
//		}
		// Temporary Update Use Flag
	}

	/**
	 * @param cause
	 */
	public CustomException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param message
	 * @param cause
	 */
	public CustomException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

}
