Update generic device dao

issue-10462/secure-pending-operation-6.2
Lasantha Dharmakeerthi 4 months ago
commit b5adeff7ab

@ -3408,108 +3408,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
} }
} }
@Override
public List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Device> devices = new ArrayList<>();
if (deviceIds == null || deviceIds.isEmpty()) return devices;
String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(","));
boolean isOwnerProvided = false;
boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false;
boolean isDeviceTypeIdProvided = false;
try {
Connection connection = getConnection();
String sql = "SELECT e.DEVICE_ID, " +
"d.DEVICE_IDENTIFICATION, " +
"e.STATUS, " +
"e.OWNER, " +
"d.NAME AS DEVICE_NAME, " +
"e.DEVICE_TYPE, " +
"e.OWNERSHIP, " +
"e.DATE_OF_LAST_UPDATE " +
"FROM DM_DEVICE d " +
"INNER JOIN DM_ENROLMENT e " +
"ON d.ID = e.DEVICE_ID " +
"WHERE d.TENANT_ID = ? " +
"AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " +
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
if (paginationRequest.getOwner() != null) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
}
if (paginationRequest.getDeviceStatus() != null) {
sql = sql + " AND e.STATUS = ?";
isDeviceStatusProvided = true;
}
if (paginationRequest.getDeviceName() != null) {
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
if (paginationRequest.getDeviceTypeId() > 0) {
sql = sql + " AND d.DEVICE_TYPE_ID = ?";
isDeviceTypeIdProvided = true;
}
sql = sql + " LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int parameterIdx = 1;
preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId());
preparedStatement.setInt(parameterIdx++, tenantId);
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(parameterIdx++, deviceId);
}
if (isOwnerProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%");
}
if (isDeviceStatusProvided) {
preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus());
}
if (isDeviceNameProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%");
}
if (isDeviceTypeIdProvided) {
preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId());
}
preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount());
preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex());
try(ResultSet resultSet = preparedStatement.executeQuery()) {
Device device;
while(resultSet.next()) {
device = new Device();
device.setId(resultSet.getInt("DEVICE_ID"));
device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION"));
device.setName(resultSet.getString("DEVICE_NAME"));
device.setType(resultSet.getString("DEVICE_TYPE"));
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS")));
enrolmentInfo.setOwner(resultSet.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP")));
enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
device.setEnrolmentInfo(enrolmentInfo);
devices.add(device);
}
}
}
return devices;
} catch (SQLException e) {
String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override @Override
public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId) public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {

@ -1870,6 +1870,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
boolean isDeviceStatusProvided = false; boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false; boolean isDeviceNameProvided = false;
boolean isDeviceTypeIdProvided = false; boolean isDeviceTypeIdProvided = false;
try { try {
Connection connection = getConnection(); Connection connection = getConnection();
String sql = "SELECT e.DEVICE_ID, " + String sql = "SELECT e.DEVICE_ID, " +
@ -1882,32 +1883,41 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"e.DATE_OF_LAST_UPDATE " + "e.DATE_OF_LAST_UPDATE " +
"FROM DM_DEVICE d " + "FROM DM_DEVICE d " +
"INNER JOIN DM_ENROLMENT e " + "INNER JOIN DM_ENROLMENT e " +
"ON d.ID = e.DEVICE_ID " +
"WHERE d.TENANT_ID = ? " + "WHERE d.TENANT_ID = ? " +
"AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " +
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; "AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
if (paginationRequest.getOwner() != null) { if (paginationRequest.getOwner() != null) {
sql = sql + " AND e.OWNER LIKE ?"; sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true; isOwnerProvided = true;
} }
if (paginationRequest.getDeviceStatus() != null) { if (paginationRequest.getDeviceStatus() != null) {
sql = sql + " AND e.STATUS = ?"; sql = sql + " AND e.STATUS = ?";
isDeviceStatusProvided = true; isDeviceStatusProvided = true;
} }
if (paginationRequest.getDeviceName() != null) { if (paginationRequest.getDeviceName() != null) {
sql = sql + " AND d.NAME LIKE ?"; sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true; isDeviceNameProvided = true;
} }
if (paginationRequest.getDeviceTypeId() > 0) { if (paginationRequest.getDeviceTypeId() > 0) {
sql = sql + " AND d.DEVICE_TYPE_ID = ?"; sql = sql + " AND d.DEVICE_TYPE_ID = ?";
isDeviceTypeIdProvided = true; isDeviceTypeIdProvided = true;
} }
sql = sql + " LIMIT ? OFFSET ?"; sql = sql + " LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int parameterIdx = 1; int parameterIdx = 1;
preparedStatement.setInt(parameterIdx++, tenantId); preparedStatement.setInt(parameterIdx++, tenantId);
for (Integer deviceId : deviceIds) { for (Integer deviceId : deviceIds) {
preparedStatement.setInt(parameterIdx++, deviceId); preparedStatement.setInt(parameterIdx++, deviceId);
} }
if (isOwnerProvided) { if (isOwnerProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%");
} }
@ -1923,6 +1933,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount()); preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount());
preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex()); preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex());
try(ResultSet resultSet = preparedStatement.executeQuery()) { try(ResultSet resultSet = preparedStatement.executeQuery()) {
Device device; Device device;
while(resultSet.next()) { while(resultSet.next()) {

Loading…
Cancel
Save