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 1e70f1b9d6..93060a2f2d 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 @@ -252,6 +252,15 @@ public interface SubscriptionManager { SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + SubscriptionResponse getStatusBaseSubscriptionsUser(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + + SubscriptionResponse getStatusBaseSubscriptionsRole(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + + SubscriptionResponse getStatusBaseSubscriptionsGroup(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + /** * Get subscription statistics related data describes by the {@link SubscriptionInfo} * @param subscriptionInfo {@link 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/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 35b1e1f548..33a06aa923 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 @@ -26,12 +26,11 @@ import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; import io.entgra.device.mgt.core.application.mgt.core.util.SubscriptionManagementHelperUtil; -import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementServiceProvider; -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.DeviceManagementConstants; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; +import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext; @@ -91,6 +90,7 @@ 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; @@ -1680,6 +1680,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { * @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 { @@ -1710,15 +1711,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } -// @Override -// public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) -// throws ApplicationManagementException { -// SubscriptionManagementHelperService subscriptionManagementHelperService = -// SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); -// return subscriptionManagementHelperService.getSubscriptions(subscriptionInfo, limit, offset); -// } - - public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { if (subscriptionInfo.getSubscriptionType()==SubscriptionMetadata.SubscriptionTypes.DEVICE){ @@ -1764,14 +1756,342 @@ public class SubscriptionManagerImpl implements SubscriptionManager { * @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 { - SubscriptionManagementHelperService subscriptionManagementHelperService = - SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); - return subscriptionManagementHelperService.getStatusBaseSubscriptions(subscriptionInfo, limit, offset); + + final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int deviceCount = 0; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); + + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { + deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. + getId(), isUnsubscribe, tenantId, null, null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription); + + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + } else { + deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. + getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + } + + + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device details"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + + public SubscriptionResponse getStatusBaseSubscriptionsRole(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int deviceCount = 0; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); + + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); + + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByRole.remove(deviceId); + } + + List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + } else { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + } + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); + + List deviceSubscriptions = SubscriptionManagementHelperUtil. + getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); + + } catch (UserStoreException e) { + String msg = "Error encountered while getting the user management store for tenant id " + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device details"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + 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()); + } + + + public SubscriptionResponse getStatusBaseSubscriptionsGroup(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + + final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int deviceCount = 0; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + + GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); + GroupDetailsDTO groupDetailsDTO; + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); + + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { + List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + for (Integer deviceId : deviceIdsOfSubscription) { + allDeviceIdsOwnByGroup.remove(deviceId); + } + + List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + } else { + groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); + List paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + + } + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); + + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); + 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); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + throw new RuntimeException(e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + public SubscriptionResponse getStatusBaseSubscriptionsUser(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int deviceCount = 0; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); + + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); + + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByUser, null, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByUser.remove(deviceId); + } + List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + + } else { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + } + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); + + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device details"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + 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()); + } + + /** * Get subscription statistics related data describes by the {@link SubscriptionInfo} * @param subscriptionInfo {@link SubscriptionInfo} @@ -1780,10 +2100,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager { */ @Override public SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - return SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo). - getSubscriptionStatistics(subscriptionInfo); + return getSubscriptionStatistics(subscriptionInfo); } + @Override public List getSubscriptionOperationsByUUIDAndDeviceID(int deviceId, String uuid) 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/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 deleted file mode 100644 index 1f13a8a5fa..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/SubscriptionManagementServiceProvider.java +++ /dev/null @@ -1,71 +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; - -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; -import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.UserBasedSubscriptionManagementHelperServiceImpl; -import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; - -import java.util.Objects; - -public class SubscriptionManagementServiceProvider { - private SubscriptionManagementServiceProvider() { - } - - public static SubscriptionManagementServiceProvider getInstance() { - return SubscriptionManagementProviderServiceHolder.INSTANCE; - } - - /** - * Retrieves the appropriate SubscriptionManagementHelperService based on the provided SubscriptionInfo. - * - * @param subscriptionInfo SubscriptionInfo object containing the subscription type. - * @return SubscriptionManagementHelperService implementation based on the subscription type. - */ - public SubscriptionManagementHelperService getSubscriptionManagementHelperService(SubscriptionInfo subscriptionInfo) { - return getSubscriptionManagementHelperService(subscriptionInfo.getSubscriptionType()); - } - - /** - * Retrieves the appropriate SubscriptionManagementHelperService based on the subscription type. - * - * @param subscriptionType Type of the subscription. - * @return SubscriptionManagementHelperService implementation based on the subscription type. - */ - private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { - if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.ROLE)) - return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.GROUP)) - return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.USER)) - return UserBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) - return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance(); - throw new UnsupportedOperationException("Subscription type: " + subscriptionType + " not supports"); - } - - private static class SubscriptionManagementProviderServiceHolder { - private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider(); - } -} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java deleted file mode 100644 index 83354e0132..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/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java +++ /dev/null @@ -1,143 +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.impl; - -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.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.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.util.ConnectionManagerUtil; -import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; -import io.entgra.device.mgt.core.application.mgt.core.util.SubscriptionManagementHelperUtil; -import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; -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 java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { - private static final Log log = LogFactory.getLog(DeviceBasedSubscriptionManagementHelperServiceImpl.class); - - private DeviceBasedSubscriptionManagementHelperServiceImpl() { - } - - public static DeviceBasedSubscriptionManagementHelperServiceImpl getInstance() { - return DeviceBasedSubscriptionManagementHelperServiceImpl.DeviceBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; - } - - @Override - public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException { - final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); - List deviceSubscriptionDTOS; - int deviceCount = 0; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - try { - ConnectionManagerUtil.openDBConnection(); - ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + - subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationDTO == null) { - String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); - - if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { - deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, null, null, - deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - - List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). - map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - - List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription); - - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - } else { - deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, - deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - } - deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); - - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); - } catch (DeviceManagementException e) { - String msg = "Error encountered while getting device details"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationManagementDAOException | DBConnectionException e) { - String msg = "Error encountered while connecting to the database"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException { - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), Collections.emptyList()); - } - - - public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) - throws ApplicationManagementException { - return null; - } - - private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder { - private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new DeviceBasedSubscriptionManagementHelperServiceImpl(); - } - -} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java deleted file mode 100644 index 8a9e19710f..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/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java +++ /dev/null @@ -1,242 +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.impl; - -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.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.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.impl.SubscriptionManagerImpl; -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.SubscriptionManagementHelperUtil; -import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; -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; -import org.wso2.carbon.context.PrivilegedCarbonContext; - -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - - -public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { - private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class); - private SubscriptionManagerImpl subscriptionManager; - - public GroupBasedSubscriptionManagementHelperServiceImpl() { - this.subscriptionManager = new SubscriptionManagerImpl(); - } - - - public static GroupBasedSubscriptionManagementHelperServiceImpl getInstance() { - return GroupBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; - } - - @Override - public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException { - - final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); - List deviceSubscriptionDTOS; - int deviceCount = 0; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - try { - ConnectionManagerUtil.openDBConnection(); - ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); - - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + - subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationDTO == null) { - String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - - GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); - GroupDetailsDTO groupDetailsDTO; - List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); - - if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { - List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), - applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), - deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); - - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - - List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). - map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - - for (Integer deviceId : deviceIdsOfSubscription) { - allDeviceIdsOwnByGroup.remove(deviceId); - } - - List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - } else { - groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), - applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), - deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); - List paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); - - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - - } - deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); - - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); - 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); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationManagementDAOException | DBConnectionException e) { - String msg = "Error encountered while connecting to the database"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementException e) { - throw new RuntimeException(e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - - } - - @Override - public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; - } - - - - @Override - public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - return subscriptionManager.getSubscriptionStatistics(subscriptionInfo); - } - -// @Override -// public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { -// return SubscriptionManager subscriptionManager = new SubscriptionManagerImpl(); -// SubscriptionStatistics stats = subscriptionManager.getSubscriptionStatistics(subscriptionInfo); -// } - - - -// @Override -// public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) -// throws ApplicationManagementException { -// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); -// final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// try { -// ConnectionManagerUtil.openDBConnection(); -// ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. -// getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + -// subscriptionInfo.getApplicationUUID(); -// log.error(msg); -// throw new NotFoundException(msg); -// } -// List subscriptionEntities = subscriptionDAO. -// getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); -// int subscriptionCount = isUnsubscribe ? subscriptionDAO.getGroupUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : -// subscriptionDAO.getGroupSubscriptionCount(applicationReleaseDTO.getId(), tenantId); -// return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); -// } catch (DBConnectionException | ApplicationManagementDAOException e) { -// String msg = "Error encountered while connecting to the database"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - -// @Override -// public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) -// throws ApplicationManagementException { -// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// try { -// ConnectionManagerUtil.openDBConnection(); -// ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. -// getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + -// subscriptionInfo.getApplicationUUID(); -// log.error(msg); -// throw new NotFoundException(msg); -// } -// List devices = HelperUtil.getGroupManagementProviderService(). -// getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); -// List deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList()); -// SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. -// getSubscriptionStatistic(deviceIdsOwnByGroup, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); -// int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); -// return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); -// } catch (ApplicationManagementDAOException e) { -// String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier(); -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (GroupManagementException e) { -// String msg = "Error encountered while getting device subscription for group: " + subscriptionInfo.getIdentifier(); -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - - private static class GroupBasedSubscriptionManagementHelperServiceImplHolder { - private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new GroupBasedSubscriptionManagementHelperServiceImpl(); - } -} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java deleted file mode 100644 index 49671aff8c..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/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java +++ /dev/null @@ -1,239 +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.impl; - -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.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.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.impl.SubscriptionManagerImpl; -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.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.List; -import java.util.Objects; -import java.util.stream.Collectors; - -public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { - private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class); - private SubscriptionManagerImpl subscriptionManager; - - private RoleBasedSubscriptionManagementHelperServiceImpl() { - this.subscriptionManager = new SubscriptionManagerImpl(); - } - - public static RoleBasedSubscriptionManagementHelperServiceImpl getInstance() { - return RoleBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; - } - - @Override - public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException { - final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); - List deviceSubscriptionDTOS; - int deviceCount = 0; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - try { - ConnectionManagerUtil.openDBConnection(); - List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); - - ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + - subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationDTO == null) { - String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); - - if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - - List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). - map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - - for (Integer deviceId : deviceIdsOfSubscription) { - deviceIdsOwnByRole.remove(deviceId); - } - - List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - } else { - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - } - deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); - - List deviceSubscriptions = SubscriptionManagementHelperUtil. - getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); - - } catch (UserStoreException e) { - String msg = "Error encountered while getting the user management store for tenant id " + tenantId; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementException e) { - String msg = "Error encountered while getting device details"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationManagementDAOException | DBConnectionException e) { - String msg = "Error encountered while connecting to the database"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; - } - - @Override - public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - return subscriptionManager.getSubscriptionStatistics(subscriptionInfo); - } - - -// @Override -// public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) -// throws ApplicationManagementException { -// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); -// final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// try { -// ConnectionManagerUtil.openDBConnection(); -// ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. -// getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + -// subscriptionInfo.getApplicationUUID(); -// log.error(msg); -// throw new NotFoundException(msg); -// } -// List subscriptionEntities = subscriptionDAO. -// getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); -// int subscriptionCount = isUnsubscribe ? subscriptionDAO.getRoleUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : -// subscriptionDAO.getRoleSubscriptionCount(applicationReleaseDTO.getId(), tenantId); -// return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); -// } catch (DBConnectionException | ApplicationManagementDAOException e) { -// String msg = "Error encountered while connecting to the database"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - -// @Override -// public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { -// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// try { -// ConnectionManagerUtil.openDBConnection(); -// ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. -// getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + -// subscriptionInfo.getApplicationUUID(); -// log.error(msg); -// throw new NotFoundException(msg); -// } -// List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); -// SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. -// getSubscriptionStatistic(deviceIdsOwnByRole, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); -// int allDeviceCount = deviceIdsOwnByRole.size(); -// return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); -// } catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) { -// String msg = "Error encountered while getting subscription statistics for role: " + subscriptionInfo.getIdentifier(); -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - - @SuppressWarnings("unchecked") - private List getDeviceIdsOwnByRole(String roleName, int tenantId) throws UserStoreException, DeviceManagementException { - UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). - getTenantUserRealm(tenantId).getUserStoreManager(); - String[] usersWithRole = - userStoreManager.getUserListOfRole(roleName); - List deviceListOwnByRole = new ArrayList<>(); - for (String user : usersWithRole) { - PaginationRequest paginationRequest = new PaginationRequest(-1, -1); - paginationRequest.setOwner(user); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); - PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getAllDevicesIdList(paginationRequest); - if (ownDeviceIds.getData() != null) { - deviceListOwnByRole.addAll((List) ownDeviceIds.getData()); - } - } - return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); - } - - private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { - private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new RoleBasedSubscriptionManagementHelperServiceImpl(); - } -} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java deleted file mode 100644 index 2b36a04236..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/impl/UserBasedSubscriptionManagementHelperServiceImpl.java +++ /dev/null @@ -1,223 +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.impl; - -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.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.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.impl.SubscriptionManagerImpl; -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.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 java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { - private static final Log log = LogFactory.getLog(UserBasedSubscriptionManagementHelperServiceImpl.class); - private SubscriptionManagerImpl subscriptionManager; - private UserBasedSubscriptionManagementHelperServiceImpl() { - this.subscriptionManager = new SubscriptionManagerImpl(); - } - - public static UserBasedSubscriptionManagementHelperServiceImpl getInstance() { - return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; - } - - @Override - public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException { - final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); - List deviceSubscriptionDTOS; - int deviceCount = 0; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - try { - ConnectionManagerUtil.openDBConnection(); - List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); - - ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + - subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationDTO == null) { - String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); - - if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, null, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - - List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). - map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - - for (Integer deviceId : deviceIdsOfSubscription) { - deviceIdsOwnByUser.remove(deviceId); - } - List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - - } else { - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - } - deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); - - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); - } catch (DeviceManagementException e) { - String msg = "Error encountered while getting device details"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationManagementDAOException | DBConnectionException e) { - String msg = "Error encountered while connecting to the database"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; - } - - @Override - public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - return subscriptionManager.getSubscriptionStatistics(subscriptionInfo); - } - - -// @Override -// public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) -// throws ApplicationManagementException { -// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); -// final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// try { -// ConnectionManagerUtil.openDBConnection(); -// ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. -// getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + -// subscriptionInfo.getApplicationUUID(); -// log.error(msg); -// throw new NotFoundException(msg); -// } -// List subscriptionEntities = subscriptionDAO. -// getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); -// int subscriptionCount = isUnsubscribe ? subscriptionDAO.getUserUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : -// subscriptionDAO.getUserSubscriptionCount(applicationReleaseDTO.getId(), tenantId); -// return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); -// } catch (DBConnectionException | ApplicationManagementDAOException e) { -// String msg = "Error encountered while connecting to the database"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - -// @Override -// public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { -// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// try { -// ConnectionManagerUtil.openDBConnection(); -// ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. -// getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + -// subscriptionInfo.getApplicationUUID(); -// log.error(msg); -// throw new NotFoundException(msg); -// } -// List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); -// SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. -// getSubscriptionStatistic(deviceIdsOwnByUser, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); -// int allDeviceCount = deviceIdsOwnByUser.size(); -// return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); -// } catch (DeviceManagementException | ApplicationManagementDAOException e) { -// String msg = "Error encountered while getting subscription statistics for user: " + subscriptionInfo.getIdentifier(); -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - - @SuppressWarnings("unchecked") - private List getDeviceIdsOwnByUser(String username) throws DeviceManagementException { - List deviceListOwnByUser = new ArrayList<>(); - PaginationRequest paginationRequest = new PaginationRequest(-1, -1); - paginationRequest.setOwner(username); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); - PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getAllDevicesIdList(paginationRequest); - if (ownDeviceIds.getData() != null) { - deviceListOwnByUser.addAll((List) ownDeviceIds.getData()); - } - return deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList()); - } - - private static class UserBasedSubscriptionManagementHelperServiceImplHolder { - private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new UserBasedSubscriptionManagementHelperServiceImpl(); - } -} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.java deleted file mode 100644 index f869e282dd..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/service/SubscriptionManagementHelperService.java +++ /dev/null @@ -1,44 +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.service; - -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.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; - -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; -}