# HG changeset patch
# User Rafa Alonso <rafael.alonso@openbravo.com>
# Date 1410287731 -7200
#      Tue Sep 09 20:35:31 2014 +0200
# Node ID 1e71aac70f247de9852a8e2364b8be6bbe0fbff3
# Parent  bb886af61ae4f3bced093ecbe6ba33f722ae4b08
[mq]: SwitchFromERPToWebPOS

diff -r bb886af61ae4 -r 1e71aac70f24 web/org.openbravo.mobile.core/source/data/ob-datasource.js
--- a/web/org.openbravo.mobile.core/source/data/ob-datasource.js	Tue Aug 05 06:05:38 2014 +0000
+++ b/web/org.openbravo.mobile.core/source/data/ob-datasource.js	Tue Sep 09 20:35:31 2014 +0200
@@ -7,10 +7,9 @@
  ************************************************************************************
  */

-/*global B, $, Backbone, _, enyo */
+/*global OB, $, Backbone, _, enyo, window */

 (function () {
-  OB = window.OB || {};
   OB.DS = window.OB.DS || {};

   OB.DS.MAXSIZE = 100;
@@ -20,13 +19,38 @@
       callback([inResponse]);
     } else {
       var response = inResponse.response;
+
+      if (!response) {
+        // the response is empty
+        response = {};
+        response.error = {};
+        response.error.message = "Unknown error";
+        response.status = 501;
+      }
+
       var status = response.status;
-      if (response.contextInfo && OB.MobileApp.model.get('context')) {
-        OB.UTIL.checkContextChange(OB.MobileApp.model.get('context'), response.contextInfo);
-      }
+
       if (status === 0) {
         callback(response.data, response.message, response.lastUpdated);
-      } else if (response.errors) {
+        return;
+      }
+
+      // an error has been sent in the successful response
+      OB.error(response);
+
+      if (status === -1) {
+        window.localStorage.clear();
+        OB.MobileApp.model.logout();
+        return;
+      }
+
+      // generic error message
+      var exception = {
+        message: 'Unknown error',
+        status: response
+      };
+
+      if (response.errors) {
         if (OB.MobileApp.model.get('isLoggingIn')) {
           OB.MobileApp.model.set("datasourceLoadFailed", true);
           OB.UTIL.showConfirmation.display('Error', response.errors.substring(0, 400), [{
@@ -43,16 +67,10 @@
             }
           });
         }
-        OB.error(response);
-        callback({
-          exception: {
-            message: response.errors.id,
-            status: response
-          }
-        });
-      } else {
-        OB.error(response);
-        OB.UTIL.showLoading(false);
+        exception.message = response.errors.id;
+      }
+
+      if (response.error && response.error.message) {
         if (OB.MobileApp.model.get('isLoggingIn')) {
           OB.MobileApp.model.set("datasourceLoadFailed", true);
           OB.UTIL.showConfirmation.display('Error', response.error.message.substring(0, 400), [{
@@ -60,31 +78,25 @@
             isConfirmButton: true,
             action: function () {
               OB.MobileApp.model.logout();
-              OB.UTIL.showLoading(true);
             }
           }], {
             onHideFunction: function () {
-              OB.UTIL.showLoading(true);
               OB.MobileApp.model.logout();
             }
           });
         }
-        if (response.error && response.error.message) {
-          callback({
-            exception: {
-              message: response.error.message,
-              status: response
-            }
-          });
-        } else {
-          callback({
-            exception: {
-              message: 'Unknow error',
-              status: response
-            }
-          });
-        }
+        exception.message = response.error.message;
       }
+
+      // if the context has changed, lock the terminal
+      if (response.contextInfo && OB.MobileApp.model.get('context')) {
+        OB.UTIL.checkContextChange(OB.MobileApp.model.get('context'), response.contextInfo, callback);
+        return;
+      }
+
+      callback({
+        exception: exception
+      });
     }
   }

@@ -352,12 +364,7 @@
     this.request.exec(params, function (data, message, lastUpdated) {

       if (data.exception) {
-        OB.error('Error in datasource', data);
-        if (data.exception.message) {
-          throw data.exception.message;
-        } else {
-          throw data.exception;
-        }
+        return;
       }

       if (lastUpdated) {
@@ -370,7 +377,9 @@
         OB.Dal.initCache(me.request.model, data, function () {
           me.trigger('ready');
         }, function () {
-          OB.error(arguments, me.request.model.prototype);
+          if (me.request.source !== "org.openbravo.retail.posterminal.master.Product") {
+            OB.error(arguments, me.request.model.prototype);
+          }
         }, incremental);
       } else {
         me.trigger('ready');
diff -r bb886af61ae4 -r 1e71aac70f24 web/org.openbravo.mobile.core/source/model/ob-terminal-model.js
--- a/web/org.openbravo.mobile.core/source/model/ob-terminal-model.js	Tue Aug 05 06:05:38 2014 +0000
+++ b/web/org.openbravo.mobile.core/source/model/ob-terminal-model.js	Tue Sep 09 20:35:31 2014 +0200
@@ -120,7 +120,7 @@
         // do nothing
       },
       login: function () {
-        this.terminal.renderLogin();
+        this.terminal.initializeCommonComponents();
       },

       initialize: function (associatedTerminal) {
@@ -607,13 +607,13 @@
               // Notifying view component that I'm rendered so login page can be
               // shown if needed
               OB.MobileApp.view.terminal.trigger('viewRendered');
-              me.navigate('login');
+              me.renderLogin();
             }
           }, function () {
             // Notifying view component that I'm rendered so login page can be
             // shown if needed
             OB.MobileApp.view.terminal.trigger('viewRendered');
-            me.navigate('login');
+            me.renderLogin();
           });
         });
       },
@@ -626,7 +626,7 @@
         // shown if needed
         OB.MobileApp.view.terminal.trigger('viewRendered');
         OB.UTIL.showLoading(false);
-        me.navigate('login');
+        me.renderLogin();
       }
     });

@@ -862,6 +862,9 @@
     }, function () {
       OB.error(arguments);
       OB.MobileApp.model.triggerLogout();
+    }, function () {
+      OB.error(arguments);
+      OB.MobileApp.model.triggerLogout();
     });
   },

@@ -1116,7 +1119,7 @@
         });
       }
     }
-    window.location = paramsStr ? window.location.pathname + paramsStr : window.location.pathname;
+    window.location.reload();
   },

   triggerLoginSuccess: function () {
