View source | Discuss this page | Page history | Printable version   
Toolbox
Main Page
Upload file
What links here
Recent changes
Help

PDF Books
Add page
Show collection (0 pages)
Collections help

Search

How to Write Store Status Aware Logic

Contents


Introduction

This howto gives insight in how to write store status aware logic which handles requests. This is logic which allows you to write code that behaves differently depending on the status of the store or server.

Before starting this howto make sure to read the howto setup a dev environment. It also makes sense to have a clear understanding of the states of an Openbravo Commerce server (offline, online, transitioning etc.).

Also the howto assumes that the reader is familiar with the main concepts/information provided by the retail developers guide.

Installing the how to module

The examples provided in this howto can be found in the Store Server Howto module. The howto module can be downloaded using the hg clone command in the modules directory:

 hg clone https://code.openbravo.com/erp/mods/org.openbravo.retail.storeserver.howto

After this execute the 'ant smartbuild -Dlocal=no' command in the main project directory.

Status Aware Request Handling: MobileServerJSONProcess - MobileServerController

To implement status aware request handling the best approach is to extend the MobileServerJSONProcess class. This class provides 4 methods which you can extend to implement status dependent functionality:

The transitioning to online methods are called but in the returned json also the serverStatus is returned (with values: TOFF for transition to offline or TON for transition to online). When the WebPOS client receives this status (TON or TOFF) it will force the user to login.

Another class which is important for store server specific functionality is the MobileServerController class. This class provides methods such as isThisAStoreServer, isThisACentralServer or getMobileServerState.

Check Product Example

In this howto we will show how to implement a service which provides a status aware behavior. The product check example adds a button to the line view in WebPOS. When clicking the button a request is done to the server with the product id. The server checks the number of products with the passed id (the answer is always 1) and returns if the central or the store server replies. If the store server is online then the central server is called. If the store server is offline then the request is handled locally.

This howto adds a line button in WebPOS. There is also a specific retail howto for doing this.

Two classes are implemented for this example and one javascript file.

To organize the code it makes sense to place the store and central code in separate packages or separate modules.


StoreServer-Dev-Different-Packages.png


Howto Artifacts

Store Server Product Check Code

The code on the store server is listed below. Some notes:

public class CheckProductStore extends MobileServerJSONProcess {
  public static final Logger log = Logger.getLogger(CheckProductStore.class);
 
  @Override
  protected boolean bypassPreferenceCheck() {
    return true;
  }
 
  protected JSONObject executeWhenOffline(JSONObject json) throws JSONException, ServletException {
    JSONArray respArray = new JSONArray();
    JSONObject result = new JSONObject();
 
    String productId = json.getString("productId");
    String hqlCheckProduct = "select count(p.id) from Product as p " + "where p.id='" + productId
        + "' ";
    Query checkProductQuery = OBDal.getInstance().getSession().createQuery(hqlCheckProduct);
 
    for (Object objIter : checkProductQuery.list()) {
      JSONObject objJson = new JSONObject();
      objJson.put("count", objIter);
      objJson.put("source", MobileServerController.getInstance().isThisAStoreServer() ? "store"
          : "central");
      respArray.put(objJson);
    }
 
    result.put(JsonConstants.RESPONSE_DATA, respArray);
    result.put(JsonConstants.RESPONSE_STATUS, JsonConstants.RPCREQUEST_STATUS_SUCCESS);
    return result;
  }
 
  protected JSONObject executeWhenOnline(JSONObject json) throws JSONException, ServletException {
    return MobileServerRequestExecutor.getInstance().executeRequest(
        MobileServerUtils.OBWSPATH
            + "org.openbravo.retail.storeserver.howto.central.CheckProductCentral", json);
  }
}


Central Server Product Check Code

The code on the central server is listed below. Some notes:

public class CheckProductCentral extends MobileServerJSONProcess {
  public static final Logger log = Logger.getLogger(CheckProductCentral.class);
 
  @Override
  protected boolean bypassPreferenceCheck() {
    return true;
  }
 
  protected JSONObject executeWhenOffline(JSONObject json) throws JSONException, ServletException {
    return execute(json);
  }
 
  protected JSONObject executeWhenOnline(JSONObject json) throws JSONException, ServletException {
    return execute(json);
  }
 
  protected JSONObject execute(JSONObject json) throws JSONException, ServletException {
    JSONArray respArray = new JSONArray();
    JSONObject result = new JSONObject();
 
    String productId = json.getString("productId");
    String hqlCheckProduct = "select count(p.id) from Product as p " + "where p.id='" + productId
        + "' ";
    Query checkProductQuery = OBDal.getInstance().getSession().createQuery(hqlCheckProduct);
 
    for (Object objIter : checkProductQuery.list()) {
      JSONObject objJson = new JSONObject();
      objJson.put("count", objIter);
      objJson.put("source", "central");
      respArray.put(objJson);
    }
 
    result.put(JsonConstants.RESPONSE_DATA, respArray);
    result.put(JsonConstants.RESPONSE_STATUS, JsonConstants.RPCREQUEST_STATUS_SUCCESS);
    return result;
  }
}

Javascript: add button, call server

The javascript file adds the button and calls the server, the more interesting part is the call to the server, it shows how the store server is called. For this see the tap function and the process definition and how it is executed.

 
  enyo.kind({
    kind: 'OB.UI.SmallButton',
    name: 'OB.OBPOSPointOfSale.UI.EditLine.StoreServerProductCheck',
    content: 'Product Check',
    classes: 'btnlink-orange',
    tap: function () {
      var process = new OB.DS.Process('org.openbravo.retail.storeserver.howto.store.CheckProductStore');
      process.exec({
        productId: this.owner.owner.line.get('product').get('id')
      }, function (data) {
        OB.UTIL.showSuccess("Call to server success count " + data[0].count + " from source " + data[0].source);
      });
    }
  });
 
  //Register the button...
  OB.OBPOSPointOfSale.UI.EditLine.prototype.actionButtons.push({
    kind: 'OB.OBPOSPointOfSale.UI.EditLine.StoreServerProductCheck',
    name: 'storeServerProductCheck'
  });

The result

The result is shown in the screenshot below:


StoreServer-Howto-add-button.png


When the user clicks the button a request is done to the store server to obtain the results. The store server then calls the central server which returns the results. If you would stop the central server then the store server will handle the call/request locally.

Retrieved from "http://wiki.openbravo.com/wiki/How_to_Write_Store_Status_Aware_Logic"

This page has been accessed 407 times. This page was last modified on 14 November 2015, at 16:13. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.