From 4b4ed5793a1934a8f3389262d546386f853c85ac Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Fri, 15 Dec 2023 10:01:52 +0530 Subject: [PATCH 1/2] Add method for get installed applications using device ID --- .../device/mgt/core/dao/ApplicationDAO.java | 10 ++++ .../mgt/core/dao/impl/ApplicationDAOImpl.java | 46 +++++++++++++++++++ .../DeviceManagementProviderService.java | 11 ++++- .../DeviceManagementProviderServiceImpl.java | 38 ++++++++++++++- 4 files changed, 102 insertions(+), 3 deletions(-) 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/ApplicationDAO.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/ApplicationDAO.java index 0c1899e9af..826c5a07e7 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/ApplicationDAO.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/ApplicationDAO.java @@ -114,4 +114,14 @@ public interface ApplicationDAO { * @throws DeviceManagementDAOException */ String getIconPath(String applicationIdentifier) throws DeviceManagementDAOException; + + /** + * This method is used to get the installed application list of a specific device + * @param deviceId ID of the device + * @param enrolmentId Enrolment ID of the device + * @param tenantId tenant ID + * @throws DeviceManagementDAOException If any database error occurred + */ + List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, 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/ApplicationDAOImpl.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/ApplicationDAOImpl.java index 1f48c3dc92..fb3ad85549 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/ApplicationDAOImpl.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/ApplicationDAOImpl.java @@ -575,4 +575,50 @@ public class ApplicationDAOImpl implements ApplicationDAO { } return applicationList; } + + public List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException { + Connection conn; + List applicationList = new ArrayList<>(); + Application application; + String sql = "SELECT " + + "ID, " + + "NAME, " + + "APP_IDENTIFIER, " + + "PLATFORM, " + + "CATEGORY, " + + "VERSION, " + + "TYPE, " + + "LOCATION_URL, " + + "IMAGE_URL, " + + "APP_PROPERTIES, " + + "MEMORY_USAGE, " + + "IS_ACTIVE, " + + "TENANT_ID " + + "FROM DM_APPLICATION " + + "WHERE DEVICE_ID = ? AND " + + "ENROLMENT_ID = ? AND " + + "TENANT_ID = ? "; + try { + conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + application = loadApplication(rs); + applicationList.add(application); + } + } + } + + } catch (SQLException e) { + String msg = "SQL Error occurred while retrieving the list of Applications " + + "installed in device id '" + deviceId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + return applicationList; + } } 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 d3b12e9816..2e35b5a254 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 @@ -1030,10 +1030,9 @@ public interface DeviceManagementProviderService { * @param iconPath Icon path of the application * @param packageName Package name of the application * @param version Version of the application - * @param tenantId Tenant ID of the application created user * @throws DeviceManagementException if any service level or DAO level error occurs */ - void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) + void saveApplicationIcon(String iconPath, String packageName, String version) throws DeviceManagementException; /** @@ -1062,4 +1061,12 @@ public interface DeviceManagementProviderService { */ List getInstalledApplicationsOnDevice(Device device, int offset, int limit) throws DeviceManagementException; + + /** + * This method is for getting the installed application list of a device + * @param device {@link Device} + * @return list of applications {@link Application} + * @throws DeviceManagementException if any service level or DAO level error occurs + */ + List getInstalledApplicationsOnDevice(Device device) 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 60750b454f..aea7c5873a 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 @@ -4953,9 +4953,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementException{ + public void saveApplicationIcon(String iconPath, String packageName, String version) throws DeviceManagementException{ + int tenantId = 0; try{ DeviceManagementDAOFactory.beginTransaction(); + tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); if(applicationDAO.getApplicationPackageCount(packageName) == 0){ applicationDAO.saveApplicationIcon(iconPath, packageName, version, tenantId); } @@ -5077,4 +5079,38 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return newApplicationList; } + + public List getInstalledApplicationsOnDevice(Device device) throws DeviceManagementException { + List applications; + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + applications = applicationDAO.getInstalledApplicationListOnDevice(device.getId(), + device.getEnrolmentInfo().getId(), tenantId); + if (applications == null) { + String msg = "Couldn't found applications for device identifier '" + device.getId() + "'"; + log.error(msg); + throw new DeviceManagementException(msg); + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving the application list of android device, " + + "which carries the id '" + device.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + List newApplicationList; + newApplicationList = this.getInstalledAppIconInfo(applications); + if (newApplicationList == null) { + String msg = "Error occurred while getting app icon info for device identifier '" + device.getId() + "'"; + log.error(msg); + throw new DeviceManagementException(msg); + } + return newApplicationList; + } } From 946431905f7bcd80bccfd5499275c4bbb64066a0 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Tue, 19 Dec 2023 11:31:48 +0530 Subject: [PATCH 2/2] Remove Tenant id from saveApplicationIcon method --- .../core/application/mgt/core/impl/ApplicationManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java index 22f5688b07..c897fcf65c 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/ApplicationManagerImpl.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/ApplicationManagerImpl.java @@ -1333,7 +1333,7 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO, tenantId); DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(iconPath, - String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion(), tenantId); + String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion()); } catch (ApplicationManagementException e) { String msg = "Error occurred while creating iconPath. Application package name : " + applicationReleaseDTO.getPackageName(); log.error(msg, e);