From 12c48540d8fa592efe003b8868c82d66776817e0 Mon Sep 17 00:00:00 2001 From: Pahansith Gunathilake Date: Tue, 1 Mar 2022 10:46:36 +0530 Subject: [PATCH] Fix device enrollment failure --- .../GroupManagementProviderServiceImpl.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 6a678c1fca..bccec01707 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -994,16 +994,19 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("Group devices to the group: " + groupId); } - Device device; + try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + List deviceIdentifierList = deviceIdentifiers.stream() + .map(DeviceIdentifier::getId) + .collect(Collectors.toCollection(ArrayList::new)); + List devicesList = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getDeviceByIdList(deviceIdentifierList); + if (devicesList == null || devicesList.isEmpty()) { + throw new DeviceNotFoundException("Couldn't find any devices for the given deviceIdentifiers '" + deviceIdentifiers + "'"); + } GroupManagementDAOFactory.beginTransaction(); - for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). - getDevice(deviceIdentifier, false); - if (device == null) { - throw new DeviceNotFoundException("Device not found for id '" + deviceIdentifier.getId() + "'"); - } + for (Device device : devicesList) { if (!this.groupDAO.isDeviceMappedToGroup(groupId, device.getId(), tenantId)) { this.groupDAO.addDevice(groupId, device.getId(), tenantId); }