Runtime components : Channels components : Mobile Channel : Tasks : Invoking server operation
  
Invoking server operation
After establishing session with the server side, eRCP application can invoke server side operation and get the synchronized result.
There are two ways to invoke the server side operation:
Basic way to invoke the server side operation
You can adopt the basic way if the input and output of server operations are the same JavaBean, and there is only one input for server operation.
Following is an example scenario of how you can get detailed customer information from CustomerID using the same input and output JavaBean:
Customer information is maintained by a JavaBean named Customer, which has several attributes: CustomerID, Address, Age, and so on. In eRCP client, you can initialize JavaBean Customer with attribute CustomerID and leave the other attributes as null, and then pass the operation name and JavaBean to the server side. Server side operation retrieves customer information based on CustomerID, and fills the other attributes back to JavaBean Customer (such as Address, and Age). In this case the input JavaBean and output JavaBean are the same. They are JavaBean Customer.
The detailed method is as follows:
/**
* Invoke server side operation and return the processing result If the
* passed java bean and returned result are the same, use this API to invoke
* server side operation. In BTT C/S client mechanism, one is passing the
* java bean to the server side, setting the values of this java bean in the
* server side operation and returning this processed java bean to client
* side. This method is implemented for this usage. Please establishSession
* before invoking this API.
*
* @param operationName
* @param param
* the Serializable JavaBean passed to server side
* @return the processed JavaBean
* @throws Exception
* if error occurs
*/
public Serializable invoke(String operationName, Serializable param) throws Exception
Advanced way to invoke server operation
You can adopt the advanced way if the input and output of server operations are different or there are more than one parameter is necessary for the operation input
In the advanced way to invoke server operation, Map is used to pass input of server operation. You can put JavaBean object and other simple attributes into Map. For those maps, key is the data ID which is defined in the context of the server operation. Value is the input JavaBean and other attributes. Response data ID is the ID of output JavaBean and is defined in the context of server operation. The UNICOM® Digital Transformation Toolkit (UDTT™) server returns the serialized JavaBean which has the name of the specified ID in the operation context.
The detailed method is as follows:
/**
* Invoke server side operation and return the processing result.
* If the passed java bean and returned result are different or you need to pass more
than one parameters,
* use this API to invoke server side operation.
* This method is generic, it supports below usage scenarios:
* 1) passing one java bean or more than one java bean to the server side and returning
the result
* Sample Code:
* User user = new User();
user.setUserName("aaaa");
user.setPassword("bbbb");

Map param = new HashMap();
param.put("userBean", user); // the key is the data id defined in the server side
context definition
Result result = (Result) adapter.invoke("MobileSignInOp", param, "resultBean");

2) passing the query conditions to the server side and returning the result
Sample Code: Query user by userId:
Map newParam = new HashMap();
newParam.put("userId", "123456"); // the userId defined in the server side context
definition
User newResult = (User) adapter.invoke("MobileQueryUserOp", newParam, "userBean");
3) You can mix the first two usages.
User user = new User();
user.setUserName("aaaa");
user.setPassword("bbbb");

Map param = new HashMap();
param.put("userBean", user); // the key is the data id defined in the server side
context definition
param.put("firstLogin","true");// the key is the data id defined in the server side
context definition
Result result = (Result) adapter.invoke("MobileSignInOp", param, "resultBean");
*
* @param operationName the operation name
* @param param The Map contains the JavaBean or Simple Java Object passed to the server side.
* The key is the data id defined in the server side operation context definition
* @param responseDataId the response data id defined in the server side operation context
*
* @return the processed JavaBean
* @throws Exception if error occurs
*/
public Serializable invoke(String operationName, Map param, String responseDataId)
See
Invoking session operation using the basic way
Invoking session operation using the advanced way
See also
Tasks
Invoking session operation using the basic way
Pass the serializable JavaBean and operation name as the parameters. After invoking the API, you can get the synchronized result. Following is the sample code for invoking server side operation to query user's password by user name in the client side:
public static void main(String[] args) throws Exception {
DataAdapter adapter = new MobileAdapter();
if (adapter.establishSession()) {
User user = new User();
// set the user name as query condition
user.setUserName("jack");
//query the user's password by invoking server side 'MobileQueryPasswordOp' operation
User resultUser = (User) adapter.invoke("MobileQueryPasswordOp", user);
//print the result
System.out.println("the user password:::" + resultUser.getPassword());
}
}
Following are the code samples for Data definition, Context definition, and Operation in the server side:
1 Data definition:
Screen capture of data definition
2 Context definition:
Screen capture of context definition
3 Operation:
Screen capture of operation
See also
Invoking server operation
Invoking session operation using the advanced way
Pass the Map which contains the passed values, operation name and result id defined in the server side context definition as the parameters. After invoking the API, you can get the synchronized result.
There are two approaches:
Approach 1: Pass JavaBean to the server side and get another JavaBean returned. Following is the sample code for login.
In the client side, input the User (username and password), and a resultbean is returned:
public static void main(String[] args) throws Exception {
DataAdapter adapter = new MobileAdapter();
if (adapter.establishSession()) {
User user = new User();
user.setUserName("aaaa");
user.setPassword("bbbb");
Map param = new HashMap();
param.put("userBean", user);
Result result = (Result) adapter.invoke("MobileSignInOp", param, "resultBean");
adapter.invoke("MobileSignInOp", param, "resultBean");
System.out.println(result);
}
}
Following are the code samples for Data definition, Context definition, and Operation implementation in the server side:
Data definition:
Screen capture of data definition
Context definition:
Screen capture of context definition
Operation implementation:
Screen capture of operation
Approach 2: Pass the simple attribute to the server side and get JavaBean returned
Following is the sample code for querying user by useid:
public static void main(String[] args) throws Exception {
  DataAdapter adapter = new MobileAdapter();
  if (adapter.establishSession()) {
    Map query = new HashMap();
    query.put("userId","111");
    //Query user by userId
    User resultUser = (User)     adapter.invoke("MobileQueryUserOp",query, "userBean");
    //print the result
    System.out.println("the user " + resultUser);
  }
}
Following are the code samples for Data definition, Context definition, and Operation implementation in the server side:
Data definition:
Screen capture of data definition
Context definition:
Screen capture of context definition
Operation implementation:
Screen capture of operation
See also
Invoking server operation