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/DeviceSubscription.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java index f3209c53b6..7d4efd22d9 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java @@ -22,6 +22,7 @@ package io.entgra.device.mgt.core.application.mgt.common; import java.sql.Timestamp; public class DeviceSubscription { + private int deviceId; private String deviceName; private String deviceIdentifier; private String deviceStatus; @@ -31,6 +32,14 @@ public class DeviceSubscription { private Timestamp dateOfLastUpdate; private SubscriptionData subscriptionData; + public int getDeviceId() { + return deviceId; + } + + public void setDeviceId(int deviceId) { + this.deviceId = deviceId; + } + public String getDeviceName() { return deviceName; } 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/util/subscription/mgt/bean/DeviceSubscriptionStatus.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java similarity index 52% rename from components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/DeviceSubscriptionStatus.java rename to components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java index 11cbb62386..6d1b128f42 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/DeviceSubscriptionStatus.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java @@ -17,8 +17,20 @@ * */ -package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean; +package io.entgra.device.mgt.core.application.mgt.common; -public enum DeviceSubscriptionStatus { - COMPLETED, ERROR, NEW, SUBSCRIBED +public class SubscriptionMetadata { + public static final class DeviceSubscriptionStatus { + public static final String NEW = "NEW"; + public static final String PENDING = "PENDING"; + public static final String COMPLETED = "COMPLETED"; + public static final String FAILED = "FAILED"; + } + + public static final class SubscriptionTypes { + public static final String ROLE = "role"; + public static final String DEVICE = "device"; + public static final String GROUP = "group"; + public static final String USER = "user"; + } } 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/util/subscription/mgt/bean/RoleBasedSubscriptionInfo.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionResponse.java similarity index 51% rename from components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/RoleBasedSubscriptionInfo.java rename to components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionResponse.java index 1e7385d173..aeaeba70be 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/RoleBasedSubscriptionInfo.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionResponse.java @@ -17,45 +17,47 @@ * */ -package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean; +package io.entgra.device.mgt.core.application.mgt.common; -public class RoleBasedSubscriptionInfo { +import java.util.List; +public class SubscriptionResponse { private String applicationUUID; - private String roleName; - private String subscriptionStatus; - public static String TRIGGERED_FROM_VALUE = "role"; - private DeviceSubscriptionStatus deviceSubscriptionStatus; + private int count; + private List data; - public String getApplicationUUID() { - return applicationUUID; + public SubscriptionResponse(String applicationUUID, int count, List data) { + this.applicationUUID = applicationUUID; + this.count = count; + this.data = data; } - public void setApplicationUUID(String applicationUUID) { + public SubscriptionResponse(String applicationUUID, List data) { this.applicationUUID = applicationUUID; + this.data = data; } - public String getRoleName() { - return roleName; + public String getApplicationUUID() { + return applicationUUID; } - public void setRoleName(String roleName) { - this.roleName = roleName; + public void setApplicationUUID(String applicationUUID) { + this.applicationUUID = applicationUUID; } - public String getSubscriptionStatus() { - return subscriptionStatus; + public int getCount() { + return count; } - public void setSubscriptionStatus(String subscriptionStatus) { - this.subscriptionStatus = subscriptionStatus; + public void setCount(int count) { + this.count = count; } - public DeviceSubscriptionStatus getDeviceSubscriptionStatus() { - return deviceSubscriptionStatus; + public List getData() { + return data; } - public void setDeviceSubscriptionStatus(DeviceSubscriptionStatus deviceSubscriptionStatus) { - this.deviceSubscriptionStatus = deviceSubscriptionStatus; + public void setData(List data) { + this.data = data; } } 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/SubscriptionStatistics.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionStatistics.java new file mode 100644 index 0000000000..80a10e98c5 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionStatistics.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +public class SubscriptionStatistics { + private float completedPercentage; + private float failedPercentage; + private float pendingPercentage; + private float newDevicesPercentage; + + public SubscriptionStatistics() {} + public SubscriptionStatistics(float completedPercentage, float failedPercentage, float pendingPercentage, + float newDevicesPercentage) { + this.completedPercentage = completedPercentage; + this.failedPercentage = failedPercentage; + this.pendingPercentage = pendingPercentage; + this.newDevicesPercentage = newDevicesPercentage; + } + + public float getCompletedPercentage() { + return completedPercentage; + } + + public void setCompletedPercentage(float completedPercentage) { + this.completedPercentage = completedPercentage; + } + + public float getFailedPercentage() { + return failedPercentage; + } + + public void setFailedPercentage(float failedPercentage) { + this.failedPercentage = failedPercentage; + } + + public float getPendingPercentage() { + return pendingPercentage; + } + + public void setPendingPercentage(float pendingPercentage) { + this.pendingPercentage = pendingPercentage; + } + + public float getNewDevicesPercentage() { + return newDevicesPercentage; + } + + public void setNewDevicesPercentage(float newDevicesPercentage) { + this.newDevicesPercentage = newDevicesPercentage; + } +} 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/dto/SubscriptionStatisticDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java new file mode 100644 index 0000000000..1f02402fae --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common.dto; + +public class SubscriptionStatisticDTO { + private int completedDeviceCount; + private int pendingDevicesCount; + private int failedDevicesCount; + + public int getCompletedDeviceCount() { + return completedDeviceCount; + } + + public void setCompletedDeviceCount(int completedDeviceCount) { + this.completedDeviceCount = completedDeviceCount; + } + + public int getPendingDevicesCount() { + return pendingDevicesCount; + } + + public void setPendingDevicesCount(int pendingDevicesCount) { + this.pendingDevicesCount = pendingDevicesCount; + } + + public int getFailedDevicesCount() { + return failedDevicesCount; + } + + public void setFailedDevicesCount(int failedDevicesCount) { + this.failedDevicesCount = failedDevicesCount; + } +} 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 7f73cd1d0e..1e70f1b9d6 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 @@ -24,6 +24,8 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; @@ -229,77 +231,34 @@ public interface SubscriptionManager { Activity getOperationAppDetails(String id) throws SubscriptionManagementException; /** - * Retrieves the group details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link SubscriptionsDTO} which contains the details of subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the group details + * Get subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data */ - 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. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @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, PaginationRequest request, - int offset, int limit) throws ApplicationManagementException; - - /** - * Retrieves the Role details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @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, PaginationRequest request, -// int offset, int limit) throws ApplicationManagementException; - List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException; - - List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; /** - * Retrieves the Device Subscription details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the device subscription details + * Get status based subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data */ - DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, - int limit) throws ApplicationManagementException; + SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; /** - * Retrieves the All Device details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the subscription details + * Get subscription statistics related data describes by the {@link SubscriptionInfo} + * @param subscriptionInfo {@link SubscriptionInfo} + * @return {@link SubscriptionStatistics} + * @throws ApplicationManagementException Throws when error encountered while getting statistics */ - DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, - int limit) throws ApplicationManagementException; + SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) 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 c68eb96e1d..e7098c37a5 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 @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; @@ -400,8 +401,10 @@ public interface SubscriptionDAO { */ List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus, - int limit, int offset) throws ApplicationManagementDAOException; + String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException; + int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, String actionStatus, String actionType, + String actionTriggeredBy) throws ApplicationManagementDAOException; /** * This method is used to get the details of device subscriptions related to a UUID. @@ -420,6 +423,10 @@ public interface SubscriptionDAO { List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException; + int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, + String actionStatus, String actionType, String actionTriggeredBy) + throws ApplicationManagementDAOException; + /** * This method is used to get the counts of all subscription types related to a UUID. * @@ -519,4 +526,8 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + + SubscriptionStatisticDTO getSubscriptionStatistic(List deviceIds, String subscriptionType, boolean isUnsubscribed, + int tenantId) throws ApplicationManagementDAOException; + } 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 d645cf7ab1..53e4409911 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 @@ -17,9 +17,11 @@ */ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; @@ -47,6 +49,7 @@ import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.StringJoiner; import java.util.stream.Collectors; @@ -1914,8 +1917,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus, - int offset, int limit) throws ApplicationManagementDAOException { + String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); @@ -2014,6 +2016,71 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } + @Override + public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, String actionStatus, String actionType, + String actionTriggeredBy) throws ApplicationManagementDAOException { + int deviceCount = 0; + try { + Connection conn = this.getDBConnection(); + StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + + "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(",")) + ") "); + + 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 ?"); + } + + 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(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 " + + appReleaseId + " and device ids " + deviceIds); + } + if (rs.next()) { + deviceCount = rs.getInt("COUNT"); + } + return deviceCount; + } + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId + + " and device ids: " + deviceIds + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for " + + "application Id: " + appReleaseId + " and device ids: " + deviceIds + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + // @Override // public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, // List deviceIds, String actionStatus, String actionType, @@ -2205,6 +2272,72 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + @Override + public int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, + String actionStatus, String actionType, String actionTriggeredBy) + throws ApplicationManagementDAOException { + int deviceCount = 0; + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + + " from the database"); + } + + String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY"; + StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "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 ?"); + } + + try { + Connection conn = this.getDBConnection(); + 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 + "%"); + } + + try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId); + } + if (rs.next()) { + deviceCount = rs.getInt("COUNT"); + } + } + return deviceCount; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscription for " + + "application Id: " + appReleaseId + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + 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); + } + } + @Override public int getAllSubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException { @@ -2596,4 +2729,56 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public SubscriptionStatisticDTO getSubscriptionStatistic(List deviceIds, String subscriptionType, + boolean isUnsubscribed, int tenantId) + throws ApplicationManagementDAOException { + SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO(); + String deviceIdsString = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + boolean doesAllEntriesRequired = true; + try { + Connection connection = getDBConnection(); + String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + + "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + + "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ deviceIdsString + ")"; + if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) { + sql = sql + " AND ACTION_TRIGGERED_FROM = ?"; + doesAllEntriesRequired = false; + } + sql = sql + " GROUP BY (STATUS)"; + + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int idx = 1; + + preparedStatement.setInt(idx++, tenantId); + preparedStatement.setBoolean(idx++, isUnsubscribed); + if (!doesAllEntriesRequired) { + preparedStatement.setString(idx, subscriptionType); + } + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + int count = resultSet.getInt("COUNT"); + String status = resultSet.getString("STATUS"); + if (Objects.equals(status, "COMPLETED")) { + subscriptionStatisticDTO.setCompletedDeviceCount(count); + } else if (Objects.equals(status, "PENDING")) { + subscriptionStatisticDTO.setPendingDevicesCount(count); + } else { + subscriptionStatisticDTO.setFailedDevicesCount(count); + } + } + } + } + return subscriptionStatisticDTO; + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting subscription statistics"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while running SQL for getting subscription statistics"; + 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 bb6c93d889..49552ea153 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 @@ -23,21 +23,18 @@ import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallRespon import io.entgra.device.mgt.core.application.mgt.common.ApplicationSubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.ApplicationType; import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; -import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; -import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; -import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionResponseDTO; import io.entgra.device.mgt.core.application.mgt.common.DeviceTypes; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.SubAction; import io.entgra.device.mgt.core.application.mgt.common.SubscribingDeviceIdHolder; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType; -import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; @@ -52,13 +49,9 @@ import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.Subs import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; -import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO; -import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO; -import io.entgra.device.mgt.core.device.mgt.core.dto.OwnerWithDeviceDTO; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext; import io.entgra.device.mgt.core.notification.logger.impl.EntgraAppInstallLoggerImpl; @@ -117,7 +110,6 @@ import io.entgra.device.mgt.core.device.mgt.core.util.MDMIOSOperationUtil; import io.entgra.device.mgt.core.device.mgt.core.util.MDMWindowsOperationUtil; import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.api.UserStoreManager; import javax.ws.rs.core.MediaType; import java.io.BufferedReader; @@ -132,7 +124,6 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -140,7 +131,6 @@ import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Function; import java.util.stream.Collectors; /** @@ -1708,1346 +1698,48 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + /** + * Get subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data + */ @Override - public List getGroupsSubscriptionDetailsByUUID( - String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) - throws ApplicationManagementException { - return null; - } - - @Override - public List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, int limit) throws ApplicationManagementException { - return null; - } - - @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { SubscriptionManagementHelperService subscriptionManagementHelperService = SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); return subscriptionManagementHelperService.getSubscriptions(subscriptionInfo, limit, offset); } + /** + * Get status based subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data + */ @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { SubscriptionManagementHelperService subscriptionManagementHelperService = SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); return subscriptionManagementHelperService.getStatusBaseSubscriptions(subscriptionInfo, limit, offset); } -// @Override -// 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"); -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List groupDetailsWithDevices = new ArrayList<>(); -// -// List groupDetails = -// subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (groupDetails == null) { -// throw new ApplicationManagementException("Group details not found for appReleaseId: " + appReleaseId); -// } -// -// GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); -// -// for (GroupSubscriptionDTO groupDetail : groupDetails) { -// -// 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, applicationDTO.getDeviceTypeId(), request.getOwner(), -// request.getDeviceName(), request.getDeviceStatus(), offset, limit); -// -// SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO(); -// groupDetailDTO.setId(groupDetailWithDevices.getGroupId()); -// groupDetailDTO.setName(groupDetail.getGroupName()); -// groupDetailDTO.setOwner(groupDetailWithDevices.getGroupOwner()); -// groupDetailDTO.setSubscribedBy(groupDetail.getSubscribedBy()); -// groupDetailDTO.setSubscribedTimestamp(groupDetail.getSubscribedTimestamp()); -// groupDetailDTO.setUnsubscribed(groupDetail.isUnsubscribed()); -// groupDetailDTO.setUnsubscribedBy(groupDetail.getUnsubscribedBy()); -// groupDetailDTO.setUnsubscribedTimestamp(groupDetail.getUnsubscribedTimestamp()); -// groupDetailDTO.setAppReleaseId(groupDetail.getAppReleaseId()); -// groupDetailDTO.setDeviceCount(groupDetailWithDevices.getDeviceCount()); -// -// // Fetch device subscriptions for each device ID in the group -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// List deviceIds = groupDetailWithDevices.getDeviceIds(); -// Map statusCounts = new HashMap<>(); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("PENDING", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// for (Integer deviceId : 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 : filteredDeviceSubscriptions) { -// if (subscription.getDeviceId() == deviceId) { -// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); -// deviceDetail.setDeviceId(subscription.getDeviceId()); -// deviceDetail.setStatus(subscription.getStatus()); -// deviceDetail.setActionType(subscription.getActionTriggeredFrom()); -// deviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); -// deviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); -// deviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); -// deviceDetail.setSubId(subscription.getId()); -// deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); -// deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); -// deviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); -// -// String status = subscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// 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 : deviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// subscribedDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); -// subscribedDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); -// subscribedDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); -// subscribedDeviceDetail.setSubId(subscribedDevice.getId()); -// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); -// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); -// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); -// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); -// subscribedDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); -// subscribedDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// newDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); -// newDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); -// newDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); -// newDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); -// newDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); -// newDevices.add(newDeviceDetail); -// statusCounts.put("NEW", statusCounts.get("NEW") + 1); -// } -// } -// } -// -// int totalDevices = deviceIds.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// 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; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// groupDetailDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// } else { -// 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.setStatusPercentages(statusPercentages); -// groupDetailsWithDevices.add(groupDetailDTO); -// } -// -// return groupDetailsWithDevices; -// } catch (ApplicationManagementDAOException e) { -// String msg = "Error occurred while fetching groups and devices for UUID: " + uuid; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "DB Connection error occurred while fetching groups and devices for UUID: " + uuid; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (GroupManagementException e) { -// String msg = "Error occurred while fetching group details and device IDs: " + e.getMessage(); -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } -// -// @Override -// 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 status; -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List userSubscriptionsWithDevices = new ArrayList<>(); -// -// List userSubscriptions = -// subscriptionDAO.getUserSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (userSubscriptions == null) { -// throw new ApplicationManagementException("User details not found for appReleaseId: " + appReleaseId); -// } -// -// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); -// -// for (SubscriptionsDTO userSubscription : userSubscriptions) { -// -// 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, applicationDTO.getDeviceTypeId(), -// request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); -// -// SubscriptionsDTO userSubscriptionDTO = new SubscriptionsDTO(); -// userSubscriptionDTO.setName(userSubscription.getName()); -// userSubscriptionDTO.setSubscribedBy(userSubscription.getSubscribedBy()); -// userSubscriptionDTO.setSubscribedTimestamp(userSubscription.getSubscribedTimestamp()); -// userSubscriptionDTO.setUnsubscribed(userSubscription.getUnsubscribed()); -// userSubscriptionDTO.setUnsubscribedBy(userSubscription.getUnsubscribedBy()); -// userSubscriptionDTO.setUnsubscribedTimestamp(userSubscription.getUnsubscribedTimestamp()); -// userSubscriptionDTO.setAppReleaseId(userSubscription.getAppReleaseId()); -// -// userSubscriptionDTO.setDeviceCount(ownerDetailsWithDevices.getDeviceCount()); -// -// // Fetch device subscriptions for each device ID associated with the user -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); -// Map statusCounts = new HashMap<>(); -// statusCounts.put("PENDING", 0); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// List subscribedDeviceSubscriptions = new ArrayList<>(); -// if (unsubscribe) { -// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// 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, 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(ownerWithDeviceByDeviceId.getUserName()); -// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// deviceDetail.setActionType(subscription.getActionTriggeredFrom()); -// deviceDetail.setStatus(subscription.getStatus()); -// deviceDetail.setActionType(subscription.getActionTriggeredFrom()); -// deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); -// deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// -// status = subscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// pendingDevices.add(deviceDetail); -// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); -// break; -// } -// isNewDevice = false; -// } -// } -// if (isNewDevice) { -// boolean isSubscribedDevice = false; -// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// 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(ownerWithDeviceByDeviceId.getDeviceTypes()); -// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// 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); -// } -// } -// } -// -// int totalDevices = deviceIds.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// 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; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// userSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// } else { -// 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; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "DB Connection error occurred while getting user subscriptions for UUID: " + uuid; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DeviceManagementDAOException e) { -// throw new RuntimeException(e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } -// -// -// @Override -// 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"); -// String roleName; -// String status; -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List roleSubscriptionsWithDevices = new ArrayList<>(); -// -// List roleSubscriptions = -// subscriptionDAO.getRoleSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (roleSubscriptions == null) { -// throw new ApplicationManagementException("Role details not found for appReleaseId: " + appReleaseId); -// } -// - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); -// -// for (SubscriptionsDTO roleSubscription : roleSubscriptions) { -// -// roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); -// -// SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); -// roleSubscriptionDTO.setName(roleSubscription.getName()); -// roleSubscriptionDTO.setSubscribedBy(roleSubscription.getSubscribedBy()); -// roleSubscriptionDTO.setSubscribedTimestamp(roleSubscription.getSubscribedTimestamp()); -// roleSubscriptionDTO.setUnsubscribed(roleSubscription.getUnsubscribed()); -// roleSubscriptionDTO.setUnsubscribedBy(roleSubscription.getUnsubscribedBy()); -// roleSubscriptionDTO.setUnsubscribedTimestamp(roleSubscription.getUnsubscribedTimestamp()); -// roleSubscriptionDTO.setAppReleaseId(roleSubscription.getAppReleaseId()); -// -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// Map statusCounts = new HashMap<>(); -// statusCounts.put("PENDING", 0); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// // getting the user list for the role -// List users = this.getUsersForRole(roleName); -// -// for (String user : users) { -// -// // for each user get the device info and device ids -// OwnerWithDeviceDTO ownerDetailsWithDevices; -// try { -// 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); -// } -// -// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); -// // now for each device id -// for (Integer deviceId : deviceIds) { -// -// List subscribedDeviceSubscriptions = new ArrayList<>(); -// if (unsubscribe) { -// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), -// request.getActionTriggeredBy(), request.getTabActionStatus()); -// } -// -// // why the fuck is this here -// 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, request.getActionStatus(), -// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); -// } catch (ApplicationManagementDAOException e) { -// throw new ApplicationManagementException("Error retrieving device subscriptions", e); -// } -// -// boolean isNewDevice = true; -// for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) { -// if (deviceSubscription.getDeviceId() == deviceId) { -// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); -// deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); -// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setStatus(deviceSubscription.getStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setActionTriggeredBy(deviceSubscription.getSubscribedBy()); -// deviceDetail.setSubId(deviceSubscription.getId()); -// deviceDetail.setActionTriggeredTimestamp(deviceSubscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// -// status = deviceSubscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// pendingDevices.add(deviceDetail); -// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); -// break; -// } -// isNewDevice = false; -// } -// } -// if (isNewDevice) { -// boolean isSubscribedDevice = false; -// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// 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(ownerWithDeviceByDeviceId.getDeviceTypes()); -// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// 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); -// } -// } -// } -// } -// -// int totalDevices = -// pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size() + subscribedDevices.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = totalDevices == 0 ? 0.0 : ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// 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; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// roleSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// -// } else { -// 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); -// } -// roleSubscriptionsWithDevices.add(roleSubscriptionDTO); -// } -// -// return roleSubscriptionsWithDevices; -// } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { -// String msg = "Error occurred in retrieving role subscriptions with devices"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "Error occurred while retrieving the database connection"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (UserStoreException e) { -// String msg = "Error occurred while retrieving users for role"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - -// @Override -// 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"); -// String roleName; -// String status; -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List roleSubscriptionsWithDevices = new ArrayList<>(); -// -// List roleSubscriptions = -// subscriptionDAO.getRoleSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (roleSubscriptions == null) { -// throw new ApplicationManagementException("Role details not found for appReleaseId: " + appReleaseId); -// } -// -// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); -// -// for (SubscriptionsDTO roleSubscription : roleSubscriptions) { -// -// roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); -// -// SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); -// roleSubscriptionDTO.setName(roleSubscription.getName()); -// roleSubscriptionDTO.setSubscribedBy(roleSubscription.getSubscribedBy()); -// roleSubscriptionDTO.setSubscribedTimestamp(roleSubscription.getSubscribedTimestamp()); -// roleSubscriptionDTO.setUnsubscribed(roleSubscription.getUnsubscribed()); -// roleSubscriptionDTO.setUnsubscribedBy(roleSubscription.getUnsubscribedBy()); -// roleSubscriptionDTO.setUnsubscribedTimestamp(roleSubscription.getUnsubscribedTimestamp()); -// roleSubscriptionDTO.setAppReleaseId(roleSubscription.getAppReleaseId()); -// -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// Map statusCounts = new HashMap<>(); -// statusCounts.put("PENDING", 0); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// List users = this.getUsersForRole(roleName); -// -// for (String user : users) { -// OwnerWithDeviceDTO ownerDetailsWithDevices; -// try { -// 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); -// } -// -// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); -// for (Integer deviceId : deviceIds) { -// -// List subscribedDeviceSubscriptions = new ArrayList<>(); -// if (unsubscribe) { -// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// 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, request.getActionStatus(), -// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); -// } catch (ApplicationManagementDAOException e) { -// throw new ApplicationManagementException("Error retrieving device subscriptions", e); -// } -// -// boolean isNewDevice = true; -// for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) { -// if (deviceSubscription.getDeviceId() == deviceId) { -// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); -// deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); -// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setStatus(deviceSubscription.getStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setActionTriggeredBy(deviceSubscription.getSubscribedBy()); -// deviceDetail.setSubId(deviceSubscription.getId()); -// deviceDetail.setActionTriggeredTimestamp(deviceSubscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// -// status = deviceSubscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// pendingDevices.add(deviceDetail); -// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); -// break; -// } -// isNewDevice = false; -// } -// } -// if (isNewDevice) { -// boolean isSubscribedDevice = false; -// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// 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(ownerWithDeviceByDeviceId.getDeviceTypes()); -// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// 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); -// } -// } -// } -// } -// -// int totalDevices = -// pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size() + subscribedDevices.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = totalDevices == 0 ? 0.0 : ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// 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; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// roleSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// -// } else { -// 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); -// } -// roleSubscriptionsWithDevices.add(roleSubscriptionDTO); -// } -// -// return roleSubscriptionsWithDevices; -// } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { -// String msg = "Error occurred in retrieving role subscriptions with devices"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "Error occurred while retrieving the database connection"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (UserStoreException e) { -// String msg = "Error occurred while retrieving users for role"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - - // Get user list for each role - public List getUsersForRole(String roleName) throws UserStoreException { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - int tenantId = ctx.getTenantId(); - UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); - String[] users = userStoreManager.getUserListOfRole(roleName); - return Arrays.asList(users); - } - - @Override - public DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, int offset, - int limit) throws ApplicationManagementException { - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); - - try { - ConnectionManagerUtil.openDBConnection(); - - ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + uuid; - log.error(msg); - throw new NotFoundException(msg); - } - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); - int appReleaseId = applicationReleaseDTO.getId(); - - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - List deviceSubscriptions = - subscriptionDAO.getDeviceSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); - - // empty response for no device subscriptions - if (deviceSubscriptions.isEmpty()) { - return new DeviceSubscriptionResponseDTO(0, Collections.emptyMap(), - new CategorizedSubscriptionResult(Collections.emptyList(), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - } - - List allDevices = - deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(), - request.getOwner(), request.getDeviceStatus()); - - List deviceIds = allDevices.stream() - .map(DeviceDetailsDTO::getDeviceId) - .collect(Collectors.toList()); - - Map statusCounts = new HashMap<>(); - statusCounts.put("PENDING", 0); - statusCounts.put("COMPLETED", 0); - statusCounts.put("ERROR", 0); - statusCounts.put("NEW", 0); - statusCounts.put("SUBSCRIBED", 0); - - List installedDevices = new ArrayList<>(); - List pendingDevices = new ArrayList<>(); - List errorDevices = new ArrayList<>(); - List newDevices = new ArrayList<>(); - List subscribedDevices = new ArrayList<>(); - - Map deviceSubscriptionMap = deviceSubscriptions.stream() - .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); - Map allDevicesMap = allDevices.stream() - .collect(Collectors.toMap(DeviceDetailsDTO::getDeviceId, Function.identity())); - - List allSubscriptionsForUnSubscribed = null; -// subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), -// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - List allSubscriptionsForSubscribed = null; -// 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() - .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); - - for (DeviceDetailsDTO device : allDevices) { - Integer deviceId = device.getDeviceId(); - OwnerWithDeviceDTO ownerWithDevice = - deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), - request.getDeviceStatus()); - if (ownerWithDevice == null || (request.getDeviceName() != null && !request.getDeviceName().isEmpty() && - (ownerWithDevice.getDeviceNames() == null || !ownerWithDevice.getDeviceNames().contains(request.getDeviceName())))) { - continue; - } - if (deviceSubscriptionMap.containsKey(deviceId)) { - DeviceSubscriptionDTO subscription = deviceSubscriptionMap.get(deviceId); - DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); - deviceDetail.setDeviceId(subscription.getDeviceId()); - deviceDetail.setSubId(subscription.getId()); - deviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - deviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - deviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - deviceDetail.setActionType(subscription.getActionTriggeredFrom()); - deviceDetail.setStatus(subscription.getStatus()); - deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); - deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); - deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); - deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); - deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); - deviceDetail.setType(ownerWithDevice.getDeviceTypes()); - deviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - - String status = subscription.getStatus(); - switch (status) { - case "COMPLETED": - installedDevices.add(deviceDetail); - statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); - break; - case "ERROR": - case "INVALID": - case "UNAUTHORIZED": - errorDevices.add(deviceDetail); - statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); - break; - case "IN_PROGRESS": - case "PENDING": - case "REPEATED": - pendingDevices.add(deviceDetail); - statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); - break; - } - } else if (unsubscribe && allSubscriptionForUnSubscribedMap.containsKey(deviceId) && !deviceSubscriptionMap.containsKey(deviceId)) { - // Check if the device subscription has unsubscribed status set to false - DeviceSubscriptionDTO allSubscription = allSubscriptionForUnSubscribedMap.get(deviceId); - if (!allSubscription.isUnsubscribed()) { - DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); - subscribedDeviceDetail.setDeviceId(allSubscription.getDeviceId()); - subscribedDeviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - subscribedDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - subscribedDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - subscribedDeviceDetail.setSubId(allSubscription.getId()); - subscribedDeviceDetail.setActionTriggeredBy(allSubscription.getSubscribedBy()); - subscribedDeviceDetail.setActionTriggeredTimestamp(allSubscription.getSubscribedTimestamp()); - subscribedDeviceDetail.setActionType(allSubscription.getActionTriggeredFrom()); - subscribedDeviceDetail.setStatus(allSubscription.getStatus()); - subscribedDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - subscribedDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - subscribedDevices.add(subscribedDeviceDetail); - statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); - } - } else if (unsubscribe && !allSubscriptionForUnSubscribedMap.containsKey(deviceId) && !deviceSubscriptionMap.containsKey(deviceId) - && (allDevicesMap.containsKey(deviceId))) { - DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); - newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - newDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - newDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - newDevices.add(newDeviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - } else if (!unsubscribe && !allSubscriptionForSubscribedMap.containsKey(deviceId) && !deviceSubscriptionMap.containsKey(deviceId) - && (allDevicesMap.containsKey(deviceId))) { - DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); - newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - newDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - newDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - newDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - newDevices.add(newDeviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - } - } - - int totalDevices = allDevices.size(); - Map statusPercentages = new HashMap<>(); - for (Map.Entry entry : statusCounts.entrySet()) { - double percentage = ((double) entry.getValue() / totalDevices) * 100; - String formattedPercentage = String.format("%.2f", percentage); - 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; - case "SUBSCRIBED": - requestedDevices = subscribedDevices; - break; - } - } else { - 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); - - return deviceSubscriptionResponse; - - } catch (ApplicationManagementDAOException e) { - String msg = "Error occurred while getting device subscriptions for the application release UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DBConnectionException e) { - String msg = "DB Connection error occurred while getting device subscriptions for UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementDAOException e) { - throw new RuntimeException(e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - + /** + * Get subscription statistics related data describes by the {@link SubscriptionInfo} + * @param subscriptionInfo {@link SubscriptionInfo} + * @return {@link SubscriptionStatistics} + * @throws ApplicationManagementException Throws when error encountered while getting statistics + */ @Override - 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"); - - try { - ConnectionManagerUtil.openDBConnection(); - - ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + uuid; - 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, request.getActionStatus(), - request.getActionType(), request.getActionTriggeredBy(), offset, limit); - - // empty response for no subscriptions - if (allSubscriptions.isEmpty()) { - return new DeviceSubscriptionResponseDTO(0, Collections.emptyMap(), - new CategorizedSubscriptionResult(Collections.emptyList(), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - } - - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - - Map allSubscriptionMap = allSubscriptions.stream() - .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); - - List pendingDevices = new ArrayList<>(); - List installedDevices = new ArrayList<>(); - List errorDevices = new ArrayList<>(); - List newDevices = new ArrayList<>(); - - Map statusCounts = new HashMap<>(); - statusCounts.put("PENDING", 0); - statusCounts.put("COMPLETED", 0); - statusCounts.put("ERROR", 0); - statusCounts.put("NEW", 0); - - List allDevices = - deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(), request.getOwner(), - request.getDeviceStatus()); - - for (DeviceDetailsDTO device : allDevices) { - Integer deviceId = device.getDeviceId(); - OwnerWithDeviceDTO ownerWithDevice = - deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), - request.getDeviceStatus()); - if (ownerWithDevice == null || (request.getDeviceName() != null && !request.getDeviceName().isEmpty() && - (ownerWithDevice.getDeviceNames() == null || !ownerWithDevice.getDeviceNames().contains(request.getDeviceName())))) { - continue; - } - if (allSubscriptionMap.containsKey(deviceId)) { - DeviceSubscriptionDTO subscription = allSubscriptionMap.get(deviceId); - DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); - deviceDetail.setDeviceId(subscription.getDeviceId()); - deviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - deviceDetail.setSubId(subscription.getId()); - deviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - deviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - deviceDetail.setActionType(subscription.getActionTriggeredFrom()); - deviceDetail.setStatus(subscription.getStatus()); - deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); - deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); - deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); - deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); - deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); - deviceDetail.setType(ownerWithDevice.getDeviceTypes()); - deviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - - String status = subscription.getStatus(); - switch (status) { - case "COMPLETED": - installedDevices.add(deviceDetail); - statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); - break; - case "ERROR": - case "INVALID": - case "UNAUTHORIZED": - errorDevices.add(deviceDetail); - statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); - break; - case "IN_PROGRESS": - case "PENDING": - case "REPEATED": - pendingDevices.add(deviceDetail); - statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); - break; - } - } else { - DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); - newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - newDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - newDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - newDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - newDevices.add(newDeviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - } - } - - int totalDevices = allDevices.size(); - Map statusPercentages = new HashMap<>(); - for (Map.Entry entry : statusCounts.entrySet()) { - double percentage = ((double) entry.getValue() / totalDevices) * 100; - String formattedPercentage = String.format("%.2f", percentage); - 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 = - new DeviceSubscriptionResponseDTO(totalDevices, statusPercentages, categorizedSubscriptionResult); - - return result; - } catch (ApplicationManagementDAOException e) { - String msg = "Error occurred while getting user subscriptions for the application release UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DBConnectionException e) { - String msg = "DB Connection error occurred while getting user subscriptions for UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementDAOException e) { - throw new RuntimeException(e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } + public SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + return SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo). + getSubscriptionStatistics(subscriptionInfo); } @Override @@ -3121,13 +1813,9 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List subscriptionCounts = new ArrayList<>(); subscriptionCounts.add(new CategorizedSubscriptionCountsDTO( - "All", + "Device", subscriptionDAO.getAllSubscriptionCount(appReleaseId, tenantId), subscriptionDAO.getAllUnsubscriptionCount(appReleaseId, tenantId))); - subscriptionCounts.add(new CategorizedSubscriptionCountsDTO( - "Device", - subscriptionDAO.getDeviceSubscriptionCount(appReleaseId, tenantId), - subscriptionDAO.getDeviceUnsubscriptionCount(appReleaseId, tenantId))); subscriptionCounts.add(new CategorizedSubscriptionCountsDTO( "Group", subscriptionDAO.getGroupSubscriptionCount(appReleaseId, tenantId), 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/util/subscription/mgt/SubscriptionManagementHelperUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java index d20294535c..e75ec5dd94 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java @@ -23,7 +23,9 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; @@ -36,23 +38,27 @@ import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) throws DeviceManagementException { + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, + boolean isUnsubscribed) + throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); PaginationRequest paginationRequest = new PaginationRequest(0, -1); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); List devices = HelperUtil.getDeviceManagementProviderService().getDevicesByDeviceIds(paginationRequest, deviceIds); - return populateDeviceData(deviceSubscriptionDTOS, devices); + return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } - private static List populateDeviceData(List deviceSubscriptionDTOS, List devices) { + private static List populateDeviceData(List deviceSubscriptionDTOS, + List devices, boolean isUnsubscribed) { List deviceSubscriptions = new ArrayList<>(); for (Device device : devices) { int idx = deviceSubscriptionDTOS.indexOf(new DeviceSubscriptionDTO(device.getId())); if (idx >= 0) { DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptionDTOS.get(idx); DeviceSubscription deviceSubscription = new DeviceSubscription(); + deviceSubscription.setDeviceId(device.getId()); deviceSubscription.setDeviceIdentifier(device.getDeviceIdentifier()); deviceSubscription.setDeviceOwner(device.getEnrolmentInfo().getOwner()); deviceSubscription.setDeviceType(device.getType()); @@ -60,10 +66,7 @@ public class SubscriptionManagementHelperUtil { deviceSubscription.setDeviceStatus(device.getEnrolmentInfo().getStatus().name()); deviceSubscription.setOwnershipType(device.getEnrolmentInfo().getOwnership().name()); deviceSubscription.setDateOfLastUpdate(new Timestamp(device.getEnrolmentInfo().getDateOfLastUpdate())); - SubscriptionData subscriptionData = new SubscriptionData(); - subscriptionData.setTriggeredBy(deviceSubscriptionDTO.getActionTriggeredFrom()); - subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); - subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus()); + SubscriptionData subscriptionData = getSubscriptionData(isUnsubscribed, deviceSubscriptionDTO); deviceSubscription.setSubscriptionData(subscriptionData); deviceSubscriptions.add(deviceSubscription); } @@ -71,13 +74,45 @@ public class SubscriptionManagementHelperUtil { return deviceSubscriptions; } + private static SubscriptionData getSubscriptionData(boolean isUnsubscribed, DeviceSubscriptionDTO deviceSubscriptionDTO) { + SubscriptionData subscriptionData = new SubscriptionData(); + subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : + deviceSubscriptionDTO.getSubscribedBy()); + subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); + subscriptionData.setDeviceSubscriptionStatus(deviceSubscriptionDTO.getStatus()); + subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getActionTriggeredFrom()); + return subscriptionData; + } + public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). - getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); + getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); } public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) { return (deviceSubscriptionStatusFilter != null && !deviceSubscriptionStatusFilter.isEmpty()) ? deviceSubscriptionStatusFilter : deviceSubscriptionStatus; } + + public static SubscriptionStatistics getSubscriptionStatistics(SubscriptionStatisticDTO subscriptionStatisticDTO, int allDeviceCount) { + SubscriptionStatistics subscriptionStatistics = new SubscriptionStatistics(); + subscriptionStatistics.setCompletedPercentage( + getPercentage(subscriptionStatisticDTO.getCompletedDeviceCount(), allDeviceCount)); + subscriptionStatistics.setPendingPercentage( + getPercentage(subscriptionStatisticDTO.getPendingDevicesCount(), allDeviceCount)); + subscriptionStatistics.setFailedPercentage( + getPercentage(subscriptionStatisticDTO.getFailedDevicesCount(), allDeviceCount)); + subscriptionStatistics.setNewDevicesPercentage(getPercentage((allDeviceCount - + subscriptionStatisticDTO.getCompletedDeviceCount() - + subscriptionStatisticDTO.getPendingDevicesCount() - + subscriptionStatisticDTO.getFailedDevicesCount()), allDeviceCount)); + return subscriptionStatistics; + } + + public static float getPercentage(int numerator, int denominator) { + if (denominator <= 0) { + return 0.0f; + } + return ((float) numerator / (float) denominator) * 100; + } } 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/util/subscription/mgt/SubscriptionManagementServiceProvider.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java index 2769278735..61602d7317 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.DeviceBasedSubscriptionManagementHelperServiceImpl; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.GroupBasedSubscriptionManagementHelperServiceImpl; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.RoleBasedSubscriptionManagementHelperServiceImpl; @@ -29,10 +30,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.serv import java.util.Objects; public class SubscriptionManagementServiceProvider { - private SubscriptionManagementServiceProvider() {} - - private static class SubscriptionManagementProviderServiceHolder { - private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider(); + private SubscriptionManagementServiceProvider() { } public static SubscriptionManagementServiceProvider getInstance() { @@ -44,10 +42,18 @@ public class SubscriptionManagementServiceProvider { } private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { - if (Objects.equals(subscriptionType, "role")) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, "group")) return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, "user")) return UserBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, "device")) return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.ROLE)) + return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.GROUP)) + return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.USER)) + return UserBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) + return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance(); throw new UnsupportedOperationException("Subscription type: " + subscriptionType + " not supports"); } + + private static class SubscriptionManagementProviderServiceHolder { + private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider(); + } } 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/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java index 30d91338c3..1963e8dcd7 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java @@ -20,34 +20,28 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; -import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; -import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; import io.entgra.device.mgt.core.application.mgt.core.exception.NotFoundException; -import io.entgra.device.mgt.core.application.mgt.core.internal.DataHolder; import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil; import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; -import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; -import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.api.UserStoreManager; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -55,20 +49,20 @@ import java.util.stream.Collectors; public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(DeviceBasedSubscriptionManagementHelperServiceImpl.class); - private DeviceBasedSubscriptionManagementHelperServiceImpl() {} - private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder { - private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new DeviceBasedSubscriptionManagementHelperServiceImpl(); + + private DeviceBasedSubscriptionManagementHelperServiceImpl() { } + public static DeviceBasedSubscriptionManagementHelperServiceImpl getInstance() { return DeviceBasedSubscriptionManagementHelperServiceImpl.DeviceBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -84,27 +78,34 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - if (Objects.equals("NEW", deviceSubscriptionStatus)) { + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy(),-1, -1); + getId(), isUnsubscribe, tenantId, null, null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); + List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + + deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); } else { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - } - - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); + deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO. + getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + deviceSubscriptionFilterCriteria.getTriggeredBy()); + } + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; log.error(msg, e); @@ -119,13 +120,20 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return Collections.emptyList(); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), Collections.emptyList()); } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) + throws ApplicationManagementException { + return null; + } + + private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder { + private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new DeviceBasedSubscriptionManagementHelperServiceImpl(); } + } 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/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java index 9e05b6fc2e..3da06cc787 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java @@ -20,13 +20,16 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; @@ -35,10 +38,12 @@ import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; +import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -50,10 +55,8 @@ import java.util.stream.Collectors; public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class); - private GroupBasedSubscriptionManagementHelperServiceImpl() {} - private static class GroupBasedSubscriptionManagementHelperServiceImplHolder { - private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new GroupBasedSubscriptionManagementHelperServiceImpl(); + + private GroupBasedSubscriptionManagementHelperServiceImpl() { } public static GroupBasedSubscriptionManagementHelperServiceImpl getInstance() { @@ -61,11 +64,12 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -89,43 +93,49 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); GroupDetailsDTO groupDetailsDTO; - List deviceIdsOwnByGroup; - if (Objects.equals("NEW", deviceSubscriptionStatus)) { - groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), - applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), - deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1); - deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + for (Integer deviceId : deviceIdsOfSubscription) { + allDeviceIdsOwnByGroup.remove(deviceId); + } + + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup, + new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + + deviceCount = allDeviceIdsOwnByGroup.size(); } else { groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); - deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + List paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); - } - - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + } + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); log.error(msg, e); @@ -143,7 +153,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -157,8 +167,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + int subscriptionCount = isUnsubscribe ? subscriptionDAO.getGroupUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : + subscriptionDAO.getGroupSubscriptionCount(applicationReleaseDTO.getId(), tenantId); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(msg, e); @@ -169,7 +182,34 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + List devices = HelperUtil.getGroupManagementProviderService(). + getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); + List deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList()); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(deviceIdsOwnByGroup, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); + return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); + } catch (ApplicationManagementDAOException e) { + String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (GroupManagementException e) { + String msg = "Error encountered while getting device subscription for group: " + subscriptionInfo.getIdentifier(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private static class GroupBasedSubscriptionManagementHelperServiceImplHolder { + private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new GroupBasedSubscriptionManagementHelperServiceImpl(); } } 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/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java index 72796a9cd4..939b7227ff 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java @@ -20,12 +20,15 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; @@ -39,6 +42,7 @@ import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -53,43 +57,25 @@ import java.util.stream.Collectors; public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class); - private RoleBasedSubscriptionManagementHelperServiceImpl() {} - private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { - private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new RoleBasedSubscriptionManagementHelperServiceImpl(); + + private RoleBasedSubscriptionManagementHelperServiceImpl() { } public static RoleBasedSubscriptionManagementHelperServiceImpl getInstance() { return RoleBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } - @SuppressWarnings("unchecked") @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); - UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). - getTenantUserRealm(tenantId).getUserStoreManager(); - String[] usersWithRole = - userStoreManager.getUserListOfRole(subscriptionInfo.getIdentifier()); - List deviceListOwnByRole = new ArrayList<>(); - for (String user : usersWithRole) { - PaginationRequest paginationRequest = new PaginationRequest(offset, limit); - paginationRequest.setOwner(user); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); - PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getAllDevicesIdList(paginationRequest); - if (ownDeviceIds.getData() != null) { - deviceListOwnByRole.addAll((List)ownDeviceIds.getData()); - } - } - - List deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); + List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); @@ -102,28 +88,37 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - if (Objects.equals("NEW", deviceSubscriptionStatus)) { + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByRole.remove(deviceId); + } + + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole, + new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + deviceCount = deviceIdsOwnByRole.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); - } + isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + } + List deviceSubscriptions = SubscriptionManagementHelperUtil. + getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (UserStoreException e) { String msg = "Error encountered while getting the user management store for tenant id " + tenantId; @@ -143,7 +138,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -157,8 +152,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + int subscriptionCount = isUnsubscribe ? subscriptionDAO.getRoleUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : + subscriptionDAO.getRoleSubscriptionCount(applicationReleaseDTO.getId(), tenantId); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(msg, e); @@ -169,7 +167,47 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(deviceIdsOwnByRole, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = deviceIdsOwnByRole.size(); + return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); + } catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) { + String msg = "Error encountered while getting subscription statistics for role: " + subscriptionInfo.getIdentifier(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @SuppressWarnings("unchecked") + private List getDeviceIdsOwnByRole(String roleName, int tenantId) throws UserStoreException, DeviceManagementException { + UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). + getTenantUserRealm(tenantId).getUserStoreManager(); + String[] usersWithRole = + userStoreManager.getUserListOfRole(roleName); + List deviceListOwnByRole = new ArrayList<>(); + for (String user : usersWithRole) { + PaginationRequest paginationRequest = new PaginationRequest(-1, -1); + paginationRequest.setOwner(user); + paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); + PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getAllDevicesIdList(paginationRequest); + if (ownDeviceIds.getData() != null) { + deviceListOwnByRole.addAll((List) ownDeviceIds.getData()); + } + } + return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); + } + + private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { + private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new RoleBasedSubscriptionManagementHelperServiceImpl(); } } 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/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java index 437bab449f..1e67916804 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java @@ -23,8 +23,12 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; @@ -37,6 +41,7 @@ import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -49,36 +54,25 @@ import java.util.stream.Collectors; public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(UserBasedSubscriptionManagementHelperServiceImpl.class); - private UserBasedSubscriptionManagementHelperServiceImpl() {} - private static class UserBasedSubscriptionManagementHelperServiceImplHolder { - private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new UserBasedSubscriptionManagementHelperServiceImpl(); + + private UserBasedSubscriptionManagementHelperServiceImpl() { } + public static UserBasedSubscriptionManagementHelperServiceImpl getInstance() { return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } - @SuppressWarnings("unchecked") @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); - List deviceListOwnByUser = new ArrayList<>(); - PaginationRequest paginationRequest = new PaginationRequest(offset, limit); - paginationRequest.setOwner(subscriptionInfo.getIdentifier()); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); - PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getAllDevicesIdList(paginationRequest); - if (ownDeviceIds.getData() != null) { - deviceListOwnByUser.addAll((List)ownDeviceIds.getData()); - } - - List deviceIdsOwnByUser = deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList()); + List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); @@ -91,30 +85,37 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - if (Objects.equals("NEW", deviceSubscriptionStatus)) { + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByUser.remove(deviceId); + } + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser, + new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + + deviceCount = deviceIdsOwnByUser.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); - } - + isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + } + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; log.error(msg, e); @@ -129,7 +130,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -143,8 +144,11 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + int subscriptionCount = isUnsubscribe ? subscriptionDAO.getUserUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : + subscriptionDAO.getUserSubscriptionCount(applicationReleaseDTO.getId(), tenantId); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(msg, e); @@ -155,7 +159,41 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(deviceIdsOwnByUser, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = deviceIdsOwnByUser.size(); + return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); + } catch (DeviceManagementException | ApplicationManagementDAOException e) { + String msg = "Error encountered while getting subscription statistics for user: " + subscriptionInfo.getIdentifier(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @SuppressWarnings("unchecked") + private List getDeviceIdsOwnByUser(String username) throws DeviceManagementException { + List deviceListOwnByUser = new ArrayList<>(); + PaginationRequest paginationRequest = new PaginationRequest(-1, -1); + paginationRequest.setOwner(username); + paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); + PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getAllDevicesIdList(paginationRequest); + if (ownDeviceIds.getData() != null) { + deviceListOwnByUser.addAll((List) ownDeviceIds.getData()); + } + return deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList()); + } + + private static class UserBasedSubscriptionManagementHelperServiceImplHolder { + private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new UserBasedSubscriptionManagementHelperServiceImpl(); } } 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/util/subscription/mgt/service/SubscriptionManagementHelperService.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.java index 8b234f0256..f869e282dd 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.java @@ -19,27 +19,26 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; -import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; -import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; -import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory; -import java.util.List; - public interface SubscriptionManagementHelperService { SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); - List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + + SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + + SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + + SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException; - void getSubscriptionStatistics() throws ApplicationManagementException; } 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/DeviceDAO.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/DeviceDAO.java index 3bf7e2d620..422395e38e 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/DeviceDAO.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/DeviceDAO.java @@ -868,6 +868,12 @@ public interface DeviceDAO { List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException; + List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException; + + int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) + throws DeviceManagementDAOException; + List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) 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 c5d2d700cd..5da24ad91d 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 @@ -488,4 +488,5 @@ public interface GroupDAO { int tenantId, String deviceOwner, String deviceName, String deviceStatus, int offset, int limit) throws GroupManagementDAOException; + int getDeviceCount(String groupName, int tenantId) 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/AbstractDeviceDAOImpl.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/AbstractDeviceDAOImpl.java index 7f77d3d3e2..93d6c75fb0 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/AbstractDeviceDAOImpl.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/AbstractDeviceDAOImpl.java @@ -3308,10 +3308,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try { Connection connection = getConnection(); String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID NOT IN " + - "(" + deviceIdStringList + ") LIMIT ? OFFSET ?"; + "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, request.getRowCount()); - preparedStatement.setInt(2, request.getStartIndex()); + preparedStatement.setInt(1, tenantId); + preparedStatement.setInt(2, request.getRowCount()); + preparedStatement.setInt(3, request.getStartIndex()); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); @@ -3326,6 +3327,58 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID IN " + + "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, tenantId); + preparedStatement.setInt(2, request.getRowCount()); + preparedStatement.setInt(3, request.getStartIndex()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids in: " + filteredDeviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) + throws DeviceManagementDAOException { + int deviceCount = 0; + String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE ID NOT IN " + + "(" + deviceIdStringList + ") AND TENANT_ID = ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, tenantId); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + deviceCount = resultSet.getInt("COUNT"); + } + } + return deviceCount; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device count"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + @Override public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException { @@ -3352,7 +3405,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } if (paginationRequest.getDeviceName() != null) { - sql = sql + " AND d.DEVICE_NAME = ?"; + sql = sql + " AND d.NAME = ?"; isDeviceNameProvided = true; } 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 416b6e6efd..65baf5a1d3 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 @@ -1556,4 +1556,29 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { throw new GroupManagementDAOException(msg, e); } } + + @Override + public int getDeviceCount(String groupName, int tenantId) throws GroupManagementDAOException { + int deviceCount = 0; + try { + Connection connection = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT COUNT(d.ID) AS COUNT FROM DM_GROUP d INNER JOIN " + + "DM_DEVICE_GROUP_MAP m ON " + + "d.ID = m.GROUP_ID WHERE d.TENANT_ID = ? AND d.GROUP_NAME = ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, tenantId); + preparedStatement.setString(2, groupName); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + deviceCount = resultSet.getInt("COUNT"); + } + } + } + return deviceCount; + } catch (SQLException e) { + String msg = "Error occurred while retrieving device count for the 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 8c04b48f68..fd43de69fd 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 @@ -1156,6 +1156,10 @@ public interface DeviceManagementProviderService { List getDevicesNotInGivenIdList(List deviceIds, PaginationRequest paginationRequest) throws DeviceManagementException; + List getDevicesInGivenIdList(List deviceIds, PaginationRequest paginationRequest) + throws DeviceManagementException; + int getDeviceCountNotInGivenIdList(List deviceIds) throws DeviceManagementException; + List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) throws DeviceManagementException; } 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 eb8b41b499..086bc4e509 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 @@ -5620,6 +5620,54 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @Override + public List getDevicesInGivenIdList(List deviceIds, PaginationRequest paginationRequest) + throws DeviceManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (paginationRequest == null) { + String msg = "Received null for pagination request"; + log.error(msg); + throw new DeviceManagementException(msg); + } + + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDevicesInGivenIdList(paginationRequest, deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting device ids"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public int getDeviceCountNotInGivenIdList(List deviceIds) + throws DeviceManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceCountNotInGivenIdList(deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting device ids"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + @Override public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) throws DeviceManagementException { 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 3104cff169..89af7b66b8 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 @@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistEx import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; +import io.entgra.device.mgt.core.device.mgt.core.dao.GroupManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; import org.wso2.carbon.user.api.AuthorizationManager; import org.wso2.carbon.user.api.UserStoreManager; @@ -389,4 +390,6 @@ public interface GroupManagementProviderService { GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus, int offset, int limit) throws GroupManagementException; + int getDeviceCount(String groupName) 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 c2c00d55c0..36b79f150f 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 @@ -1725,4 +1725,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupDetailsWithDevices; } + @Override + public int getDeviceCount(String groupName) throws GroupManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + GroupManagementDAOFactory.openConnection(); + return groupDAO.getDeviceCount(groupName, tenantId); + } catch (SQLException | GroupManagementDAOException e) { + String msg = "Error occurred while retrieving device count."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } }