tcdlpds 5 months ago
commit 898a12dc31

@ -305,6 +305,12 @@ public interface DeviceManagementService {
required = false) required = false)
@QueryParam("groupId") @QueryParam("groupId")
int groupId, int groupId,
@ApiParam(
name = "excludeGroupId",
value = "Id of the group that needs to get the devices that are not belong.",
required = false)
@QueryParam("excludeGroupId")
int excludeGroupId,
@ApiParam( @ApiParam(
name = "since", name = "since",
value = "Checks if the requested variant was created since the specified date-time.\n" + value = "Checks if the requested variant was created since the specified date-time.\n" +

@ -156,6 +156,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("customProperty") String customProperty, @QueryParam("customProperty") String customProperty,
@QueryParam("status") List<String> status, @QueryParam("status") List<String> status,
@QueryParam("groupId") int groupId, @QueryParam("groupId") int groupId,
@QueryParam("excludeGroupId") int excludeGroupId,
@QueryParam("since") String since, @QueryParam("since") String since,
@HeaderParam("If-Modified-Since") String ifModifiedSince, @HeaderParam("If-Modified-Since") String ifModifiedSince,
@QueryParam("requireDeviceInfo") boolean requireDeviceInfo, @QueryParam("requireDeviceInfo") boolean requireDeviceInfo,
@ -218,7 +219,22 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
request.setStatusList(status); request.setStatusList(status);
} }
} }
// this is the user who initiates the request
if (excludeGroupId != 0) {
request.setGroupId(excludeGroupId);
if (user != null && !user.isEmpty()) {
request.setOwner(MultitenantUtils.getTenantAwareUsername(user));
} else if (userPattern != null && !userPattern.isEmpty()) {
request.setOwnerPattern(userPattern);
}
result = dms.getDevicesNotInGroup(request, requireDeviceInfo);
devices.setList((List<Device>) result.getData());
devices.setCount(result.getRecordsTotal());
return Response.status(Response.Status.OK).entity(devices).build();
}
String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
if (groupId != 0) { if (groupId != 0) {

@ -157,7 +157,7 @@ public class DeviceManagementServiceImplTest {
.toReturn(this.deviceAccessAuthorizationService); .toReturn(this.deviceAccessAuthorizationService);
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 1, null, null, false, null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
} }
@ -177,22 +177,22 @@ public class DeviceManagementServiceImplTest {
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null,null, DEFAULT_STATUS_LIST, 1, null, null, false, null,null, DEFAULT_STATUS_LIST, 1, 0, null, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, null, DEFAULT_OWNERSHIP, .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, null, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 1, null, null, false, null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP, .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 1, null, null, false, null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP, .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 1, null, null, true, null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, true,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
} }
@ -307,7 +307,7 @@ public class DeviceManagementServiceImplTest {
Mockito.when(deviceAccessAuthorizationService.isDeviceAdminUser()).thenReturn(true); Mockito.when(deviceAccessAuthorizationService.isDeviceAdminUser()).thenReturn(true);
deviceManagementService.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, deviceManagementService.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null,
DEFAULT_ROLE, DEFAULT_OWNERSHIP, null,null, DEFAULT_STATUS_LIST, 1, DEFAULT_ROLE, DEFAULT_OWNERSHIP, null,null, DEFAULT_STATUS_LIST, 1,
null, null, false, 10, 5); 0, null, null, false, 10, 5);
} }
@Test(description = "Testing get devices when user is the device admin") @Test(description = "Testing get devices when user is the device admin")
@ -326,11 +326,11 @@ public class DeviceManagementServiceImplTest {
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP
, null, null, DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); , null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, false, 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, null, DEFAULT_USERNAME, DEFAULT_ROLE, DEFAULT_OWNERSHIP .getDevices(null, TEST_DEVICE_TYPE, null, DEFAULT_USERNAME, DEFAULT_ROLE, DEFAULT_OWNERSHIP
, null, null, DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); , null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, false, 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
} }
@ -352,7 +352,7 @@ public class DeviceManagementServiceImplTest {
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, "newuser", null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, "newuser", null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 0, null, null, false, null, null, DEFAULT_STATUS_LIST, 0, 0, null, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode());
Mockito.reset(this.deviceAccessAuthorizationService); Mockito.reset(this.deviceAccessAuthorizationService);
@ -374,17 +374,17 @@ public class DeviceManagementServiceImplTest {
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 0, null, ifModifiedSince, false, null, null, DEFAULT_STATUS_LIST, 0, 0, null, ifModifiedSince, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 0, null, ifModifiedSince, true, null, null, DEFAULT_STATUS_LIST, 0, 0, null, ifModifiedSince, true,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 0, null, "ErrorModifiedSince", null, null, DEFAULT_STATUS_LIST, 0, 0, null, "ErrorModifiedSince",
false, 10, 5); false, 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
} }
@ -405,17 +405,17 @@ public class DeviceManagementServiceImplTest {
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null,DEFAULT_STATUS_LIST, 0, since, null, false, null, null,DEFAULT_STATUS_LIST, 0, 0, since, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null,DEFAULT_STATUS_LIST, 0, since, null, true, null, null,DEFAULT_STATUS_LIST, 0, 0, since, null, true,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
response = this.deviceManagementService response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null,DEFAULT_STATUS_LIST, 0, "ErrorSince", null, false, null, null,DEFAULT_STATUS_LIST, 0, 0, "ErrorSince", null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
} }
@ -438,7 +438,7 @@ public class DeviceManagementServiceImplTest {
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 1, null, null, false, null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
Mockito.reset(this.deviceManagementProviderService); Mockito.reset(this.deviceManagementProviderService);
@ -461,7 +461,7 @@ public class DeviceManagementServiceImplTest {
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
null, null, DEFAULT_STATUS_LIST, 1, null, null, false, null, null, DEFAULT_STATUS_LIST, 1, 0, null, null, false,
10, 5); 10, 5);
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
Mockito.reset(this.deviceAccessAuthorizationService); Mockito.reset(this.deviceAccessAuthorizationService);

@ -844,4 +844,24 @@ public interface DeviceDAO {
List<String> getAgentVersions(int tenantId) throws DeviceManagementDAOException; List<String> getAgentVersions(int tenantId) throws DeviceManagementDAOException;
List<Device> getDevicesEnrolledSince(Date since) throws DeviceManagementDAOException; List<Device> getDevicesEnrolledSince(Date since) throws DeviceManagementDAOException;
List<Device> getDevicesEnrolledPriorTo(Date priorTo) throws DeviceManagementDAOException; List<Device> getDevicesEnrolledPriorTo(Date priorTo) throws DeviceManagementDAOException;
/**
* This method is used to search for devices that are not in a specific group.
*
* @param request PaginationRequest object holding the data for pagination
* @param tenantId tenant id.
* @return returns paginated list of devices.
* @throws DeviceManagementDAOException
*/
List<Device> searchDevicesNotInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to get device count that are not within a specific group.
*
* @param request PaginationRequest object holding the data for pagination
* @param tenantId tenant id
* @return Device count
* @throws DeviceManagementDAOException
*/
int getCountOfDevicesNotInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
} }

@ -3190,4 +3190,112 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
public abstract void refactorDeviceStatus (Connection conn, List<Device> validDevices) public abstract void refactorDeviceStatus (Connection conn, List<Device> validDevices)
throws DeviceManagementDAOException; throws DeviceManagementDAOException;
@Override
public int getCountOfDevicesNotInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException {
int deviceCount = 0;
int groupId = request.getGroupId();
String deviceType = request.getDeviceType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
String owner = request.getOwner();
boolean isOwnerProvided = false;
String ownerPattern = request.getOwnerPattern();
boolean isOwnerPatternProvided = false;
String ownership = request.getOwnership();
boolean isOwnershipProvided = false;
List<String> statusList = request.getStatusList();
boolean isStatusProvided = false;
Date since = request.getSince();
boolean isSinceProvided = false;
try {
Connection conn = getConnection();
String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT " +
"FROM DM_ENROLMENT e, " +
"(SELECT gd.ID AS DEVICE_ID, " +
"gd.DESCRIPTION, " +
"gd.NAME, " +
"gd.DEVICE_IDENTIFICATION " +
"FROM DM_DEVICE gd " +
"WHERE gd.ID NOT IN (SELECT dgm.DEVICE_ID " +
"FROM DM_DEVICE_GROUP_MAP dgm " +
"WHERE dgm.GROUP_ID = ?) " +
"AND gd.TENANT_ID = ?";
if (deviceName != null && !deviceName.isEmpty()) {
sql += " AND gd.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql += " AND 1=1";
if (since != null) {
sql += " AND gd.LAST_UPDATED_TIMESTAMP > ?";
isSinceProvided = true;
}
sql += " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND e.TENANT_ID = ?";
if (deviceType != null && !deviceType.isEmpty()) {
sql += " AND e.DEVICE_TYPE = ?";
isDeviceTypeProvided = true;
}
if (ownership != null && !ownership.isEmpty()) {
sql += " AND e.OWNERSHIP = ?";
isOwnershipProvided = true;
}
if (owner != null && !owner.isEmpty()) {
sql += " AND e.OWNER = ?";
isOwnerProvided = true;
} else if (ownerPattern != null && !ownerPattern.isEmpty()) {
sql += " AND e.OWNER LIKE ?";
isOwnerPatternProvided = true;
}
if (statusList != null && !statusList.isEmpty()) {
sql += buildStatusQuery(statusList);
isStatusProvided = true;
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIdx = 1;
stmt.setInt(paramIdx++, groupId);
stmt.setInt(paramIdx++, tenantId);
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, "%" + deviceName + "%");
}
if (isSinceProvided) {
stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime()));
}
stmt.setInt(paramIdx++, tenantId);
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, deviceType);
}
if (isOwnershipProvided) {
stmt.setString(paramIdx++, ownership);
}
if (isOwnerProvided) {
stmt.setString(paramIdx++, owner);
} else if (isOwnerPatternProvided) {
stmt.setString(paramIdx++, ownerPattern + "%");
}
if (isStatusProvided) {
for (String status : statusList) {
stmt.setString(paramIdx++, status);
}
}
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
return deviceCount;
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving count of devices not in group: " + groupId;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
} }

@ -724,7 +724,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
//Add the query for owner //Add the query for owner
if (owner != null && !owner.isEmpty()) { if (owner != null && !owner.isEmpty()) {
sql = sql + " AND e.OWNER = ?"; sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true; isOwnerProvided = true;
} else if (ownerPattern != null && !ownerPattern.isEmpty()) { } else if (ownerPattern != null && !ownerPattern.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?"; sql = sql + " AND e.OWNER LIKE ?";
@ -776,7 +776,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setString(paramIdx++, ownership); stmt.setString(paramIdx++, ownership);
} }
if (isOwnerProvided) { if (isOwnerProvided) {
stmt.setString(paramIdx++, owner); stmt.setString(paramIdx++, "%" + owner + "%");
} else if (isOwnerPatternProvided) { } else if (isOwnerPatternProvided) {
stmt.setString(paramIdx++, ownerPattern + "%"); stmt.setString(paramIdx++, ownerPattern + "%");
} }
@ -1689,4 +1689,172 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
} }
@Override
public List<Device> searchDevicesNotInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException {
List<Device> devices = null;
int groupId = request.getGroupId();
String deviceType = request.getDeviceType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
String owner = request.getOwner();
boolean isOwnerProvided = false;
String ownerPattern = request.getOwnerPattern();
boolean isOwnerPatternProvided = false;
String ownership = request.getOwnership();
boolean isOwnershipProvided = false;
List<String> statusList = request.getStatusList();
boolean isStatusProvided = false;
Date since = request.getSince();
boolean isSinceProvided = false;
String serial = request.getSerialNumber();
boolean isSerialProvided = false;
try {
Connection conn = getConnection();
String sql = "SELECT d1.DEVICE_ID, " +
"d1.DESCRIPTION, " +
"d1.NAME AS DEVICE_NAME, " +
"e.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, " +
"d1.LAST_UPDATED_TIMESTAMP, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.IS_TRANSFERRED, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, " +
"e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, " +
"(SELECT gd.DEVICE_ID, " +
"gd.DESCRIPTION, " +
"gd.NAME, " +
"gd.DEVICE_IDENTIFICATION, " +
"gd.LAST_UPDATED_TIMESTAMP " +
"FROM " +
"(SELECT d.ID AS DEVICE_ID, " +
"d.DESCRIPTION, " +
"d.NAME, " +
"d.DEVICE_IDENTIFICATION, " +
"d.LAST_UPDATED_TIMESTAMP " +
"FROM DM_DEVICE d " +
"WHERE d.ID NOT IN " +
"(SELECT dgm.DEVICE_ID " +
"FROM DM_DEVICE_GROUP_MAP dgm " +
"WHERE dgm.GROUP_ID = ?) " +
"AND d.TENANT_ID = ?";
if (deviceName != null && !deviceName.isEmpty()) {
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql = sql + ") gd";
sql = sql + " WHERE 1 = 1";
if (since != null) {
sql = sql + " AND gd.LAST_UPDATED_TIMESTAMP > ?";
isSinceProvided = true;
}
sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND e.TENANT_ID = ? ";
if (deviceType != null && !deviceType.isEmpty()) {
sql = sql + " AND e.DEVICE_TYPE = ?";
isDeviceTypeProvided = true;
}
if (ownership != null && !ownership.isEmpty()) {
sql = sql + " AND e.OWNERSHIP = ?";
isOwnershipProvided = true;
}
if (owner != null && !owner.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
} else if (ownerPattern != null && !ownerPattern.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerPatternProvided = true;
}
if (statusList != null && !statusList.isEmpty()) {
sql += buildStatusQuery(statusList);
isStatusProvided = true;
}
if (serial != null || !request.getCustomProperty().isEmpty()) {
if (serial != null) {
sql += "AND EXISTS (" +
"SELECT VALUE_FIELD " +
"FROM DM_DEVICE_INFO di " +
"WHERE di.DEVICE_ID = d1.DEVICE_ID " +
"AND di.KEY_FIELD = 'serial' " +
"AND di.VALUE_FIELD LIKE ?) ";
isSerialProvided = true;
}
if (!request.getCustomProperty().isEmpty()) {
for (Map.Entry<String, String> entry : request.getCustomProperty().entrySet()) {
sql += "AND EXISTS (" +
"SELECT VALUE_FIELD " +
"FROM DM_DEVICE_INFO di2 " +
"WHERE di2.DEVICE_ID = d1.DEVICE_ID " +
"AND di2.KEY_FIELD = '" + entry.getKey() + "' " +
"AND di2.VALUE_FIELD LIKE ?)";
}
}
}
sql = sql + " LIMIT ? OFFSET ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIdx = 1;
stmt.setInt(paramIdx++, groupId);
stmt.setInt(paramIdx++, tenantId);
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, "%" + deviceName + "%");
}
if (isSinceProvided) {
stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime()));
}
stmt.setInt(paramIdx++, tenantId);
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, deviceType);
}
if (isOwnershipProvided) {
stmt.setString(paramIdx++, ownership);
}
if (isOwnerProvided) {
stmt.setString(paramIdx++, "%" + owner + "%");
} else if (isOwnerPatternProvided) {
stmt.setString(paramIdx++, "%" + ownerPattern + "%");
}
if (isStatusProvided) {
for (String status : statusList) {
stmt.setString(paramIdx++, status);
}
}
if (isSerialProvided) {
stmt.setString(paramIdx++, "%" + serial + "%");
}
if (!request.getCustomProperty().isEmpty()) {
for (Map.Entry<String, String> entry : request.getCustomProperty().entrySet()) {
stmt.setString(paramIdx++, "%" + entry.getValue() + "%");
}
}
stmt.setInt(paramIdx++, request.getRowCount());
stmt.setInt(paramIdx, request.getStartIndex());
try (ResultSet rs = stmt.executeQuery()) {
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving information of" +
" devices not belonging to group : " + groupId;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
} }

@ -1103,4 +1103,18 @@ public interface DeviceManagementProviderService {
* @throws OperationManagementException if an error occurs while fetching the operation details. * @throws OperationManagementException if an error occurs while fetching the operation details.
*/ */
OperationDTO getOperationDetailsById(int operationId) throws OperationManagementException; OperationDTO getOperationDetailsById(int operationId) throws OperationManagementException;
/**
* Method to retrieve all the devices that are not in a group with pagination support.
*
* @param request PaginationRequest object holding the data for pagination
* @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required
* along with the device data.
* @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices.
*/
PaginationResult getDevicesNotInGroup(PaginationRequest request, boolean requireDeviceInfo)
throws DeviceManagementException;
} }

@ -5457,4 +5457,58 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return operationDetails; return operationDetails;
} }
@Override
public PaginationResult getDevicesNotInGroup(PaginationRequest request, boolean requireDeviceInfo)
throws DeviceManagementException {
if (request == null) {
String msg = "Received incomplete pagination request for method getDevicesNotInGroup";
log.error(msg);
throw new DeviceManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get devices not in group with pagination " + request.toString() +
" and requiredDeviceInfo: " + requireDeviceInfo);
}
PaginationResult paginationResult = new PaginationResult();
List<Device> devicesNotInGroup = null;
int count = 0;
int tenantId = this.getTenantId();
DeviceManagerUtil.validateDeviceListPageSize(request);
try {
DeviceManagementDAOFactory.openConnection();
if (request.getGroupId() != 0) {
devicesNotInGroup = deviceDAO.searchDevicesNotInGroup(request, tenantId);
count = deviceDAO.getCountOfDevicesNotInGroup(request, tenantId);
} else {
String msg = "Group ID is not provided for method getDevicesNotInGroup";
log.error(msg);
throw new DeviceManagementException(msg);
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving device list that are not in the specified group for the current tenant";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getDevicesNotInGroup";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
if (requireDeviceInfo && devicesNotInGroup != null && !devicesNotInGroup.isEmpty()) {
paginationResult.setData(populateAllDeviceInfo(devicesNotInGroup));
} else {
paginationResult.setData(devicesNotInGroup);
}
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult;
}
} }

Loading…
Cancel
Save