From 0e01b1c0ee885bf15c779c525192d4aec37b5828 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Fri, 19 Jul 2024 19:51:42 +0530 Subject: [PATCH 1/4] Fix filtering issues --- .../mgt/common/SubscriptionResponse.java | 63 +++++++++++++++++++ .../common/services/SubscriptionManager.java | 5 +- .../mgt/core/dao/SubscriptionDAO.java | 2 + .../GenericSubscriptionDAOImpl.java | 60 +++++++++++++++++- .../core/impl/SubscriptionManagerImpl.java | 5 +- .../mgt/SubscriptionManagementHelperUtil.java | 12 ++-- ...bscriptionManagementHelperServiceImpl.java | 34 ++++++---- ...bscriptionManagementHelperServiceImpl.java | 30 ++++++--- ...bscriptionManagementHelperServiceImpl.java | 28 ++++++--- ...bscriptionManagementHelperServiceImpl.java | 30 ++++++--- .../SubscriptionManagementHelperService.java | 5 +- .../core/device/mgt/core/dao/DeviceDAO.java | 3 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 34 ++++++++-- .../DeviceManagementProviderService.java | 1 + .../DeviceManagementProviderServiceImpl.java | 21 +++++++ 15 files changed, 277 insertions(+), 56 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionResponse.java 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/SubscriptionResponse.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 new file mode 100644 index 0000000000..aeaeba70be --- /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/SubscriptionResponse.java @@ -0,0 +1,63 @@ +/* + * 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; + +import java.util.List; + +public class SubscriptionResponse { + private String applicationUUID; + private int count; + private List data; + + public SubscriptionResponse(String applicationUUID, int count, List data) { + this.applicationUUID = applicationUUID; + this.count = count; + this.data = data; + } + + public SubscriptionResponse(String applicationUUID, List data) { + this.applicationUUID = applicationUUID; + this.data = data; + } + + public String getApplicationUUID() { + return applicationUUID; + } + + public void setApplicationUUID(String applicationUUID) { + this.applicationUUID = applicationUUID; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public List getData() { + return data; + } + + 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/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..bf5434283b 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,7 @@ 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.SubscriptionType; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; @@ -267,10 +268,10 @@ public interface SubscriptionManager { */ // List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, // int offset, int limit) throws ApplicationManagementException; - List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; /** 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..8924fa4733 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 @@ -402,6 +402,8 @@ public interface SubscriptionDAO { List deviceIds, String actionStatus, String actionType, String actionTriggeredBy, String tabActionStatus, int limit, int offset) throws ApplicationManagementDAOException; + int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, + String actionTriggeredBy) throws ApplicationManagementDAOException; /** * This method is used to get the details of device subscriptions related to a UUID. diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index d645cf7ab1..b9a459635d 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 @@ -1915,7 +1915,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc 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 { + 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 +2014,64 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } + @Override + public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, + String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { + int deviceCount = 0; + try { + Connection conn = this.getDBConnection(); + StringBuilder sql = new StringBuilder("SELECT " + + "COUNT(DS.ID) AS DEVICE_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 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); + + 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 (rs.next()) { + deviceCount = rs.getInt("DEVICE_COUNT"); + } + } + + return deviceCount; + + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID"; + 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; + 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, 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..3b14c5d68e 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 @@ -27,6 +27,7 @@ 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.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; @@ -1722,7 +1723,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @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); @@ -1730,7 +1731,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @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); 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..ca1077633f 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 @@ -36,17 +36,20 @@ 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())); @@ -61,7 +64,8 @@ public class SubscriptionManagementHelperUtil { deviceSubscription.setOwnershipType(device.getEnrolmentInfo().getOwnership().name()); deviceSubscription.setDateOfLastUpdate(new Timestamp(device.getEnrolmentInfo().getDateOfLastUpdate())); SubscriptionData subscriptionData = new SubscriptionData(); - subscriptionData.setTriggeredBy(deviceSubscriptionDTO.getActionTriggeredFrom()); + subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : + deviceSubscriptionDTO.getSubscribedBy()); subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus()); deviceSubscription.setSubscriptionData(subscriptionData); 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..a7edd0359e 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 @@ -24,6 +24,7 @@ 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.SubscriptionResponse; 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; @@ -40,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; @@ -65,10 +67,11 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @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 +87,34 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(), + getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType().toUpperCase(), 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(), subscriptionInfo. + getSubscriptionType().toUpperCase(), deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType().toUpperCase(), + 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,9 +129,9 @@ 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 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..59240df4d5 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 @@ -24,6 +24,7 @@ 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.SubscriptionResponse; 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; @@ -39,6 +40,7 @@ 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; @@ -61,11 +63,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,6 +92,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); GroupDetailsDTO groupDetailsDTO; @@ -108,9 +112,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr 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 { groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), @@ -118,14 +124,17 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, + isUnsubscribe, tenantId, deviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), null, limit, offset); - } - - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + 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 +152,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 +166,9 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(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/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..eb5c13fb4c 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 @@ -24,6 +24,7 @@ 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.SubscriptionResponse; 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.exception.ApplicationManagementException; @@ -39,6 +40,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; @@ -65,10 +67,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri @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 { @@ -102,6 +105,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), @@ -112,18 +116,23 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri 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.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), null, limit, offset); - } - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + 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 +152,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 +166,9 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(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/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..ee982575d1 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,6 +23,7 @@ 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.SubscriptionResponse; 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.exception.ApplicationManagementException; @@ -37,6 +38,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; @@ -60,10 +62,11 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri @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 { @@ -91,6 +94,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), @@ -101,20 +105,25 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri 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.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, null, + isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), null, limit, offset); - } + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy()); + } - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); - + 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 +138,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 +152,9 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(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/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..743f51b3bb 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 @@ -23,6 +23,7 @@ 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.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -37,9 +38,9 @@ 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; 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..a071b0f6eb 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,9 @@ public interface DeviceDAO { List getDevicesNotInGivenIdList(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/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..3c08335e7a 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,31 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @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 +3378,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/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..5a8439431f 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 @@ -1155,6 +1155,7 @@ public interface DeviceManagementProviderService { List getDevicesNotInGivenIdList(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..c5353ab84e 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,27 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @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 { From 6b6787e0835907d48782e425eda5635c677271b6 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sat, 20 Jul 2024 00:37:16 +0530 Subject: [PATCH 2/4] Add statistics to group based supscription mgt helper --- .../mgt/common/SubscriptionStatistics.java | 68 +++++++++++++++++++ .../common/dto/SubscriptionStatisticDTO.java | 50 ++++++++++++++ .../mgt/core/dao/SubscriptionDAO.java | 5 ++ .../GenericSubscriptionDAOImpl.java | 52 ++++++++++++++ .../mgt/SubscriptionManagementHelperUtil.java | 37 ++++++++-- ...bscriptionManagementHelperServiceImpl.java | 6 +- ...bscriptionManagementHelperServiceImpl.java | 28 +++++++- ...bscriptionManagementHelperServiceImpl.java | 5 +- ...bscriptionManagementHelperServiceImpl.java | 4 +- .../SubscriptionManagementHelperService.java | 4 +- .../core/device/mgt/core/dao/GroupDAO.java | 1 + .../core/dao/impl/AbstractGroupDAOImpl.java | 25 +++++++ .../GroupManagementProviderService.java | 3 + .../GroupManagementProviderServiceImpl.java | 14 ++++ 14 files changed, 288 insertions(+), 14 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionStatistics.java create mode 100644 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 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.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 8924fa4733..e4b893f6ea 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; @@ -521,4 +522,8 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + + SubscriptionStatisticDTO getSubscriptionStatistic(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 b9a459635d..86a401b064 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 @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; 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 +48,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; @@ -2654,4 +2656,54 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public SubscriptionStatisticDTO getSubscriptionStatistic(String subscriptionType, boolean isUnsubscribed, int tenantId) + throws ApplicationManagementDAOException { + SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO(); + boolean doesAllEntriesRequired = true; + try { + Connection connection = getDBConnection(); + String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + + "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + + "TENANT_ID = ? AND UNSUBSCRIBED = ?"; + if (!Objects.equals(subscriptionType, "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/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 ca1077633f..61a6613249 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,11 +23,14 @@ 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; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import org.jetbrains.annotations.NotNull; import java.sql.Timestamp; import java.util.ArrayList; @@ -63,11 +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(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : - deviceSubscriptionDTO.getSubscribedBy()); - subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); - subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus()); + SubscriptionData subscriptionData = getSubscriptionData(isUnsubscribed, deviceSubscriptionDTO); deviceSubscription.setSubscriptionData(subscriptionData); deviceSubscriptions.add(deviceSubscription); } @@ -75,6 +74,16 @@ 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()); @@ -84,4 +93,22 @@ public class SubscriptionManagementHelperUtil { 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)); + return subscriptionStatistics; + } + + public static float getPercentage(int numerator, int denominator) { + if (denominator <= 0) { + return 0.0f; + } + return (float) numerator/denominator; + } } 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 a7edd0359e..a58dcad547 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 @@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; @@ -135,7 +136,8 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + return null; } + } 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 59240df4d5..cd4e92d623 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 @@ -25,9 +25,11 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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.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; @@ -44,6 +46,7 @@ import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProvide import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jetbrains.annotations.NotNull; import org.wso2.carbon.context.PrivilegedCarbonContext; import java.util.List; @@ -126,7 +129,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); + null, -1, -1); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), @@ -179,7 +182,26 @@ 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(); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(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(); + } } } 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 eb5c13fb4c..2daad13bf2 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 @@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -179,7 +180,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + return null; } } 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 ee982575d1..26c9134499 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 @@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -165,7 +166,8 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { // todo: analytics engine + return null; } } 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 743f51b3bb..437fc6d9fa 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 @@ -24,6 +24,7 @@ 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.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -42,5 +43,6 @@ public interface SubscriptionManagementHelperService { throws ApplicationManagementException; SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - void getSubscriptionStatistics() throws ApplicationManagementException; + SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) + 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/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/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..5410aa1960 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 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/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(); + } + } } From 41d8c8ade89390694923c71c2ce04c9db27b1898 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sat, 20 Jul 2024 20:06:15 +0530 Subject: [PATCH 3/4] Fix issues in statistics --- .../mgt/common/DeviceSubscription.java | 9 + .../common/services/SubscriptionManager.java | 68 +- .../core/impl/SubscriptionManagerImpl.java | 1339 +---------------- .../mgt/SubscriptionManagementHelperUtil.java | 7 +- ...bscriptionManagementHelperServiceImpl.java | 13 +- ...bscriptionManagementHelperServiceImpl.java | 4 +- ...bscriptionManagementHelperServiceImpl.java | 64 +- ...bscriptionManagementHelperServiceImpl.java | 49 +- .../core/dao/impl/AbstractGroupDAOImpl.java | 2 +- 9 files changed, 103 insertions(+), 1452 deletions(-) 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.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 bf5434283b..63274700fc 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 @@ -25,6 +25,7 @@ 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,78 +230,13 @@ 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 - */ - 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; SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; SubscriptionResponse getStatusBaseSubscriptions(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 - */ - DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, - int limit) throws ApplicationManagementException; - - /** - * Retrieves the All Device details associated with a given app release UUID. - * - * @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 - */ - 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/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 3b14c5d68e..b8ae361f6e 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,22 +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; @@ -53,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; @@ -118,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; @@ -133,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; @@ -141,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; /** @@ -1709,19 +1698,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - @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 SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -1738,1317 +1714,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager { 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(); - } - } - - @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 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 61a6613249..9488181f91 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 @@ -59,6 +59,7 @@ public class SubscriptionManagementHelperUtil { 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()); @@ -102,6 +103,10 @@ public class SubscriptionManagementHelperUtil { getPercentage(subscriptionStatisticDTO.getPendingDevicesCount(), allDeviceCount)); subscriptionStatistics.setFailedPercentage( getPercentage(subscriptionStatisticDTO.getFailedDevicesCount(), allDeviceCount)); + subscriptionStatistics.setNewDevicesPercentage(getPercentage((allDeviceCount - + subscriptionStatisticDTO.getCompletedDeviceCount() - + subscriptionStatisticDTO.getPendingDevicesCount() - + subscriptionStatisticDTO.getFailedDevicesCount()), allDeviceCount)); return subscriptionStatistics; } @@ -109,6 +114,6 @@ public class SubscriptionManagementHelperUtil { if (denominator <= 0) { return 0.0f; } - return (float) numerator/denominator; + 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/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 a58dcad547..584ed8a4ac 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,37 +20,27 @@ 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.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; @@ -136,7 +126,8 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @Override - public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) + throws ApplicationManagementException { return null; } 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 cd4e92d623..deaec45e43 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 @@ -171,7 +171,9 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } List subscriptionEntities = subscriptionDAO. getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); + 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); 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 2daad13bf2..17f223e9d3 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,7 +20,6 @@ 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; @@ -28,6 +27,7 @@ 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; @@ -66,7 +66,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri return RoleBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } - @SuppressWarnings("unchecked") @Override public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -77,23 +76,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri 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); @@ -117,7 +100,8 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = deviceManagementProviderService.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 { @@ -169,7 +153,9 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } List subscriptionEntities = subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); + 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); @@ -181,6 +167,40 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri @Override public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - return null; + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId).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()); } } 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 26c9134499..8b41019faa 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 @@ -27,6 +27,7 @@ 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; @@ -61,7 +62,6 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } - @SuppressWarnings("unchecked") @Override public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -72,17 +72,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri 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); @@ -155,7 +145,9 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } List subscriptionEntities = subscriptionDAO. getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); + 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); @@ -167,7 +159,34 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri @Override public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - // todo: analytics engine - return null; + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()).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()); } } 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 5410aa1960..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 @@ -1564,7 +1564,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { 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 TENANT_ID = ? AND d.GROUP_NAME = ?"; + "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); From 3596de1b2820b367ad0c8f787554552a070a8b00 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sun, 21 Jul 2024 19:27:33 +0530 Subject: [PATCH 4/4] Fix device count issues --- .../mgt/common/SubscriptionMetadata.java} | 18 ++- .../common/services/SubscriptionManager.java | 22 ++++ .../mgt/core/dao/SubscriptionDAO.java | 14 ++- .../GenericSubscriptionDAOImpl.java | 109 +++++++++++++++--- .../core/impl/SubscriptionManagerImpl.java | 28 ++++- .../mgt/SubscriptionManagementHelperUtil.java | 3 +- ...SubscriptionManagementServiceProvider.java | 22 ++-- .../mgt/bean/RoleBasedSubscriptionInfo.java | 61 ---------- ...bscriptionManagementHelperServiceImpl.java | 29 +++-- ...bscriptionManagementHelperServiceImpl.java | 58 +++++----- ...bscriptionManagementHelperServiceImpl.java | 45 +++++--- ...bscriptionManagementHelperServiceImpl.java | 43 ++++--- .../SubscriptionManagementHelperService.java | 10 +- .../core/device/mgt/core/dao/DeviceDAO.java | 3 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 27 +++++ .../DeviceManagementProviderService.java | 3 + .../DeviceManagementProviderServiceImpl.java | 27 +++++ 17 files changed, 339 insertions(+), 183 deletions(-) rename 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 => io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java} (52%) delete mode 100644 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 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.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 63274700fc..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 @@ -230,12 +230,34 @@ public interface SubscriptionManager { */ Activity getOperationAppDetails(String id) throws SubscriptionManagementException; + /** + * 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 + */ SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + /** + * 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 + */ SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + /** + * 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 + */ SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException; /** 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 e4b893f6ea..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 @@ -401,9 +401,9 @@ 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; - int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, + 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; /** @@ -423,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. * @@ -523,7 +527,7 @@ public interface SubscriptionDAO { */ int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - SubscriptionStatisticDTO getSubscriptionStatistic(String subscriptionType, boolean isUnsubscribed, 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 86a401b064..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,6 +17,7 @@ */ 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; @@ -1916,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 limit, int offset) 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"); @@ -2017,15 +2017,16 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, - String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { + 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(DS.ID) AS DEVICE_COUNT " + 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 = ?"); + + "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 = ? "); @@ -2034,7 +2035,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); } if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { - sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); + sql.append(" AND DS.SUBSCRIBED_BY LIKE ?"); } try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { @@ -2042,6 +2043,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc 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); @@ -2054,21 +2058,24 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } 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("DEVICE_COUNT"); + deviceCount = rs.getInt("COUNT"); } + return deviceCount; } - - return deviceCount; - } catch (SQLException e) { - String msg = "Error occurred while running SQL to get device subscription data for application ID"; + 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; + + "application Id: " + appReleaseId + " and device ids: " + deviceIds + "."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -2265,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 { @@ -2658,16 +2731,18 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public SubscriptionStatisticDTO getSubscriptionStatistic(String subscriptionType, boolean isUnsubscribed, int tenantId) + 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 = ?"; - if (!Objects.equals(subscriptionType, "DEVICE")) { + "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; } 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 b8ae361f6e..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 @@ -1698,6 +1698,14 @@ 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 SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -1706,6 +1714,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { 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 SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -1714,6 +1730,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return subscriptionManagementHelperService.getStatusBaseSubscriptions(subscriptionInfo, limit, offset); } + /** + * 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 SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { return SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo). @@ -1791,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 9488181f91..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 @@ -30,7 +30,6 @@ 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; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; -import org.jetbrains.annotations.NotNull; import java.sql.Timestamp; import java.util.ArrayList; @@ -87,7 +86,7 @@ public class SubscriptionManagementHelperUtil { public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). - getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); + getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); } public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) { 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/bean/RoleBasedSubscriptionInfo.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/bean/RoleBasedSubscriptionInfo.java deleted file mode 100644 index 1e7385d173..0000000000 --- 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 +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean; - -public class RoleBasedSubscriptionInfo { - - private String applicationUUID; - private String roleName; - private String subscriptionStatus; - public static String TRIGGERED_FROM_VALUE = "role"; - private DeviceSubscriptionStatus deviceSubscriptionStatus; - - public String getApplicationUUID() { - return applicationUUID; - } - - public void setApplicationUUID(String applicationUUID) { - this.applicationUUID = applicationUUID; - } - - public String getRoleName() { - return roleName; - } - - public void setRoleName(String roleName) { - this.roleName = roleName; - } - - public String getSubscriptionStatus() { - return subscriptionStatus; - } - - public void setSubscriptionStatus(String subscriptionStatus) { - this.subscriptionStatus = subscriptionStatus; - } - - public DeviceSubscriptionStatus getDeviceSubscriptionStatus() { - return deviceSubscriptionStatus; - } - - public void setDeviceSubscriptionStatus(DeviceSubscriptionStatus deviceSubscriptionStatus) { - this.deviceSubscriptionStatus = deviceSubscriptionStatus; - } -} 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 584ed8a4ac..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 @@ -22,6 +22,7 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.imp 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.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; @@ -48,11 +49,10 @@ 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; } @@ -80,10 +80,10 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc 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().toUpperCase(), - deviceSubscriptionFilterCriteria.getTriggeredBy(),-1, -1); + getId(), isUnsubscribe, tenantId, null, null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -96,14 +96,14 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); } else { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo. - getSubscriptionType().toUpperCase(), deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); + getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType().toUpperCase(), + deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO. + getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { @@ -131,4 +131,9 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc 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 deaec45e43..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,10 +20,10 @@ 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; @@ -38,6 +38,7 @@ 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; @@ -46,7 +47,6 @@ import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProvide import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.jetbrains.annotations.NotNull; import org.wso2.carbon.context.PrivilegedCarbonContext; import java.util.List; @@ -55,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() { @@ -99,41 +97,41 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr 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 = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + for (Integer deviceId : deviceIdsOfSubscription) { + allDeviceIdsOwnByGroup.remove(deviceId); + } + + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); + 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, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); @@ -190,8 +188,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr 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(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByGroup, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (ApplicationManagementDAOException e) { @@ -206,4 +207,9 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr 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 17f223e9d3..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 @@ -23,6 +23,7 @@ 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; @@ -56,10 +57,8 @@ 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() { @@ -91,28 +90,30 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri 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 = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByRole.remove(deviceId); + } + + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + deviceCount = deviceIdsOwnByRole.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. getDeviceSubscriptionData(deviceSubscriptionDTOS, @@ -151,7 +152,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - List subscriptionEntities = subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); int subscriptionCount = isUnsubscribe ? subscriptionDAO.getRoleUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : subscriptionDAO.getRoleSubscriptionCount(applicationReleaseDTO.getId(), tenantId); @@ -171,9 +172,10 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); - int allDeviceCount = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId).size(); + 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(); @@ -198,9 +200,14 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). getAllDevicesIdList(paginationRequest); if (ownDeviceIds.getData() != null) { - deviceListOwnByRole.addAll((List)ownDeviceIds.getData()); + 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 8b41019faa..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,6 +23,7 @@ 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; @@ -53,11 +54,10 @@ 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; } @@ -87,29 +87,30 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri 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 = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByUser.remove(deviceId); + } + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); + deviceCount = deviceIdsOwnByUser.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, @@ -163,9 +164,10 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); - int allDeviceCount = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()).size(); + 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(); @@ -185,8 +187,13 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). getAllDevicesIdList(paginationRequest); if (ownDeviceIds.getData() != null) { - deviceListOwnByUser.addAll((List)ownDeviceIds.getData()); + 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 437fc6d9fa..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,30 +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.SubscriptionResponse; 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.SubscriptionsDTO; 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(); + SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) 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 a071b0f6eb..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,9 @@ 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; 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 3c08335e7a..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 @@ -3327,6 +3327,33 @@ 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 { 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 5a8439431f..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 @@ -1155,6 +1155,9 @@ 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) 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 c5353ab84e..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,33 @@ 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 {