How to Write Online Transition Handlers
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 central 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.
Online Transition Handlers
The idea for online transition handlers is that they are called when the server transitions back to the online status. During offline status, transactions can be stored on the server. These transactions should potentially be send to the central server. The online transition handlers provide a hook to implement this type of logic.
An online transition handler only needs to extend the MobileServerTransitionToOnlineHandler class and implement 2 methods:
- processTransactions: in this method the online transition handler should send all the offline transactions to the central server, or do other meaningful actions.
- isReadyToGoOnline: should return true if the transition handler was able to execute its logic correctly.
Example code 1
The code snippet below shows an example of an online transition handler. This example loops through non-processed import entries and sends them to a server.
public class HowToTransitionToOnlineExample extends MobileServerTransitionToOnlineHandler { private boolean isProcessing = false; @Override public void processTransactions() { try { isProcessing = true; OBContext.setAdminMode(); final OBQuery<ImportEntry> entries = OBDal.getInstance().createQuery(ImportEntry.class, ImportEntry.PROPERTY_IMPORTSTATUS + "='Initial'"); entries.setFilterOnReadableOrganization(false); for (ImportEntry entry : entries.list()) { // send to the central server MobileServerRequestExecutor.getInstance().executeRequest( MobileServerUtils.OBWSPATH + "org.openbravo.retail.storeserver.howto.central.ProcessEntry", new JSONObject(entry.getJsonInfo())); entry.setImportStatus("Processed"); } } catch (JSONException e) { throw new OBException(e); } finally { OBContext.restorePreviousMode(); isProcessing = false; } } @Override public boolean isReadyToGoOnline() { return !isProcessing; } }
Example code 2
Another example can be found in the store server synchronization module. This transition handler makes sure that all the transactions have been synced by Symmetric DS before allowing online transition.