# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1557990059 -19800
#      Thu May 16 12:30:59 2019 +0530
# Node ID cb413503e4095e67d50c71e3ef00c9b42b576942
# Parent  23dbf292a83a5073b06e78a8bae728525f1d86d9
Related to issue 40570 : MobileServercontroller.isThereACentralServerDefined is not using bind-parameters

diff -r 23dbf292a83a -r cb413503e409 src/org/openbravo/mobile/core/servercontroller/MobileServerController.java
--- a/src/org/openbravo/mobile/core/servercontroller/MobileServerController.java	Thu May 16 12:22:33 2019 +0530
+++ b/src/org/openbravo/mobile/core/servercontroller/MobileServerController.java	Thu May 16 12:30:59 2019 +0530
@@ -150,42 +150,20 @@
       return null;
     }
 
-    OBContext.setAdminMode(false);
-    try {
-      // Get the central server, don't find ourselves to prevent infinite loops
-      // note: filters for the client of the store
-      OBQuery<MobileServerDefinition> servers = OBDal.getInstance()
-          .createQuery(MobileServerDefinition.class,
-              "client.id=:clientId and " + MobileServerDefinition.PROPERTY_ACTIVE + "=true and "
-                  + MobileServerDefinition.PROPERTY_MOBILESERVERKEY + "!= :mobileServerKey and "
-                  + MobileServerDefinition.PROPERTY_SERVERTYPE + "='"
-                  + MobileServerUtils.MAIN_SERVER + "' order by "
-                  + MobileServerDefinition.PROPERTY_PRIORITY);
+    MobileServerDefinition result = null;
+    for (MobileServerDefinition srv : getMobileServerDefinitionList(thisServer.getClient().getId(),
+        mobileServerKey, MobileServerUtils.MAIN_SERVER)) {
+      if (result == null) {
+        result = srv;
+      } else {
+        log.warn("Multiple central servers are reachable " + result + " other " + srv);
+      }
+    }
 
-      // disable the client and organization filters to allow reading this server definition
-      // with a client 0 (for instance from an ant task)
-      // it is safe to do it because there is a unique constraints on the mobile server key
-      servers.setFilterOnReadableClients(false);
-      servers.setFilterOnReadableOrganization(false);
-      servers.setNamedParameter("clientId", thisServer.getClient().getId());
-      servers.setNamedParameter("mobileServerKey", mobileServerKey);
-
-      MobileServerDefinition result = null;
-      for (MobileServerDefinition srv : servers.list()) {
-        if (result == null) {
-          result = srv;
-        } else {
-          log.warn("Multiple central servers are reachable " + result + " other " + srv);
-        }
-      }
-
-      if (result == null) {
-        throw new OBException("No central server defined/reachable");
-      }
-      return result;
-    } finally {
-      OBContext.restorePreviousMode();
+    if (result == null) {
+      throw new OBException("No central server defined/reachable");
     }
+    return result;
   }
 
   public boolean isCentralServerOnline() {
@@ -220,40 +198,47 @@
     if (thisServerClientId == null) {
       return false;
     }
-    try {
-      OBContext.setAdminMode(false);
-
-      // Get the central server
-      // note: filters for the client of the store
-      OBQuery<MobileServerDefinition> servers = OBDal.getInstance()
-          .createQuery(MobileServerDefinition.class,
-              "client.id=:clientId and " + MobileServerDefinition.PROPERTY_ACTIVE + "=true and "
-                  + MobileServerDefinition.PROPERTY_MOBILESERVERKEY + "!='" + mobileServerKey
-                  + "' and " + MobileServerDefinition.PROPERTY_SERVERTYPE + "='"
-                  + MobileServerUtils.MAIN_SERVER + "' order by "
-                  + MobileServerDefinition.PROPERTY_PRIORITY);
-
-      servers.setNamedParameter("clientId", thisServerClientId);
-
-      MobileServerDefinition result = null;
-      for (MobileServerDefinition srv : servers.list()) {
-        if (result == null) {
-          result = srv;
-        } else {
-          log.warn("Multiple central servers are reachable " + result + " other " + srv);
-        }
+    MobileServerDefinition result = null;
+    for (MobileServerDefinition srv : getMobileServerDefinitionList(thisServerClientId,
+        mobileServerKey, MobileServerUtils.MAIN_SERVER)) {
+      if (result == null) {
+        result = srv;
+      } else {
+        log.warn("Multiple central servers are reachable " + result + " other " + srv);
       }
-      return result != null;
-    } finally {
-      OBContext.restorePreviousMode();
     }
-
+    return result != null;
   }
 
   private void checkMultiServer() {
     Check.isTrue(isMultiServer, "There is no mobile server key defined for this server");
   }
 
+  private List<MobileServerDefinition> getMobileServerDefinitionList(final String clientId,
+      final String mobileServerDefinitionKey, final String serverType) {
+    OBContext.setAdminMode(false);
+    try {
+      OBQuery<MobileServerDefinition> servers = OBDal.getInstance()
+          .createQuery(MobileServerDefinition.class, "client.id=:clientId and "//
+              + MobileServerDefinition.PROPERTY_ACTIVE + "=true and "//
+              + MobileServerDefinition.PROPERTY_MOBILESERVERKEY + "!= :mobileServerKey and "
+              + MobileServerDefinition.PROPERTY_SERVERTYPE + "= :serverType "//
+              + "order by " + MobileServerDefinition.PROPERTY_PRIORITY);
+
+      // disable the client and organization filters to allow reading this server definition
+      // with a client 0 (for instance from an ant task)
+      // it is safe to do it because there is a unique constraints on the mobile server key
+      servers.setFilterOnReadableClients(false);
+      servers.setFilterOnReadableOrganization(false);
+      servers.setNamedParameter("clientId", clientId);
+      servers.setNamedParameter("mobileServerKey", mobileServerDefinitionKey);
+      servers.setNamedParameter("serverType", serverType);
+      return servers.list();
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+  }
+
   /**
    * Creates the message to be send to another server. Note the caller has to call commit to make
    * sure the message gets send.
