New examples for howtos
authorMartin Taal <martin.taal@openbravo.com>
Thu, 12 Nov 2015 15:52:39 +0100
changeset 2 4184d825caa1
parent 1 c3b286dfee66
child 3 b1dafc97de17
New examples for howtos
src/org/openbravo/retail/storeserver/howto/central/HowtoCentralMessageHandler.java
src/org/openbravo/retail/storeserver/howto/central/ProcessEntry.java
src/org/openbravo/retail/storeserver/howto/store/CheckProductStore.java
src/org/openbravo/retail/storeserver/howto/store/HowToTransitionToOnlineExample.java
src/org/openbravo/retail/storeserver/howto/store/HowtoStoreMessageHandler.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/retail/storeserver/howto/central/HowtoCentralMessageHandler.java	Thu Nov 12 15:52:39 2015 +0100
@@ -0,0 +1,50 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2015 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+package org.openbravo.retail.storeserver.howto.central;
+
+import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.mobile.core.servercontroller.MobileServerController;
+import org.openbravo.mobile.core.servercontroller.MobileServerMessageHandler;
+import org.openbravo.model.common.enterprise.Organization;
+
+/**
+ * Example code to how a message can be handled.
+ */
+public class HowtoCentralMessageHandler extends MobileServerMessageHandler {
+
+  @Override
+  public boolean processMessage(Organization org, String json) {
+    if (!MobileServerController.getInstance().isThisACentralServer()) {
+      return false;
+    }
+    try {
+      final JSONObject jsonObject = new JSONObject(json);
+      // check if we can handle the message
+      if (jsonObject.has("type") && "howtoMessageFromStore".equals(jsonObject.get("type"))) {
+        // we can, send a message back to the store that it was done
+
+        System.err.println("Sending back message to the store");
+
+        // send a message to the store server that a local action was performed
+        MobileServerController.getInstance().sendMessageToStore(
+            OBDal.getInstance().get(Organization.class, jsonObject.getString("organization")),
+            "{'type': 'howtoMessageFromCentral', 'action': 'confirmed', 'organization':'"
+                + OBContext.getOBContext().getCurrentOrganization().getId() + "'}");
+        return true;
+      }
+      return false;
+
+    } catch (Exception e) {
+      throw new OBException(e);
+    }
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/retail/storeserver/howto/central/ProcessEntry.java	Thu Nov 12 15:52:39 2015 +0100
@@ -0,0 +1,47 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2015 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+package org.openbravo.retail.storeserver.howto.central;
+
+import javax.servlet.ServletException;
+
+import org.apache.log4j.Logger;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.mobile.core.servercontroller.MobileServerJSONProcess;
+import org.openbravo.service.json.JsonConstants;
+
+/**
+ * Example code to show that code is called for the store server/central server.
+ * 
+ * This code is called from the store server, the central server executes the same code if it is
+ * online or offline. In other scenarios it can call an ERP or other external system.
+ */
+public class ProcessEntry extends MobileServerJSONProcess {
+  public static final Logger log = Logger.getLogger(ProcessEntry.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 {
+    System.err.println("Received json " + json);
+    JSONObject result = new JSONObject();
+    result.put(JsonConstants.RESPONSE_STATUS, JsonConstants.RPCREQUEST_STATUS_SUCCESS);
+    return result;
+  }
+}
\ No newline at end of file
--- a/src/org/openbravo/retail/storeserver/howto/store/CheckProductStore.java	Wed Nov 11 23:42:26 2015 +0100
+++ b/src/org/openbravo/retail/storeserver/howto/store/CheckProductStore.java	Thu Nov 12 15:52:39 2015 +0100
@@ -15,6 +15,7 @@
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.Query;
+import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.mobile.core.servercontroller.MobileServerController;
 import org.openbravo.mobile.core.servercontroller.MobileServerJSONProcess;
@@ -55,6 +56,7 @@
 
     result.put(JsonConstants.RESPONSE_DATA, respArray);
     result.put(JsonConstants.RESPONSE_STATUS, JsonConstants.RPCREQUEST_STATUS_SUCCESS);
+
     return result;
   }
 
@@ -66,6 +68,11 @@
       return executeWhenOffline(json);
     }
 
+    // send a message to the central server that a local action was performed
+    MobileServerController.getInstance().sendMessageToCentral(
+        "{'type': 'howtoMessageFromStore', 'action': 'localQueryActionPerformed', 'organization':'"
+            + OBContext.getOBContext().getCurrentOrganization().getId() + "'}");
+
     // this is a store server, call the central server
     return MobileServerRequestExecutor.getInstance().executeRequest(
         MobileServerUtils.OBWSPATH
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/retail/storeserver/howto/store/HowToTransitionToOnlineExample.java	Thu Nov 12 15:52:39 2015 +0100
@@ -0,0 +1,58 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2015 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+package org.openbravo.retail.storeserver.howto.store;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
+import org.openbravo.mobile.core.servercontroller.MobileServerRequestExecutor;
+import org.openbravo.mobile.core.servercontroller.MobileServerTransitionToOnlineHandler;
+import org.openbravo.mobile.core.servercontroller.MobileServerUtils;
+import org.openbravo.service.importprocess.ImportEntry;
+
+/**
+ * Example code to show how an online transition handler can be implemented.
+ */
+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;
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/retail/storeserver/howto/store/HowtoStoreMessageHandler.java	Thu Nov 12 15:52:39 2015 +0100
@@ -0,0 +1,41 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2015 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+package org.openbravo.retail.storeserver.howto.store;
+
+import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.mobile.core.servercontroller.MobileServerController;
+import org.openbravo.mobile.core.servercontroller.MobileServerMessageHandler;
+import org.openbravo.model.common.enterprise.Organization;
+
+/**
+ * Example code to how a message can be handled.
+ */
+public class HowtoStoreMessageHandler extends MobileServerMessageHandler {
+
+  @Override
+  public boolean processMessage(Organization org, String json) {
+    if (!MobileServerController.getInstance().isThisAStoreServer()) {
+      return false;
+    }
+    try {
+      final JSONObject jsonObject = new JSONObject(json);
+      // check if we can handle the message
+      if (jsonObject.has("type") && "howtoMessageFromCentral".equals(jsonObject.get("type"))) {
+        // we can, send a message back to the store that it was done
+        System.err.println("Message received!");
+        return true;
+      }
+      return false;
+
+    } catch (Exception e) {
+      throw new OBException(e);
+    }
+  }
+}
\ No newline at end of file