package ru.bdo.rfc;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;
import java.io.*;
import ru.bdo.methods.CommonMethod;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class TestJCO3Metod extends CommonMethod {
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";
static {
Properties p = new Properties();
try {
p.load(TestJCO3.class.getClassLoader().getResourceAsStream("r3d.properties"));
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, p.getProperty("jco.client.mshost"));
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, p.getProperty("jco.client.sysnr"));
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, p.getProperty("jco.client.client"));
connectProperties.setProperty(DestinationDataProvider.JCO_USER, p.getProperty("jco.client.user"));
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, p.getProperty("jco.client.passwd"));
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, p.getProperty("jco.client.lang"));
createDataFile(ABAP_AS, "jcoDestination", connectProperties);
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "5");
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
connectProperties.clear();
connectProperties.setProperty(DestinationDataProvider.JCO_MSHOST, p.getProperty("jco.client.mshost"));
connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, p.getProperty("jco.client.r3name"));
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, p.getProperty("jco.client.client"));
connectProperties.setProperty(DestinationDataProvider.JCO_USER, p.getProperty("jco.client.user"));
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, p.getProperty("jco.client.passwd"));
connectProperties.setProperty(DestinationDataProvider.JCO_GROUP, p.getProperty("jco.client.group"));
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, p.getProperty("jco.client.lang"));
createDataFile(ABAP_MS, "jcoDestination", connectProperties);
}
static void createDataFile(String name, String suffix, Properties properties) {
File cfg = new File(name + "." + suffix);
try {
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "");
fos.close();
} catch (Exception e) {
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
static void deleteDataFile(String name, String suffix)
{
File cfg = new File(name+"."+suffix);
try {
if(cfg.exists()){
cfg.delete();
}
} catch (Exception e) {
throw new RuntimeException("Unable to delete the destination file " + cfg.getName(), e);
}
}
public TestJCO3Metod() {
super(TestJCO3Metod.class, TestJCO3Metod.class.getName().indexOf(".") > 0 ? TestJCO3Metod.class.getName().substring(TestJCO3Metod.class.getName().lastIndexOf(".") + 1) : TestJCO3Metod.class.getName());
}
protected void executeMethod(IDfSession dfSession, PrintWriter output) throws DfException {
try {
trace("Begin execution...");
JCoDestination destination = null;
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
trace("Attributes{");
trace(destination.getAttributes().toString());
trace("}");
JCoFunction function = destination.getRepository().getFunction("ZEMC_KURATORS_GETLIST");
if (function == null)
throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
try {
function.execute(destination);
} catch (AbapException e) {
trace(e.toString());
return;
}
JCoTable codes = function.getTableParameterList().getTable("KURATORS");
for (int i = 0; i < codes.getNumRows(); i++) {
codes.setRow(i);
trace(codes.getString("ATWRT") + " " + codes.getString("ATWTB"));
}
trace("Complete...");
} catch (JCoException e) {
trace(e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}finally {
deleteDataFile(ABAP_AS, "jcoDestination");
deleteDataFile(ABAP_AS_POOLED, "jcoDestination");
deleteDataFile(ABAP_MS, "jcoDestination");
}
}
protected StringBuffer logSB = null;
protected void trace(String s) {
if (logSB == null) logSB = new StringBuffer();
logSB.append(s).append("\r\n");
super.trace(s);
}
public static void main(String[] args) throws DfException {
TestJCO3Metod aues = new TestJCO3Metod();
Map<String, String[]> hm = new HashMap<String, String[]>();
hm.put("docbase", new String[]{"kinef"});
hm.put("user", new String[]{"dmadmin"});
hm.put("ticket", new String[]{"dmadmin_3_kinef"});
hm.put("mode", new String[]{"0"});
try {
File file = new File("C:/file.txt");
PrintWriter printWriter = new PrintWriter("C:/file.txt");
try {
aues.execute(hm, printWriter);
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}