java与SAP集成回写单条记录模版(JCO3)

调用sap的公共类代码
package com.tbsap.jcosap;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

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;

public class SAPJCO {
static String ABAP_AS_POOLED = "ABAP_PUBLIC_600_SAPJCO_POOL";
private static void createDataFile()

{
Properties properties = new Properties();
properties.setProperty(DestinationDataProvider.JCO_ASHOST, "sapIP");
properties.setProperty(DestinationDataProvider.JCO_SYSNR, "系统ID(00)");
properties.setProperty(DestinationDataProvider.JCO_CLIENT, "客户端");
properties.setProperty(DestinationDataProvider.JCO_USER, "用户");
properties.setProperty(DestinationDataProvider.JCO_PASSWD, "密码");
properties.setProperty(DestinationDataProvider.JCO_LANG, "预言");
String name = ABAP_AS_POOLED;
String suffix = "jcoDestination";
File cfg = new File(name+"."+suffix);
if(!cfg.exists())
{
try
{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}

}
public static JCoDestination getJcoConnection() throws JCoException{

createDataFile();
return JCoDestinationManager.getDestination(ABAP_AS_POOLED);
}
}

单条记录回写sap标准函数的一种方式代码
package com.topband.web.struts.action.supplier;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.actions.DispatchAction;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.tbsap.jcosap.SAPJCO;
import javax.servlet.http.HttpSession;

public class ItemAction extends DispatchAction{

public ItemAction() {
super();
}
public ActionForward ItemHXSAP(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest reuqest,
HttpServletResponse response) {
Supplier supplierbean = new Supplier();
JCoDestination destination = null;
HttpSession session = reuqest.getSession(true);
Long userId = new Long(session.getAttribute("userId").toString()); //取当前用户的ID
Long orgId = new Long(session.getAttribute("orgId").toString()); //取当前用户的orgID
String itembo = reuqest.getParameter("itembo");
String itemdesc = reuqest.getParameter("itemdesc");
String wbitem = reuqest.getParameter("wbitem");
String dw = reuqest.getParameter("dw");
String checkwl = reuqest.getParameter("checkwl");
String wlz = reuqest.getParameter("wlz");
String msgs = "";
String text = "";
PrintWriter out = supplierbean.getWriterOut(response);
SAPJCO sapjco = new SAPJCO();
try
{
destination = sapjco.getJcoConnection();
JCoFunction function = destination.getRepository().getFunction("BAPI_MATERIAL_SAVEDATA");
JCoFunction function1 = destination.getRepository().getFunction("BAPI_TRANSACTION_COMMIT");
if (function != null)
{

//JCoTable HEADDATA = function.getTableParameterList().getTable("HEADDATA");
//JCoTable CLIENTDATA = function.getTableParameterList().getTable("CLIENTDATA");
//JCoTable CLIENTDATAX = function.getTableParameterList().getTable("CLIENTDATAX");
//String lifnr = function1.getExportParameterList().getString("LIFNR");
JCoTable MATERIALDESCRIPTION = function.getTableParameterList().getTable("MATERIALDESCRIPTION");
JCoTable UNITSOFMEASURE = function.getTableParameterList().getTable("UNITSOFMEASURE");
//JCoTable msg = function.getTableParameterList().getTable("RETURN");

JCoParameterList paramList = function.getImportParameterList();
JCoStructure HEADDATA =paramList.getStructure("HEADDATA");
JCoStructure CLIENTDATA =paramList.getStructure("CLIENTDATA");
JCoStructure CLIENTDATAX =paramList.getStructure("CLIENTDATAX");
//HEADDATA.appendRow();
System.out.println("A"+itembo);
System.out.println("B"+itemdesc);
System.out.println("C"+wbitem);
System.out.println("D"+dw);
System.out.println("E"+checkwl);
if(itembo ==null || "null".equals(itembo)){
HEADDATA.setValue("MATERIAL", "");
}else{
String itembo1 = itembo.trim();
char[] itembo2 = itembo1.toCharArray();
HEADDATA.setValue("MATERIAL",itembo2);
}
HEADDATA.setValue("BASIC_VIEW","X");
HEADDATA.setValue("IND_SECTOR","M");
HEADDATA.setValue("MATL_TYPE","FERT");

MATERIALDESCRIPTION.appendRow();
MATERIALDESCRIPTION.setValue("LANGU_ISO","ZH");
if(itemdesc ==null || "null".equals(itemdesc)){
MATERIALDESCRIPTION.setValue("MATL_DESC", "");
}else{
String itemdesc1 = itemdesc.trim();
char[] itemdesc2 = itemdesc1.toCharArray();
MATERIALDESCRIPTION.setValue("MATL_DESC",itemdesc2);
}
//CLIENTDATA.appendRow();
if(dw ==null || "null".equals(dw)){
CLIENTDATA.setValue("BASE_UOM", "");
}else{
String dw1 = dw.trim();
char[] dw2 = dw1.toCharArray();
CLIENTDATA.setValue("BASE_UOM",dw2);
}
if(wbitem ==null || "null".equals(wbitem)){
CLIENTDATA.setValue("EXTMATLGRP", "");
}else{
String wbitem1 = wbitem.trim();
char[] wbitem2 = wbitem1.toCharArray();
CLIENTDATA.setValue("EXTMATLGRP",wbitem2);
}
if(checkwl ==null || "null".equals(checkwl)){
CLIENTDATA.setValue("PROD_MEMO", "");
}else{
String checkwl1 = checkwl.trim();
char[] checkwl12 = checkwl1.toCharArray();
CLIENTDATA.setValue("PROD_MEMO",checkwl12);
}

if(wlz ==null || "null".equals(wlz)){
CLIENTDATA.setValue("MATL_GROUP", "");
}else{
String wlz1 = wlz.trim();
char[] wlz2 = wlz1.toCharArray();
CLIENTDATA.setValue("MATL_GROUP",wlz2);
}

if(wlz ==null || "null".equals(wlz)){
CLIENTDATA.setValue("MATL_GROUP", "");
}else{
String wlz1 = wlz.trim();
char[] wlz2 = wlz1.toCharArray();
CLIENTDATA.setValue("MATL_GROUP",wlz2);
}
//CLIENTDATAX.appendRow();
CLIENTDATA.setValue("BASIC_MATL","X");
CLIENTDATAX.setValue("BASIC_MATL","X");
CLIENTDATAX.setValue("MATL_GROUP","X");
CLIENTDATAX.setValue("BASE_UOM","X");
CLIENTDATAX.setValue("PROD_MEMO","X");
CLIENTDATAX.setValue("EXTMATLGRP","X");
UNITSOFMEASURE.appendRow();
if(dw ==null || "null".equals(dw)){
UNITSOFMEASURE.setValue("ALT_UNIT", "");
}else{
String dw1 = dw.trim();
char[] dw2 = dw1.toCharArray();
UNITSOFMEASURE.setValue("ALT_UNIT",dw2);
}
function.execute(destination);
JCoStructure msg = function.getExportParameterList().getStructure("RETURN");
//接受SAP 传出参数
//for (int i = 0; i < msg.getNumRows(); ++i){
// msg.setRow(i);
msgs = msg.getString("TYPE");
text = msg.getString("MESSAGE");
System.out.println("值"+msgs);
System.out.println("值2"+text);
if(msgs=="S" || "S".equals(msgs)){
JCoParameterList paramList1 = function1.getImportParameterList();
paramList1.setValue("WAIT", "X");
function1.execute(destination);
}
// }
out.print(msgs.concat(",").concat(text)); //反馈描述信息
}
}catch(AbapException e)
{
destination = null;
} catch (JCoException e) {
destination = null;
e.printStackTrace();
}finally{
destination = null;
}

return null;

}


}


单个回写sap自己写的函数模版代码
package com.topband.web.struts.action.supplier;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.actions.DispatchAction;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
import com.tbsap.jcosap.SAPJCO;
import com.topband.supplier.model.SupplierBean;
import javax.servlet.http.HttpSession;

public class zhugx01 extends DispatchAction{
public zhugx01() {
super();
}
public ActionForward LSHXSAP(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest reuqest,
HttpServletResponse response) {
Supplier supplierbean = new Supplier();
JCoDestination destination = null;
HttpSession session = reuqest.getSession(true);
Long userId = new Long(session.getAttribute("userId").toString()); //取当前用户的ID
Long orgId = new Long(session.getAttribute("orgId").toString()); //取当前用户的orgID
String workid = reuqest.getParameter("id"); //流程记录ID
int type = Integer.parseInt(reuqest.getParameter("type")); //类型
String zhz = reuqest.getParameter("zhz"); //账户组
String gysbh = reuqest.getParameter("gysbh"); //供应商编号
String sylx = reuqest.getParameter("sylx"); //使用类型
String suppliertype = reuqest.getParameter("supplier"); //使用类型
String msgs = "";
String text = "";
String cczzid = "";
String dj = "";
String zt = "";
PrintWriter out = supplierbean.getWriterOut(response);
SAPJCO sapjco = new SAPJCO();
try
{
destination = sapjco.getJcoConnection();
if(suppliertype == "L" || "L".equals(suppliertype)){
SupplierBean lsbean = (SupplierBean)supplierbean.GetLSupplier(workid);
int number = supplierbean.SelectLsfz(gysbh,lsbean.getCgzz());
if(lsbean.equals("null") || lsbean ==null){
out.print("E,没有找到信息记录不能回写SAP"); //反馈描述信息
}else{
if(zhz == "Z001" || "Z001".equals(zhz) || zhz == "Z003" || "Z003".equals(zhz)){
JCoFunction function1 = destination.getRepository().getFunction("ZMM_VENDOR_YC");

JCoTable supplier1 = function1.getTableParameterList().getTable("ZTEMP_VENDOR");
JCoTable msg1 = function1.getTableParameterList().getTable("ZMSGTAB1");
JCoParameterList paramList = function1.getImportParameterList();
supplier1.appendRow();
zt = "原厂供应商";
if(type == 0){
//新建
dj = "0";
paramList.setValue("ZBZ","I");
}else if(type == 1 && (sylx == "" || "".equals(sylx) || sylx == "0" || "0".equals(sylx))){
//修改
dj = "";
paramList.setValue("ZBZ","U");
}else if(type == 1 && (sylx == "D" || "D".equals(sylx))){
//修改冻结
dj = "D";
paramList.setValue("ZBZ","D");
}else if(type == 1 && (sylx == "J" || "J".equals(sylx))){
//修改解冻
dj = "J";
paramList.setValue("ZBZ","J");
}

if(lsbean.getGysbh() ==null || "null".equals(lsbean.getGysbh()) ){
supplier1.setValue("LIFNR", "");
}else{
String LIFNR1 = lsbean.getGysbh().trim();
char[] LIFNR = LIFNR1.toCharArray();
supplier1.setValue("LIFNR",LIFNR);
}

if(lsbean.getCgzz() ==null || "null".equals(lsbean.getCgzz()) ){
supplier1.setValue("ZEKORG", "");
}else{
cczzid = supplierbean.Selectcgzz(lsbean.getCgzz());
System.out.println("采购组织ID"+cczzid);

char[] cgzz = cczzid.toCharArray();
supplier1.setValue("ZEKORG",cgzz);
}
if(lsbean.getSkzh() ==null || "null".equals(lsbean.getSkzh())){
supplier1.setValue("ZYHZH", "");
}else{
String ZYHZH1 = lsbean.getSkzh().trim();
char[] ZYHZH = ZYHZH1.toCharArray();
supplier1.setValue("ZYHZH",ZYHZH);
}

function1.execute(destination);
String lifnr = function1.getExportParameterList().getString("LIFNR");

for (int i = 0; i < msg1.getNumRows(); ++i){
msg1.setRow(i);
msgs = msg1.getString("MSGTYP1");
text = msg1.getString("TEXT");
System.out.println("值"+msgs);
System.out.println("值2"+text);

}

if(msgs=="S" || "S".equals(msgs)){

out.print(msgs.concat(",").concat(text)); //反馈描述信息
if(number >0){
supplierbean.UpdateLsfz(lsbean,dj,zt,cczzid);
}else{
supplierbean.AddLsfz(lsbean,lifnr,userId,orgId,dj,zt,cczzid,null);
}
}else{
out.print(msgs.concat(",").concat(text)); //反馈描述信息
}
}
}
}
}catch(AbapException e)
{
destination = null;
} catch (JCoException e) {
destination = null;
e.printStackTrace();
}finally{
destination = null;
}
return null;
}

}

你可能感兴趣的:(java)