Merge pull request 'Fix device count related issues' (#464) from rajitha/device-mgt-core:local-appm-imp into APPM_Imp

Reviewed-on: community/device-mgt-core#464
APPM_Imp
Rajitha Kumara 4 months ago
commit 1825e68309

@ -23,6 +23,7 @@ import java.sql.Timestamp;
public class DeviceSubscription {
private int deviceId;
private int subscriptionId;
private String deviceName;
private String deviceIdentifier;
private String deviceStatus;
@ -32,6 +33,14 @@ public class DeviceSubscription {
private Timestamp dateOfLastUpdate;
private SubscriptionData subscriptionData;
public int getSubscriptionId() {
return subscriptionId;
}
public void setSubscriptionId(int subscriptionId) {
this.subscriptionId = subscriptionId;
}
public int getDeviceId() {
return deviceId;
}

@ -26,6 +26,7 @@ public class SubscriptionData {
private String triggeredBy;
private String subscriptionType;
private Timestamp triggeredAt;
private int subscriptionId;
public String getDeviceSubscriptionStatus() {
return deviceSubscriptionStatus;
@ -58,4 +59,12 @@ public class SubscriptionData {
public void setTriggeredAt(Timestamp triggeredAt) {
this.triggeredAt = triggeredAt;
}
public int getSubscriptionId() {
return subscriptionId;
}
public void setSubscriptionId(int subscriptionId) {
this.subscriptionId = subscriptionId;
}
}

@ -40,10 +40,10 @@ import java.util.stream.Collectors;
public class SubscriptionManagementHelperUtil {
public static List<DeviceSubscription> getDeviceSubscriptionData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS,
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria,
boolean isUnsubscribed)
boolean isUnsubscribed, int limit, int offset)
throws DeviceManagementException {
List<Integer> deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
PaginationRequest paginationRequest = new PaginationRequest(0, -1);
PaginationRequest paginationRequest = new PaginationRequest(offset, limit);
paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName());
paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus());
paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner());
@ -51,6 +51,17 @@ public class SubscriptionManagementHelperUtil {
return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed);
}
public static int getTotalDeviceSubscriptionCount(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS,
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria)
throws DeviceManagementException {
List<Integer> deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
PaginationRequest paginationRequest = new PaginationRequest(-1, -1);
paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName());
paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus());
paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner());
return HelperUtil.getDeviceManagementProviderService().getDeviceCountByDeviceIds(paginationRequest, deviceIds);
}
private static List<DeviceSubscription> populateDeviceData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS,
List<Device> devices, boolean isUnsubscribed) {
List<DeviceSubscription> deviceSubscriptions = new ArrayList<>();
@ -82,6 +93,7 @@ public class SubscriptionManagementHelperUtil {
subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp());
subscriptionData.setDeviceSubscriptionStatus(deviceSubscriptionDTO.getStatus());
subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getActionTriggeredFrom());
subscriptionData.setSubscriptionId(deviceSubscriptionDTO.getId());
return subscriptionData;
}

@ -98,14 +98,13 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc
} else {
deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.
getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null,
deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit);
deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO.
getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null,
deviceSubscriptionFilterCriteria.getTriggeredBy());
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting device details";

@ -99,11 +99,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
GroupDetailsDTO groupDetailsDTO;
List<String> dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus());
List<Integer> allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds();
if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
List<Integer> allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds();
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null,
null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
@ -130,12 +130,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus,
null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, dbSubscriptionStatus,
null, deviceSubscriptionFilterCriteria.getTriggeredBy());
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (GroupManagementException e) {
String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier();
@ -193,7 +192,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false);
List<Integer> deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList());
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByGroup, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId);
getSubscriptionStatistic(deviceIdsOwnByGroup, null, isUnsubscribe, tenantId);
int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier());
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (ApplicationManagementDAOException e) {

@ -112,13 +112,12 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset);
deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy());
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.
getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (UserStoreException e) {
@ -175,7 +174,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
ConnectionManagerUtil.openDBConnection();
List<Integer> deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId);
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByRole, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId);
getSubscriptionStatistic(deviceIdsOwnByRole, null, isUnsubscribe, tenantId);
int allDeviceCount = deviceIdsOwnByRole.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) {

@ -107,15 +107,14 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus,
null, deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset);
null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus,
null, deviceSubscriptionFilterCriteria.getTriggeredBy());
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting device details";
@ -167,7 +166,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
ConnectionManagerUtil.openDBConnection();
List<Integer> deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier());
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByUser, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId);
getSubscriptionStatistic(deviceIdsOwnByUser, null, isUnsubscribe, tenantId);
int allDeviceCount = deviceIdsOwnByUser.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (DeviceManagementException | ApplicationManagementDAOException e) {

@ -876,4 +876,7 @@ public interface DeviceDAO {
List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
}

@ -3428,7 +3428,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
if (paginationRequest.getOwner() != null) {
sql = sql + " AND e.OWNER = ?";
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
}
@ -3438,10 +3438,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
if (paginationRequest.getDeviceName() != null) {
sql = sql + " AND d.NAME = ?";
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql = sql + " LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int parameterIdx = 1;
preparedStatement.setInt(parameterIdx++, tenantId);
@ -3451,11 +3453,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
if (isOwnerProvided)
preparedStatement.setString(parameterIdx++, paginationRequest.getOwner());
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%");
if (isDeviceStatusProvided)
preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus());
if (isDeviceNameProvided)
preparedStatement.setString(parameterIdx, paginationRequest.getDeviceName());
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%");
preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount());
preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex());
try(ResultSet resultSet = preparedStatement.executeQuery()) {
Device device;
@ -3483,4 +3488,64 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
@Override
public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
int deviceCount = 0;
if (deviceIds == null || deviceIds.isEmpty()) return deviceCount;
String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(","));
boolean isOwnerProvided = false;
boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false;
try {
Connection connection = getConnection();
String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) AS COUNT FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " +
"WHERE d.ID = e.DEVICE_ID AND 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;
}
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int parameterIdx = 1;
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() + "%");
try(ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
deviceCount = resultSet.getInt("COUNT");
}
}
}
return deviceCount;
} catch (SQLException e) {
String msg = "Error occurred while retrieving device count for device ids in: " + deviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
}

@ -1162,4 +1162,6 @@ public interface DeviceManagementProviderService {
List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException;
public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException;
}

@ -5692,4 +5692,30 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection();
}
}
@Override
public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (paginationRequest == null) {
String msg = "Received null for pagination request";
log.error(msg);
throw new DeviceManagementException(msg);
}
try {
DeviceManagementDAOFactory.openConnection();
return deviceDAO.getDeviceCountByDeviceIds(paginationRequest, deviceIds, tenantId);
} catch (DeviceManagementDAOException e) {
String msg = "Error encountered while getting devices for device ids in " + deviceIds;
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error encountered while getting the database connection";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
}

Loading…
Cancel
Save