Fix issues related to new devices and stats

publisher-oauth-client-v5.2.3
Lasantha Dharmakeerthi 3 months ago
commit 8f6d213154

@ -527,8 +527,8 @@ public interface SubscriptionDAO {
*/
int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException;
SubscriptionStatisticDTO getSubscriptionStatistic(List<Integer> deviceIds, String subscriptionType, boolean isUnsubscribed,
int tenantId) throws ApplicationManagementDAOException;
SubscriptionStatisticDTO getSubscriptionStatistic(List<Integer> deviceIds, boolean isUnsubscribed,
int tenantId, int appReleaseId) throws ApplicationManagementDAOException;
/**
* This method is used to get the counts of devices related to a UUID.
*

@ -18,11 +18,9 @@
package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata;
import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO;
import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl;
import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException;
@ -2792,43 +2790,33 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
}
// todo: fixed the status
@Override
public SubscriptionStatisticDTO getSubscriptionStatistic(List<Integer> deviceIds, String subscriptionType,
boolean isUnsubscribed, int tenantId)
public SubscriptionStatisticDTO getSubscriptionStatistic(List<Integer> deviceIds, boolean isUnsubscribed, int tenantId, int appReleaseId)
throws ApplicationManagementDAOException {
SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO();
if (deviceIds == null || deviceIds.isEmpty()) return subscriptionStatisticDTO;
boolean doesAllEntriesRequired = true;
try {
Connection connection = getDBConnection();
String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " +
"STATUS FROM AP_DEVICE_SUBSCRIPTION " +
"WHERE TENANT_ID = ? " +
"AND UNSUBSCRIBED = ?" +
"AND AP_APP_RELEASE_ID = ? " +
"AND UNSUBSCRIBED = ? " +
"AND DM_DEVICE_ID IN (" +
deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")";
if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) {
sql += " AND ACTION_TRIGGERED_FROM = ?";
doesAllEntriesRequired = false;
}
sql += " GROUP BY (STATUS)";
deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") " +
"GROUP BY (STATUS)";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int idx = 1;
preparedStatement.setInt(idx++, tenantId);
preparedStatement.setInt(idx++, appReleaseId);
preparedStatement.setBoolean(idx++, isUnsubscribed);
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(idx++, deviceId);
}
if (!doesAllEntriesRequired) {
preparedStatement.setString(idx, subscriptionType);
}
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
// add the error and in progress

@ -1,29 +0,0 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package io.entgra.device.mgt.core.application.mgt.core.util;
public class SubscriptionManagementUtil {
public static final class DeviceSubscriptionStatus {
public static final String COMPLETED = "COMPLETED";
public static final String ERROR ="ERROR";
public static final String NEW = "NEW";
public static final String SUBSCRIBED = "SUBSCRIBED";
}
}

@ -36,7 +36,6 @@ import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import org.apache.commons.logging.Log;
@ -97,20 +96,17 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc
List<Integer> deviceIdsOfSubscription = deviceSubscriptionDTOS.stream().
map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription,
new PaginationRequest(offset, limit));
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription);
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription);
} else {
deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.
getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null,
deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
}
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);

@ -39,7 +39,6 @@ import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException;
import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO;
@ -115,11 +114,8 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
allDeviceIdsOwnByGroup.remove(deviceId);
}
List<Integer> paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup,
new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = allDeviceIdsOwnByGroup.size();
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup);
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
} else {
groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
@ -130,9 +126,10 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus,
null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
}
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
@ -188,11 +185,19 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
if (applicationReleaseDTO == null) {
String msg = "Couldn't find an application release for application release UUID: " +
subscriptionInfo.getApplicationUUID();
log.error(msg);
throw new NotFoundException(msg);
}
List<Device> devices = HelperUtil.getGroupManagementProviderService().
getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false);
List<Integer> deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList());
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByGroup, null, isUnsubscribe, tenantId);
getSubscriptionStatistic(deviceIdsOwnByGroup, isUnsubscribe, tenantId, applicationReleaseDTO.getId());
int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier());
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (ApplicationManagementDAOException e) {

@ -111,18 +111,16 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
deviceIdsOwnByRole.remove(deviceId);
}
List<Integer> paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole,
new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = deviceIdsOwnByRole.size();
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole);
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
}
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.
getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset);
@ -180,9 +178,17 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
if (applicationReleaseDTO == null) {
String msg = "Couldn't find an application release for application release UUID: " +
subscriptionInfo.getApplicationUUID();
log.error(msg);
throw new NotFoundException(msg);
}
List<Integer> deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId);
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByRole, null, isUnsubscribe, tenantId);
getSubscriptionStatistic(deviceIdsOwnByRole, isUnsubscribe, tenantId, applicationReleaseDTO.getId());
int allDeviceCount = deviceIdsOwnByRole.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) {

@ -107,19 +107,17 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
for (Integer deviceId : deviceIdsOfSubscription) {
deviceIdsOwnByUser.remove(deviceId);
}
List<Integer> paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser,
new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser);
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = deviceIdsOwnByUser.size();
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus,
null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
}
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId());
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
@ -171,9 +169,17 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
if (applicationReleaseDTO == null) {
String msg = "Couldn't find an application release for application release UUID: " +
subscriptionInfo.getApplicationUUID();
log.error(msg);
throw new NotFoundException(msg);
}
List<Integer> deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier());
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByUser, null, isUnsubscribe, tenantId);
getSubscriptionStatistic(deviceIdsOwnByUser, isUnsubscribe, tenantId, applicationReleaseDTO.getId());
int allDeviceCount = deviceIdsOwnByUser.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (DeviceManagementException | ApplicationManagementDAOException e) {

@ -865,10 +865,10 @@ public interface DeviceDAO {
*/
int getCountOfDevicesNotInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
List<Integer> getDevicesNotInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
List<Integer> getDevicesInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
List<Integer> getDevicesInGivenIdList(List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
int getDeviceCountNotInGivenIdList(List<Integer> deviceIds, int tenantId)

@ -3301,7 +3301,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
@Override
public List<Integer> getDevicesNotInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
public List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Integer> filteredDeviceIds = new ArrayList<>();
try {
@ -3312,8 +3312,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")";
}
sql += " LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int paraIdx = 1;
preparedStatement.setInt(paraIdx++, tenantId);
@ -3324,8 +3322,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
preparedStatement.setInt(paraIdx++, request.getRowCount());
preparedStatement.setInt(paraIdx, request.getStartIndex());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
@ -3341,7 +3337,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
@Override
public List<Integer> getDevicesInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
public List<Integer> getDevicesInGivenIdList(List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Integer> filteredDeviceIds = new ArrayList<>();
if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds;
@ -3351,19 +3347,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
Connection connection = getConnection();
String sql = "SELECT ID AS DEVICE_ID " +
"FROM DM_DEVICE " +
"WHERE ID IN (" + deviceIdStringList + ")" +
" AND TENANT_ID = ? " +
"LIMIT ? " +
"OFFSET ?";
"WHERE ID IN (" + deviceIdStringList + ") " +
"AND TENANT_ID = ? ";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int paraIdx = 1;
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(paraIdx++, deviceId);
}
preparedStatement.setInt(paraIdx++, tenantId);
preparedStatement.setInt(paraIdx++, request.getRowCount());
preparedStatement.setInt(paraIdx, request.getStartIndex());
preparedStatement.setInt(paraIdx, tenantId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
@ -3426,6 +3418,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
boolean isOwnerProvided = false;
boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false;
boolean isDeviceTypeIdProvided = false;
try {
Connection connection = getConnection();
String sql = "SELECT e.DEVICE_ID, " +
@ -3439,8 +3433,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"FROM DM_DEVICE d " +
"INNER JOIN DM_ENROLMENT e " +
"ON d.ID = e.DEVICE_ID " +
"WHERE d.DEVICE_TYPE_ID = ? " +
"AND d.TENANT_ID = ? " +
"WHERE d.TENANT_ID = ? " +
"AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " +
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
@ -3459,6 +3452,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
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)) {
@ -3470,12 +3468,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
preparedStatement.setInt(parameterIdx++, deviceId);
}
if (isOwnerProvided)
if (isOwnerProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%");
if (isDeviceStatusProvided)
}
if (isDeviceStatusProvided) {
preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus());
if (isDeviceNameProvided)
}
if (isDeviceNameProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%");
}
if (isDeviceTypeIdProvided) {
preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId());
}
preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount());
preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex());
@ -3506,7 +3510,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
// todo: fix the join query
@Override
public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
@ -3517,6 +3520,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
boolean isOwnerProvided = false;
boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false;
boolean isDeviceTypeIdProvided = false;
try {
Connection connection = getConnection();
String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) AS COUNT " +
@ -3525,7 +3529,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"ON d.ID = e.DEVICE_ID " +
"WHERE e.TENANT_ID = ? " +
"AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " +
"AND d.DEVICE_TYPE_ID = ? " +
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
if (paginationRequest.getOwner() != null) {
@ -3543,6 +3546,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
isDeviceNameProvided = true;
}
if (paginationRequest.getDeviceTypeId() > 0) {
sql = sql + " AND d.DEVICE_TYPE_ID = ?";
isDeviceTypeIdProvided = true;
}
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int parameterIdx = 1;
preparedStatement.setInt(parameterIdx++, tenantId);
@ -3551,13 +3559,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
preparedStatement.setInt(parameterIdx++, deviceId);
}
preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId());
if (isOwnerProvided)
if (isOwnerProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%");
if (isDeviceStatusProvided)
}
if (isDeviceStatusProvided) {
preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus());
if (isDeviceNameProvided)
preparedStatement.setString(parameterIdx, "%" + paginationRequest.getDeviceName() + "%");
}
if (isDeviceNameProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%");
}
if (isDeviceTypeIdProvided) {
preparedStatement.setInt(parameterIdx, paginationRequest.getDeviceTypeId());
}
try(ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {

@ -1869,6 +1869,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
boolean isOwnerProvided = false;
boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false;
boolean isDeviceTypeIdProvided = false;
try {
Connection connection = getConnection();
String sql = "SELECT e.DEVICE_ID, " +
@ -1881,8 +1882,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"e.DATE_OF_LAST_UPDATE " +
"FROM DM_DEVICE d " +
"INNER JOIN DM_ENROLMENT e " +
"WHERE d.ID = e.DEVICE_ID " +
"AND d.TENANT_ID = ? " +
"WHERE d.TENANT_ID = ? " +
"AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " +
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
if (paginationRequest.getOwner() != null) {
@ -1897,6 +1897,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
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;
@ -1904,12 +1908,19 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(parameterIdx++, deviceId);
}
if (isOwnerProvided)
if (isOwnerProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%");
if (isDeviceStatusProvided)
}
if (isDeviceStatusProvided) {
preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus());
if (isDeviceNameProvided)
}
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()) {
@ -1937,78 +1948,4 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<Integer> getDevicesInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Integer> filteredDeviceIds = new ArrayList<>();
if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds;
String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(","));
try {
Connection connection = getConnection();
String sql = "SELECT ID AS DEVICE_ID " +
"FROM DM_DEVICE WHERE ID IN " +
"(" + deviceIdStringList + ") " +
"AND TENANT_ID = ? " +
"LIMIT ? " +
"OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int paraIdx = 1;
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(paraIdx++, deviceId);
}
preparedStatement.setInt(paraIdx++, tenantId);
preparedStatement.setInt(paraIdx++, request.getRowCount());
preparedStatement.setInt(paraIdx, request.getStartIndex());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
}
}
return filteredDeviceIds;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving device ids in: " + filteredDeviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<Integer> getDevicesNotInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Integer> filteredDeviceIds = new ArrayList<>();
try {
Connection connection = getConnection();
String sql = "SELECT ID AS DEVICE_ID " +
"FROM DM_DEVICE" +
" WHERE TENANT_ID = ?";
if (deviceIds != null && !deviceIds.isEmpty()) {
sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")";
}
sql += " LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int paraIdx = 1;
preparedStatement.setInt(paraIdx++, tenantId);
if (deviceIds != null && !deviceIds.isEmpty()) {
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(paraIdx++, deviceId);
}
}
preparedStatement.setInt(paraIdx++, request.getRowCount());
preparedStatement.setInt(paraIdx, request.getStartIndex());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
}
}
return filteredDeviceIds;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving device ids not in: " + filteredDeviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
}

@ -82,6 +82,7 @@ public class OracleDeviceDAOImpl extends SQLServerDeviceDAOImpl {
boolean isOwnerProvided = false;
boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false;
boolean isDeviceTypeIdProvided = false;
try {
Connection connection = getConnection();
String sql = "SELECT e.DEVICE_ID, " +
@ -110,6 +111,11 @@ public class OracleDeviceDAOImpl extends SQLServerDeviceDAOImpl {
sql += " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
if (paginationRequest.getDeviceTypeId() > 0) {
sql += " AND d.DEVICE_TYPE_ID = ?";
isDeviceTypeIdProvided = true;
}
sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int parameterIdx = 1;
@ -126,6 +132,10 @@ public class OracleDeviceDAOImpl extends SQLServerDeviceDAOImpl {
if (isDeviceNameProvided) {
preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%");
}
if (isDeviceTypeIdProvided) {
preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId());
}
preparedStatement.setInt(parameterIdx++, paginationRequest.getStartIndex());
preparedStatement.setInt(parameterIdx, paginationRequest.getRowCount());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
@ -152,82 +162,4 @@ public class OracleDeviceDAOImpl extends SQLServerDeviceDAOImpl {
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<Integer> getDevicesInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Integer> filteredDeviceIds = new ArrayList<>();
if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds;
String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(","));
try {
Connection connection = getConnection();
String sql = "SELECT ID AS DEVICE_ID " +
"FROM DM_DEVICE WHERE ID IN " +
"(" + deviceIdStringList + ") " +
"AND TENANT_ID = ? " +
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int paraIdx = 1;
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(paraIdx++, deviceId);
}
preparedStatement.setInt(paraIdx++, tenantId);
preparedStatement.setInt(paraIdx++, request.getStartIndex());
preparedStatement.setInt(paraIdx, request.getRowCount());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
}
}
return filteredDeviceIds;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving device ids in: " + deviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<Integer> getDevicesNotInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Integer> filteredDeviceIds = new ArrayList<>();
try {
Connection connection = getConnection();
String sql = "SELECT ID AS DEVICE_ID " +
"FROM DM_DEVICE " +
"WHERE TENANT_ID = ?";
if (deviceIds != null && !deviceIds.isEmpty()) {
sql += " AND ID NOT IN (" + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")";
}
sql += "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int paraIdx = 1;
preparedStatement.setInt(paraIdx++, tenantId);
if (deviceIds != null && !deviceIds.isEmpty()) {
for (Integer deviceId : deviceIds) {
preparedStatement.setInt(paraIdx++, deviceId);
}
}
preparedStatement.setInt(paraIdx++, request.getStartIndex());
preparedStatement.setInt(paraIdx, request.getRowCount());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
}
}
return filteredDeviceIds;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving device ids not in: " + deviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
}

@ -58,7 +58,6 @@ import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoCluster;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -1153,15 +1152,15 @@ public interface DeviceManagementProviderService {
Device updateDeviceName(Device device, String deviceType, String deviceId)
throws DeviceManagementException, DeviceNotFoundException, ConflictException;
List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds)
throws DeviceManagementException;
List<Integer> getDevicesInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
List<Integer> getDevicesInGivenIdList(List<Integer> deviceIds)
throws DeviceManagementException;
int getDeviceCountNotInGivenIdList(List<Integer> deviceIds) throws DeviceManagementException;
List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException;
public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException;
}

@ -161,7 +161,6 @@ import javax.xml.bind.Marshaller;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.sql.Array;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
@ -5594,18 +5593,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
public List<Integer> getDevicesNotInGivenIdList(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.getDevicesNotInGivenIdList(paginationRequest, deviceIds, tenantId);
return deviceDAO.getDevicesNotInGivenIdList(deviceIds, tenantId);
} catch (DeviceManagementDAOException e) {
String msg = "Error encountered while getting device ids";
log.error(msg, e);
@ -5620,19 +5614,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public List<Integer> getDevicesInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
public List<Integer> getDevicesInGivenIdList(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.getDevicesInGivenIdList(paginationRequest, deviceIds, tenantId);
return deviceDAO.getDevicesInGivenIdList(deviceIds, tenantId);
} catch (DeviceManagementDAOException e) {
String msg = "Error encountered while getting device ids";
log.error(msg, e);

Loading…
Cancel
Save