package th.co.ais.ssbsrfc.control; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.ArrayUtils; import th.co.ais.ssbsrfc.config.Constant; import th.co.ais.ssbsrfc.config.StateConfig; import th.co.ais.ssbsrfc.instance.AFInstance; import th.co.ais.ssbsrfc.instance.EC02Instance; import th.co.ais.ssbsrfc.instance.FMInstance; import th.co.ais.ssbsrfc.instance.ManageTime; import th.co.ais.ssbsrfc.instance.Timeout; import th.co.ais.ssbsrfc.interfaces.EQXMsg; import th.co.ais.ssbsrfc.message.MessageParser; import th.co.ais.ssbsrfc.utils.Global; import th.co.ais.ssbsrfc.utils.Log; import ec02.af.abstracts.AbstractAF; import ec02.af.data.EquinoxRawData; import ec02.utils.AppLog; public class FlowManager { private AbstractAF abstractAF = null; private EC02Instance ec02Instance = null; private AFInstance afInstance = null; private EquinoxRawData equinoxRawData = null; private int flow = 0; private String type = EQXMsg.EMPTY; // private int[] arrSingleFlow = {Constant.FLOW_Loan_Potential_Jobs_Tracking, Constant.FLOW_Loan_Potential_Registration, Constant.FLOW_Loan_Potential_Request_Handling, Constant.FLOW_Loan_UI, Constant.FLOW_Loan_Activation, Constant.FLOW_Loan_Customer_Registration}; private FMInstance ins = null; private ArrayList listFlow = new ArrayList(); private Map dicInvoke = new LinkedHashMap(); private ArrayList listTimeout = new ArrayList(); private ArrayList globalListTimeout = new ArrayList(); private int globalDialogueId = -1; private boolean debug = false; public FlowManager(AbstractAF abstractAF, EC02Instance ec02Instance) { super(); this.abstractAF = abstractAF; this.ec02Instance = ec02Instance; this.afInstance = ec02Instance.getAFInstance(); } public void start(EquinoxRawData equinoxRawData) { this.equinoxRawData = equinoxRawData; this.listFlow = this.afInstance.getListFlow(); this.dicInvoke = this.afInstance.getDicInvoke(); this.globalDialogueId = this.afInstance.getGlobalDialogueId(); this.listTimeout = this.afInstance.getListTimeout(); this.globalListTimeout = this.afInstance.getGlobalListTimeout(); if (this.debug) { AppLog.d("####### TABLE LISTFLOW #######"); AppLog.d("## SIZE:" + this.ec02Instance.getAFInstance().getListFlow().size()); AppLog.d("## INPUT TIMESTAMP:" + this.ec02Instance.getAFInstance().getStartFlowTimeStamp()); AppLog.d("------------------------------"); for (FMInstance i : this.ec02Instance.getAFInstance().getListFlow()) { AppLog.d(i.toString()); } AppLog.d("############# END ############"); } if (this.listFlow.size() != 0) { // try { // case e01 if (this.abstractAF.getUtils().getGlobalData().isRecieve()) { String invoke = equinoxRawData.getInvoke(); this.setInstanceForInvoke(invoke); if (this.debug) { AppLog.d("## CASE EO1 GET INVOKE:" + invoke); } } else { String ret = equinoxRawData.getRet(); if (ret.equals("4")) { // Timeout String unique = equinoxRawData.getRawDataAttribute("unique"); this.setInstanceForUnique(unique); if (this.debug) { AppLog.d("## CASE TIMEOUT GET UNIQUE:" + unique); } } else { String type = equinoxRawData.getType(); if ((type.toLowerCase().equals("request") || type.toLowerCase().equals("noreply")) && !ret.equals("1") && !ret.equals("2") && !ret.equals("3")) { String eventType = equinoxRawData.getRawEventType(); String ssName = StateConfig.getSubStateFromEventType(eventType); int flow = StateConfig.getFlowFromEventType(eventType); // if (flow == -1 && type.toLowerCase().equals("noreply")) { // String invoke = equinoxRawData.getInvoke(); // if (invoke != null && invoke.contains(".")) { // Object code = new Invoke(invoke).dev; // String strCode = (String)code; // if (strCode != null && strCode.equals("16")) { // flow = Constant.FLOW_Loan_Potential_Request_Handling; // } // } // } if (this.debug) { AppLog.d("## CASE REQUEST MESSAGE GET FLOW:" + flow); AppLog.d("## EVENTTYPE:" + eventType); AppLog.d("## SUBSTATE FOR EVENTTYPE:" + ssName); } if (flow != -1) { this.flow = flow; String firstState = StateConfig.arrFirstStateFlow[flow-1]; if (this.debug) { AppLog.d("## FLOW:" + this.flow); AppLog.d("## FIRST STATE:" + firstState); } if (ssName.equals(firstState) && !ArrayUtils.contains(Constant.arrSingleFlow, this.flow)) { this.setInstance(new AFInstance()); if (this.debug) { AppLog.d("## CASE START STATE FOR MULTI FLOW::NEW INSTANCE"); } } else if (/*ssName.equals(firstState) && */ArrayUtils.contains(Constant.arrSingleFlow, this.flow)) { this.setInstanceForFlow(flow); if (this.debug) { AppLog.d("## CASE SINGLE FLOW::GET INSTANCE FOR FLOW:" + this.flow); } } else { // not first state this.setInstanceForFlow(flow); if (this.debug) { AppLog.d("## CASE NOT FIRST STATE::GET INSTANCE FOR FLOW:" + this.flow); } } } else { // // if (eventType.equals(StateConfig.Incoming_DRR_Request) || eventType.equals(StateConfig.Incoming_DUR_Request)) { // String key = "Dialogue-Id"; // String dialogueId = this.equinoxRawData.getRawDataAttribute(key); // this.equinoxRawData.getRawDataAttributes().remove(key); // // SET FLOW from Dialogue-Id // this.setInstanceForDialogueId(dialogueId); // if (this.debug) { // AppLog.d("## CASE DRR/DUR REQUEST::GET INSTANCE FOR DIALOGUE_ID:" + dialogueId); // } // } else { // impossible. int index = this.listFlow.size() - 1; FMInstance ins = this.listFlow.get(index); this.listFlow.remove(index); AFInstance data = (AFInstance) MessageParser.fromJson(ins.getData(), AFInstance.class); this.setInstance(data); if (this.debug) { AppLog.d("## CASE IMPOSSIBLE::GET INSTANCE FOR INDEX:" + index); } // } } // } else if (type.toLowerCase().equals("response") && (ret.equals("1") || ret.equals("2") || ret.equals("3"))) { // int index = this.listFlow.size() - 1; // FMInstance ins = this.listFlow.get(index); // this.listFlow.remove(index); // AFInstance data = (AFInstance) MessageParser.fromJson(ins.getData(), AFInstance.class); // this.setInstance(data); // if (this.debug) { // AppLog.d("## CASE IMPOSSIBLE::GET INSTANCE FOR INDEX:" + index); // } } else { String invoke = equinoxRawData.getInvoke(); this.setInstanceForInvoke(invoke); if (this.debug) { AppLog.d("## CASE RESPONE MESSAGE::GET INSTANCE FOR INVOKE:" + invoke); } } } } // } catch (Exception e) { // if (this.debug) { // AppLog.d("## EXCEPTION(GET START MULTI FLOW):" + e); // } // int index = this.listFlow.size() - 1; // FMInstance ins = this.listFlow.get(index); // this.listFlow.remove(index); // AFInstance data = (AFInstance) MessageParser.fromJson(ins.getData(), AFInstance.class); // this.setInstance(data); // } } else { if (this.debug) { AppLog.d("## FLOWMANAGER START"); AppLog.d("## 1.START FLOW TIMESTAMP:" + this.ec02Instance.getAFInstance().getStartFlowTimeStamp()); AppLog.d("## 2.INPUT TIMESTAMP:" + Global.inputTimeStamp); AppLog.d("## 3.CURRENT START:" + this.ec02Instance.getAFInstance().getCurrentState()); } } if (this.debug) { AppLog.d("## GET INSTANCE START FLOW TIMESTAMP:" + this.ec02Instance.getAFInstance().getStartFlowTimeStamp()); } } public void end(String curState, String nxtState, List eqxRawDataListInput, List eqxRawDataListOutput) { Log.detailLog(this.ec02Instance, this.abstractAF, curState, nxtState, eqxRawDataListInput, eqxRawDataListOutput); Log.summaryLog(this.ec02Instance, this.abstractAF); this.listFlow = this.ec02Instance.getAFInstance().getListFlow(); this.dicInvoke = this.ec02Instance.getAFInstance().getDicInvoke(); this.globalDialogueId = this.ec02Instance.getAFInstance().getGlobalDialogueId(); this.globalListTimeout = this.ec02Instance.getAFInstance().getGlobalListTimeout(); // clear this.ec02Instance.getAFInstance().setListFlow(null); this.ec02Instance.getAFInstance().setDicInvoke(null); this.ec02Instance.getAFInstance().setGlobalListTimeout(null); if (!nxtState.equals(StateConfig.STATE_IDLE)) { // Gson gsonBuilder = new GsonBuilder().create(); AFInstance af = this.ec02Instance.getAFInstance(); String data = Global.encodeInstance(af); int flow = af.getFlow(); String date = af.getStartFlowTimeStamp(); String dialogueId = af.getDialogueIdForMultiFlow(); String unique = Global.getUnique(); FMInstance newIns = new FMInstance(flow, date, data, dialogueId); newIns.setUnique(unique); if (this.debug) { AppLog.d("## FLOWMANAGER END"); AppLog.d("## FLOW:" + flow); AppLog.d("## DATE:" + date); AppLog.d("## DIALOGUE_ID:" + dialogueId); AppLog.d("## UNIQUE:" + unique); } ManageTime mTimeout = new ManageTime(this.ec02Instance); Timeout ins = mTimeout.getInsTime(); if (ins != null) { ManageTime mt = new ManageTime(); mt.setListTimeout(this.ec02Instance.getAFInstance().getGlobalListTimeout()); mt.setTimeout(newIns.getUnique(), "" + ins.getTimeout(), ins.getDate()); this.ec02Instance.getAFInstance().setGlobalListTimeout(mt.getListTimeout()); } // this.ec02Instance.getAFInstance().putListFlow(newIns); this.listFlow = this.listFlow == null ? new ArrayList() : this.listFlow; this.listFlow.add(newIns); if (this.debug) { AppLog.d("## LIST FLOW COUNT:" + this.listFlow.size()); } } // this.ec02Instance.getAFInstance().setStartFlowTimeStamp(""); if (this.debug) { AppLog.d("####### TABLE LISTFLOW #######"); AppLog.d("## SIZE:" + this.listFlow.size()); AppLog.d("------------------------------"); for (FMInstance i : this.listFlow) { AppLog.d(i.toString()); } AppLog.d("############# END ############"); AppLog.d("####### TABLE DIC INVOKE #######"); AppLog.d("## SIZE:" + this.dicInvoke.size()); AppLog.d("------------------------------"); if (this.dicInvoke != null) { Iterator it = this.ec02Instance.getAFInstance().getDicInvoke().entrySet().iterator(); while (it.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry pair = (Map.Entry)it.next(); AppLog.d(pair.getKey()+ ":" + pair.getValue().toString()); } } AppLog.d("############# END ############"); } this.setInstance(new AFInstance()); } public FMInstance find(FMInstance ins) { if (this.debug) { AppLog.d("####### TABLE LISTFLOW #######"); AppLog.d("## SIZE:" + this.listFlow.size()); AppLog.d("------------------------------"); for (FMInstance i : this.listFlow) { AppLog.d(i.toString()); } AppLog.d("############# END ############"); AppLog.d("FMInstance find:" + ins.toString()); } FMInstance newIns = null; for (FMInstance i : this.listFlow) { if (ins.getUnique() != null && i.getUnique().equals(ins.getUnique())) { // AppLog.d("## unique"); // AppLog.d("unique:" + ins.getUnique()); newIns = i; break; } else if (ins.getDialogueId() != null) { // AppLog.d("## dialogueId"); if (i.getDialogueId().equals(ins.getDialogueId())) { // AppLog.d("getDialogueId:" + ins.getDialogueId()); newIns = i; break; } } else if (ins.getDate() != null) { // AppLog.d("## date"); if (i.getFlow() == ins.getFlow() && i.getDate().equals(ins.getDate())) { // AppLog.d("flow:" + i.getFlow() + " date:" + i.getDate()); newIns = i; break; } } else { // AppLog.d("## flow"); if (i.getFlow() == ins.getFlow()) { // AppLog.d("flow:" + i.getFlow() ); newIns = i; break; } } } // remove if (newIns != null) { this.listFlow.remove(newIns); if (this.debug) { AppLog.d("## REMOVE:" +newIns.getFlow() + " " + newIns.getDate() + " " + newIns.getUnique()); AppLog.d("## globalListTimeout:" + this.globalListTimeout.size()); } // remove globalListTimeout try { Timeout mIns = null; for (Timeout mt : this.globalListTimeout) { if (newIns.getUnique() != null && mt.getName() != null && mt.getName().equals(newIns.getUnique())){ mIns = mt; break; } } // check remove if (mIns != null) { this.globalListTimeout.remove(mIns); } if (this.debug) { AppLog.d("## REMOVE GLOBAL_LIST_TIMEOUT COUNT:" + this.globalListTimeout.size()); } } catch (Exception e) { if (this.debug) { AppLog.e("## Exception(remove globalListTimeout:" + this.globalListTimeout.size() + "):" + e.getMessage()); } } } return newIns; } public void setInstance(AFInstance ins) { ins.setListFlow(this.listFlow); ins.setDicInvoke(this.dicInvoke); ins.setGlobalDialogueId(this.globalDialogueId); ins.setGlobalListTimeout(this.globalListTimeout); if (ins.getStartFlowTimeStamp() == null && this.ec02Instance.getAFInstance().getStartFlowTimeStamp() != null) { ins.setStartFlowTimeStamp(this.ec02Instance.getAFInstance().getStartFlowTimeStamp()); } // set instance this.ec02Instance.setAFInstance(ins); if (this.debug) { AppLog.d("## FUNCTION SET INSTANCE"); AppLog.d("## 1.START FLOW TIMESTAMP:" + ins.getStartFlowTimeStamp()); AppLog.d("## 2.INPUT TIMESTAMP:" + this.ec02Instance.getAFInstance().getStartFlowTimeStamp()); } } public void setInstanceForInvoke(String invoke) { // AppLog.d("#d# invoke :" + invoke); FMInstance ins = this.dicInvoke.get(invoke); // AppLog.d("setInstanceForInvoke:" + ins.getFlow() + " " + ins.getDate()); this.dicInvoke.remove(invoke); this.setInstance(ins); } public void setInstanceForFlow(int flow) { FMInstance ins = new FMInstance(flow); // AppLog.d(ins.toString()); this.setInstance(ins); } public void setInstanceForDialogueId(String dialogueId) { FMInstance ins = new FMInstance(flow, null, null, dialogueId); this.setInstance(ins); } public void setInstanceForUnique(String unique) { FMInstance ins = new FMInstance(); ins.setUnique(unique); ArrayList listTimeout = this.ec02Instance.getAFInstance().getListTimeout(); for (Timeout timeout : listTimeout) { this.dicInvoke.remove(timeout.getName()); } this.setInstance(ins); } public void setInstance(FMInstance ins) { if (this.debug) { AppLog.d("## FIND AF_INSTANCE"); AppLog.d(ins.toString()); } try { ins = this.find(ins); if (ins == null) { this.setInstance(new AFInstance()); if (this.debug) { AppLog.d("## NOSUCH OBJECT::NEW AF_INSTANCE"); } } else { // AFInstance data = (AFInstance) MessageParser.fromJson(ins.getData(), AFInstance.class); AFInstance data = Global.decodeInstance(ins.getData()); this.setInstance(data); if (this.debug) { AppLog.d("## OBJECT DATA"); AppLog.d("## 1.FLOW:" + data.getFlow()); AppLog.d("## 2.START FLOW TIMESTAMP:" + data.getStartFlowTimeStamp()); } } } catch (Exception e) { if (this.debug) { AppLog.e("## EXCEPTION:" + e.getMessage()); } } } public AbstractAF getAbstractAF() { return abstractAF; } public void setAbstractAF(AbstractAF abstractAF) { this.abstractAF = abstractAF; } public EC02Instance getEc02Instance() { return ec02Instance; } public void setEc02Instance(EC02Instance ec02Instance) { this.ec02Instance = ec02Instance; } public int getFlow() { return flow; } public void setFlow(int flow) { this.flow = flow; } public String getType() { return type; } public void setType(String type) { this.type = type; } public FMInstance getIns() { return ins; } public void setIns(FMInstance ins) { this.ins = ins; } public ArrayList getListFlow() { return listFlow; } public void setListFlow(ArrayList listFlow) { this.listFlow = listFlow; } public Map getDicInvoke() { return dicInvoke; } public void setDicInvoke(Map dicInvoke) { this.dicInvoke = dicInvoke; } public AFInstance getAfInstance() { return afInstance; } public void setAfInstance(AFInstance afInstance) { this.afInstance = afInstance; } public ArrayList getListTimeout() { return listTimeout; } public void setListTimeout(ArrayList listTimeout) { this.listTimeout = listTimeout; } public EquinoxRawData getEquinoxRawData() { return equinoxRawData; } public void setEquinoxRawData(EquinoxRawData equinoxRawData) { this.equinoxRawData = equinoxRawData; } }