From 010ccde84e07d920cc7c8b7ec63e942f9e81b6fc Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Tue, 5 Nov 2024 07:55:32 +0530 Subject: [PATCH] Add api impl for retrieving app installable devices --- .../common/services/SubscriptionManager.java | 26 ++++ .../core/impl/SubscriptionManagerImpl.java | 125 ++++++++++++++++++ 2 files changed, 151 insertions(+) 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 fd16e70872..cde89e3eae 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 @@ -180,6 +180,16 @@ public interface SubscriptionManager { */ PaginationResult getAppInstalledDevices(PaginationRequest request, String appUUID) throws ApplicationManagementException; + /*** + * This method used to get the app id ,device ids and pass them to DM service method. + * + * @param appUUID UUID of the application release. + * @param request paginated request object. + * @return deviceDetails - device details for given application release. + * @throws {@link ApplicationManagementException} Exception of the application management + */ + PaginationResult getAppInstallableDevices(PaginationRequest request, String appUUID) throws ApplicationManagementException; + /*** * This method used to get category details. * @@ -196,6 +206,22 @@ public interface SubscriptionManager { String subType, Boolean uninstalled, String searchName) throws ApplicationManagementException; + /*** + * This method used to get category details. + * + * @param appUUID UUID of the application release. + * @param subType subscription type of the application. + * @param offsetValue offset value for get paginated request. + * @param limitValue limit value for get paginated request. + * @param uninstalled a Boolean flag indicating the filter criteria for retrieve subscription data + * @param searchName an optional search term to filter the results by name. If null or empty, no filtering by name is applied. + * @return {@link PaginationResult} pagination result of the category details. + * @throws {@link ApplicationManagementException} Exception of the application management + */ + PaginationResult getAppInstallableSubscribers(int offsetValue, int limitValue, String appUUID, + String subType, Boolean uninstalled, String searchName) + throws ApplicationManagementException; + /** * This method is responsible to provide application subscription data for given application release 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 92f0734c9f..ea2fd03734 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 @@ -137,6 +137,7 @@ import java.util.Properties; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; +import javax.ws.rs.core.Response; /** * This is the default implementation for the Subscription Manager. @@ -1497,6 +1498,75 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + @Override + public PaginationResult getAppInstallableDevices(PaginationRequest request, String appUUID) + throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil + .getDeviceManagementProviderService(); + + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId); + int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); + + List deviceSubscriptionDTOS = subscriptionDAO + .getDeviceSubscriptions(applicationReleaseId, tenantId, null, null); + if (deviceSubscriptionDTOS.isEmpty()) { + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(new ArrayList<>()); + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + return paginationResult; + } + List deviceIdList = new ArrayList<>(); + deviceSubscriptionDTOS.forEach(deviceSubscriptionDTO -> { + if ((!deviceSubscriptionDTO.isUnsubscribed() && !Operation.Status.PENDING.toString() + .equalsIgnoreCase(deviceSubscriptionDTO.getStatus())) || (deviceSubscriptionDTO.isUnsubscribed() + && !Operation.Status.COMPLETED.toString() + .equalsIgnoreCase(deviceSubscriptionDTO.getStatus()))) { + deviceIdList.add(deviceSubscriptionDTO.getDeviceId()); + } + }); + + if (deviceIdList.isEmpty()) { + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(deviceIdList); + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + return paginationResult; + } + //pass the device id list to device manager service method + try { + PaginationResult deviceDetails = deviceManagementProviderService.getAppSubscribedDevices + (request, deviceIdList); + + if (deviceDetails == null) { + String msg = "Couldn't found an subscribed devices details for device ids: " + deviceIdList; + log.error(msg); + throw new NotFoundException(msg); + } + return deviceDetails; + + } catch (DeviceManagementException e) { + String msg = "service error occurred while getting data from the service"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } catch (ApplicationManagementDAOException e) { + String msg = + "Error occurred when get application release data for application" + " release UUID: " + appUUID; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "DB Connection error occurred while getting device details that " + "given application id"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + @Override public PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, String subType, Boolean uninstalled, String searchName) @@ -1544,6 +1614,61 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + @Override + public PaginationResult getAppInstallableSubscribers(int offsetValue, int limitValue, String appUUID, String subType, + Boolean uninstalled, String searchName) + throws ApplicationManagementException, UserStoreException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + PaginationResult paginationResult = new PaginationResult(); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId); + int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); + + List subscriptionList = new ArrayList<>(); + List subscribedList = new ArrayList<>(); + List deviceList = new ArrayList<>();; + int count = 0; + + if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { + subscribedList = subscriptionDAO + .getAppSubscribedUsers(offsetValue, limitValue, applicationReleaseId, tenantId, uninstalled, searchName); + count = subscriptionDAO.getSubscribedUserCount(applicationReleaseId, tenantId, uninstalled, searchName); + for (String user : subscribedList) { + deviceList = deviceManagementProviderService.getDevicesOfUser(user, false) + } + } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { + subscribedList = subscriptionDAO + .getAppSubscribedRoles(offsetValue, limitValue, applicationReleaseId, tenantId, uninstalled, searchName); + count = subscriptionDAO.getSubscribedRoleCount(applicationReleaseId, tenantId, uninstalled, searchName); + for (String role : subscribedList) { + deviceList = deviceManagementProviderService.getAllDevicesOfRole(role, false) + } + } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { + subscribedList = subscriptionDAO + .getAppSubscribedGroups(offsetValue, limitValue, applicationReleaseId, tenantId, uninstalled, searchName); + count = subscriptionDAO.getSubscribedGroupCount(applicationReleaseId, tenantId, uninstalled, searchName); + } + + paginationResult.setData(subscriptionList); + paginationResult.setRecordsFiltered(count); + paginationResult.setRecordsTotal(count); + return paginationResult; + } catch (ApplicationManagementDAOException e) { + String msg = + "Error occurred when get application release data for application" + " release UUID: " + appUUID; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "DB Connection error occurred while getting category details that " + "given application id"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + @Override public CategorizedSubscriptionResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, String action, String installedVersion) throws ApplicationManagementException {