画面の作成(トランザクション)では、あらかじめ作成して保存した XMLファイルのデータを取得し、画面に表示する機能を説明しました。ただし、実際のビジネス環境では、さまざまなデータベースからデータを検索し、入力したデータをデータベースに保存するなどの複雑な業務処理が必要になります。
nexacro platformアプリケーションは、画面(Form)から入力されたデータをサーバーに送信し、データを受信するために transaction()メソッドを使用します。transaction()メソッドは、入力された変数とデータセットを XML形式に変換してサーバーに転送し、コールバック関数を使用して受信したデータを処理します。
このような過程で、クライアントによって配信した変数とデータセットを処理し、データベース内のデータを処理するためにサーバ側で動作するサービスが必要になります。サーバーに実装されたサービスでは、要求されたデータをさまざまなデータベースから取得して適切な形で加工して、クライアントに配信します。要求に問題が生じたときどのような問題なのかを確認することができるエラーコードとメッセージを返します。
サービスは、JSP、Servlet、ASP、PHPなどのサーバー環境によって、さまざまなプログラミング言語で作成することができます。この章では、簡単なJSPサービスを作成し、どのように動作するか確認します。
サービス
画面で入力されたデータを transaction()メソッドを使用してサーバーに送信して処理し、サーバーに保存されたデータを再度検索するサービスを実装します。
簡単なテストのためにデータベースに接続する代わりに、ファイル形式でサーバーに保存されます。
以下の3つのサービスを説明します。
initdata.jsp: デフォルトデータを作成し、サーバーにファイルとして保存します。
search.jsp: 保存されたファイルからデータをロードしてデータセットを作成し、クライアントに送信します。
save_list.jsp: サーバーに送信されたデータをファイルとして保存します。
X-API
配布ファイル
nexacro platform X-APIライブラリは、データ処理のためのサービスの実装時に必要な機能をライブラリの形で実装して提供しています。提供するファイルは、次のとおりです。
nexacro-xapi-1.0.jar (X-APIライブラリファイル)
commons-logging-1.1.1.jar (http://commons.apache.org/proper/commons-logging/)
nexacro14_server_license.xml (ライセンスファイル)
docs > api (X-APIマニュアル)
例では、データ処理のために nexacro platform X-APIを使用しています。X-APIは、データ処理のために必要な機能を実装したライブラリであるだけで、X-APIを必ず使用する必要はありません。
インストール
jarファイルとライセンスファイルを使用している WASと、WEB-INFフォルダの下にコピーします。
/WEB-INF/lib/nexacro-xapi-1.0.jar /WEB-INF/lib/commons-logging-1.1.1.jar /WEB-INF/lib/nexacro14_server_license.xml
ライブラリをコピーする場所は、使用しているWASの設定によって異なる場合があります。
ライブラリファイルとライセンスファイルは、同じ場所にコピーしなければ認識することができません。
正常に配布が設置されたとしたら、下のコードでインストールするかどうかを確認することができます。
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>JarInfo</title>
<style>
* { font-family: Verdana }
</style>
</head>
<body>
<pre>
<%
new com.nexacro.xapi.util.JarInfo().info(out);
%>
</pre>
</body>
</html>専用オブジェクト
接続されたライブラリで使用される専用オブジェクトは次のようです。
PlatformData : データを保存するデフォルトオブジェクト
PlatformRequest : JSP要求時に XML Format Dataをロードして客体化する Inputオブジェクト
PlatformResponse : JSP要求時に XML Format Dataを出力する outputオブジェクト
DatasetList & Dataset : データを2次元 Table形または Table Arrayタイプで保存
VariableList & Variable : I/O引数値として使用される単一値を保存
詳しくは、ライブラリに含まれた X-APIマニュアルを参照してください。
initdata.jsp
デフォルトデータを作成し、サーバーにファイルとして保存します。
pseudo code
// 1. Javaライブラリ指定(nexacro platform X-API含め)
// 2. MIMEタイプ定義
// 3. nexacro platformデフォルトオブジェクト(PlatformData)生成
try {
// 4. データ処理
// 5. ErrorCode, ErrorMsg処理(成功メッセージ)
} catch (Error) {
// 5. ErrorCode, ErrorMsg処理(失敗メッセージ)
}
// 6. 結果データをクライアントに送るコードの実装
Javaライブラリ指定
JSPサービスを作成するために、デフォルトJavaライブラリを指定します。コードは次のとおりです。
<!-- 1. library import --> <%@ page import="java.io.*" %> <%@ page import="com.nexacro.xapi.data.*" %> <%@ page import="com.nexacro.xapi.tx.*" %>
MIMEタイプ定義
XML生成のための MIME(Multipurpose Internet Mail Extensions)タイプを定義します。
<!-- 2. mime type define --> <%@ page contentType="text/xml; charset=UTF-8" %>
デフォルトオブジェクト(PlatformData)の生成
データを処理するためのデフォルトオブジェクトを宣言します。デフォルトオブジェクトである PlatformDataは nexacro platformアプリケーションで使用されるすべてのデータ(Dataset、変数)を一度に入れることができるオブジェクトです。
PlatformDataを宣言するコードは次のとおりです。
/** 3. nexacro platform Basic Object creation **/ PlatformData pdata = new PlatformData();
データセットを生成して Fileで保存
データセットを生成してカラム情報を入力し、2つの rowを作成した後、各 rowごとにカラムを入力します。生成されたデータセットは、簡単に扱うことができるように PlatformDataに登録します。
登録された PlatformDataオブジェクトを使用して"./saveFile.bin"というファイル名で保存します。データセットを生成してファイルで保存するコードは次のとおりです。
/** 4. handle data : save data to file **/
/** 4.1 create Dataset and input basic data to the Dataset **/
DataSet ds = new DataSet("customers");
ds.addColumn("id",DataTypes.STRING, 4);
ds.addColumn("name",DataTypes.STRING, 16);
...
int row = 0;
int i = 0;
String[] customers = new String[8];
customers[0] = "TC-001";
customers[1] = "Harry A. Brown";
...
for (i = 0; i < 2; i++)
{
row = ds.newRow();
ds.set(row,"id",customers[0]);
ds.set(row,"name",customers[1]);
...
}
pdata.addDataSet(ds);
/** 4.2 save Dataset to a file **/
String targetFilename = "./saveFile.bin";
OutputStream target = new FileOutputStream(targetFilename);
PlatformResponse res = new PlatformResponse(target,
PlatformType.CONTENT_TYPE_BINARY);
res.setData(pdata);
res.sendData();
target.close();ErrorCode、ErrorMsg処理
例外が発生したときに処理するための部分です。
/** 5.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
/** 5.2 set the ErrorCode and ErrorMsg about success**/
nErrorCode = 0;
strErrorMsg = "SUCC";
} catch (Throwable th) {
/** 5.3 set the ErrorCode and ErrorMsg about fail **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
}
/** 5.4 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);結果データをクライアントに送る
初期値が正常にファイルに保存されているかどうかをユーザーに提供するために PlatformDataオブジェクトを使用します。この時、前に保存した ErrorCodeと ErrorMsgが提供されます。
VariableListは PlatformDataのメンバーなので、実行した結果値は、PlatformDataオブジェクトに含まれています。これから、PlatformDataのデータを nexacro platformで処理することができる XML Formatで抽出して送信する部分を実装してみましょう。データを転送する機能を簡単に実装するために PlatformResponseオブジェクトを作成し、PlatformDataオブジェクトからデータを出力させるコードは次のとおりです。
/** 6. Send the result data to Client **/ HttpPlatformResponse res = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML,"UTF-8"); res.setData(senddata); res.sendData();
正常にデータが処理されている場合、クライアントに送信される XML値は以下のとおりです。
<Root xmlns="http://www.nexacro.com/platform/dataset" ver="5000"> <Parameters> <Parameter id="ErrorCode" type="int">0</Parameter> <Parameter id="ErrorMsg" type="string">SUCC</Parameter> </Parameters> </Root>
全体コード
<!-- 1.library import -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>
<!-- 2. mime type define -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%
/** 3. nexacro platform Basic Object creation **/
PlatformData pdata = new PlatformData();
/** 5-1. ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
/** 4. handle data : save data to file **/
/** 4.1 create Dataset and input basic data to the Dataset **/
DataSet ds = new DataSet("customers");
ds.addColumn("id",DataTypes.STRING, 4);
ds.addColumn("name",DataTypes.STRING, 16);
ds.addColumn("email", DataTypes.STRING, 32);
ds.addColumn("phone", DataTypes.STRING, 16);
ds.addColumn("comp_name", DataTypes.STRING, 32);
ds.addColumn("department", DataTypes.STRING, 32);
ds.addColumn("comp_phone", DataTypes.STRING, 16);
ds.addColumn("comp_addr", DataTypes.STRING, 256);
int row = 0;
int i = 0;
String[] customers = new String[8];
customers[0] = "TC-001";
customers[1] = "Harry A. Brown";
customers[2] = "ceo@tobesoft.com";
customers[3] = "6987-6543";
customers[4] = "TOBESOFT";
customers[5] = "0";
customers[6] = "6506-7000";
customers[7] = "Seoul";
for (i = 0; i < 1; i++)
{
row = ds.newRow();
ds.set(row,"id",customers[0]);
ds.set(row,"name",customers[1]);
ds.set(row,"email",customers[2]);
ds.set(row,"phone",customers[3]);
ds.set(row,"comp_name",customers[4]);
ds.set(row,"department",customers[5]);
ds.set(row,"comp_phone",customers[6]);
ds.set(row,"comp_addr",customers[7]);
}
pdata.addDataSet(ds);
/** 4.2 save Dataset to a file **/
String targetFilename = "./saveFile.bin";
OutputStream target = new FileOutputStream(targetFilename);
PlatformResponse res = new PlatformResponse(target,
PlatformType.CONTENT_TYPE_BINARY);
res.setData(pdata);
res.sendData();
target.close();
System.out.println("after file write..");
/** 5.2 set the ErrorCode and ErrorMsg about success**/
nErrorCode = 0;
strErrorMsg = "SUCC";
} catch (Throwable th) {
/** 5.3 set the ErrorCode and ErrorMsg about fail **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
}
/** 5.4 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
/** 6. send the result data to Client **/
HttpPlatformResponse res = new HttpPlatformResponse(response,
PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();
%>データ初期化イベント
画面の作成(トランザクション)で作成した画面にボタンコンポーネントを追加し、次のように onclickイベントを追加します。画面内のボタンをクリックすると、initdata.jspサービスが呼び出され、サーバーにデータセットが含まれているファイルを生成します。
this.btnInitdata_onclick = function(obj:Button, e:nexacro.ClickEventInfo)
{
var id = "initdata";
var url = "SvcList::initdata.jsp";
var reqDs = "";
var respDs = "";
var args = "";
var callback = "received";
this.transaction(id, url, reqDs, respDs, args, callback);
}
this.received = function(id, code, message)
{
if (code == 0) {
this.alert(message);
trace(message);
} else {
this.alert("Error["+code+"]:"+message);
trace("Error["+code+"]:"+message);
}
}urlプロパティは、完全な URLを指定することができます。毎回完全な URLを指定するのが面倒で、頻繁に使用するドメインであれば、TypeDefinitionにサービス URLを追加し、変更されたファイル名のみを指定することができます。
指定されたサービスは、次のように使用します。
var url = "[Service ID]::[file name]";
search.jsp
保存されたファイルからデータをロードしてデータセットを作成し、クライアントに送信します。
pseudo code
// 1. Javaライブラリ指定(nexacro platform X-API含め)
// 2. MIMEタイプ定義
// 3. nexacro platformデフォルトオブジェクト(PlatformData)生成
try {
// 4. データ処理
// 5. ErrorCode, ErrorMsg処理(成功メッセージ)
} catch (Error) {
// 5. ErrorCode, ErrorMsg処理(失敗メッセージ)
}
// 6. 結果データをクライアントに送るコードの実装
Javaライブラリ指定
JSPサービスを作成するために、デフォルトJavaライブラリを指定します。コードは次のとおりです。
<!-- 1. library import --> <%@ page import="java.io.*" %> <%@ page import="com.nexacro.xapi.data.*" %> <%@ page import="com.nexacro.xapi.tx.*" %>
MIMEタイプ定義
XML生成のための MIME(Multipurpose Internet Mail Extensions)タイプを定義します。
<!-- 2. mime type define --> <%@ page contentType="text/xml; charset=UTF-8" %>
デフォルトオブジェクト(PlatformData)生成
データを処理するためのデフォルトオブジェクトを宣言します。デフォルトオブジェクトである PlatformDataは nexacro platformアプリケーションで使用されるすべてのデータ(Dataset、変数)を一度に入れることができるオブジェクトです。
PlatformDataを宣言するコードは次のとおりです。
/** 3. nexacro platform Basic Object creation **/ PlatformData pdata = new PlatformData();
ファイル内容を読み取ってデータセットを生成
"./saveFile.bin"ファイルの内容を読み取って PlatformDataオブジェクトに保存します。PlatformDataオブジェクトにデータセットが含まれています。
ファイルの内容を読み取って PlatformDataに入れるコードは次のとおりです。
/** 4. handle data : load data from file **/ /** 4.1 load data from file **/ String sourceFilename = "./saveFile.bin"; InputStream source = new FileInputStream(sourceFilename); PlatformRequest req = new PlatformRequest(source, PlatformType.CONTENT_TYPE_BINARY); req.receiveData(); source.close(); /** 4.2 copy data from loaded data to Dataset **/ pdata = req.getData();
ErrorCode、ErrorMsg処理
例外が発生したときに処理するための部分です。
/** 5.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
/** 5.2 set the ErrorCode and ErrorMsg about success**/
nErrorCode = 0;
strErrorMsg = "SUCC";
} catch (Throwable th) {
/** 5.3 set the ErrorCode and ErrorMsg about fail **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
}
/** 5.4 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData pdata = new PlatformData();
VariableList varList = pdata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);結果データをクライアントに送る
初期値が正常にファイルに保存されているかどうかをユーザーに提供するために PlatformDataオブジェクトを使用します。この時、前に保存した ErrorCodeと ErrorMsgが提供されます。
initdata.jspで PlatformDataオブジェクトは ErrorCodeと ErrorMsgのみ持っていますが、search.jspでは、PlatformDataにクライアントが使用する名刺リストデータセットを含んでいます。
VariableListは PlatformDataのメンバーなので、実行した結果値は、PlatformDataオブジェクトに含まれています。これから、PlatformDataのデータを nexacro platformで処理することができる XML Formatで抽出して送信する部分を実装してみましょう。データを転送する機能を簡単に実装するために PlatformResponseオブジェクトを作成し、PlatformDataオブジェクトからデータを出力させるコードは次のとおりです。
/** 6. Send the result data to Client **/ HttpPlatformResponse res = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML,"UTF-8"); res.setData(pdata); res.sendData();
正常にデータが処理されている場合、クライアントに送信される XML値は以下のとおりです。
<Root xmlns="http://www.nexacro.com/platform/dataset" ver="5000"> <Parameters> <Parameter id="ErrorCode" type="int">0</Parameter> <Parameter id="ErrorMsg" type="string">SUCC</Parameter> </Parameters> <Dataset id="customers"> <ColumnInfo> <Column id="id" type="string" size="4"/> <Column id="name" type="string" size="16"/> <Column id="email" type="string" size="32"/> <Column id="phone" type="string" size="16"/> <Column id="comp_name" type="string" size="32"/> <Column id="department" type="string" size="32"/> <Column id="comp_phone" type="string" size="16"/> <Column id="comp_addr" type="string" size="256"/> </ColumnInfo> <Rows> <Row> <Col id="id">TC-001</Col> <Col id="name">Harry A. Brown</Col> <Col id="email">ceo@tobesoft.com</Col> <Col id="phone">6987-6543</Col> <Col id="comp_name">TOBESOFT</Col> <Col id="department">0</Col> <Col id="comp_phone">6506-7000</Col> <Col id="comp_addr">Seoul</Col> </Row> </Rows> </Dataset> </Root>
全体コード
<!-- 1.library import -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>
<!-- 2. mime type define -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%
/** 3. nexacro platform Basic Object creation **/
PlatformData pdata = new PlatformData();
/** 5.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
/** 4. handle data : load data from file **/
/** 4.1 load data from file **/
String sourceFilename = "./saveFile.bin";;
InputStream source = new FileInputStream(sourceFilename);
PlatformRequest req = new PlatformRequest(source,
PlatformType.CONTENT_TYPE_BINARY);
req.receiveData();
source.close();
/** 4.2 copy data from loaded data to Dataset **/
pdata = req.getData();
/** 5.2 set the ErrorCode and ErrorMsg about success**/
nErrorCode = 0;
strErrorMsg = "SUCC";
} catch (Throwable th) {
/** 5.3 set the ErrorCode and ErrorMsg about fail **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
}
/** 5.4 save the ErrorCode and ErrorMsg for sending Client **/
VariableList varList = pdata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
/** 6. send the result data to Client **/
HttpPlatformResponse res = new HttpPlatformResponse(response,
PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(pdata);
res.sendData();
%>データ検索イベント
画面の作成(トランザクション)で作成した画面で検索ボタンに指定された onclickイベントを修正します。画面内のボタンをクリックすると search.jspサービスが呼び出されながらサーバーに保存されたファイルをロードしてデータセットを返します。
サーバーから送信されるデータセットの情報は、画面内作成された dsCustomersデータセットに含まれて、該当データをグリッドに表示します。
this.divCommand_btnSearch_onclick = function(obj:Button, e:nexacro.ClickEventInfo)
{
var id = "search";
var url = "SvcList::search.jsp";
var reqDs = "";
var respDs = "dsCustomers=customers";
var args = "";
var callback = "received";
this.transaction(id, url, reqDs, respDs, args, callback);
}
this.received = function(id, code, message)
{
if (code == 0) {
var rowcount = this.dsCustomers.rowcount;
this.alert(rowcount + " numbers of data have been found.");
trace(rowcount + " numbers of data have been found.");
} else {
this.alert("Error["+code+"]:"+message);
trace("Error["+code+"]:"+message);
}
}データベース接続
例では、ファイルに保存されたデータセットを取得、そのままクライアントに転送します。ファイルではなく、データベースに接続した場合には、次のように適用することができます。
<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import = "java.io.*" %>
<%@ page contentType="text/xml; charset=utf-8" %>
<%
/****** Service API initialization ******/
PlatformData pdata = new PlatformData();
int nErrorCode = 0;
String strErrorMsg = "START";
/******* JDBC Connection *******/
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;;DatabaseName=Sample",
"guest","guest");
stmt = conn.createStatement();
try {
/******* SQL query *************/
String SQL = "select * from sample_customers_list";
rs = stmt.executeQuery(SQL);
/********* Dataset Create ************/
DataSet ds = new DataSet("customers");
ds.addColumn("id",DataTypes.STRING, 4);
ds.addColumn("name",DataTypes.STRING, 16);
ds.addColumn("email", DataTypes.STRING, 32);
ds.addColumn("phone", DataTypes.STRING, 16);
ds.addColumn("comp_name", DataTypes.STRING, 32);
ds.addColumn("department", DataTypes.STRING, 32);
ds.addColumn("comp_phone", DataTypes.STRING, 16);
ds.addColumn("comp_addr", DataTypes.STRING, 256);
int row = 0;
while(rs.next())
{
row = ds.newRow();
ds.set(row, "id", rs.getString("id"));
ds.set(row, "name", rs.getString("name"));
ds.set(row, "email", rs.getString("email"));
ds.set(row, "phone", rs.getString("phone"));
ds.set(row, "comp_name", rs.getString("comp_name"));
ds.set(row, "department", rs.getString("department"));
ds.set(row, "comp_phone", rs.getString("comp_phone"));
ds.set(row, "comp_addr", rs.getString("comp_addr"));
}
/********* Adding Dataset to PlatformData ************/
pdata.addDataSet(ds);
nErrorCode = 0;
strErrorMsg = "SUCC";
}
catch(SQLException e) {
nErrorCode = -1;
strErrorMsg = e.getMessage();
}
/******** JDBC Close *******/
if ( stmt != null ) try { stmt.close(); } catch (Exception e) {}
if ( conn != null ) try { conn.close(); } catch (Exception e) {}
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
/******** XML data Create ******/
HttpPlatformResponse res = new HttpPlatformResponse(response,
PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(pdata);
res.sendData();
%>save_list.jsp
サーバーに送信されたデータをファイルとして保存します。
pseudo code
initdata.jsp, search.jspとは異なる「クライアント要請」という手順が追加されました。
// 1. Javaライブラリ指定(nexacro platform X-API含め)
// 2. MIMEタイプ定義
// 3. nexacro platformデフォルトオブジェクト(PlatformData)生成
try {
// 4. クライアント要請
// 5. データ処理
// 6. ErrorCode, ErrorMsg処理(成功メッセージ)
} catch (Error) {
// 6. ErrorCode, ErrorMsg処理(失敗メッセージ)
}
// 6. 結果データをクライアントに送るコードの実装
Javaライブラリ指定
JSPサービスを作成するために、デフォルトJavaライブラリを指定します。コードは次のとおりです。
<!-- 1. library import --> <%@ page import="java.io.*" %> <%@ page import="com.nexacro.xapi.data.*" %> <%@ page import="com.nexacro.xapi.tx.*" %>
MIMEタイプ定義
XML生成のための MIME(Multipurpose Internet Mail Extensions)タイプを定義します。
<!-- 2. mime type define --> <%@ page contentType="text/xml; charset=UTF-8" %>
デフォルトオブジェクト(PlatformData)生成
データを処理するためのデフォルトオブジェクトを宣言します。デフォルトオブジェクトである PlatformDataは nexacro platformアプリケーションで使用されるすべてのデータ(Dataset、変数)を一度に入れることができるオブジェクトです。
PlatformDataを宣言するコードは次のとおりです。
/** 3. nexacro platform Basic Object creation **/ PlatformData pdata = new PlatformData();
クライアント要請
クライアントがパラメータとして送信されたデータセットを受信して処理します。
/** 4. receive client request **/ // create HttpPlatformRequest for receive data from client HttpPlatformRequest req = new HttpPlatformRequest(request); req.receiveData();
クライアントが送信したデータを抽出した後、ファイルに保存する
クライアントが送信した情報を処理することができるように PlatformDataに変換した後、データセットを抽出します。生成された PlatformDataは「./saveFile.bin」 ファイルで保存します。
/** 5. handle data : load data from file **/
/** 5.1 load data from http object **/
pdata = req.getData();
/** get Dataset from received data **/
DataSet ds = pdata.getDataSet("dsCustomers");
/** save data to file with init data **/
String targetFilename = "./saveFile.bin";
OutputStream target = new FileOutputStream(targetFilename);
PlatformResponse res = new PlatformResponse(target,
PlatformType.CONTENT_TYPE_BINARY);
res.setData(pdata);
res.sendData();
target.close();ErrorCode、ErrorMsg処理
例外が発生したときに処理するための部分です。正常に処理した場合には、正常に保存されたメッセージを返します。
/** 6.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
/** 6.2 set the ErrorCode and ErrorMsg about success**/
nErrorCode = 0;
strErrorMsg = "person list saved complete : row count("+ds.getRowCount()+")";
} catch (Throwable th) {
/** 6.3 set the ErrorCode and ErrorMsg about fail **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
}
/** 6.4 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);結果データをクライアントに送る
初期値が正常にファイルに保存されているかどうかをユーザーに提供するために PlatformDataオブジェクトを使用します。この時、前に保存した ErrorCodeと ErrorMsgが提供されます。
VariableListは PlatformDataのメンバーなので、実行した結果値は、PlatformDataオブジェクトに含まれています。これから、PlatformDataのデータを nexacro platformで処理することができる XML Formatで抽出して送信する部分を実装してみましょう。データを転送する機能を簡単に実装するために PlatformResponseオブジェクトを作成し、PlatformDataオブジェクトからデータを出力させるコードは次のとおりです。
/** 7. Send the result data to Client **/ HttpPlatformResponse res = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML,"UTF-8"); res.setData(senddata); res.sendData();
全体コード
<!-- 1. library import -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>
<!-- 2. mime type define -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%
/** 3. nexacro platform Basic Object creation **/
PlatformData pdata = new PlatformData();
/** 6.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
/** 4. receive client request **/
// create HttpPlatformRequest for receive data from client
HttpPlatformRequest req = new HttpPlatformRequest(request);
req.receiveData();
/** 5. handle data : load data from file **/
/** 5.1 load data from http object **/
pdata = req.getData();
/** get Dataset from received data **/
DataSet ds = pdata.getDataSet("dsCustomers");
/** save data to file with init data **/
String targetFilename = "./saveFile.bin";
OutputStream target = new FileOutputStream(targetFilename);
PlatformResponse res = new PlatformResponse(target,
PlatformType.CONTENT_TYPE_BINARY);
res.setData(pdata);
res.sendData();
target.close();
/** 6.2 set the ErrorCode and ErrorMsg about success **/
nErrorCode = 0;
strErrorMsg = "person list saved complete : row count("+ds.getRowCount()+")";
} catch (Throwable th) {
/** 6.3 set the ErrorCode and ErrorMsg about fail **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
System.out.println("ERROR:"+strErrorMsg);
}
/** 6.4 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
/** 7. send the result data to Client **/
HttpPlatformResponse res = new HttpPlatformResponse(response,
PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();
%>データ保存イベント
画面の作成(トランザクション)で作成した画面にボタンコンポーネントを追加し、次のように onclickイベントを追加します。画面内のボタンをクリックすると save_list.jspサービスが呼び出されながら画面で修正したデータセットをサーバーに送信して再びファイルに保存します。
データを変更することができる機能を簡単に追加します。画面内のグリッドをダブルクリックして、Grid Contents Editorを実行した後、Name項目を選択して edittypeプロパティを「text」に変更します。データ検索後、該当項目をダブルクリックすると、データを変更することができます。
修正したデータを保存した後、画面を更新し検索ボタンをクリックすると、変更されたデータが検索されることを確認することができます。
this.btnSaveList_onclick = function(obj:Button, e:nexacro.ClickEventInfo)
{
var id = "search";
var url = "SvcList::save_list.jsp";
var reqDs = "customers=dsCustomers";
var respDs = "";
var args = "";
var callback = "received";
this.transaction(id, url, reqDs, respDs, args, callback);
}
this.received = function(id, code, message)
{
if (code == 0) {
this.alert(message);
trace(message);
} else {
this.alert("Error["+code+"]:"+message);
trace("Error["+code+"]:"+message);
}
}