From 9f76c003a67ca80d401b5d1942f54538c7298778 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Wed, 3 Mar 2021 09:58:04 +0530 Subject: [PATCH] Fix app subscription adding issue for removed devices --- .../core/impl/SubscriptionManagerImpl.java | 15 ++- .../carbon/device/mgt/core/dao/DeviceDAO.java | 12 +++ .../carbon/device/mgt/core/dao/GroupDAO.java | 13 +++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 67 ++++++++++++++ .../core/dao/impl/AbstractGroupDAOImpl.java | 61 ++++++++++++ .../DeviceManagementProviderService.java | 28 ++++++ .../DeviceManagementProviderServiceImpl.java | 92 ++++++++++++++++--- .../GroupManagementProviderService.java | 11 +++ .../GroupManagementProviderServiceImpl.java | 68 ++++++++++++-- 9 files changed, 343 insertions(+), 24 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index ea3c24db245..19008cbb0db 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -67,6 +67,7 @@ import org.wso2.carbon.device.application.mgt.core.util.HelperUtil; import org.wso2.carbon.device.application.mgt.core.util.OAuthUtils; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.MDMAppConstants; import org.wso2.carbon.device.mgt.common.app.mgt.App; import org.wso2.carbon.device.mgt.common.app.mgt.MobileAppTypes; @@ -441,6 +442,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List errorDeviceIdentifiers = new ArrayList<>(); String deviceTypeName = null; + List allowingDeviceStatuses = new ArrayList<>(); + allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); + try { if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { deviceTypeName = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()).getName(); @@ -472,21 +478,24 @@ public class SubscriptionManagerImpl implements SubscriptionManager { for (T param : params) { String username = (String) param; subscribers.add(username); - devices.addAll(deviceManagementProviderService.getDevicesOfUser(username)); + devices.addAll(deviceManagementProviderService.getDevicesOfUser(username, + allowingDeviceStatuses, false )); } } else { if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { for (T param : params) { String roleName = (String) param; subscribers.add(roleName); - devices.addAll(deviceManagementProviderService.getAllDevicesOfRole(roleName)); + devices.addAll(deviceManagementProviderService + .getAllDevicesOfRole(roleName, allowingDeviceStatuses, false)); } } else { if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { for (T param : params) { String groupName = (String) param; subscribers.add(groupName); - devices.addAll(groupManagementProviderService.getAllDevicesOfGroup(groupName, true)); + devices.addAll(groupManagementProviderService.getAllDevicesOfGroup(groupName, + allowingDeviceStatuses, true)); } } else { String msg = diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index a93f800ae30..38ef50a9230 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -383,6 +383,18 @@ public interface DeviceDAO { */ List getDevicesOfUser(PaginationRequest request, int tenantId) throws DeviceManagementDAOException; + /** + * This method is using to retrieve devices of a given user of given device statues + * + * @param username Username + * @param tenantId Tenant Id + * @param deviceStatuses Device Statuses + * @returnList of devices + * @throws DeviceManagementDAOException if error ccured while getting devices from the database + */ + List getDevicesOfUser(String username, int tenantId, List deviceStatuses) + throws DeviceManagementDAOException; + /** * This method is used to retrieve the device count of a given tenant. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java index 1951d234782..79b536feaa5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java @@ -236,6 +236,19 @@ public interface GroupDAO { List getDevices(int groupId, int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException; + /** + * Get All the devices that are in one of the given device status and belongs to given group + * + * @param groupName Group name + * @param deviceStatuses Device Statuses + * @param tenantId Tenant Id + * @return List of devices + * @throws GroupManagementDAOException if error occurred while retreving list of devices that are in one of the + * given device status and belongs to the given group + */ + List getAllDevicesOfGroup(String groupName, List deviceStatuses, int tenantId) + throws GroupManagementDAOException; + List getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index df8a3a0fb33..a7d7029c0ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -930,6 +930,73 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return devices; } + @Override + public List getDevicesOfUser(String username, int tenantId, List deviceStatuses) + throws DeviceManagementDAOException { + List devices = new ArrayList<>(); + try { + Connection conn = this.getConnection(); + StringJoiner joiner = new StringJoiner(",","SELECT " + + "e1.OWNER, " + + "e1.OWNERSHIP, " + + "e1.ENROLMENT_ID, " + + "e1.DEVICE_ID, " + + "e1.STATUS, " + + "e1.IS_TRANSFERRED, " + + "e1.DATE_OF_LAST_UPDATE, " + + "e1.DATE_OF_ENROLMENT, " + + "d.DESCRIPTION, " + + "d.NAME AS DEVICE_NAME, " + + "d.DEVICE_IDENTIFICATION, " + + "t.NAME AS DEVICE_TYPE " + + "FROM " + + "DM_DEVICE d, " + + "(SELECT " + + "e.OWNER, " + + "e.OWNERSHIP, " + + "e.ID AS ENROLMENT_ID, " + + "e.DEVICE_ID, " + + "e.STATUS, " + + "e.IS_TRANSFERRED, " + + "e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT " + + "FROM " + + "DM_ENROLMENT e " + + "WHERE " + + "e.TENANT_ID = ? AND " + + "LOWER(e.OWNER) = LOWER(?) AND " + + "e.STATUS IN (", + ")) e1, " + + "DM_DEVICE_TYPE t " + + "WHERE d.ID = e1.DEVICE_ID AND " + + "t.ID = d.DEVICE_TYPE_ID " + + "ORDER BY e1.DATE_OF_LAST_UPDATE DESC"); + + deviceStatuses.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + + try (PreparedStatement stmt = conn.prepareStatement(query)) { + int index = 1; + stmt.setInt(index++, tenantId); + stmt.setString(index++, username); + for (String deviceId : deviceStatuses) { + stmt.setObject(index++, deviceId); + } + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } + } + } catch (SQLException e) { + String msg = "Error occurred while fetching the list of devices belongs to '" + username + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + return devices; + } + @Override public int getCountOfDevicesInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 80fe1c8ad75..11e00c9f334 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; @@ -767,6 +768,66 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } @Override + public List getAllDevicesOfGroup(String groupName, List deviceStatuses, int tenantId) + throws GroupManagementDAOException { + List devices = new ArrayList<>(); + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + StringJoiner joiner = new StringJoiner(",","SELECT " + + "d1.DEVICE_ID, " + + "d1.DESCRIPTION, " + + "d1.NAME AS DEVICE_NAME, " + + "d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, " + + "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, t.NAME AS DEVICE_TYPE " + + "FROM " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID " + + "FROM DM_DEVICE d, " + + "(SELECT dgm.DEVICE_ID " + + "FROM DM_DEVICE_GROUP_MAP dgm " + + "WHERE dgm.GROUP_ID = (SELECT ID FROM DM_GROUP WHERE GROUP_NAME = ? )) dgm1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.STATUS IN (", + ")"); + + deviceStatuses.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + + try (PreparedStatement stmt = conn.prepareStatement(query)) { + int index = 1; + stmt.setString(index++, groupName); + stmt.setInt(index++, tenantId); + stmt.setInt(index++, tenantId); + for (String deviceId : deviceStatuses) { + stmt.setObject(index++, deviceId); + } + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } + } + } catch (SQLException e) { + String msg = "Error occurred while fetching the list of devices belongs to '" + groupName + "'"; + log.error(msg, e); + throw new GroupManagementDAOException(msg, e); + } + return devices; + } + + + @Override public List getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException { Connection conn; List devices; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 04413340dc9..fb3dd195271 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -417,6 +417,20 @@ public interface DeviceManagementProviderService { */ List getDevicesOfUser(String userName, boolean requireDeviceInfo) throws DeviceManagementException; + /** + * Method to get the list of devices owned by an user of given device statuses + * + * @param username username + * @param deviceStatuses device statuses + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getDevicesOfUser(String username, List deviceStatuses, boolean requireDeviceInfo) + throws DeviceManagementException; + /** * This method returns the list of device owned by a user of given device type. * @@ -463,6 +477,20 @@ public interface DeviceManagementProviderService { */ List getAllDevicesOfRole(String roleName, boolean requireDeviceInfo) throws DeviceManagementException; + /** + * Method to get the list of devices that are in one ohe given status and owned by users of a particular user-role. + * + * @param roleName Role name of the users + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @param deviceStatuses List of device statuses + * @return List of devices + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getAllDevicesOfRole(String roleName, List deviceStatuses, boolean requireDeviceInfo) + throws DeviceManagementException; + /** * This method is used to retrieve list of devices based on the device status with paging information. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index a622cf95d7b..74561119735 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -2226,6 +2226,45 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return userDevices; } + @Override + public List getDevicesOfUser(String username, List deviceStatuses, boolean requireDeviceInfo) + throws DeviceManagementException { + if (username == null) { + String msg = "Username null in getDevicesOfUser"; + log.error(msg); + throw new DeviceManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Get devices of user with username '" + username + "' and requiredDeviceInfo " + requireDeviceInfo); + } + List userDevices; + try { + DeviceManagementDAOFactory.openConnection(); + userDevices = deviceDAO.getDevicesOfUser(username, this.getTenantId(), deviceStatuses); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving the list of devices that " + + "belong to the user '" + username + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source to get devices of user: " + + username; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getDevicesOfUser for username '" + username + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + if (requireDeviceInfo) { + return this.populateAllDeviceInfo(userDevices); + } + return userDevices; + } + @Override public List getDevicesOfUser(String username, String deviceType) throws DeviceManagementException { return this.getDevicesOfUser(username, deviceType, true); @@ -2393,20 +2432,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.debug("Get devices of role '" + role + "' and requiredDeviceInfo: " + requireDeviceInfo); } List devices = new ArrayList<>(); - String[] users; + String[] users = getUserListOfRole(role); int tenantId = this.getTenantId(); - try { - users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) - .getUserStoreManager().getUserListOfRole(role); - } catch (UserStoreException e) { - String msg = "Error occurred while obtaining the users, who are assigned with the role '" + role + "'"; - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } catch (Exception e) { - String msg = "Error occurred in getAllDevicesOfRole for role '" + role + "'"; - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } List userDevices; for (String user : users) { @@ -2432,6 +2459,45 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return devices; } + @Override + public List getAllDevicesOfRole(String role, List deviceStatuses, boolean requireDeviceInfo) + throws DeviceManagementException { + if (role == null || role.isEmpty()) { + String msg = "Received empty role for the method getAllDevicesOfRole"; + log.error(msg); + throw new DeviceManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Get devices of role '" + role + "' and requiredDeviceInfo: " + requireDeviceInfo); + } + List devices = new ArrayList<>(); + String[] users = getUserListOfRole(role); + + for (String user : users) { + devices.addAll(getDevicesOfUser(user, deviceStatuses, requireDeviceInfo)); + } + return devices; + } + + private String[] getUserListOfRole(String role) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get users of role '" + role); + } + int tenantId = this.getTenantId(); + try { + return DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) + .getUserStoreManager().getUserListOfRole(role); + } catch (UserStoreException e) { + String msg = "Error occurred while obtaining the users, who are assigned with the role '" + role + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getAllDevicesOfRole for role '" + role + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + } + @Override public int getDeviceCount(String username) throws DeviceManagementException { if (username == null) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index cca1768bd14..f729218314f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -194,6 +194,17 @@ public interface GroupManagementProviderService { */ List getAllDevicesOfGroup(String groupName, boolean requireDeviceProps) throws GroupManagementException; + /** + * Get all devices that are in one of the given device status and belongs to given group. + * + * @param groupName Group name. + * @param deviceStatuses Device statuses list. + * @param requireDeviceProps to include device properties. + * @return List of devices in group. + * @throws GroupManagementException if error occurred while fetching devices + */ + List getAllDevicesOfGroup(String groupName, List deviceStatuses, boolean requireDeviceProps) + throws GroupManagementException; /** * This method is used to retrieve the device count of a given group. 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 6ec253a1960..b28cbefe251 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 @@ -38,6 +38,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; @@ -729,13 +730,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); devices = this.groupDAO.getAllDevicesOfGroup(groupName, tenantId); if (requireDeviceProps) { - DeviceManagementDAOFactory.openConnection(); - for (Device device : devices) { - Device retrievedDevice = deviceDAO.getDeviceProps(device.getDeviceIdentifier(), tenantId); - if (retrievedDevice != null && !retrievedDevice.getProperties().isEmpty()) { - device.setProperties(retrievedDevice.getProperties()); - } - } + return loadDeviceProperties(devices); } } catch (GroupManagementDAOException | SQLException e) { String msg = "Error occurred while getting devices in group."; @@ -747,9 +742,66 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); + } + return devices; + } + + @Override + public List getAllDevicesOfGroup(String groupName, List deviceStatuses, boolean requireDeviceProps) + throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Group devices of group: " + groupName); + } + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + List devices; + try { + GroupManagementDAOFactory.openConnection(); + devices = this.groupDAO.getAllDevicesOfGroup(groupName, deviceStatuses, tenantId); if (requireDeviceProps) { - DeviceManagementDAOFactory.closeConnection(); + return loadDeviceProperties(devices); + } + } catch (GroupManagementDAOException | SQLException e) { + String msg = "Error occurred while getting devices in group."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getDevices for group name: " + groupName; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + return devices; + } + + /** + * Load Dice properties of given list of devices + * + * @param devices list of devices + * @return list of devices which contains device properties + * @throws GroupManagementException if error occurred while loading device properties of devices which are in a + * particular device group + */ + private List loadDeviceProperties(List devices) throws GroupManagementException { + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceManagementDAOFactory.openConnection(); + for (Device device : devices) { + Device retrievedDevice = deviceDAO.getDeviceProps(device.getDeviceIdentifier(), tenantId); + if (retrievedDevice != null && !retrievedDevice.getProperties().isEmpty()) { + device.setProperties(retrievedDevice.getProperties()); + } } + } catch (SQLException e) { + String msg = "Error occurred while opening the connection for loading device properties of group devices."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while loading device properties of group devices."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } return devices; }