From 9ea055853a5fbf8f77bede52258d052290870ec1 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Mon, 22 Jun 2020 09:17:59 +0530 Subject: [PATCH] Persit multiple locations in bulk --- .../details/mgt/DeviceInformationManager.java | 3 ++ .../details/mgt/dao/DeviceDetailsDAO.java | 11 +++++ .../mgt/dao/impl/DeviceDetailsDAOImpl.java | 40 +++++++++++++++++++ .../impl/DeviceInformationManagerImpl.java | 29 ++++++++++++++ 4 files changed, 83 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index 0e401955ea..df47d83e18 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -80,6 +80,9 @@ public interface DeviceInformationManager { void addDeviceLocation(Device device, DeviceLocation deviceLocation) throws DeviceDetailsMgtException; + void addDeviceLocations(Device device, List deviceLocations) throws + DeviceDetailsMgtException; + /** * This method will return the device location with latitude, longitude, address etc.. * @param deviceIdentifier - Device identifier, device type. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java index a97b541b07..af0c930fcd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java @@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import java.util.List; import java.util.Map; /** @@ -115,6 +116,16 @@ public interface DeviceDetailsDAO { void addDeviceLocationInfo(Device device, DeviceLocation deviceLocation, int tenantId) throws DeviceDetailsMgtDAOException; + /** + * Add device location information to the database + * @param device Device object + * @param deviceLocation Device Location Object + * @param tenantId Tenant Id + * @throws DeviceDetailsMgtDAOException + */ + void addDeviceLocationsInfo(Device device, List deviceLocation, int tenantId) + throws DeviceDetailsMgtDAOException; + void updateDeviceInformation(int deviceId, int enrollmentId, DeviceInfo newDeviceInfo) throws DeviceDetailsMgtDAOException; void updateDeviceLocation(DeviceLocation deviceLocation, int enrollmentId) throws DeviceDetailsMgtDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index 9e69e3875d..126079c0e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -36,6 +36,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; +import java.util.List; import java.util.Map; public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { @@ -424,6 +425,45 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } } + @Override + public void addDeviceLocationsInfo(Device device, List deviceLocation, + int tenantId) throws DeviceDetailsMgtDAOException { + Connection conn; + String errMessage; + String sql = "INSERT INTO " + + "DM_DEVICE_HISTORY_LAST_SEVEN_DAYS " + + "(DEVICE_ID, DEVICE_ID_NAME, TENANT_ID, DEVICE_TYPE_NAME, LATITUDE, LONGITUDE, SPEED, HEADING, " + + "TIMESTAMP, GEO_HASH, DEVICE_OWNER, DEVICE_ALTITUDE, DISTANCE) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + try { + conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (DeviceLocation location : deviceLocation) { + stmt.setInt(1, device.getId()); + stmt.setString(2, device.getDeviceIdentifier()); + stmt.setInt(3, tenantId); + stmt.setString(4, device.getType()); + stmt.setDouble(5, location.getLatitude()); + stmt.setDouble(6, location.getLongitude()); + stmt.setFloat(7, location.getSpeed()); + stmt.setFloat(8, location.getBearing()); + stmt.setLong(9, System.currentTimeMillis()); + stmt.setString(10, GeoHashGenerator.encodeGeohash(location)); + stmt.setString(11, device.getEnrolmentInfo().getOwner()); + stmt.setDouble(12, location.getAltitude()); + stmt.setDouble(13, location.getDistance()); + stmt.addBatch(); + } + stmt.executeBatch(); + } + + } catch (SQLException e) { + errMessage = "Error occurred while updating the device location information to database."; + log.error(errMessage); + throw new DeviceDetailsMgtDAOException(errMessage, e); + } + } + @Override public void updateDeviceInformation(int deviceId, int enrollmentId, DeviceInfo newDeviceInfo) throws DeviceDetailsMgtDAOException { Connection conn; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 873441968e..2b4756d0f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -395,6 +395,35 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } + @Override + public void addDeviceLocations(Device device, List deviceLocations) throws DeviceDetailsMgtException { + try { + DeviceLocation mostRecentDeviceLocation = deviceLocations.get(deviceLocations.size() - 1); + mostRecentDeviceLocation.setDeviceId(device.getId()); + DeviceManagementDAOFactory.beginTransaction(); + DeviceLocation previousLocation = deviceDetailsDAO.getDeviceLocation(device.getId(), + device.getEnrolmentInfo().getId()); + if (previousLocation == null) { + deviceDetailsDAO.addDeviceLocation(mostRecentDeviceLocation, device.getEnrolmentInfo().getId()); + } else { + deviceDetailsDAO.updateDeviceLocation(mostRecentDeviceLocation, device.getEnrolmentInfo().getId()); + } + + deviceDetailsDAO.addDeviceLocationsInfo(device, deviceLocations, + CarbonContext.getThreadLocalCarbonContext().getTenantId()); + + DeviceManagementDAOFactory.commitTransaction(); + } catch (TransactionManagementException e) { + throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location " + + "information.", e); + } catch (DeviceDetailsMgtDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceDetailsMgtException("Error occurred while adding the device location information.", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + @Override public DeviceLocation getDeviceLocation(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { Device device = getDevice(deviceId);