SubWaitChargeRequest.java 13.5 KB
package th.co.ais.ssbsrfc.substate;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Random;

import ec02.af.abstracts.AbstractAF;
import ec02.af.data.EquinoxRawData;
import ec02.utils.AppLog;
import th.co.ais.ssbsrfc.config.Constant;
import th.co.ais.ssbsrfc.config.StateConfig;
import th.co.ais.ssbsrfc.instance.AccessSecurity;
import th.co.ais.ssbsrfc.instance.AdjustmentInfo;
import th.co.ais.ssbsrfc.instance.AdjustmentObj;
import th.co.ais.ssbsrfc.instance.AdjustmentRequest;
import th.co.ais.ssbsrfc.instance.AdjustmentRequestIns;
import th.co.ais.ssbsrfc.instance.ChargeResponseIns;
import th.co.ais.ssbsrfc.instance.CheckRequest;
import th.co.ais.ssbsrfc.instance.EC02Instance;
import th.co.ais.ssbsrfc.instance.EQXRawInstance;
import th.co.ais.ssbsrfc.instance.OwnershipInfo;
import th.co.ais.ssbsrfc.instance.RequestHeader;
import th.co.ais.ssbsrfc.instance.Stat;
import th.co.ais.ssbsrfc.instance.SubAccessCode;
import th.co.ais.ssbsrfc.utils.EqxStringUtils;
import th.co.ais.ssbsrfc.utils.Global;

public class SubWaitChargeRequest extends WaitSubState {

	AdjustmentRequestIns adjustmentRequest = new AdjustmentRequestIns();
	HashMap<String, String> map = new HashMap<String, String>();
	private ChargeResponseIns chargeResponseIns = new ChargeResponseIns();

	public void configSubstate(EC02Instance ec02Instance, EquinoxRawData equinoxRawData) {
		this._state = StateConfig.SSTATE_W_CHARGEREQUEST;
		this._eventType = StateConfig.Incoming_ChargeRequest_Request;
		this._stateType = Constant.SUBSTATE_TYPE_W_REQUEST;
		if (equinoxRawData != null) {
			ec02Instance.getAFInstance().setOrig(equinoxRawData.getOrig());
			ec02Instance.getAFInstance().setInvoke(equinoxRawData.getInvoke());
			ec02Instance.getAFInstance().setInitialInvoke(equinoxRawData.getInvoke());
		}
		ec02Instance.getAFInstance().setFlow(Constant.FLOW_CHARGE_REQUEST);
	}

	public Object getMessage(EC02Instance ec02Instance, EquinoxRawData equinoxRawData) {
		String value = EqxStringUtils.getMessage(equinoxRawData, ec02Instance);
		AppLog.d("## PARAMETERS : " + value);
		String page = equinoxRawData.getRawDataAttribute("val");
		CheckRequest checkRequest = new CheckRequest();
		checkRequest.setDn(page);
		String[] dnList = checkRequest.getDn().split("&", -1);
		page = dnList[0].substring("ssid=".length());
		int i = 0;
		while (i < dnList.length) {

			map.put(dnList[i].substring(0, dnList[i].indexOf("=")), dnList[i].substring(dnList[i].indexOf("=") + 1));
			i++;
		}

		DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Calendar cal = Calendar.getInstance();
		Random random = new Random();
		String rand = "000";
		int x = random.nextInt(1000);
		rand += x;
		rand = rand.substring(rand.length() - 3);
		ec02Instance.getAFInstance().setRand(rand);
		String ChargeId = dateFormat.format(cal.getTime()) + rand;
		ec02Instance.getAFInstance().setChargeId(ChargeId);
		ec02Instance.getAFInstance().setDn(map);
		this.type = Constant.TYPE_SUCCESS;

		AppLog.d("########Random : " + rand);
		AppLog.d("########MessageSeq : " + ChargeId);

		RequestHeader requestHeader = new RequestHeader();

		requestHeader.setVersion(
				ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("AdjustmentRequestVersion").get(0));
		AppLog.d("AdjustmentRequestVersion :" + Global.getEC02WarmConfig("AdjustmentRequestVersion"));
		requestHeader.setBusinessCode(
				ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("BusinessCode_Deduct").get(0));
		AppLog.d("BusinessCode_Deduct :" + Global.getEC02WarmConfig("BusinessCode_Deduct"));
		requestHeader.setMessageSeq(ChargeId);
		OwnershipInfo ownershipInfo = new OwnershipInfo();
		if (null == ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("BEID")) {
			ownershipInfo.setBEID("101");
			AppLog.d("BEID :" + Global.getEC02WarmConfig("BEID"));
		} else {
			ownershipInfo.setBEID(ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("BEID").get(0));
			AppLog.d("BEID :" + Global.getEC02WarmConfig("BEID"));
		}
		ownershipInfo.setBRID(ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("BRID").get(0));
		AppLog.d("BRID :" + Global.getEC02WarmConfig("BRID"));
		requestHeader.setOwnershipInfo(ownershipInfo);
		AccessSecurity accessSecurity = new AccessSecurity();
		accessSecurity.setLoginSystemCode(
				ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("LoginSystemCode").get(0));
		AppLog.d("LoginSystemCode :" + Global.getEC02WarmConfig("LoginSystemCode"));
		accessSecurity.setPassword(ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("Password").get(0));
		AppLog.d("Password :" + Global.getEC02WarmConfig("Password"));
		requestHeader.setAccessSecurity(accessSecurity);
		if (null == ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("AccessMode")) {
			requestHeader.setAccessMode("3");
			AppLog.d("AccessMode :" + Global.getEC02WarmConfig("AccessMode"));
		} else {
			requestHeader
					.setAccessMode(ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("AccessMode").get(0));
			AppLog.d("AccessMode :" + Global.getEC02WarmConfig("AccessMode"));
		}

		AdjustmentRequest adjustmentrequest = new AdjustmentRequest();
		adjustmentrequest.setAdjustmentSerialNo(map.get("ssid"));
		AdjustmentObj adjustmentObj = new AdjustmentObj();
		SubAccessCode subAccessCode = new SubAccessCode();
		subAccessCode.setPrimaryIdentity(map.get("ms"));
		adjustmentObj.setSubAccessCode(subAccessCode);
		adjustmentrequest.setAdjustmentObj(adjustmentObj);
		adjustmentrequest.setOpType(ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("OpType").get(0));
		AppLog.d("OpType :" + Global.getEC02WarmConfig("OpType"));
		AdjustmentInfo adjustmentInfo = new AdjustmentInfo();
		adjustmentInfo
				.setBalanceType(ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("BalanceType").get(0));
		AppLog.d("BalanceType :" + Global.getEC02WarmConfig("BalanceType"));
		adjustmentInfo.setAdjustmentType(
				ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("AdjustmentType_Deduct").get(0));
		AppLog.d("AdjustmentType_Deduct :" + Global.getEC02WarmConfig("AdjustmentType_Deduct"));
		adjustmentInfo.setAdjustmentAmt(map.get("fee"));
		adjustmentInfo
				.setCurrencyID(ec02Instance.getAbstractAF().getUtils().getHmWarmConfig().get("CurrencyID").get(0));
		AppLog.d("CurrencyID :" + Global.getEC02WarmConfig("CurrencyID"));
		adjustmentrequest.setAdjustmentInfo(adjustmentInfo);

		adjustmentRequest.setRequestHeader(requestHeader);
		adjustmentRequest.setAdjustmentRequest(adjustmentrequest);
		AppLog.d("SSID:" + map.get("ssid"));
		ec02Instance.getAFInstance().setAdjustmentRequestIns(adjustmentRequest);
		ec02Instance.getAFInstance().setMs(map.get("ms"));
		return value;
	}

	public void verifyMessage(AbstractAF abstractAF, EC02Instance ec02Instance, EquinoxRawData equinoxRawData) {
		this._resultCode = "000";
		this._resultDescription = "SUCCESS";
		this.type = Constant.TYPE_SUCCESS;
		if (null == map.get("page")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("page:" + "null");
		} else if (!map.get("page").equals("chargeRequest")) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("page:" + map.get("page"));
		}
		if (null == map.get("scp")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("scp:" + "null");
		} else if ("".equals(map.get("scp")) || map.get("scp").length() > 3) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("scp:" + "INVALID");
		}
		if (null == map.get("pwd")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("pwd:" + "null");
		} else if ("".equals(map.get("pwd")) || map.get("pwd").length() > 8) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("pwd:" + "INVALID");
		}
		if (null == map.get("ms")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("ms:" + "null");
		} else if ("".equals(map.get("ms")) || map.get("ms").length() > 10) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("ms:" + "INVALID");
		}
		if (null == map.get("fee")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("fee:" + "null");
		} else if ("".equals(map.get("fee")) || map.get("fee").length() > 5) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("fee:" + "INVALID");
		}
		if (null == map.get("mod")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("mod:" + "null");
		} else if ("".equals(map.get("mod")) || map.get("mod").length() > 1) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("mod:" + "INVALID");
		}
		if (null == map.get("cat")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("cat:" + "null");
		} else if ("".equals(map.get("cat")) || map.get("cat").length() > 1) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("cat:" + "INVALID");
		}
		if (null == map.get("dat")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("dat:" + "null");
		} else if ("".equals(map.get("dat")) || map.get("dat").length() > 100) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("dat:" + "INVALID");
		}
		if (null == map.get("nid")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("nid:" + "null");
		} else if ("".equals(map.get("nid")) || map.get("nid").length() > 5) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("nid:" + "INVALID");
		}
		if (null == map.get("ssid")) {
			this.type = Constant.TYPE_MISSING;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :MISSING PARAMETER");
			AppLog.d("ssid:" + "null");
		} else if ("".equals(map.get("ssid")) || map.get("ssid").length() > 20) {
			this.type = Constant.TYPE_INVALID;
			this._resultCode = "322";
			this._resultDescription = "INCOMPLETE_DATA";
			AppLog.d("## VERIFY(PARAMETER) :INVALID PARAMETER");
			AppLog.d("ssid:" + "INVALID");
		}

	}

	public void buildMessage(AbstractAF abstractAF, EC02Instance ec02Instance, EquinoxRawData equinoxRawData) {
		switch (this.type) {
		case Constant.TYPE_INVALID:
		case Constant.TYPE_MISSING: {
			this.type = Constant.TYPE_ERROR;
			chargeResponseIns.setResult(this._resultCode);
			chargeResponseIns.setPrepaidsubscriberflag("1");
			ec02Instance.getAFInstance().setResultCode(this._resultCode);
			ec02Instance.getAFInstance().setResultDescription(this._resultDescription);
			chargeResponseIns.setChargingID(ec02Instance.getAFInstance().getChargeId());
			String message = th.co.ais.ssbsrfc.message.MessageParser.toXml(chargeResponseIns);
			EQXRawInstance eqxRaw = SubMessages.getRawChargeRequestResponse(abstractAF, ec02Instance,
					ec02Instance.getAFInstance().getOrig(), ec02Instance.getAFInstance().getInvoke(), message);
			this.equinoxRawDataList.add(this.ec02Builder.getHTTPResponse(eqxRaw));
			ec02Instance.getAFInstance().setCurrentState(StateConfig.SSTATE_END);
			break;
		}
		case Constant.TYPE_SUCCESS:
		default: {
			ec02Instance.getAFInstance().setResultCode(this._resultCode);
			ec02Instance.getAFInstance().setResultDescription(this._resultDescription);
			// this.isStat = false;
			ec02Instance.getAFInstance().getListStat()
					.add(new Stat(StateConfig.PROJECT_STAT_NAME + "chargeRequest STAT"));
			ec02Instance.getAFInstance().setCurrentState(StateConfig.SSTATE_W_ADJUSTMENT_CHARGEREQUEST);
			EQXRawInstance eqxRaw = SubMessages.getRawAdjustmentRequest(abstractAF, ec02Instance,
					StateConfig.Incoming_Adjustment_ChargeRequest_Response, adjustmentRequest);

			this.equinoxRawDataList.add(this.ec02Builder.getRequest(eqxRaw));
		}
		}
	}
}