Creating Data Transaction Service using X-API

Creating a Data Transactions Screen handled XML file data that is already written and stored. In actual business environments, however, data transaction usually involves more complicated tasks such as querying multiple databases and saving input data into databases.

Nexacro Platform applications employ the transaction() method to transmit inputs from a form to a server and to receive data from a server. The method transforms input variables and datasets into an XML format to transmit them to a server. The method uses a callback to process data from a server.

The foregoing course of operation requires services on the server side to handle variables and datasets sent from clients and manipulate data in databases. The services on the server side fetch data at the request of clients and send the data to them after processing it appropriately. If there are problems for client requests, the services will return error codes and messages, which will help clients figure out the natures of the problems.

Those services can be written with various programming languages such as JSP, Servlet, ASP, and PHP according to server environments. This chapter will demonstrate how to make simple services with JSP and how they work.

Service

You will be taught how to develop a group of services for using the transaction() method to deliver input data from a form to a server and to retrieve data stored in the server.

You will try to retrieve the data stored in the server instead of accessing to a database for the sake of simplicity.

The following three services will be explained.

X-API

Provided Files

The Nexacro Platform X-API library provides functions necessary for developing services for data processing. The provided files are as below.

The X-APIs of Nexacro Platform, used for the example in this manual, are just a library that contains functions necessary for data processing. Therefore, they are not a must-use.

Installation

Copy the two JAR files and a license file to the WEB-INF folder of your web application server (WAS).

/WEB-INF/lib/nexacro-xapi-1.0.jar
/WEB-INF/lib/commons-logging-1.1.1.jar
/WEB-INF/lib/nexacro14_server_license.xml

The location for copying the library can vary according to the settings of your WAS.

Library files and license file must be in the same location for the identification of those files.

You can verify whether the installation has been successful with the below code.

<%@ 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>

Objects

The followings are objects included in the X-API library.

See the X-API manual included in the library for further explanations

initdata.jsp

This is a service to create basic data and save it to the server in the form of a file.

pseudo code

// 1. Designating a Java library (including nexacro platform X-API)
// 2. Defining a MIME type
// 3. Creating a basic object of Nexacro Platform (PlatformData)
try {
	// 4. Processing data
	// 5. Processing ErrorCode and ErrorMsg (success message)
} catch (Error) {
	// 5. Processing ErrorCode and ErrorMsg (failure message)
}
// 6. Sending result data to the client

Coding

Designating a Java library

Designate a basic Java library to write a JSP service. The necessary code is as below.

<!-- 1. Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>

Defining a MIME type

Define a MIME (Multipurpose Internet Mail Extensions) type for creating a XML file.

<!-- 2. Defining a MIME type -->
<%@ page contentType="text/xml; charset=UTF-8" %>

Creating a basic object of Nexacro Platform (PlatformData)

Declare PlatformData as a basic object for data processing. PlatformData is an object that can store all types of data used in Nexacro Platform's applications such as datasets and variables.

Write the below code to declare PlatformData.

/** 3. Creating a basic object of Nexacro Platform **/
PlatformData pdata = new PlatformData();

Processing data: saving data as a file

Create a dataset, enter information about the column, create two rows, and input column values to each row. Register the dataset in PlatformData for easy handling of the dataset.

Save PlatformData under the name saveFile.bin. The below code is for creating a dataset and saving it as a file.

/** 4. Processing data: saving data as a file **/
/** 4.1 Creating a dataset and inputting 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 Saving a dataset as 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();

Processing ErrorCode and ErrorMsg

The below code is for handling exceptions.

/** 5.1 Processing ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
	/** 5.2 Setting ErrorCode and ErrorMsg for success **/
	nErrorCode = 0;
	strErrorMsg = "SUCC";
} catch (Throwable th) {
	/** 5.3 Setting ErrorCode and ErrorMsg for failure **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}

/** 5.4 Saving ErrorCode and ErrorMsg to send them to the client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

Sending result data to the client

Use PlatformData to let the client know whether the initial data has been successfully saved. For here, the previously-saved ErrorCode and ErrorMsg will be delivered.

Since VariableList is a member of PlatformData, the result of processing ErrorCode is included in PlatformData. Now, you will learn how to extract XML data from PlatformData and send it to the client as Nexacro Platform can handle XML format. First, create the object PlatformResponse to facilitate data transfer. Then, write the below code to extract data from PlatformData.

/** 6. Sending result data to the client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, 
	PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();

If the data is successfully processed, the client will receive the below XML data.

<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>

Overall Code

<!-- 1.Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>

<!-- 2. Defining a MIME type -->
<%@ page contentType="text/xml; charset=UTF-8" %>

<%
/** 3. Creating a basic object of Nexacro Platform **/
PlatformData pdata = new PlatformData();

/** 5-1. Processing ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";

try {
    /** 4. Processing data: saving data as a file **/
    /** 4.1 Creating a dataset and inputting 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 Saving a 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 Setting ErrorCode and ErrorMsg for success**/
    nErrorCode = 0;
    strErrorMsg = "SUCC";
    
} catch (Throwable th) {
    /** 5.3 Setting ErrorCode and ErrorMsg for failure **/
    nErrorCode = -1;
    strErrorMsg = th.getMessage();
}

/** 5.4 Saving the ErrorCode and ErrorMsg to send them to the client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
    
/** 6. Sending result data to the client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, 
	PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();
%>

Data Initialization Event

Add a button component to the screen developed in the Chapter 6. Creating a Data Transactions Screen and then add the onclick event as seen in the below code. Clicking the button will call up the initdata.jsp service, creating a file that contains a dataset in the server.

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);
     }
}

Although you can enter a full URL whenever you need to specify the url property, you can make the process simple by adding frequently-used domains to TypeDefinition as service URLs. Then, you just need to specify a file name without having to write a long URL.

The following is how to use the specified service.

var url = "[Service ID]::[file name]";

search.jsp

This is a service to create a dataset by reading data from the saved file and then sends the dataset to a client.

pseudo code

// 1. Designating a Java library (including nexacro platform X-API)
// 2. Defining a MIME type
// 3. Creating a basic object of Nexacro Platform (PlatformData)
try {
	// 4. Processing data
	// 5. Processing ErrorCode and ErrorMsg (success message)
} catch (Error) {
	// 5. Processing ErrorCode and ErrorMsg (failure message)
}
// 6. Sending result data to the client

Coding

Designating a Java library

Designate a basic Java library to write a JSP service. The necessary code is as below.

<!-- 1. Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>

Defining a MIME type

Define a MIME (Multipurpose Internet Mail Extensions) type for creating a XML file.

<!-- 2. Defining a MIME type -->
<%@ page contentType="text/xml; charset=UTF-8" %>

Creating a basic object of Nexacro Platform (PlatformData)

Declare PlatformData as a basic object for data processing. PlatformData is an object that can store all types of data used in Nexacro Platform's applications such as datasets and variables.

Write the below code to declare PlatformData.

/** 3. Creating a basic object of Nexacro Platform **/
PlatformData pdata = new PlatformData();

Processing data: Loading data from the file

Load data from the saveFile.bin file and save it to PlatformData. The basic object contains the dataset.

The below code is for reading data from the file and saving it into PlatformData.

/** 4. Processing data: Loading data from the file: loading data from the file **/
/** 4.1 Loading data from the 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 Copying the loaded data to the dataset **/
pdata = req.getData();

Processing ErrorCode and ErrorMsg

The below code is for handling exceptions.

/** 5.1 Processing ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
	/** 5.2 Setting ErrorCode and ErrorMsg for success**/
	nErrorCode = 0;
	strErrorMsg = "SUCC";
} catch (Throwable th) {
	/** 5.3 Setting ErrorCode and ErrorMsg for failure **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}

/** 5.4 Saving ErrorCode and ErrorMsg to send them to the client **/
PlatformData pdata = new PlatformData();
VariableList varList = pdata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

Sending result data to the client

Use PlatformData to let the client know whether the initial data has been successfully saved. For here, the previously-saved ErrorCode and ErrorMsg will be delivered.

While the PlatformData object in initdata.jsp only contains ErrorCode and ErrorMsg, the PlatformData in search.jsp also includes a dataset containing the name list that the client uses.

Since VariableList is a member of PlatformData, the result of processing ErrorCode is included in PlatformData. Now, you will learn how to extract XML data from PlatformData and send it to the client as Nexacro Platform can handle XML format. First, create the object PlatformResponse to facilitate data transfer. Then, write the below code to extract data from PlatformData.

/** 6. Sending result data to the client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, 
	PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(pdata);
res.sendData();

If the data is successfully processed, the client will receive the below XML data.

<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>

Overall Code

<!-- 1.Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>

<!-- 2. Defining a MIME type -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%
/** 3. Creating a basic object of Nexacro Platform **/
PlatformData pdata = new PlatformData();

/** 5.1 Processing ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";

try {
	/** 4. Processing data : Loading data from the file: loading data from the file **/
	/** 4.1 Loading data from the 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 Copying the loaded data to the dataset **/
	pdata = req.getData();

	/** 5.2 Setting ErrorCode and ErrorMsg for success **/
	nErrorCode = 0;
	strErrorMsg = "SUCC";

} catch (Throwable th) {
	/** 5.3 Setting ErrorCode and ErrorMsg for failure **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}

/** 5.4 Saving ErrorCode and ErrorMsg to send them to the client **/
VariableList varList = pdata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

/** 6. Sending result data to the client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, 
	PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(pdata);
res.sendData();
%>

Data Search Event

Modify the onclick event applied to the Search button in the screen developed in the Chapter 6. Creating a Data Transactions Screen. Clicking the button will call up the search.jsp service, reading the file saved in the server and returning a dataset.

The information on the dataset sent from the server will be contained by the dsCustomers dataset written within the developed screen before being output through the relevant grid.

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);
     }
}

Connecting to a Database

In the example presented in this manual, you just brought a dataset file already saved in the server and sent it to a client. In contrast, the below code shows how to connect to a database instead of just accessing a file.

<%@ 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

This is a service to modify the file according to the data sent from a client.

pseudo code

Unlike initdata.jsp and search.jsp, there is a step called "Receiving a request from the client."

// 1. Designating a Java library (including nexacro platform X-API)
// 2. Defining a MIME type
// 3. Creating a basic object of Nexacro Platform (PlatformData)
try {
	// 4. Receiving a request from the client
	// 5. Processing data
	// 6. Processing ErrorCode and ErrorMsg (success message)
} catch (Error) {
	// 6. Processing ErrorCode and ErrorMsg (failure message)
}
// 7. Sending result data to the client

Coding

Designating a Java library

Designate a basic Java library to write a JSP service. The necessary code is as below.

<!-- 1. Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>

Defining a MIME type

Define a MIME (Multipurpose Internet Mail Extensions) type for creating a XML file.

<!-- 2. Defining a MIME type -->
<%@ page contentType="text/xml; charset=UTF-8" %>

Creating a basic object of Nexacro Platform (PlatformData)

Declare PlatformData as a basic object for data processing. PlatformData is an object that can store all types of data used in Nexacro Platform's applications such as datasets and variables.

Write the below code to declare PlatformData.

/** 3. Creating a basic object of Nexacro Platform **/
PlatformData pdata = new PlatformData();

Receiving a request from the client

The server handles the dataset sent from the client as a parameter.

/** 4. Receiving a request from the client **/
// create HttpPlatformRequest for receive data from client
HttpPlatformRequest req = new HttpPlatformRequest(request);
req.receiveData();

Extracting and saving the data sent by the client

Transform the data sent by the client into PlatformData and extract the dataset from the basic object to allow the server to handle the data. Save the created PlatformData as a file under the name saveFile.bin.

/** 5. Processing data: Loading data from the file **/
/** 5.1 Loading data from the http object **/ 
pdata = req.getData();

/** Obtaining a dataset from the received data **/
DataSet ds = pdata.getDataSet("dsCustomers");

/** Saving data as a 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();

Processing ErrorCode and ErrorMsg

The below code is for handling exceptions. If the data processing is successful, the server will return a message that data has been successfully saved.

/** 6.1 Processing ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
	/** 6.2 Setting ErrorCode and ErrorMsg for success **/
	nErrorCode = 0;
	strErrorMsg = "person list saved complete : row count("+ds.getRowCount()+")";
} catch (Throwable th) {
	/** 6.3 Setting ErrorCode and ErrorMsg for failure **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}

/** 6.4 Saving ErrorCode and ErrorMsg to send them to the client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

Sending result data to the client

Use PlatformData to let the client know whether the initial data has been successfully saved. For here, the previously-saved ErrorCode and ErrorMsg will be delivered.

Since VariableList is a member of PlatformData, the result of processing ErrorCode is included in PlatformData. Now, you will learn how to extract XML data from PlatformData and send it to the client as Nexacro Platform can handle XML format. First, create the object PlatformResponse to facilitate data transfer. Then, write the below code to extract data from PlatformData.

/** 7. Sending result data to the client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, 
	PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();

Overall Code

<!-- 1. Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro.xapi.data.*" %>
<%@ page import="com.nexacro.xapi.tx.*" %>

<!-- 2. Defining a MIME type -->
<%@ page contentType="text/xml; charset=UTF-8" %>

<%
/** 3. Creating a basic object of Nexacro Platform **/
PlatformData pdata = new PlatformData();

/** 6.1 Processing ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";

try {
	/** 4. Receiving a request from the client **/
	// create HttpPlatformRequest for receive data from client
	HttpPlatformRequest req = new HttpPlatformRequest(request);
	req.receiveData();
	/** 5. Processing data: Loading data from the file **/
	/** 5.1 Loading data from the http object **/ 
	pdata = req.getData();

	/** Obtaining a dataset from the received data **/
	DataSet ds = pdata.getDataSet("dsCustomers");

	/** Saving data as a 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 Setting ErrorCode and ErrorMsg for success **/
	nErrorCode = 0;
	strErrorMsg = "person list saved complete : row count("+ds.getRowCount()+")";
	
} catch (Throwable th) {
	/** 6.3 Setting ErrorCode and ErrorMsg for failure **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
	System.out.println("ERROR:"+strErrorMsg); 
}

/** 6.4 Saving ErrorCode and ErrorMsg to send them to the client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

/** 7. Sending result data to the client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, 
	PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();
%>

Data Saving Event

Add a button component to the screen developed in the Chapter 6. Creating a Data Transactions Screen and then add the onclick event as seen in the below code. Clicking the button will call up the save_list.jsp service, sending the dataset modified in the screen to the server and saving it in the server as a file.

You can add a simple function that can modify data. Double-click the grid in the screen to open the Grid Contents Editor. Select the Name cell and change the edittype property to text.

You can change the data by double-clicking a cell where a name is written.

Click the Save button to transmit data to the server.

Click the Search button after refreshing the page, and you will see the data on the person that you intended to search for.

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);
     }
}