Persit multiple locations in bulk

merge-requests/580/head
inoshperera 4 years ago
parent 9237f8a54f
commit 9ea055853a

@ -80,6 +80,9 @@ public interface DeviceInformationManager {
void addDeviceLocation(Device device, DeviceLocation deviceLocation) throws DeviceDetailsMgtException;
void addDeviceLocations(Device device, List<DeviceLocation> deviceLocations) throws
DeviceDetailsMgtException;
/**
* This method will return the device location with latitude, longitude, address etc..
* @param deviceIdentifier - Device identifier, device type.

@ -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> deviceLocation, int tenantId)
throws DeviceDetailsMgtDAOException;
void updateDeviceInformation(int deviceId, int enrollmentId, DeviceInfo newDeviceInfo) throws DeviceDetailsMgtDAOException;
void updateDeviceLocation(DeviceLocation deviceLocation, int enrollmentId) throws DeviceDetailsMgtDAOException;

@ -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> 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;

@ -395,6 +395,35 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
}
}
@Override
public void addDeviceLocations(Device device, List<DeviceLocation> 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);

Loading…
Cancel
Save