package com.weaver.seconddev.interfaces.workflow.action;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.ext.DestinationDataProvider;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.general.BaseBean;

public class SAPConn {

//    private static final Logger logger_cb5e740f = LoggerFactory.getLogger(SAPConn.class);

    static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";

    static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

    static String ABAP_MS = "ABAP_MS_WITHOUT_POOL";

    private static JCoDestination destination = null;

    private static BaseBean bb = new BaseBean();

    private static String sapclient = "";

    private static String userid = "";

    private static String password = "";

    private static String hostname = "";

    private static String systemnumber = "";

    private static String Language = "";

    private static String SAPROUTER = "";

    /*
     *初始化连接
     */
    public void SAPConn() {
    }

    /*
     *初始化连接
     */
    private static void init() {
        try {
//            logger_cb5e740f.error(String.valueOf("-------------SAP Client Connects Start ----------------"));
            // sapclient = bb.getPropValue("SAPConn", "SAPClient");
            // userid = bb.getPropValue("SAPConn", "Userid");
            // password = bb.getPropValue("SAPConn", "Password");
            // hostname = bb.getPropValue("SAPConn", "HostName");
            // systemnumber = bb.getPropValue("SAPConn", "SystemNumber");
            // Language = bb.getPropValue("SAPConn", "Language");
            // 测试200  正式 800
            sapclient = "200";
            // 测试OAUSER 正式 OAADMIN
            userid = "OAUSER";
            // 测试 htjt2016   正式 HTjt@group.COM
            password = "htjt2016";
            // 测试10.6.6.80  正式 10.6.6.88
            hostname = "10.6.6.80";
            systemnumber = "00";
            Language = "ZH";
            // SAPROUTER = "/H/202.111.37.66/W/3456789/H/";
            Properties connectProperties = new Properties();
            connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, hostname);
            connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, systemnumber);
            connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, sapclient);
            // connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, SAPROUTER);
            connectProperties.setProperty(DestinationDataProvider.JCO_USER, userid);
            connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, password);
            connectProperties.setProperty(DestinationDataProvider.JCO_LANG, Language);
            createDataFile(ABAP_AS, "jcoDestination", connectProperties);
            // 测试3
            connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "6");
            // 测试10
            connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "20");
            createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
//            logger_cb5e740f.error(String.valueOf("get connection success"));
        } catch (Exception e) {
//            logger_cb5e740f.error(String.valueOf("get connection error:" + e));
        }
    }

    /*
     *创建连接
     */
    static void createDataFile(String name, String suffix, Properties properties) {
        File cfg = new File(name + "." + suffix);
//        logger_cb5e740f.error(String.valueOf(cfg.getPath()));
        if (!cfg.exists()) {
            try {
                FileOutputStream fos = new FileOutputStream(cfg, false);
                properties.store(fos, "for tests only !");
//                logger_cb5e740f.error(String.valueOf("createDataFile"));
                fos.close();
            } catch (Exception e) {
//                logger_cb5e740f.error(String.valueOf("createDataFile" + e.toString()));
                throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
            }
        }
    }

    /*
     *获取连接
     */
    public static JCoDestination getJCoDestination() {
        try {
            init();
            destination = JCoDestinationManager.getDestination(ABAP_AS);
            destination.ping();
            System.out.println("ABAP_AS 	Attributes:");
            System.out.println(destination.getAttributes());
            System.out.println();
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
            destination.ping();
//            logger_cb5e740f.error(String.valueOf("ABAP_AS_POOLED	Attributes:"));
//            logger_cb5e740f.error(String.valueOf(destination.getAttributes()));
//            logger_cb5e740f.error("ABAP_AS_POOLED	Attributes:");
//            logger_cb5e740f.error(String.valueOf(destination.getAttributes()));
        } catch (Exception e) {
            e.printStackTrace();
//            logger_cb5e740f.error(String.valueOf("getJCoDestination error:" + e.toString()));
        }
        return destination;
    }
}
