Creating Data Transaction Service using X-API

The sample presented in this chapter cannot be executed with a local web server. You should set Tomcat or another application server.

Refer to Installing Apache Tomcat for the configuration of a Tomcat server.

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-based apps 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.

The extension .jsp stands for JavaServer Page, which is a technology that helps developers create dynamically generated web pages by inserting Java code into HTML code. While basic structure of a screen is developed with HTML, Java code is used to receive data from a database and perform calculation.

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

You should install Tomcat and create a context file used in this example. Please refer to Installing Apache Tomcat to learn in detail how to install Tomcat and create a context file.

1

As for the context file, create the CustomerList.xml file in the path presented below.

C:\Program Files\Apache Software Foundation\Tomcat 8.5\conf\Catalina\localhost
$r_title(CustomerList.xml)

<Context path="/CustomerList" docBase="E:\88_TEST\02_BUILD\CustomerList"
  debug="0" prvileged="true" reloadable="true">
  <Logger className="org.apache.catalina.logger.FileLogger"
    directory="logs" prefix="localhost_log." suffix=".txt"
    timestame="true"/>
</Context>

2

Download the X-API file and unzip it. As you find two folders—docs and lib—after the decompression, we will use the two .jar files from the lib folder.

3

Create a folder that has been designated as docBase and create the WEB-INF folder inside it. Then, again, create the lib folder under the WEB-INF folder and copy the JAR files and license file to lib.

E:\88_TEST\02_BUILD\CustomerList\WEB-INF\lib

4

Create a .jsp file for a test in the folder designated as docBase.

E:\88_TEST\02_BUILD\CustomerList

$r_title(test.jsp)

<%@ page contentType="text/html; charset=UTF-8" %>

<html>
	<head>
	<title>JarInfo</title>
	<style>
		* { font-family: Verdana }
	</style>
	</head>
	<body>
		<pre>
<%
	new com.nexacro17.xapi.util.JarInfo().info(out);
%>
		</pre>
	</body>
</html>

5

Check whether the information on X-API is shown properly when you access the below address through a web browser. If you see the information, it proves that X-API has been installed successfully.

http://localhost:8080/CustomerList/test.jsp

WEB-INF is an acronym for Web information. This directory cannot be accessed by users although it is located within a context root directory. It contains information for the system to refer to internally.

The lib directory is a space for storing JAR files. JAR is an acronym for Java Archive, and a JAR file functions as a library to aggregate many Java class files and associated resources.

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

You need to restart your application server after copying library files.

You may not need the restart depending on the settings of the application server.

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

Objects

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

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

Add Buttons

Add Button components to the existing screen for saving and initializing data.

Components

Properties

Values

Descriptions

1 Button

id

btnSaveList

text

Save

A string written on the Button

2 Button

id

btnInitdata


text

Initdata

A string written on the Button

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-based apps 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] = "Dustin Kim";
...

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

$r_title(initdata.jsp)

<!-- 1.Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro17.xapi.data.*" %>
<%@ page import="com.nexacro17.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] = "Tzuyu";
    customers[2] = "ceo@twice.com";
    customers[3] = "6987-6543";
    customers[4] = "TWICE";
    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.divCommand_btnInitdata_onclick = function(obj:nexacro.Button,  e:nexacro.ClickEventInfo)
{
     var id = "initdata";  
     var url = "SvcList::initdata.jsp";
     var reqDs = "";
     var respDs = "";
     var args = "";
     var callback = "initdata_received";

     this.transaction(id, url, reqDs, respDs, args, callback);	
}

this.initdata_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]";

Saved File

If you call the initdata.jsp file by clicking the button, the saveFile.bin file will be created under the folder where an application server is installed (Apache Tomcat, in this sample).

If you want to save the file in a certain path, modify the initdata.jsp file as presented below.

//String targetFilename = "./saveFile.bin";
File targetFile = new File("c:/saveFile.bin");
OutputStream target = new FileOutputStream(targetFile);

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-based apps 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">Dustin Kim</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

$r_title(search.jsp)

<!-- 1.Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro17.xapi.data.*" %>
<%@ page import="com.nexacro17.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:nexacro.Button,  e:nexacro.ClickEventInfo)
{
     var id = "search";  
     var url = "SvcList::search.jsp";
     var reqDs = "";
     var respDs = "dsCustomers=customers";
     var args = "";
     var callback = "search_received";

     this.transaction(id, url, reqDs, respDs, args, callback);	
}

this.search_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.

The below code, written for the connection with a database, is presented just as a sample. You need to modify the code when you apply it to your real operating environment.

$r_title(search.jsp (JDBC Connect))
<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.io.*" %>
<%@ page import="com.nexacro17.xapi.data.*" %>
<%@ page import="com.nexacro17.xapi.tx.*" %>

<%@ 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-based apps 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("customers");

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

$r_title(save_list.jsp)

<!-- 1. Designating a Java library -->
<%@ page import="java.io.*" %>
<%@ page import="com.nexacro17.xapi.data.*" %>
<%@ page import="com.nexacro17.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("customers");

	/** 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.divCommand_btnSaveList_onclick = function(obj:nexacro.Button,  e:nexacro.ClickEventInfo)
{
     var id = "save_list";  
     var url = "SvcList::save_list.jsp";
     var reqDs = "customers=dsCustomers";
     var respDs = "";
     var args = "";
     var callback = "save_list_received";

     this.transaction(id, url, reqDs, respDs, args, callback);
}

this.save_list_received = function(id, code, message)
{
     if (code == 0) {
          this.alert(message);
          trace(message);
     } else {
          this.alert("Error["+code+"]:"+message);
          trace("Error["+code+"]:"+message);
     }
}