diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/CategorizedSubscriptionResult.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/CategorizedSubscriptionResult.java index 28cc436115..e26e567379 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/CategorizedSubscriptionResult.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/CategorizedSubscriptionResult.java @@ -60,6 +60,26 @@ public class CategorizedSubscriptionResult { this.subscribedDevices = subscribedDevices; } + public CategorizedSubscriptionResult(List devices, String tabActionStatus) { + switch (tabActionStatus) { + case "COMPLETED": + this.installedDevices = devices; + break; + case "PENDING": + this.pendingDevices = devices; + break; + case "ERROR": + this.errorDevices = devices; + break; + case "NEW": + this.newDevices = devices; + break; + case "SUBSCRIBED": + this.subscribedDevices = devices; + break; + } + } + public List getInstalledDevices() { return installedDevices; } @@ -100,4 +120,3 @@ public class CategorizedSubscriptionResult { this.subscribedDevices = subscribedDevices; } } - diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java index de9f2977e6..c8ca40813a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java @@ -233,8 +233,9 @@ public interface SubscriptionManager { * @return {@link SubscriptionsDTO} which contains the details of subscriptions. * @throws ApplicationManagementException if an error occurs while fetching the group details */ - List getGroupsSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit) - throws ApplicationManagementException; + public List getGroupsSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, + PaginationRequest request, int offset, + int limit) throws ApplicationManagementException; /** * Retrieves the user details associated with a given app release UUID. @@ -246,8 +247,8 @@ public interface SubscriptionManager { * @return {@link SubscriptionsDTO} which contains the details of subscriptions. * @throws ApplicationManagementException if an error occurs while fetching the user details */ - List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit) - throws ApplicationManagementException; + List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, + int offset, int limit) throws ApplicationManagementException; /** * Retrieves the Role details associated with a given app release UUID. @@ -259,8 +260,8 @@ public interface SubscriptionManager { * @return {@link SubscriptionsDTO} which contains the details of subscriptions. * @throws ApplicationManagementException if an error occurs while fetching the role details */ - List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit) - throws ApplicationManagementException; + List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, + int offset, int limit) throws ApplicationManagementException; /** * Retrieves the Device Subscription details associated with a given app release UUID. @@ -272,8 +273,9 @@ public interface SubscriptionManager { * @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions. * @throws ApplicationManagementException if an error occurs while fetching the device subscription details */ - DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit) - throws ApplicationManagementException; + DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, + PaginationRequest request, int offset, + int limit) throws ApplicationManagementException; /** * Retrieves the All Device details associated with a given app release UUID. @@ -285,8 +287,9 @@ public interface SubscriptionManager { * @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions. * @throws ApplicationManagementException if an error occurs while fetching the subscription details */ - DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit) - throws ApplicationManagementException; + DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, + PaginationRequest request, int offset, + int limit) throws ApplicationManagementException; /** * This method is responsible for retrieving device subscription details related to the given UUID. diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index 3bc8918fcc..ec3717391a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -390,12 +390,16 @@ public interface SubscriptionDAO { * @param appReleaseId the appReleaseId of the application release. * @param unsubscribe the Status of the subscription. * @param tenantId id of the current tenant. + * @param actionStatus Status of the action + * @param actionType type of the action + * @param actionTriggeredBy subscribed by * @param deviceIds deviceIds deviceIds to retrieve data. * @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds) - throws ApplicationManagementDAOException; + List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, String actionStatus, String actionType, + String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException; /** * This method is used to get the details of device subscriptions related to a UUID. @@ -403,13 +407,16 @@ public interface SubscriptionDAO { * @param appReleaseId the appReleaseId of the application release. * @param unsubscribe the Status of the subscription. * @param tenantId id of the current tenant. + * @param actionStatus Status of the action + * @param actionType type of the action + * @param actionTriggeredBy subscribed by * @param offset the offset for the data set * @param limit the limit for the data set * @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) - throws ApplicationManagementDAOException; + List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, + String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException; /** * This method is used to get the counts of all subscription types related to a UUID. diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index fb2e2cffd6..8ffb95f039 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1911,8 +1911,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds) - throws ApplicationManagementDAOException { + public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, String actionStatus, String actionType, + String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); @@ -1920,7 +1921,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc try { Connection conn = this.getDBConnection(); String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT " + StringBuilder sql = new StringBuilder("SELECT " + "DS.ID AS ID, " + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " @@ -1932,16 +1933,39 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.DM_DEVICE_ID AS DEVICE_ID " + "FROM AP_DEVICE_SUBSCRIPTION DS " + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + - deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") " - + "ORDER BY " + subscriptionStatusTime + " DESC"; + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); - try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setBoolean(2, unsubscribe); - ps.setInt(3, tenantId); + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS = ? "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); + } + + sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC"); + + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); for (int i = 0; i < deviceIds.size(); i++) { - ps.setInt(4 + i, deviceIds.get(i)); + ps.setInt(paramIdx++, deviceIds.get(i)); + } + + if (actionStatus != null && !actionStatus.isEmpty()) { + ps.setString(paramIdx++, actionStatus); + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); + } + try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved device subscriptions for application release id " @@ -1980,6 +2004,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, + String actionStatus, String actionType, String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId @@ -1987,7 +2012,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT " + String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY"; + StringBuilder sql = new StringBuilder("SELECT " + "DS.ID AS ID, " + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " @@ -1995,21 +2021,45 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " - + "DS.STATUS AS STATUS," + + "DS.STATUS AS STATUS, " + "DS.DM_DEVICE_ID AS DEVICE_ID " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID=? " - + "ORDER BY " + subscriptionStatusTime + " DESC " - + "LIMIT ? OFFSET ?"; + + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); + + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS = ? "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ? "); + } + + sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC ") + .append("LIMIT ? OFFSET ?"); try { Connection conn = this.getDBConnection(); - try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setBoolean(2, unsubscribe); - ps.setInt(3, tenantId); - ps.setInt(4, limit); - ps.setInt(5, offset); + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); + + if (actionStatus != null && !actionStatus.isEmpty()) { + ps.setString(paramIdx++, actionStatus); + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); + } + + ps.setInt(paramIdx++, limit); + ps.setInt(paramIdx++, offset); + try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved device subscriptions for application release id " @@ -2040,7 +2090,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - String msg = "Error occurred while while running SQL to get device subscription data for application ID: " + appReleaseId; + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 498abe0b02..a8a0a8cfa6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1704,12 +1704,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public List getGroupsSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset, - int limit) throws ApplicationManagementException { + public List getGroupsSubscriptionDetailsByUUID( + String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) + throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); - String groupName; - String status; try { ConnectionManagerUtil.openDBConnection(); @@ -1720,8 +1720,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.error(msg); throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); int appReleaseId = applicationReleaseDTO.getId(); - List groupDetailsWithDevices = new ArrayList<>(); List groupDetails = @@ -1733,11 +1733,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager { GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); for (GroupSubscriptionDTO groupDetail : groupDetails) { - groupName = groupDetail.getGroupName(); + + if (StringUtils.isNotBlank(request.getGroupName()) && !request.getGroupName().equals(groupDetail.getGroupName())) { + continue; + } + + String groupName = StringUtils.isNotBlank(request.getGroupName()) ? request.getGroupName() : groupDetail.getGroupName(); // Retrieve group details and device IDs for the group using the service layer GroupDetailsDTO groupDetailWithDevices = - groupManagementProviderService.getGroupDetailsWithDevices(groupName, offset, limit); + groupManagementProviderService.getGroupDetailsWithDevices( + groupName, applicationDTO.getDeviceTypeId(), request.getOwner(), + request.getDeviceName(), request.getDeviceStatus(), offset, limit); SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO(); groupDetailDTO.setId(groupDetailWithDevices.getGroupId()); @@ -1760,24 +1767,29 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List deviceIds = groupDetailWithDevices.getDeviceIds(); Map statusCounts = new HashMap<>(); - statusCounts.put("PENDING", 0); statusCounts.put("COMPLETED", 0); statusCounts.put("ERROR", 0); + statusCounts.put("PENDING", 0); statusCounts.put("NEW", 0); statusCounts.put("SUBSCRIBED", 0); - // Get subscribed devices if unsubscribed devices are requested - List subscribedDeviceSubscriptions = new ArrayList<>(); - if (unsubscribe) { - subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - appReleaseId, !unsubscribe, tenantId, deviceIds); - } - for (Integer deviceId : deviceIds) { - List deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - groupDetail.getAppReleaseId(), unsubscribe, tenantId, deviceIds); + // Get subscribed devices if unsubscribed devices are requested + List deviceSubscriptions; + if (unsubscribe) { + deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( + appReleaseId, !unsubscribe, tenantId, deviceIds, + request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); + } else { + deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( + groupDetail.getAppReleaseId(), false, tenantId, deviceIds, + request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); + } + List filteredDeviceSubscriptions = deviceSubscriptions.stream() + .filter(subscription -> StringUtils.isBlank(request.getTabActionStatus()) || subscription.getStatus().equals(request.getTabActionStatus())) + .collect(Collectors.toList()); boolean isNewDevice = true; - for (DeviceSubscriptionDTO subscription : deviceSubscriptions) { + for (DeviceSubscriptionDTO subscription : filteredDeviceSubscriptions) { if (subscription.getDeviceId() == deviceId) { DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); deviceDetail.setDeviceId(subscription.getDeviceId()); @@ -1795,7 +1807,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { deviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); deviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); - status = subscription.getStatus(); + String status = subscription.getStatus(); switch (status) { case "COMPLETED": installedDevices.add(deviceDetail); @@ -1813,13 +1825,17 @@ public class SubscriptionManagerImpl implements SubscriptionManager { pendingDevices.add(deviceDetail); statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); break; + default: + newDevices.add(deviceDetail); + statusCounts.put("NEW", statusCounts.get("NEW") + 1); + break; } isNewDevice = false; } } if (isNewDevice) { boolean isSubscribedDevice = false; - for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { + for (DeviceSubscriptionDTO subscribedDevice : deviceSubscriptions) { if (subscribedDevice.getDeviceId() == deviceId) { DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); @@ -1861,17 +1877,38 @@ public class SubscriptionManagerImpl implements SubscriptionManager { statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); } - CategorizedSubscriptionResult categorizedSubscriptionResult; - if (subscribedDevices.isEmpty()) { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + List requestedDevices = new ArrayList<>(); + if (StringUtils.isNotBlank(request.getTabActionStatus())) { + switch (request.getTabActionStatus()) { + case "COMPLETED": + requestedDevices = installedDevices; + break; + case "PENDING": + requestedDevices = pendingDevices; + break; + case "ERROR": + requestedDevices = errorDevices; + break; + case "NEW": + requestedDevices = newDevices; + break; + case "SUBSCRIBED": + requestedDevices = subscribedDevices; + break; + } + groupDetailDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); } else { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); + CategorizedSubscriptionResult categorizedSubscriptionResult; + if (subscribedDevices.isEmpty()) { + categorizedSubscriptionResult = + new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + } else { + categorizedSubscriptionResult = + new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); + } + groupDetailDTO.setDevices(categorizedSubscriptionResult); } - groupDetailDTO.setDevices(categorizedSubscriptionResult); groupDetailDTO.setStatusPercentages(statusPercentages); - groupDetailsWithDevices.add(groupDetailDTO); } @@ -1894,11 +1931,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit) + public List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, + PaginationRequest request, int offset, int limit) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); - String userName; String status; try { @@ -1910,8 +1947,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.error(msg); throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); int appReleaseId = applicationReleaseDTO.getId(); - List userSubscriptionsWithDevices = new ArrayList<>(); List userSubscriptions = @@ -1923,11 +1960,17 @@ public class SubscriptionManagerImpl implements SubscriptionManager { DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); for (SubscriptionsDTO userSubscription : userSubscriptions) { - userName = userSubscription.getName(); + + if (StringUtils.isNotBlank(request.getUserName()) && !request.getUserName().equals(userSubscription.getName())) { + continue; + } + + String userName = StringUtils.isNotBlank(request.getUserName()) ? request.getUserName() : userSubscription.getName(); // Retrieve owner details and device IDs for the user using the service layer OwnerWithDeviceDTO ownerDetailsWithDevices = - deviceManagementProviderService.getOwnersWithDeviceIds(userName); + deviceManagementProviderService.getOwnersWithDeviceIds(userName, applicationDTO.getDeviceTypeId(), + request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); SubscriptionsDTO userSubscriptionDTO = new SubscriptionsDTO(); userSubscriptionDTO.setName(userSubscription.getName()); @@ -1958,21 +2001,29 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List subscribedDeviceSubscriptions = new ArrayList<>(); if (unsubscribe) { subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - appReleaseId, !unsubscribe, tenantId, deviceIds); + appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), + request.getActionTriggeredBy(), request.getTabActionStatus()); } for (Integer deviceId : deviceIds) { List deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - userSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds); + userSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), + request.getActionTriggeredBy(), request.getTabActionStatus()); + OwnerWithDeviceDTO ownerWithDeviceByDeviceId = + deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), + request.getDeviceStatus()); + if (ownerWithDeviceByDeviceId == null) { + continue; + } boolean isNewDevice = true; for (DeviceSubscriptionDTO subscription : deviceSubscriptions) { if (subscription.getDeviceId() == deviceId) { DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); deviceDetail.setDeviceId(subscription.getDeviceId()); deviceDetail.setSubId(subscription.getId()); - deviceDetail.setDeviceOwner(ownerDetailsWithDevices.getUserName()); - deviceDetail.setDeviceStatus(ownerDetailsWithDevices.getDeviceStatus()); - deviceDetail.setDeviceName(ownerDetailsWithDevices.getDeviceNames()); + deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); + deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); + deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); deviceDetail.setActionType(subscription.getActionTriggeredFrom()); deviceDetail.setStatus(subscription.getStatus()); deviceDetail.setActionType(subscription.getActionTriggeredFrom()); @@ -1981,8 +2032,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); - deviceDetail.setType(ownerDetailsWithDevices.getDeviceTypes()); - deviceDetail.setDeviceIdentifier(ownerDetailsWithDevices.getDeviceIdentifiers()); + deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); + deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); status = subscription.getStatus(); switch (status) { @@ -2012,16 +2063,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (subscribedDevice.getDeviceId() == deviceId) { DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); - subscribedDeviceDetail.setDeviceName(ownerDetailsWithDevices.getDeviceNames()); - subscribedDeviceDetail.setDeviceOwner(ownerDetailsWithDevices.getUserName()); - subscribedDeviceDetail.setDeviceStatus(ownerDetailsWithDevices.getDeviceStatus()); + subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); + subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); + subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); subscribedDeviceDetail.setSubId(subscribedDevice.getId()); subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); - subscribedDeviceDetail.setType(ownerDetailsWithDevices.getDeviceTypes()); - subscribedDeviceDetail.setDeviceIdentifier(ownerDetailsWithDevices.getDeviceIdentifiers()); + subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); + subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); subscribedDevices.add(subscribedDeviceDetail); statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); isSubscribedDevice = true; @@ -2031,11 +2082,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (!isSubscribedDevice) { DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceOwner(ownerDetailsWithDevices.getUserName()); - newDeviceDetail.setDeviceStatus(ownerDetailsWithDevices.getDeviceStatus()); - newDeviceDetail.setDeviceName(ownerDetailsWithDevices.getDeviceNames()); - newDeviceDetail.setType(ownerDetailsWithDevices.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerDetailsWithDevices.getDeviceIdentifiers()); + newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); + newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); + newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); + newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); + newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); newDevices.add(newDeviceDetail); statusCounts.put("NEW", statusCounts.get("NEW") + 1); } @@ -2050,20 +2101,42 @@ public class SubscriptionManagerImpl implements SubscriptionManager { statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); } - CategorizedSubscriptionResult categorizedSubscriptionResult; - if (subscribedDevices.isEmpty()) { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + List requestedDevices = new ArrayList<>(); + if (StringUtils.isNotBlank(request.getTabActionStatus())) { + switch (request.getTabActionStatus()) { + case "COMPLETED": + requestedDevices = installedDevices; + break; + case "PENDING": + requestedDevices = pendingDevices; + break; + case "ERROR": + requestedDevices = errorDevices; + break; + case "NEW": + requestedDevices = newDevices; + break; + case "SUBSCRIBED": + requestedDevices = subscribedDevices; + break; + } + userSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); } else { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); - } - userSubscriptionDTO.setDevices(categorizedSubscriptionResult); - userSubscriptionDTO.setStatusPercentages(statusPercentages); + CategorizedSubscriptionResult categorizedSubscriptionResult; + if (subscribedDevices.isEmpty()) { + categorizedSubscriptionResult = + new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + } else { + categorizedSubscriptionResult = + new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, + subscribedDevices); + } + userSubscriptionDTO.setDevices(categorizedSubscriptionResult); + userSubscriptionDTO.setStatusPercentages(statusPercentages); + } userSubscriptionsWithDevices.add(userSubscriptionDTO); } - return userSubscriptionsWithDevices; } catch (ApplicationManagementDAOException e) { String msg = "Error occurred while getting user subscriptions for the application release UUID: " + uuid; @@ -2081,7 +2154,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit) + public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, + PaginationRequest request, int offset, int limit) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); @@ -2097,8 +2171,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.error(msg); throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); int appReleaseId = applicationReleaseDTO.getId(); - List roleSubscriptionsWithDevices = new ArrayList<>(); List roleSubscriptions = @@ -2110,7 +2184,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); for (SubscriptionsDTO roleSubscription : roleSubscriptions) { - roleName = roleSubscription.getName(); + + roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); roleSubscriptionDTO.setName(roleSubscription.getName()); @@ -2139,7 +2214,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { for (String user : users) { OwnerWithDeviceDTO ownerDetailsWithDevices; try { - ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user); + ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(), + request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); } catch (DeviceManagementDAOException e) { throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e); } @@ -2150,13 +2226,20 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List subscribedDeviceSubscriptions = new ArrayList<>(); if (unsubscribe) { subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - appReleaseId, !unsubscribe, tenantId, deviceIds); + appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), + request.getActionTriggeredBy(), request.getTabActionStatus()); + } + OwnerWithDeviceDTO ownerWithDeviceByDeviceId = + deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), + request.getDeviceStatus()); + if (ownerWithDeviceByDeviceId == null) { + continue; } - List deviceSubscriptions; try { deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds); + roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), + request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); } catch (ApplicationManagementDAOException e) { throw new ApplicationManagementException("Error retrieving device subscriptions", e); } @@ -2166,9 +2249,9 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (deviceSubscription.getDeviceId() == deviceId) { DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); - deviceDetail.setDeviceName(ownerDetailsWithDevices.getDeviceNames()); - deviceDetail.setDeviceOwner(ownerDetailsWithDevices.getUserName()); - deviceDetail.setDeviceStatus(ownerDetailsWithDevices.getDeviceStatus()); + deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); + deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); + deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); deviceDetail.setStatus(deviceSubscription.getStatus()); deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); @@ -2178,8 +2261,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); - deviceDetail.setType(ownerDetailsWithDevices.getDeviceTypes()); - deviceDetail.setDeviceIdentifier(ownerDetailsWithDevices.getDeviceIdentifiers()); + deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); + deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); status = deviceSubscription.getStatus(); switch (status) { @@ -2209,16 +2292,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (subscribedDevice.getDeviceId() == deviceId) { DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); - subscribedDeviceDetail.setDeviceName(ownerDetailsWithDevices.getDeviceNames()); - subscribedDeviceDetail.setDeviceOwner(ownerDetailsWithDevices.getUserName()); - subscribedDeviceDetail.setDeviceStatus(ownerDetailsWithDevices.getDeviceStatus()); + subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); + subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); + subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); subscribedDeviceDetail.setSubId(subscribedDevice.getId()); subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); - subscribedDeviceDetail.setType(ownerDetailsWithDevices.getDeviceTypes()); - subscribedDeviceDetail.setDeviceIdentifier(ownerDetailsWithDevices.getDeviceIdentifiers()); + subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); + subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); subscribedDevices.add(subscribedDeviceDetail); statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); isSubscribedDevice = true; @@ -2228,11 +2311,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (!isSubscribedDevice) { DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceName(ownerDetailsWithDevices.getDeviceNames()); - newDeviceDetail.setDeviceOwner(ownerDetailsWithDevices.getUserName()); - newDeviceDetail.setDeviceStatus(ownerDetailsWithDevices.getDeviceStatus()); - newDeviceDetail.setType(ownerDetailsWithDevices.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerDetailsWithDevices.getDeviceIdentifiers()); + newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); + newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); + newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); + newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); + newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); newDevices.add(newDeviceDetail); statusCounts.put("NEW", statusCounts.get("NEW") + 1); } @@ -2249,23 +2332,46 @@ public class SubscriptionManagerImpl implements SubscriptionManager { statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); } - CategorizedSubscriptionResult categorizedSubscriptionResult; - if (subscribedDevices.isEmpty()) { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + List requestedDevices = new ArrayList<>(); + if (StringUtils.isNotBlank(request.getTabActionStatus())) { + switch (request.getTabActionStatus()) { + case "COMPLETED": + requestedDevices = installedDevices; + break; + case "PENDING": + requestedDevices = pendingDevices; + break; + case "ERROR": + requestedDevices = errorDevices; + break; + case "NEW": + requestedDevices = newDevices; + break; + case "SUBSCRIBED": + requestedDevices = subscribedDevices; + break; + } + roleSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); + } else { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); + CategorizedSubscriptionResult categorizedSubscriptionResult; + if (subscribedDevices.isEmpty()) { + categorizedSubscriptionResult = + new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + } else { + categorizedSubscriptionResult = + new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, + subscribedDevices); + } + roleSubscriptionDTO.setDevices(categorizedSubscriptionResult); + roleSubscriptionDTO.setStatusPercentages(statusPercentages); + roleSubscriptionDTO.setDeviceCount(totalDevices); } - roleSubscriptionDTO.setDevices(categorizedSubscriptionResult); - roleSubscriptionDTO.setStatusPercentages(statusPercentages); - roleSubscriptionDTO.setDeviceCount(totalDevices); - roleSubscriptionsWithDevices.add(roleSubscriptionDTO); } return roleSubscriptionsWithDevices; - } catch (ApplicationManagementDAOException e) { + } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { String msg = "Error occurred in retrieving role subscriptions with devices"; log.error(msg, e); throw new ApplicationManagementException(msg, e); @@ -2292,7 +2398,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, int offset, + public DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -2307,6 +2413,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.error(msg); throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); int appReleaseId = applicationReleaseDTO.getId(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); @@ -2321,7 +2428,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } List allDevices = - deviceManagementProviderService.getDevicesByTenantId(tenantId); + deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(), + request.getOwner(), request.getDeviceStatus()); List deviceIds = allDevices.stream() .map(DeviceDetailsDTO::getDeviceId) @@ -2346,9 +2454,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { .collect(Collectors.toMap(DeviceDetailsDTO::getDeviceId, Function.identity())); List allSubscriptionsForUnSubscribed = - subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds); + subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), + request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); List allSubscriptionsForSubscribed = - subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds); + subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds, request.getActionStatus(), + request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); Map allSubscriptionForUnSubscribedMap = allSubscriptionsForUnSubscribed.stream() .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); Map allSubscriptionForSubscribedMap = allSubscriptionsForSubscribed.stream() @@ -2357,7 +2467,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { for (DeviceDetailsDTO device : allDevices) { Integer deviceId = device.getDeviceId(); OwnerWithDeviceDTO ownerWithDevice = - deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId); + deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), + request.getDeviceStatus()); if (ownerWithDevice == null) { continue; } @@ -2450,14 +2561,35 @@ public class SubscriptionManagerImpl implements SubscriptionManager { statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); } - CategorizedSubscriptionResult categorizedSubscriptionResult; - if (subscribedDevices.isEmpty()) { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + List requestedDevices = new ArrayList<>(); + if (StringUtils.isNotBlank(request.getTabActionStatus())) { + switch (request.getTabActionStatus()) { + case "COMPLETED": + requestedDevices = installedDevices; + break; + case "PENDING": + requestedDevices = pendingDevices; + break; + case "ERROR": + requestedDevices = errorDevices; + break; + case "NEW": + requestedDevices = newDevices; + break; + case "SUBSCRIBED": + requestedDevices = subscribedDevices; + break; + } } else { - categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); + requestedDevices.addAll(installedDevices); + requestedDevices.addAll(pendingDevices); + requestedDevices.addAll(errorDevices); + requestedDevices.addAll(newDevices); + requestedDevices.addAll(subscribedDevices); } + + CategorizedSubscriptionResult categorizedSubscriptionResult = + new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); DeviceSubscriptionResponseDTO deviceSubscriptionResponse = new DeviceSubscriptionResponseDTO(totalDevices, statusPercentages, categorizedSubscriptionResult); @@ -2479,8 +2611,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit) - throws ApplicationManagementException { + public DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, + int offset, int limit) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); @@ -2493,10 +2625,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.error(msg); throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); int appReleaseId = applicationReleaseDTO.getId(); List allSubscriptions = - subscriptionDAO.getAllSubscriptionsDetails(appReleaseId, unsubscribe, tenantId, offset, limit); + subscriptionDAO.getAllSubscriptionsDetails(appReleaseId, unsubscribe, tenantId, request.getActionStatus(), + request.getActionType(), request.getActionTriggeredBy(), offset, limit); // empty response for no subscriptions if (allSubscriptions.isEmpty()) { @@ -2522,12 +2656,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { statusCounts.put("NEW", 0); List allDevices = - deviceManagementProviderService.getDevicesByTenantId(tenantId); + deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(), request.getOwner(), + request.getDeviceStatus()); for (DeviceDetailsDTO device : allDevices) { Integer deviceId = device.getDeviceId(); OwnerWithDeviceDTO ownerWithDevice = - deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId); + deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), + request.getDeviceStatus()); if (ownerWithDevice == null) { continue; } @@ -2590,6 +2726,29 @@ public class SubscriptionManagerImpl implements SubscriptionManager { statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); } + List requestedDevices = new ArrayList<>(); + if (StringUtils.isNotBlank(request.getTabActionStatus())) { + switch (request.getTabActionStatus()) { + case "COMPLETED": + requestedDevices = installedDevices; + break; + case "PENDING": + requestedDevices = pendingDevices; + break; + case "ERROR": + requestedDevices = errorDevices; + break; + case "NEW": + requestedDevices = newDevices; + break; + } + } else { + requestedDevices.addAll(installedDevices); + requestedDevices.addAll(pendingDevices); + requestedDevices.addAll(errorDevices); + requestedDevices.addAll(newDevices); + } + CategorizedSubscriptionResult categorizedSubscriptionResult = new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); DeviceSubscriptionResponseDTO result = diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java index c0783fe18f..95b8a92c6c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java @@ -41,6 +41,14 @@ public class PaginationRequest { private Date since; private String filter; private String serialNumber; + private String groupName; + private String roleName; + private String userName; + private String deviceStatus; + private String tabActionStatus; + private String actionStatus; + private String actionType; + private String actionTriggeredBy; private Map customProperty = new HashMap<>(); private Map property = new HashMap<>(); private List statusList = new ArrayList<>(); @@ -220,4 +228,68 @@ public class PaginationRequest { + this.ownership + "' Status '" + this.statusList + "' owner '" + this.owner + "' groupId: " + this.groupId + " start index: " + this.startIndex + ", SortColumns: " + this.sortColumn; } + + public String getDeviceStatus() { + return deviceStatus; + } + + public void setDeviceStatus(String deviceStatus) { + this.deviceStatus = deviceStatus; + } + + public String getActionStatus() { + return actionStatus; + } + + public void setActionStatus(String actionStatus) { + this.actionStatus = actionStatus; + } + + public String getActionType() { + return actionType; + } + + public void setActionType(String actionType) { + this.actionType = actionType; + } + + public String getActionTriggeredBy() { + return actionTriggeredBy; + } + + public void setActionTriggeredBy(String actionTriggeredBy) { + this.actionTriggeredBy = actionTriggeredBy; + } + + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getTabActionStatus() { + return tabActionStatus; + } + + public void setTabActionStatus(String tabActionStatus) { + this.tabActionStatus = tabActionStatus; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java index 93c456f929..70b791db7b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java @@ -101,29 +101,42 @@ public interface EnrollmentDAO { * Retrieves owners and the list of device IDs related to an owner. * * @param owner the owner whose device IDs need to be retrieved + * @param allowingDeviceStatuses statuses of devices need to be retrieved * @param tenantId the ID of the tenant + * @param deviceOwner owner of the device + * @param deviceName name of the device + * @param deviceStatus status of the device * @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user * @throws DeviceManagementDAOException if an error occurs while fetching the data */ - OwnerWithDeviceDTO getOwnersWithDevices(String owner, int tenantId) throws DeviceManagementDAOException; + OwnerWithDeviceDTO getOwnersWithDevices(String owner, List allowingDeviceStatuses, int tenantId, int deviceTypeId, + String deviceOwner, String deviceName, String deviceStatus) throws DeviceManagementDAOException; /** * Retrieves a list of device IDs with owners and device status. * * @param deviceId the deviceId of the device which user need to be retrieved * @param tenantId the ID of the tenant + * @param deviceOwner owner of the device + * @param deviceName name of the device + * @param deviceStatus status of the device * @return {@link OwnerWithDeviceDTO} which contains a list of devices * @throws DeviceManagementDAOException if an error occurs while fetching the data */ - OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId) + OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId, String deviceOwner, String deviceName, String deviceStatus) throws DeviceManagementDAOException; /** * Retrieves owners and the list of device IDs with device status. * * @param tenantId the ID of the tenant + * @param allowingDeviceStatuses the allowed device statuses of devices + * @param deviceTypeId the device type id + * @param deviceOwner owner of the device + * @param deviceStatus status of the device * @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user * @throws DeviceManagementDAOException if an error occurs while fetching the data */ - List getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException; + List getDevicesByTenantId(int tenantId, List allowingDeviceStatuses, int deviceTypeId, String deviceOwner, + String deviceStatus) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java index 5071f7a400..c5d2d700cd 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java @@ -473,13 +473,19 @@ public interface GroupDAO { * Get group details and list of device IDs related to the group. * * @param groupName Group name + * @param allowingDeviceStatuses the statuses of devices + * @param deviceTypeId the device type id * @param tenantId Tenant ID + * @param deviceOwner owner of the device + * @param deviceName name of the device + * @param deviceStatus status of the device * @param offset the offset for the data set * @param limit the limit for the data set * @return {@link GroupDetailsDTO} which containing group details and a list of device IDs * @throws GroupManagementDAOException if an error occurs while retrieving the group details and devices */ - GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int tenantId, int offset, int limit) + GroupDetailsDTO getGroupDetailsWithDevices(String groupName, List allowingDeviceStatuses, int deviceTypeId, + int tenantId, String deviceOwner, String deviceName, String deviceStatus, int offset, int limit) throws GroupManagementDAOException; } \ No newline at end of file diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java index dad35f0795..1160b4bb79 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java @@ -564,30 +564,73 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } @Override - public OwnerWithDeviceDTO getOwnersWithDevices(String owner, int tenantId) - throws DeviceManagementDAOException { + public OwnerWithDeviceDTO getOwnersWithDevices(String owner, List allowingDeviceStatuses, int tenantId, + int deviceTypeId, String deviceOwner, String deviceName, + String deviceStatus) throws DeviceManagementDAOException { Connection conn = null; OwnerWithDeviceDTO ownerDetails = new OwnerWithDeviceDTO(); List deviceIds = new ArrayList<>(); int deviceCount = 0; - String sql = "SELECT e.DEVICE_ID, e.OWNER, e.STATUS AS DEVICE_STATUS, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE AS DEVICE_TYPE, e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " + + StringBuilder deviceFilters = new StringBuilder(); + for (int i = 0; i < allowingDeviceStatuses.size(); i++) { + deviceFilters.append("?"); + if (i < allowingDeviceStatuses.size() - 1) { + deviceFilters.append(","); + } + } + + StringBuilder sql = new StringBuilder( + "SELECT e.DEVICE_ID, " + + "e.OWNER, " + + "e.STATUS AS DEVICE_STATUS, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE AS DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " + "FROM DM_ENROLMENT e " + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + - "WHERE e.OWNER = ? AND e.TENANT_ID = ?"; + "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); + + if (deviceOwner != null && !deviceOwner.isEmpty()) { + sql.append(" AND e.OWNER LIKE ?"); + } + if (deviceName != null && !deviceName.isEmpty()) { + sql.append(" AND d.NAME LIKE ?"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + sql.append(" AND e.STATUS = ?"); + } + try { conn = this.getConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, owner); - stmt.setInt(2, tenantId); + try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { + int index = 1; + stmt.setString(index++, owner); + stmt.setInt(index++, tenantId); + stmt.setInt(index++, deviceTypeId); + for (String status : allowingDeviceStatuses) { + stmt.setString(index++, status); + } + + if (deviceOwner != null && !deviceOwner.isEmpty()) { + stmt.setString(index++, "%" + deviceOwner + "%"); + } + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(index++, "%" + deviceName + "%"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + stmt.setString(index++, deviceStatus); + } try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { if (ownerDetails.getUserName() == null) { ownerDetails.setUserName(rs.getString("OWNER")); - ownerDetails.setDeviceStatus(rs.getString("DEVICE_STATUS")); - ownerDetails.setDeviceNames(rs.getString("DEVICE_NAME")); } + ownerDetails.setDeviceStatus(rs.getString("DEVICE_STATUS")); + ownerDetails.setDeviceNames(rs.getString("DEVICE_NAME")); + ownerDetails.setDeviceTypes(rs.getString("DEVICE_TYPE")); + ownerDetails.setDeviceIdentifiers(rs.getString("DEVICE_IDENTIFICATION")); deviceIds.add(rs.getInt("DEVICE_ID")); deviceCount++; } @@ -598,34 +641,61 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } - ownerDetails.setDeviceIds(deviceIds); - ownerDetails.setDeviceTypes("DEVICE_TYPE"); - ownerDetails.setDeviceIdentifiers("DEVICE_IDENTIFICATION"); ownerDetails.setDeviceCount(deviceCount); return ownerDetails; } @Override - public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId) - throws DeviceManagementDAOException { + public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId, String deviceOwner, String deviceName, + String deviceStatus) throws DeviceManagementDAOException { OwnerWithDeviceDTO deviceOwnerWithStatus = new OwnerWithDeviceDTO(); Connection conn = null; - String sql = "SELECT e.DEVICE_ID, e.OWNER, e.STATUS AS DEVICE_STATUS, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION " + + List deviceIds = new ArrayList<>(); + + StringBuilder sql = new StringBuilder( + "SELECT e.DEVICE_ID, " + + "e.OWNER, " + + "e.STATUS AS DEVICE_STATUS, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION " + "FROM DM_ENROLMENT e " + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + - "WHERE e.DEVICE_ID = ? AND e.TENANT_ID = ?"; + "WHERE e.DEVICE_ID = ? AND e.TENANT_ID = ?"); + + if (deviceOwner != null && !deviceOwner.isEmpty()) { + sql.append(" AND e.OWNER LIKE ?"); + } + if (deviceName != null && !deviceName.isEmpty()) { + sql.append(" AND d.NAME LIKE ?"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + sql.append(" AND e.STATUS = ?"); + } + try { conn = this.getConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, deviceId); - stmt.setInt(2, tenantId); + try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { + int paramIndex = 1; + stmt.setInt(paramIndex++, deviceId); + stmt.setInt(paramIndex++, tenantId); + + // Set filter parameters if provided + if (deviceOwner != null && !deviceOwner.isEmpty()) { + stmt.setString(paramIndex++, "%" + deviceOwner + "%"); + } + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(paramIndex++, "%" + deviceName + "%"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + stmt.setString(paramIndex++, deviceStatus); + } try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { deviceOwnerWithStatus.setUserName(rs.getString("OWNER")); deviceOwnerWithStatus.setDeviceStatus(rs.getString("DEVICE_STATUS")); - List deviceIds = new ArrayList<>(); deviceIds.add(rs.getInt("DEVICE_ID")); deviceOwnerWithStatus.setDeviceIds(deviceIds); deviceOwnerWithStatus.setDeviceNames(rs.getString("DEVICE_NAME")); @@ -643,18 +713,51 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } @Override - public List getDevicesByTenantId(int tenantId) - throws DeviceManagementDAOException { + public List getDevicesByTenantId(int tenantId, List allowingDeviceStatuses, int deviceTypeId, + String deviceOwner, String deviceStatus) throws DeviceManagementDAOException { List devices = new ArrayList<>(); - String sql = "SELECT DEVICE_ID, OWNER, STATUS, DEVICE_TYPE, DEVICE_IDENTIFICATION " + - "FROM DM_ENROLMENT " + - "WHERE TENANT_ID = ?"; + if (allowingDeviceStatuses.isEmpty()) { + return devices; + } + + StringBuilder deviceFilters = new StringBuilder(); + for (int i = 0; i < allowingDeviceStatuses.size(); i++) { + deviceFilters.append("?"); + if (i < allowingDeviceStatuses.size() - 1) { + deviceFilters.append(","); + } + } + + StringBuilder sql = new StringBuilder("SELECT e.DEVICE_ID, e.OWNER, e.STATUS, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION " + + "FROM DM_ENROLMENT e " + + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + + "WHERE e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ") AND d.DEVICE_TYPE_ID = ?"); + + if (deviceOwner != null && !deviceOwner.isEmpty()) { + sql.append(" AND e.OWNER LIKE ?"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + sql.append(" AND e.STATUS = ?"); + } + Connection conn = null; try { conn = this.getConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, tenantId); + try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { + int index = 1; + stmt.setInt(index++, tenantId); + for (String status : allowingDeviceStatuses) { + stmt.setString(index++, status); + } + stmt.setInt(index++, deviceTypeId); + + if (deviceOwner != null && !deviceOwner.isEmpty()) { + stmt.setString(index++, "%" + deviceOwner + "%"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + stmt.setString(index++, deviceStatus); + } try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 780ba43cc6..1eba6c1a7e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -1441,7 +1441,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } @Override - public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int tenantId, int offset, int limit) + public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, List allowedStatuses, int deviceTypeId, int tenantId, + String deviceOwner, String deviceName, String deviceStatus, int offset, int limit) throws GroupManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get group details and device IDs for group: " + groupName); @@ -1454,7 +1455,15 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { Map deviceTypes = new HashMap<>(); Map deviceIdentifiers = new HashMap<>(); - String sql = + StringBuilder statusPlaceholders = new StringBuilder(); + for (int i = 0; i < allowedStatuses.size(); i++) { + statusPlaceholders.append("?"); + if (i < allowedStatuses.size() - 1) { + statusPlaceholders.append(","); + } + } + + StringBuilder sql = new StringBuilder( "SELECT " + " g.ID AS GROUP_ID, " + " g.GROUP_NAME, " + @@ -1473,16 +1482,45 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { "WHERE " + " g.GROUP_NAME = ? " + " AND g.TENANT_ID = ? " + - "LIMIT ? OFFSET ?"; + " AND d.DEVICE_TYPE_ID = ? " + + " AND e.STATUS IN (" + statusPlaceholders + ")"); + + if (deviceOwner != null && !deviceOwner.isEmpty()) { + sql.append(" AND e.OWNER LIKE ?"); + } + if (deviceName != null && !deviceName.isEmpty()) { + sql.append(" AND d.NAME LIKE ?"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + sql.append(" AND e.STATUS = ?"); + } + + sql.append(" LIMIT ? OFFSET ?"); Connection conn = null; try { conn = GroupManagementDAOFactory.getConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, groupName); - stmt.setInt(2, tenantId); - stmt.setInt(3, limit); - stmt.setInt(4, offset); + try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { + int index = 1; + stmt.setString(index++, groupName); + stmt.setInt(index++, tenantId); + stmt.setInt(index++, deviceTypeId); + for (String status : allowedStatuses) { + stmt.setString(index++, status); + } + + if (deviceOwner != null && !deviceOwner.isEmpty()) { + stmt.setString(index++, "%" + deviceOwner + "%"); + } + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(index++, "%" + deviceName + "%"); + } + if (deviceStatus != null && !deviceStatus.isEmpty()) { + stmt.setString(index++, deviceStatus); + } + + stmt.setInt(index++, limit); + stmt.setInt(index++, offset); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { @@ -1500,6 +1538,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { deviceIdentifiers.put(deviceId, rs.getString("DEVICE_IDENTIFICATION")); } } + } groupDetails.setDeviceIds(deviceIds); groupDetails.setDeviceCount(deviceIds.size()); groupDetails.setDeviceOwners(deviceOwners); @@ -1508,11 +1547,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { groupDetails.setDeviceTypes(deviceTypes); groupDetails.setDeviceIdentifiers(deviceIdentifiers); return groupDetails; + } catch (SQLException e) { + String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName; + log.error(msg, e); + throw new GroupManagementDAOException(msg, e); } - } catch (SQLException e) { - String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName; - log.error(msg, e); - throw new GroupManagementDAOException(msg, e); - } } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java index 9ab44929a1..10d3598ff5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java @@ -1082,27 +1082,40 @@ public interface DeviceManagementProviderService { * Get owner details and device IDs for a given owner and tenant. * * @param owner the name of the owner. + * @param deviceTypeId the device type id] + * @param deviceOwner owner of the device + * @param deviceName name of the device + * @param deviceStatus status of the device * @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user. * @throws DeviceManagementException if an error occurs while fetching owner details. */ - OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner) throws DeviceManagementDAOException; + OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus) + throws DeviceManagementDAOException; /** * Get owner details and device IDs for a given owner and tenant. * * @param deviceId the deviceId of the device. + * @param deviceOwner owner of the device + * @param deviceName name of the device + * @param deviceStatus status of the device * @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user. * @throws DeviceManagementException if an error occurs while fetching owner details. */ - OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId) throws DeviceManagementDAOException; + OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, String deviceOwner, String deviceName, String deviceStatus) + throws DeviceManagementDAOException; /** * Get owner details and device IDs for a given owner and tenant. * @param tenantId the tenant id which devices need to be retried + * @param deviceTypeId the device type id + * @param deviceOwner owner of the device + * @param deviceStatus status of the device * @return {@link DeviceDetailsDTO} which contains devices details. * @throws DeviceManagementException if an error occurs while fetching owner details. */ - List getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException; + List getDevicesByTenantId(int tenantId, int deviceTypeId, String deviceOwner, String deviceStatus) + throws DeviceManagementDAOException; /** * Get operation details by operation code. diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index a3039ba356..b07f915e43 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5354,13 +5354,19 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner) throws DeviceManagementDAOException { + public OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus) + throws DeviceManagementDAOException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); OwnerWithDeviceDTO ownerWithDeviceDTO; + List allowingDeviceStatuses = new ArrayList<>(); + allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); + try { DeviceManagementDAOFactory.openConnection(); - ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, tenantId); + ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus); if (ownerWithDeviceDTO == null) { String msg = "No data found for owner: " + owner; log.error(msg); @@ -5384,13 +5390,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override - public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId) throws DeviceManagementDAOException { + public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, String deviceOwner, String deviceName, String deviceStatus) + throws DeviceManagementDAOException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); OwnerWithDeviceDTO deviceOwnerWithStatus; try { DeviceManagementDAOFactory.openConnection(); - deviceOwnerWithStatus = enrollmentDAO.getOwnerWithDeviceByDeviceId(deviceId, tenantId); + deviceOwnerWithStatus = enrollmentDAO.getOwnerWithDeviceByDeviceId(deviceId, tenantId, deviceOwner, deviceName, deviceStatus); if (deviceOwnerWithStatus == null) { throw new DeviceManagementDAOException("No data found for device ID: " + deviceId); } @@ -5411,11 +5418,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException { + public List getDevicesByTenantId(int tenantId, int deviceTypeId, String deviceOwner, String deviceStatus) + throws DeviceManagementDAOException { List devices; + List allowingDeviceStatuses = new ArrayList<>(); + allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); try { DeviceManagementDAOFactory.openConnection(); - devices = enrollmentDAO.getDevicesByTenantId(tenantId); + devices = enrollmentDAO.getDevicesByTenantId(tenantId, allowingDeviceStatuses, deviceTypeId, deviceOwner, deviceStatus); if (devices == null || devices.isEmpty()) { String msg = "No devices found for tenant ID: " + tenantId; log.error(msg); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java index fac06bfccf..3104cff169 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java @@ -377,11 +377,16 @@ public interface GroupManagementProviderService { * Get group details and device IDs for a given group name. * * @param groupName the name of the group. + * @param deviceTypeId the device type id + * @param deviceOwner owner of the device + * @param deviceName name of the device + * @param deviceStatus status of the device * @param offset the offset for the data set * @param limit the limit for the data set * @return {@link GroupDetailsDTO} which containing group details and a list of device IDs * @throws GroupManagementException if an error occurs while fetching group details. */ - GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int offset, int limit) throws GroupManagementException; + GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus, + int offset, int limit) throws GroupManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index c42feed9b8..c2c00d55c0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.service; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; @@ -1688,17 +1689,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } @Override - public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int offset, int limit) - throws GroupManagementException { + public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus, + int offset, int limit) throws GroupManagementException { if (log.isDebugEnabled()) { log.debug("Retrieving group details and device IDs for group: " + groupName); } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupDetailsDTO groupDetailsWithDevices; + List allowingDeviceStatuses = new ArrayList<>(); + allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); try { GroupManagementDAOFactory.openConnection(); - groupDetailsWithDevices = this.groupDAO.getGroupDetailsWithDevices(groupName, tenantId, offset, limit); + groupDetailsWithDevices = this.groupDAO.getGroupDetailsWithDevices(groupName, allowingDeviceStatuses, + deviceTypeId, tenantId, deviceOwner, deviceName, deviceStatus, offset, limit); } catch (GroupManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName; log.error(msg, e);