FlowManager.java
16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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<FMInstance> listFlow = new ArrayList<FMInstance>();
private Map<String, FMInstance> dicInvoke = new LinkedHashMap<String, FMInstance>();
private ArrayList<Timeout> listTimeout = new ArrayList<Timeout>();
private ArrayList<Timeout> globalListTimeout = new ArrayList<Timeout>();
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<EquinoxRawData> eqxRawDataListInput, List<EquinoxRawData> 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<FMInstance>() : 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<Timeout> 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<FMInstance> getListFlow() {
return listFlow;
}
public void setListFlow(ArrayList<FMInstance> listFlow) {
this.listFlow = listFlow;
}
public Map<String, FMInstance> getDicInvoke() {
return dicInvoke;
}
public void setDicInvoke(Map<String, FMInstance> dicInvoke) {
this.dicInvoke = dicInvoke;
}
public AFInstance getAfInstance() {
return afInstance;
}
public void setAfInstance(AFInstance afInstance) {
this.afInstance = afInstance;
}
public ArrayList<Timeout> getListTimeout() {
return listTimeout;
}
public void setListTimeout(ArrayList<Timeout> listTimeout) {
this.listTimeout = listTimeout;
}
public EquinoxRawData getEquinoxRawData() {
return equinoxRawData;
}
public void setEquinoxRawData(EquinoxRawData equinoxRawData) {
this.equinoxRawData = equinoxRawData;
}
}