T2Util.java 4.95 KB
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;
    }

}