SAP Server Side JavaScript解决方案
服务器端的JavaScript, 看下wikipedia的介绍:
https://en.wikipedia.org/wiki/JavaScript#Server-side_JavaScript
Server-side JavaScript
In December 1995, soon after releasing JavaScript for browsers, Netscape introduced an implementation of the language for server-side scripting with Netscape Enterprise Server.
Since 1996, the IIS web-server has supported Microsoft’s implementation of server-side Javascript – JScript – in ASP and .NET pages.
Since the mid-2000s, additional server-side JavaScript implementations have been introduced, such as Node.js in 2009.
再看SAP的Server Side(服务器端) JavaScript解决方案:SAP Extended Application Service
创建一个新的package:
创建一个新的Application:
创建一个新的文件test.xsjs, 内容如下:
var userInput = $.request.parameters.get("userStuff");
$.response.contentType = "text/html";
$.response.setBody(userInput);
这个hello world应用是一个简单的echo应用:将用户通过url传进来的数据直接输出。
测试:
我的ABAP系统有个函数名叫ZDIS_GET_UPSELL_MATERIALS,输入一个customer ID和product ID,会输出为这对客户和product组合维护的一组Upsell product ID和描述信息。
测试如下:
下面是使用Java消费该函数的代码:
package jco;
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.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRepository;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;
/**
* basic examples for Java to ABAP communication
* See help: https://help.sap.com/saphelp_nwpi711/helpdata/en/48/70792c872c1b5ae10000000a42189c/frameset.htm
*/
public class StepByStepClient
{
static String DESTINATION_NAME = "ABAP_AS_WITHOUT_POOL";
static public final String ABAP_DURATION = "abapLayerDuration";
static public final String UPSELL_PRODUCT = "upsellProducts";
static public final String PRODUCT_ID = "productID";
static public final String PRODUCT_TEXT = "productText";
static private Properties prepareProperty(){
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "your abap system host name");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "111");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "WANGJER");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "your password");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
createDestinationDataFile(DESTINATION_NAME, connectProperties);
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");
createDestinationDataFile(DESTINATION_NAME, connectProperties);
return connectProperties;
}
static public void main(String[] arg) {
createDestinationDataFile(DESTINATION_NAME, prepareProperty());
JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
JCoRepository repo = destination.getRepository();
JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_UPSELL_MATERIALS");
JCoParameterList imports = stfcConnection.getImportParameterList();
String customerID = "1000040";
String materialID = "11";
imports.setValue("IV_CUSTOMER_ID", customerID);
imports.setValue("IV_MATERIAL_ID", materialID);
stfcConnection.execute(destination);
JCoParameterList exports = stfcConnection.getExportParameterList();
// int result = exports.getInt("EV_RESULT");
int abapDuration = exports.getInt("EV_DURATION");
StringBuilder sb = new StringBuilder();
sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ",");
sb.append("\"" + UPSELL_PRODUCT + "\":[");
JCoTable codes = exports.getTable("ET_MATERIALS");
int row = codes.getNumRows();
System.out.println("Total rows: " + row);
System.out.println("ABAP duration: " + abapDuration);
for( int i = 0; i < row; i++){
codes.setRow(i);
System.out.println(codes.getString("MATERIAL_ID") + '\t' + codes.getString("MATERIAL_TEXT"));
sb.append("{\"" + PRODUCT_ID + "\":" + codes.getString("MATERIAL_ID") + ","
+ "\"" + PRODUCT_TEXT + "\":\"" + codes.getString("MATERIAL_TEXT") + "\"");
if( i < row - 1){
sb.append("},");
}
else{
sb.append("}");
}
}
sb.append("]}");
System.out.println("Final json: " + sb.toString());
} catch (JCoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static private void createDestinationDataFile(String destinationName, Properties connectProperties)
{
File destCfg = new File(destinationName+".jcoDestination");
try
{
FileOutputStream fos = new FileOutputStream(destCfg, false);
connectProperties.store(fos, "for tests only !");
fos.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to create the destination files", e);
}
}
}
为简单起见没有使用Google的gson库进行Json的序列化。
执行结果:
DAO在Hybris里的定义:
A DAO (Data Access Object) is an interface to the storage back end system. DAOs store and retrieve objects. You use DAOs to save, remove, and find models. DAOs are the place to put SQL or FlexibleSearch statements and nowhere else. This is to ensure further decoupling from the underlying storage facility. DAOs interact with services via models and with the database via FlexibleSearch and SQL statements.
确实这是一个common的concept, 就是对持久层的CRUD进行封装,以实现上一层对持久层具体实现的解耦。
随便抽一个ABAP的DAO实现:
Hybris里所有DAO实现的super class是hybris标准的框架DAO, 定义在如下namespace里. 讨论都是一个DAO作为interface定义操作,
另一个default开头的DAO提供default实现: 拼SQL然后call flexibile search.
这里的flexible search就相当于ABAP的OPEN SQL, 把DB的access同具体的DB provider解耦。
runtime时具体用哪个DAO的实现,是在Spring的配置文件里注入的:
- 点赞
- 收藏
- 关注作者
评论(0)