import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
@Component
@Slf4j
public class CrmGetConnection {
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
@Autowired
private CrmParameters crmParameters;
@PostConstruct
public void initProperties() {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, crmParameters.getJco_ashost());
connectProperties.setProperty(DestinationDataProvider.JCO_MSSERV, crmParameters.getJco_msserv());
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, crmParameters.getJco_sysnr());
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, crmParameters.getJco_client());
connectProperties.setProperty(DestinationDataProvider.JCO_USER, crmParameters.getJco_user());
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, crmParameters.getJco_passwd());
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, crmParameters.getJco_lang());
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, crmParameters.getJco_pool_capacity());
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, crmParameters.getJco_peak_limit());
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
private static void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name+"."+suffix);
if(cfg.exists()){
cfg.deleteOnExit();
}
try{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}catch (Exception e){
System.out.println("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
public JCoDestination connect(){
JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
} catch (JCoException e) {
}
return destination;
}
}
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
public class CrmParameters {
@Value("${crm.jco_ashost}")
private String jco_ashost;
@Value("${crm.jco_sysnr}")
private String jco_sysnr;
@Value("${crm.jco_client}")
private String jco_client;
@Value("${crm.jco_user}")
private String jco_user;
@Value("${crm.jco_passwd}")
private String jco_passwd;
@Value("${crm.jco_lang}")
private String jco_lang;
@Value("${crm.jco_pool_capacity}")
private String jco_pool_capacity;
@Value("${crm.jco_peak_limit}")
private String jco_peak_limit;
@Value("${crm.flag_f}")
private String flag_f;
@Value("${crm.jco_msserv}")
private String jco_msserv;
}
#crm10.0.64.138 crmprd.sanygroup.com
crm.jco_ashost=172.16.9.172
crm.jco_msserv=8443
#00
crm.jco_sysnr=01
crm.jco_client=800
crm.jco_user=CC_RFC
crm.jco_passwd=63gg5bG#
crm.jco_lang=ZH
crm.jco_pool_capacity=3
crm.jco_peak_limit=10
crm.flag_f=海外
具体引用
public String bindDeviceToUser(String deviceNum[], Integer userId,String language,String bindingTime) {
DeviceInfosDao mapper = sqlSessionTemplate.getMapper(DeviceInfosDao.class);
JCoFunction function = null;
JCoStructure jCoStructure=null;
JCoParameterList jCoParameterList=null;
String balance="";
String EV_MTYPE=null;
String EQU_NAME=null;
String EQU_NO=null;
String EQU_TYPE=null;
String prodGroupDesc=null;
String regionDesc=null;
String agentDesc=null;
String countryDesc=null;
String crm_language=language;
if("zh_CN".equals(language)){
crm_language="ZH";
}else if("en_US".equals(language)){
crm_language="EN";
}
JCoDestination destination = crmGetConnection.connect();
if(null!=deviceNum){
for(int i=0;i<deviceNum.length;i++){
if(mapper.checkDeviceInfo(deviceNum[i],userId)>0)
return "C";
try {
function = destination.getRepository().getFunction("ZFM_R_CC_GET_EQUIPMENT_INFO");
jCoParameterList=function.getImportParameterList();
jCoParameterList.setValue("IV_EQU_NO", deviceNum[i]);
JCoStructure structure = jCoParameterList.getStructure("IS_BASE");
structure.setValue("USER",crmParameters.getJco_user());
structure.setValue("LANGU",crm_language);
structure.setValue("FLAG_F",crmParameters.getFlag_f());
jCoParameterList.setValue("IS_BASE", structure);
function.execute(destination);
EV_MTYPE=function.getExportParameterList().getString("EV_MTYPE");
String EV_MSG=function.getExportParameterList().getString("EV_MSG");
jCoStructure= function.getExportParameterList().getStructure("ES_EQU_INFO");
EQU_NO= (String) jCoStructure.getValue("EQU_NO");
EQU_NAME= (String) jCoStructure.getValue("EQU_NAME");
EQU_TYPE= (String) jCoStructure.getValue("EQU_TYPE");
prodGroupDesc= (String) jCoStructure.getValue("PROD_GROUP_DESC");
regionDesc= (String) jCoStructure.getValue("REGION_DESC");
countryDesc= (String) jCoStructure.getValue("COUNTRY_DESC");
agentDesc= (String) jCoStructure.getValue("AGENT_DESC");
log.info("绑定设备:crm返回消息="+EV_MSG);
} catch (JCoException e) {
e.printStackTrace();
}
if("S".equals(EV_MTYPE)){
DeviceInfoEntity deviceInfoEntity = new DeviceInfoEntity();
deviceInfoEntity.setDeviceName(EQU_NAME);
deviceInfoEntity.setDeviceNum(EQU_NO);
deviceInfoEntity.setDeviceType(EQU_TYPE);
deviceInfoEntity.setProdGroupDesc(prodGroupDesc);
deviceInfoEntity.setRegionDesc(regionDesc);
deviceInfoEntity.setCountryDesc(countryDesc);
deviceInfoEntity.setAgentDesc(agentDesc);
if(mapper.checkDuplicateDataOfDevice(deviceNum[i])==0) {
mapper.addDeviceInfo(deviceInfoEntity);
}
mapper.bindDeviceToUser(deviceNum[i],userId,bindingTime);
}else if("E".equals(EV_MTYPE)){
return "E";
}else if(null==EV_MTYPE){
return "sapErr";
}
}
return "S";
}
return "设备号不能为空!";