From 918302475b2ee4a5ca0648280736c0a860d9c3bf Mon Sep 17 00:00:00 2001
From: mharindu <milanharindu.ucsc@gmail.com>
Date: Thu, 7 Apr 2016 17:28:27 +0530
Subject: [PATCH] Fixed incorrect location coordinate issue (EMM-1313)

---
 .../mgt/mobile/android/impl/AndroidDeviceManager.java     | 8 +++++---
 .../android/impl/dao/impl/AndroidDeviceDAOImpl.java       | 2 +-
 .../mgt/mobile/impl/android/AndroidDeviceManager.java     | 8 +++++---
 .../impl/android/dao/impl/AndroidDeviceDAOImpl.java       | 2 +-
 4 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManager.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManager.java
index e4996c0e4..545de690a 100644
--- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManager.java
+++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/AndroidDeviceManager.java
@@ -149,16 +149,18 @@ public class AndroidDeviceManager implements DeviceManager {
     @Override
     public boolean enrollDevice(Device device) throws DeviceManagementException {
         boolean status = false;
-        MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
+        boolean isEnrolled = this.isEnrolled(
+                new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
+
         try {
             if (log.isDebugEnabled()) {
                 log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
             }
-            boolean isEnrolled = this.isEnrolled(
-                    new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
+
             if (isEnrolled) {
                 this.modifyEnrollment(device);
             } else {
+                MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
                 AndroidDAOFactory.beginTransaction();
                 status = daoFactory.getMobileDeviceDAO().addMobileDevice(mobileDevice);
                 AndroidDAOFactory.commitTransaction();
diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java
index 0d831901d..1c72d858b 100644
--- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java
+++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java
@@ -116,7 +116,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
 			stmt.setString(5, mobileDevice.getVendor());
 			stmt.setString(6, mobileDevice.getMobileDeviceId());
 			stmt.setString(7, properties.get(AndroidPluginConstants.DEVICE_NAME));
-			stmt.setString(8, mobileDevice.getLongitude());
+			stmt.setString(8, mobileDevice.getLatitude());
 			stmt.setString(9, mobileDevice.getLongitude());
 			stmt.setString(10, mobileDevice.getImei());
 			stmt.setString(11, mobileDevice.getImsi());
diff --git a/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java b/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java
index 472589e6c..e56b4f768 100644
--- a/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java
+++ b/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java
@@ -143,16 +143,18 @@ public class AndroidDeviceManager implements DeviceManager {
     @Override
     public boolean enrollDevice(Device device) throws DeviceManagementException {
         boolean status = false;
-        MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
+        boolean isEnrolled = this.isEnrolled(
+                new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
+
         try {
             if (log.isDebugEnabled()) {
                 log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
             }
-            boolean isEnrolled = this.isEnrolled(
-                    new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
+
             if (isEnrolled) {
                 this.modifyEnrollment(device);
             } else {
+                MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
                 AndroidDAOFactory.beginTransaction();
                 status = daoFactory.getMobileDeviceDAO().addMobileDevice(mobileDevice);
                 AndroidDAOFactory.commitTransaction();
diff --git a/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java b/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java
index a43fcab9d..84ad8512e 100644
--- a/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java
+++ b/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java
@@ -116,7 +116,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
 			stmt.setString(5, mobileDevice.getVendor());
 			stmt.setString(6, mobileDevice.getMobileDeviceId());
 			stmt.setString(7, properties.get(AndroidPluginConstants.DEVICE_NAME));
-			stmt.setString(8, mobileDevice.getLongitude());
+			stmt.setString(8, mobileDevice.getLatitude());
 			stmt.setString(9, mobileDevice.getLongitude());
 			stmt.setString(10, mobileDevice.getImei());
 			stmt.setString(11, mobileDevice.getImsi());