T2Util.java
4.95 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
package com.cjs.site.util.t2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.cjs.site.biz.ServiceException;
import com.cjs.site.util.lang.StringUtil;
import com.hundsun.t2sdk.common.core.context.ContextUtil;
import com.hundsun.t2sdk.common.share.dataset.DatasetService;
import com.hundsun.t2sdk.impl.client.T2Services;
import com.hundsun.t2sdk.interfaces.IClient;
import com.hundsun.t2sdk.interfaces.T2SDKException;
import com.hundsun.t2sdk.interfaces.share.dataset.IDataset;
import com.hundsun.t2sdk.interfaces.share.dataset.IDatasets;
import com.hundsun.t2sdk.interfaces.share.event.EventTagdef;
import com.hundsun.t2sdk.interfaces.share.event.EventType;
import com.hundsun.t2sdk.interfaces.share.event.IEvent;
/**
* 恒生T2接口工具类
*
* @author tongyufu
*
*/
public final class T2Util {
private static T2Services server;
private static IClient client;
static {
try {
server = T2Services.getInstance();
String configString = T2Util.class.getResource("/t2sdk-config.xml").getPath();
server.setT2sdkConfigString(configString);
server.init();
server.start();
client = server.getClient("myserver");
} catch (T2SDKException e) {
e.printStackTrace();
}
}
/**
* 发送请求
*
* @param params 请求参数
* @param funccode 接口编号
* @throws T2SDKException
*/
public static T2Result request(Map<String, Object> params,
String funccode) throws ServiceException {
try {
IEvent event = ContextUtil.getServiceContext().getEventFactory()
.getEventByAlias(funccode, EventType.ET_REQUEST);
//请求参数
IDataset requestParam = DatasetService.getDefaultInstance().getDataset();
for (String key : params.keySet()) {
requestParam.addColumn(key);
}
requestParam.appendRow();
for (String key : params.keySet()) {
requestParam.updateString(key, StringUtil.castString(params.get(key)));
}
event.putEventData(requestParam);
event.setStringAttributeValue(EventTagdef.TAG_SYSTEM_NO, "2");
//发送请求
IEvent rsp = client.sendReceive(event, 10000);
T2Result result = new T2Result();
result.setCode(rsp.getReturnCode());
//响应状态
result.setData(T2Util.getData(rsp.getEventDatas()));
if (result.getCode() != 0) {
result.setError(T2Util.getError(rsp.getEventDatas()));
}
return result;
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("请求T2接口出错", e);
}
}
/**获取错误信息*/
private static T2Error getError(IDatasets datasets) {
T2Error error = new T2Error();
for (int i = 0; i < datasets.getDatasetCount(); i++) {
// 开始读取单个结果集的信息
IDataset ds = datasets.getDataset(i);
// 遍历单个结果集记录,遍历前首先将指针置到开始
ds.beforeFirst();
while (ds.hasNext()) {
ds.next();
for (int j = 1; j <= ds.getColumnCount(); j++) {
String columnName = ds.getColumnName(j);
if ("error_info".equals(columnName)) {
String value = ds.getValue(j).toString();
value = value.replace("[", "");
String[] infos = value.split("]");
error.setErrorInfo(infos[1]);
error.setOccurDate(infos[2]);
} else if ("error_pathinfo".equals(columnName)) {
error.setErrorPathInfo(ds.getValue(j).toString());
} else if ("error_no".equals(columnName)) {
error.setErrorNo(ds.getValue(j).toString());
}
}
}
}
return error;
}
private static List<Map<String, Object>> getData(IDatasets datasets) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < datasets.getDatasetCount(); i++) {
Map<String, Object> row = new HashMap<String, Object>();
// 开始读取单个结果集的信息
IDataset ds = datasets.getDataset(i);
// 遍历单个结果集记录,遍历前首先将指针置到开始
ds.beforeFirst();
while (ds.hasNext()) {
ds.next();
row = new HashMap<String, Object>();
for (int j = 1; j <= ds.getColumnCount(); j++) {
row.put(ds.getColumnName(j), ds.getValue(j));
}
list.add(row);
}
}
return list;
}
}