package com.celnet.datadump.config; import com.alibaba.fastjson.JSONArray; import com.celnet.datadump.mapper.CustomMapper; import com.celnet.datadump.util.BulkUtil; import com.celnet.datadump.util.EmailUtil; import com.google.common.collect.Lists; import com.sforce.async.BulkConnection; import com.sforce.soap.partner.PartnerConnection; import com.sforce.ws.ConnectionException; import com.sforce.ws.ConnectorConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Queue; /** * @author Red * @description * @date 2022/09/23 */ @Component @Slf4j public class SalesforceConnect { @Resource private CustomMapper customerMapper; /** * 创建连接 * @return */ public PartnerConnection createConnect() { try { List> poll = customerMapper.list("code,value","org_config",null); //遍历poll,找出code值为SOURCE_ORG_URL,SOURCE_ORG_USERNAME,SOURCE_ORG_PASSWORD的value值 Map map = new HashMap<>(); for (Map map1 : poll) { if ("SOURCE_ORG_URL".equals(map1.get("code"))) { map.put("url", (String) map1.get("value")); } if ("SOURCE_ORG_USERNAME".equals(map1.get("code"))) { map.put("username", (String) map1.get("value")); } if ("SOURCE_ORG_PASSWORD".equals(map1.get("code"))) { map.put("password", String.valueOf(map1.get("value"))); } } String username = map.get("username"); ConnectorConfig config = new ConnectorConfig(); config.setUsername(username); config.setPassword(map.get("password")); String url = map.get("url"); config.setAuthEndpoint(url); config.setServiceEndpoint(url); config.setConnectionTimeout(60 * 60 * 1000); config.setReadTimeout(60 * 60 * 1000); return new PartnerConnection(config); } catch (ConnectionException e) { String message = "源ORG连接配置错误!"; String format = String.format("ORG连接异常!, \ncause:\n%s", message); EmailUtil.send("DataDump ERROR", format); log.error("exception message", e); return null; } } /** * 创建Bulk连接 * @author kris */ public BulkConnection createBulkConnect() { try { List> poll = customerMapper.list("code,value","org_config",null); //遍历poll,找出code值为SOURCE_ORG_URL,SOURCE_ORG_USERNAME,SOURCE_ORG_PASSWORD的value值 Map map = new HashMap<>(); for (Map map1 : poll) { if ("SOURCE_ORG_URL".equals(map1.get("code"))) { map.put("url", (String) map1.get("value")); } if ("SOURCE_ORG_USERNAME".equals(map1.get("code"))) { map.put("username", (String) map1.get("value")); } if ("SOURCE_ORG_PASSWORD".equals(map1.get("code"))) { map.put("password", String.valueOf(map1.get("value"))); } } // // String username = map.get("username"); // ConnectorConfig config = new ConnectorConfig(); // config.setUsername(username); // config.setPassword(map.get("password")); // String url = map.get("url"); // config.setAuthEndpoint(url); // config.setServiceEndpoint(url); // config.setConnectionTimeout(60 * 60 * 1000); // config.setReadTimeout(60 * 60 * 1000); // PartnerConnection connection = new PartnerConnection(config); // config.setRestEndpoint(url); // config.setSessionId(connection.getSessionHeader().getSessionId()); return BulkUtil.getBulkConnection(map.get("username"),map.get("password"),map.get("url")); } catch (Exception e) { String message = "源ORG连接配置错误!"; String format = String.format("ORG连接异常!, \ncause:\n%s", message); EmailUtil.send("DataDump ERROR", format); log.error("exception message", e); return null; } } }