Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Web Service Structure in EMP has mainly 3 parts:

...

Migrated From http://intranet.wavelet.asia/projects/tech/wiki/How_to_use_RESTful_Web_Service

Navigate to source code: src/webservices/webservices/rest/
Under this rest there have Controller, Resources and Model. To create a new web service need to maintain existing structure.

URL to access web service: http://empdomain.biz/ws/api/webservicename/actionname

 

Example:

webservicename and actionname: These will be found in the resource file. Suppose for Item Webservice name need to open file: RestItemResouce.java. Inside the path function webservice name written: itemService. In the inner path function actionname written get. So the full path will be http://cloud.waveletempdomain.biz/demo/ws/api/itemService/get

 

Actions

EMP has following Actions:

 

Action Name: get

get action mainly used to retrieve data from server. It has 3 parameters:

Parameter NameData TypeRequired/OptionalSample DataDescription
userNameStringRequiredchinuserName is being used for Authentication of the user to perform this action
passwordStringRequired123456password is being used for Authentication of the user to perform this action
queryObjectStringRequiredSample json data of queryObject parameterThis is a json data of SQL query

Sample Code to call get action using JAVA Platform

 

Action Name: getCount

getCount action mainly used to retrieve number of records available in server for a particular query. It has 3 parameters:

Parameter NameData TypeRequired/OptionalSample DataDescription
userNameStringRequiredchinuserName is being used for Authentication of the user to perform this action
passwordStringRequired123456password is being used for Authentication of the user to perform this action
queryObjectStringRequiredSample json data of queryObject parameterThis is a json data of SQL query

Sample Code to call getCount action using JAVA Platform

 

Action Name: add

add action mainly used to send records to server to add in particular table. It has 3 parameters:

Parameter NameData TypeRequired/OptionalSample DataDescription
userNameStringRequiredchinuserName is being used for Authentication of the user to perform this action
passwordStringRequired123456password is being used for Authentication of the user to perform this action
txqueueJsonStringRequiredSample json data of txqueueJson parameterThis is a json data with all information required to create a Tx Queue. It can hold data for Sales Order Index, Sales Order Item, Cust Invoice Index, Cust Invoice Item, Receipt Index and Tc Terminal

Sample Code to call ADD action using JAVA PlatformMost of the webservices contains: getgetCount and create actions.

 

calling the get action:

get action takes 3 parameters

1. userName (String) : for authentication.
2. password (String) : for authentication.
3. queryObject (json String): QueryObject is a customized class inside EMP to make query easy. For JAVA client application can use this QueryObject directly and convert into string. For non-java client application can pass in following json formatted string:

{"conditions":["lastupdate='2013-10-17 16:22:06.0'"],"tablename":"","orderBy":" ORDER BY lastupdate","offset":0,"limit":10,"page":0,"count":-1}

A complete function to call get action is as follow:

@protected void getItemFromServer() {
String json = null;
String queryJson = "";
Integer offset = new Integer(0);
Integer loopNumber = new Integer(0);
Integer totalUpdate = new Integer(0);
totalUpdate = getUpdateCountItemFromServer();
if(totalUpdate < 1) {
return;
} else {
BigDecimal loopTemp = new BigDecimal(totalUpdate / WSResponse.QUERY_LIMIT);
loopNumber = loopTemp.intValue() + 1;
}
Gson gson = new Gson();
String[] conditions = new String[]{ItemBean.LASTUPDATE +" >= '" +
Utilities.getLastSyncTime(mContext, ItemBean.TABLENAME) + "'"};
QueryObject query = new QueryObject(conditions);
try {
for(int i=0; i&lt;loopNumber; i++) {
query.offset = WSResponse.QUERY_LIMIT * i;
query.limit = WSResponse.QUERY_LIMIT;
query.orderBy = " ORDER BY " + ItemBean.LASTUPDATE;
queryJson = gson.toJson(query);
String urlParam="userName="+ URLEncoder.encode(mAccount.name,"UTF-8")+"&password="
+ URLEncoder.encode(mAccountManager.getPassword(mAccount),"UTF-8")+"" +
"&queryObject="+URLEncoder.encode(queryJson,"UTF-8");
String urlStr = arBean.loadObjectByUsername(mAccount.name).mContent + WSResponse.WEBSERVICE_GET_ITEM;
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setFixedLengthStreamingMode(urlParam.getBytes().length);
if (con != null) {
try {
java.io.OutputStream os = con.getOutputStream();
os.write(urlParam.getBytes());
BufferedReader rd  = new BufferedReader(new InputStreamReader(con.getInputStream()));
json = rd.readLine();
Gson gsonWs = new Gson();
WSResponse wsr = gsonWs.fromJson(json, WSResponse.class);
Collection col = new Vector();
col = wsr.getItems();
if(col != null) {
ItemBean itmBean = new ItemBean(mContext);
int count = itmBean.insertObject(col);
SyncLogBean slBean = new SyncLogBean(mContext);
slBean.updateObject(ItemBean.TABLENAME, wsr.getMsg());
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(con != null)
con.disconnect();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}@

 

calling the getCount action:

getCount action takes 3 parameters

1. userName (String) : for authentication.
2. password (String) : for authentication.
3. queryObject (json String): QueryObject is a customized class inside EMP to make query easy. For JAVA client application can use this QueryObject directly and convert into string. For non-java client application can pass in following json formatted string:

{"conditions":["last_edit='2013-10-15 17:35:03.0'"],"tablename":"","orderBy":"","offset":0,"limit":-1,"page":0,"count":-1}

A complete function to call get action is as follow:

@protected Integer getUpdateCountItemFromServer() {
Integer count = new Integer(0);
String json = null;
String queryJson = "";
Integer limit = new Integer(10);
Integer offset = new Integer(0);
Gson gson = new Gson();
String[] conditions = new String[]{ItemBean.LASTUPDATE +" >= '" +
Utilities.getLastSyncTime(mContext, ItemBean.TABLENAME) + "'"};
QueryObject query = new QueryObject(conditions);
try {
queryJson = gson.toJson(query);
String urlParam="userName="+ URLEncoder.encode(mAccount.name,"UTF-8")+"&password="
+ URLEncoder.encode(mAccountManager.getPassword(mAccount),"UTF-8")+"" +
"&queryObject="+URLEncoder.encode(queryJson,"UTF-8");
String urlStr = arBean.loadObjectByUsername(mAccount.name).mContent + WSResponse.WEBSERVICE_GET_COUNT_ITEM;
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setFixedLengthStreamingMode(urlParam.getBytes().length);
if (con != null) {
try {
java.io.OutputStream os = con.getOutputStream();
os.write(urlParam.getBytes());
BufferedReader rd  = new BufferedReader(new InputStreamReader(con.getInputStream()));
json = rd.readLine();
Gson gsonWs = new Gson();
WSResponse wsr = gsonWs.fromJson(json, WSResponse.class);
count = wsr.getCount();
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(con != null)
con.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return count;
}@

 

calling the create action:

create action takes 3 parameters

1. userName (String) : for authentication.
2. password (String) : for authentication.
3. object (json String): This is a json formatted string of ItemObject if need to create an Item in EMP.

 

Structure of WSResponse.java file:

this WSResponse will parse the output.

@package asia.wavelet.android.util;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Vector;

import android.util.Log;
import asia.wavelet.android.accounting.BranchObject;
import asia.wavelet.android.accounting.DocLinkObject;
import asia.wavelet.android.accounting.OfficialReceiptObject;
import asia.wavelet.android.application.AppRegObject;
import asia.wavelet.android.application.CardPaymentConfigObject;
import asia.wavelet.android.application.EImageObject;
import asia.wavelet.android.application.ProfitCostCenterObject;
import asia.wavelet.android.application.StringTemplateObject;
import asia.wavelet.android.application.TcTerminalObject;
import asia.wavelet.android.bean.CardPaymentConfigBean;
import asia.wavelet.android.customer.CustAccountObject;
import asia.wavelet.android.customer.CustUserObject;
import asia.wavelet.android.inventory.BOMLinkObject;
import asia.wavelet.android.inventory.BOMObject;
import asia.wavelet.android.inventory.ItemObject;
import asia.wavelet.android.pos.DocumentItemObject;
import asia.wavelet.android.pos.DocumentObject;
import asia.wavelet.android.user.UserObject;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

public class WSResponse {
public static final String WEBSERVICE_VALIDATE_USER = "userService/validateUser";
public static final String WEBSERVICE_GET_USER = "userService/getUserObject";
public static final String WEBSERVICE_GET_USERS = "userService/get";
public static final String WEBSERVICE_GET_BRANCH = "branchService/get";
public static final String WEBSERVICE_GET_PCCENTER = "pcCenterService/get";
public static final String WEBSERVICE_GET_CUSTOMER = "customerService/get";
public static final String WEBSERVICE_GET_CUST_USER = "custUserService/get";
public static final String WEBSERVICE_GET_ITEM = "itemService/get";
public static final String WEBSERVICE_GET_BOM = "bomService/get";
public static final String WEBSERVICE_GET_BOM_LINK = "bomLinkService/get";
public static final String WEBSERVICE_GET_CARD_PAYMENT_CONFIG = "cardPaymentConfigService/get";
public static final String WEBSERVICE_GET_STRING_TEMPLATE = "stringTemplateService/get";
public static final String WEBSERVICE_GET_APP_REG = "appRegService/get";
public static final String WEBSERVICE_GET_EIMAGE = "eImageService/get";
public static final String WEBSERVICE_GET_TC_TERMINAL = "tcTerminalService/get";
public static final String WEBSERVICE_GET_SALES_ORDER = "salesOrderService/get";
public static final String WEBSERVICE_GET_CUST_INVOICE = "custInvoiceService/get";
public static final String WEBSERVICE_GET_DOCUMENT_ITEM = "documentItemService/get";
public static final String WEBSERVICE_GET_SERIAL_NUMBER_DELTA = "serialNumberDeltaService/get";
public static final String WEBSERVICE_GET_SO_SERIAL = "soItemSerialService/get";
public static final String WEBSERVICE_GET_RECEIPT = "receiptService/get";
public static final String WEBSERVICE_GET_DOC_LINK = "docLinkService/get";
public static final String WEBSERVICE_GET_COUNT_BOM_LINK = "bomLinkService/getCount";
public static final String WEBSERVICE_GET_COUNT_BOM = "bomService/getCount";
public static final String WEBSERVICE_GET_COUNT_ITEM = "itemService/getCount";
public static final String WEBSERVICE_GET_COUNT_PCCENTER = "pcCenterService/getCount";
public static final String WEBSERVICE_GET_COUNT_BRANCH = "branchService/getCount";
public static final String WEBSERVICE_GET_COUNT_CUST_ACCOUNT = "customerService/getCount";
public static final String WEBSERVICE_GET_COUNT_CUST_USER = "custUserService/getCount";
public static final String WEBSERVICE_GET_COUNT_USER = "userService/getCount";
public static final String WEBSERVICE_GET_COUNT_CARD_PAYMENT_CONFIG = "cardPaymentConfigService/getCount";
public static final String WEBSERVICE_GET_COUNT_STRING_TEMPLATE = "stringTemplateService/getCount";
public static final String WEBSERVICE_GET_COUNT_APP_REG = "appRegService/getCount";
public static final String WEBSERVICE_GET_COUNT_EIMAGE = "eImageService/getCount";
public static final String WEBSERVICE_GET_COUNT_SALES_ORDER = "salesOrderService/getCount";
public static final String WEBSERVICE_GET_COUNT_CUST_INVOICE = "custInvoiceService/getCount";

public static final String WEBSERVICE_PROCESS_TXQUEUE = "txQueueService/processHT";
public static final Integer QUERY_LIMIT = new Integer(10);
private boolean userValidation;
private String resultJSON;
private String msgCode;
private String msg;
public WSResponse(){
this.userValidation = false;
this.resultJSON = "";
this.msgCode = "";
this.msg = "";
}
public UserObject getUserObj() {
Collection col = new Vector();
UserObject usrObj = null;
Gson gson = new Gson();
usrObj = gson.fromJson(resultJSON, UserObject.class);
return usrObj;
}
public Collection getBranches() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;BranchObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getPcCenters() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;ProfitCostCenterObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getCustomers() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;CustAccountObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getCustUsers() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;CustUserObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getUsers() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;UserObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getItems() {
Log.d("====INSIDE getItems IN WSRESPONSE====", resultJSON);
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;ItemObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
Log.d("====ITEM RESULT JSON====", resultJSON);
return col;
}
public Collection getBom() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;BOMObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getBomLink() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;BOMLinkObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getCardPaymentConfigs() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;CardPaymentConfigObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getStringTemplates() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;StringTemplateObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getAppReg() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;AppRegObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getEImage() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;EImageObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection getTcTerminal() {
Collection col = new Vector();
Gson gson = new Gson();
Type token = new TypeToken&lt;Collection&lt;TcTerminalObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
return col;
}
public Collection&lt;DocumentObject&gt; getDocuments() {
Collection col = new Vector();
Gson gson = new Gson();
try {
Type token = new TypeToken&lt;Collection&lt;DocumentObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
} catch (Exception e) {
e.printStackTrace();
}
return col;
}
public Collection&lt;DocumentItemObject&gt; getDocumentItems() {
Collection col = new Vector();
Gson gson = new Gson();
try {
Type token = new TypeToken&lt;Collection&lt;DocumentItemObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
} catch (Exception e) {
e.printStackTrace();
}
return col;
}
public Collection&lt;DocLinkObject&gt; getDocLinks() {
Collection col = new Vector();
Gson gson = new Gson();
try {
Type token = new TypeToken&lt;Collection&lt;DocLinkObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
} catch (Exception e) {
e.printStackTrace();
}
return col;
}
public Collection&lt;OfficialReceiptObject&gt; getReceipts() {
Collection col = new Vector();
Gson gson = new Gson();
try {
Type token = new TypeToken&lt;Collection&lt;OfficialReceiptObject&gt;>() {}.getType();
col = gson.fromJson(resultJSON, token);
} catch (Exception e) {
e.printStackTrace();
}
return col;
}
public Integer getCount() {
Integer count = new Integer(0);
try {
count = new Integer(msgCode);
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
public boolean isuserValidation() {
return userValidation;
}
public void setuserValidation(boolean userValidation) {
this.userValidation = userValidation;
}
public String getresultJSON() {
return resultJSON;
}
public boolean getResultJSONBoolean() {
if("true".equals(resultJSON))
return true;
else
return false;
}
public void setresultJSON(String resultJSON) {
this.resultJSON = resultJSON;
}
public String getmsgCode() {
return msgCode;
}
public void setmsgCode(String msgCode) {
this.msgCode = msgCode;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}@