From ab87a8199ab4846d18aca401a77fc1f7ad4d8f22 Mon Sep 17 00:00:00 2001 From: ruwin Date: Wed, 30 Oct 2024 12:33:17 +0530 Subject: [PATCH] Add capability to filter apps by system installed apps or user installed apps Implemented a feature to filter applications based on their type, allowing users to choose between system apps and user-installed apps. This enhancement improves the device app management experience by providing more control over app visibility. Users can now easily filter and view applications based on their installation type. --- .../mgt/common/app/mgt/Application.java | 6 +- .../device/mgt/core/dao/ApplicationDAO.java | 2 +- .../mgt/core/dao/impl/ApplicationDAOImpl.java | 107 ++++++++++-------- .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 4 +- .../src/test/resources/sql/h2.sql | 1 + .../src/main/resources/dbscripts/cdm/h2.sql | 1 + .../main/resources/dbscripts/cdm/mssql.sql | 1 + .../main/resources/dbscripts/cdm/mysql.sql | 1 + .../main/resources/dbscripts/cdm/oracle.sql | 1 + .../resources/dbscripts/cdm/postgresql.sql | 1 + 11 files changed, 71 insertions(+), 56 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/Application.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/Application.java index 24532f8e36..6b983e89ab 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/Application.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/app/mgt/Application.java @@ -55,7 +55,7 @@ public class Application implements Serializable { @ApiModelProperty(name = "isActive", value = "Is the application actively running", required = true) private boolean isActive; @ApiModelProperty(name = "isSystemApp", value = "Is the application a system app", required = true) - private boolean isSystemApp; + private int isSystemApp; @ApiModelProperty(name = "hourlyUsage", value = "App hourly usage") private long hourlyUsage; @ApiModelProperty(name = "dailyUsage", value = "App daily usage") @@ -188,11 +188,11 @@ public class Application implements Serializable { isActive = active; } - public boolean isSystemApp() { + public int isSystemApp() { return isSystemApp; } - public void setSystemApp(boolean system) { + public void setSystemApp(int system) { isSystemApp = system; } 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 30fb957122..c074ba098d 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 @@ -103,7 +103,7 @@ public interface ApplicationDAO { * @param tenantId tenant ID * @throws DeviceManagementDAOException If any database error occurred */ - List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId) + List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId, int isSystemApp) 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 0115f9c07c..884bfc3237 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 @@ -73,7 +73,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { stmt.setBoolean(11, application.isActive()); stmt.setInt(12, deviceId); stmt.setInt(13, enrolmentId); - stmt.setBoolean(14, application.isSystemApp()); + stmt.setInt(14, application.isSystemApp()); stmt.addBatch(); } stmt.executeBatch(); @@ -105,7 +105,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { stmt.setString(7, application.getImageUrl()); stmt.setInt(8, application.getMemoryUsage()); stmt.setBoolean(9, application.isActive()); - stmt.setBoolean(10, application.isSystemApp()); + stmt.setInt(10, application.isSystemApp()); stmt.setInt(11, application.getId()); stmt.addBatch(); } @@ -213,7 +213,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { try { conn = this.getConnection(); stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + - "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID " + + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, IS_SYSTEM_APP, TENANT_ID " + "FROM DM_APPLICATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND APP_IDENTIFIER = ? AND " + "VERSION = ? AND TENANT_ID = ?"); stmt.setInt(1, deviceId); @@ -224,7 +224,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { rs = stmt.executeQuery(); if (rs.next()) { - application = this.loadApplication(rs); + application = this.loadApplicationDetails(rs); } return application; } catch (SQLException e) { @@ -239,6 +239,45 @@ public class ApplicationDAOImpl implements ApplicationDAO { return DeviceManagementDAOFactory.getConnection(); } + private Application loadApplicationDetails(ResultSet rs) throws DeviceManagementDAOException { + ByteArrayInputStream bais; + ObjectInputStream ois; + Properties properties; + + Application application = new Application(); + try { + application.setId(rs.getInt("ID")); + application.setName(rs.getString("NAME")); + application.setType(rs.getString("TYPE")); + + if (rs.getBytes("APP_PROPERTIES") != null) { + byte[] appProperties = rs.getBytes("APP_PROPERTIES"); + bais = new ByteArrayInputStream(appProperties); + + ois = new ObjectInputStream(bais); + properties = (Properties) ois.readObject(); + application.setAppProperties(properties); + } + application.setCategory(rs.getString("CATEGORY")); + application.setImageUrl(rs.getString("IMAGE_URL")); + application.setLocationUrl(rs.getString("LOCATION_URL")); + application.setPlatform(rs.getString("PLATFORM")); + application.setVersion(rs.getString("VERSION")); + application.setMemoryUsage(rs.getInt("MEMORY_USAGE")); + application.setActive(rs.getBoolean("IS_ACTIVE")); + application.setSystemApp(rs.getInt("IS_SYSTEM_APP")); + application.setApplicationIdentifier(rs.getString("APP_IDENTIFIER")); + + } catch (IOException e) { + throw new DeviceManagementDAOException("IO error occurred fetch at app properties", e); + } catch (ClassNotFoundException e) { + throw new DeviceManagementDAOException("Class not found error occurred fetch at app properties", e); + } catch (SQLException e) { + throw new DeviceManagementDAOException("SQL error occurred fetch at application", e); + } + return application; + } + @Override public List getInstalledApplications(int deviceId, int enrolmentId, int tenantId) throws DeviceManagementDAOException { @@ -250,7 +289,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { try { conn = this.getConnection(); stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + - "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM DM_APPLICATION " + + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, IS_SYSTEM_APP, TENANT_ID FROM DM_APPLICATION " + "WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?"); stmt.setInt(1, deviceId); @@ -259,7 +298,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { rs = stmt.executeQuery(); while (rs.next()) { - application = loadApplication(rs); + application = loadApplicationDetails(rs); applications.add(application); } } catch (SQLException e) { @@ -424,45 +463,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { return application; } - private Application loadApplicationDetails(ResultSet rs) throws DeviceManagementDAOException { - ByteArrayInputStream bais; - ObjectInputStream ois; - Properties properties; - - Application application = new Application(); - try { - application.setId(rs.getInt("ID")); - application.setName(rs.getString("NAME")); - application.setType(rs.getString("TYPE")); - if (rs.getBytes("APP_PROPERTIES") != null) { - byte[] appProperties = rs.getBytes("APP_PROPERTIES"); - bais = new ByteArrayInputStream(appProperties); - - ois = new ObjectInputStream(bais); - properties = (Properties) ois.readObject(); - application.setAppProperties(properties); - } - application.setCategory(rs.getString("CATEGORY")); - application.setImageUrl(rs.getString("IMAGE_URL")); - application.setLocationUrl(rs.getString("LOCATION_URL")); - application.setPlatform(rs.getString("PLATFORM")); - application.setVersion(rs.getString("VERSION")); - application.setMemoryUsage(rs.getInt("MEMORY_USAGE")); - application.setActive(rs.getBoolean("IS_ACTIVE")); - application.setSystemApp(rs.getBoolean("IS_SYSTEM_APP")); - application.setApplicationIdentifier(rs.getString("APP_IDENTIFIER")); - - } catch (IOException e) { - throw new DeviceManagementDAOException("IO error occurred fetch at app properties", e); - } catch (ClassNotFoundException e) { - throw new DeviceManagementDAOException("Class not found error occurred fetch at app properties", e); - } catch (SQLException e) { - throw new DeviceManagementDAOException("SQL error occurred fetch at application", e); - } - - return application; - } @Override public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) @@ -589,7 +590,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { } @Override - public List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId) + public List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId, int isSystemApp) throws DeviceManagementDAOException { Connection conn; List applicationList = new ArrayList<>(); @@ -613,16 +614,23 @@ public class ApplicationDAOImpl implements ApplicationDAO { "WHERE DEVICE_ID = ? AND " + "ENROLMENT_ID = ? AND " + "TENANT_ID = ? " + + (isSystemApp != 0 ? "AND IS_SYSTEM_APP = ? " : "") + "LIMIT ? " + - "OFFSET ?"; + "OFFSET ? "; try { conn = this.getConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, deviceId); stmt.setInt(2, enrolmentId); stmt.setInt(3, tenantId); - stmt.setInt(4, limit); - stmt.setInt(5, offset); + int paramIndex = 4; + if (isSystemApp != 0) { + stmt.setInt(paramIndex, isSystemApp); + paramIndex++; + } + stmt.setInt(paramIndex, limit); + paramIndex++; + stmt.setInt(paramIndex, offset); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { application = loadApplicationDetails(rs); @@ -633,13 +641,14 @@ public class ApplicationDAOImpl implements ApplicationDAO { } catch (SQLException e) { String msg = "SQL Error occurred while retrieving the list of Applications " + - "installed in device id '" + deviceId; + "installed on device id '" + deviceId + "'"; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } return applicationList; } + public List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int tenantId) throws DeviceManagementDAOException { Connection conn; 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 ee8c5b5871..3ecceb5824 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 @@ -1055,7 +1055,7 @@ public interface DeviceManagementProviderService { * @return list of applications {@link Application} * @throws DeviceManagementException if any service level or DAO level error occurs */ - List getInstalledApplicationsOnDevice(Device device, int offset, int limit) + List getInstalledApplicationsOnDevice(Device device, int offset, int limit, int isSystemApp) throws DeviceManagementException; /** * This method is for getting the installed application list of a device 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 df57af8444..9824efb5e9 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 @@ -5157,13 +5157,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getInstalledApplicationsOnDevice(Device device, int offset, int limit) throws DeviceManagementException { + public List getInstalledApplicationsOnDevice(Device device, int offset, int limit, int isSystemApp) throws DeviceManagementException { List applications; try { DeviceManagementDAOFactory.openConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); applications = applicationDAO.getInstalledApplicationListOnDevice(device.getId(), - device.getEnrolmentInfo().getId(), offset, limit, tenantId); + device.getEnrolmentInfo().getId(), offset, limit, tenantId, isSystemApp); if (applications == null) { String msg = "Couldn't found applications for device identifier '" + device.getId() + "'"; log.error(msg); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql index 323f52762a..14faa421b5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql @@ -439,6 +439,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( APP_PROPERTIES BLOB NULL, MEMORY_USAGE INTEGER NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, + IS_SYSTEM_APP TINYINT NOT NULL DEFAULT 0, DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 6ba0077069..33f1d94fe8 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -405,6 +405,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( APP_PROPERTIES BLOB NULL, MEMORY_USAGE INTEGER NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, + IS_SYSTEM_APP TINYINT NOT NULL DEFAULT 0, DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 272066798d..c9053f4633 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -462,6 +462,7 @@ CREATE TABLE DM_APPLICATION ( APP_PROPERTIES VARBINARY(MAX) NULL, MEMORY_USAGE INTEGER NULL, IS_ACTIVE BIT NOT NULL DEFAULT 0, + IS_SYSTEM_APP TINYINT NOT NULL DEFAULT 0, DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 773fe53b34..2ae41ebd9c 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -462,6 +462,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( APP_PROPERTIES BLOB NULL, MEMORY_USAGE INTEGER(10) NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, + IS_SYSTEM_APP TINYINT(1) NOT NULL DEFAULT 0, DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 1b1071c290..03bf1f42a5 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -728,6 +728,7 @@ CREATE TABLE DM_APPLICATION ( APP_PROPERTIES BLOB NULL, MEMORY_USAGE NUMBER(10) NULL, IS_ACTIVE NUMBER(10) DEFAULT 0 NOT NULL, + IS_SYSTEM_APP NUMBER(1) DEFAULT 0 NOT NULL, DEVICE_ID NUMBER(10) NOT NULL, ENROLMENT_ID NUMBER(10) NOT NULL, TENANT_ID NUMBER(10) NOT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 31591893e1..392a9fafc3 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -486,6 +486,7 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( APP_PROPERTIES BYTEA NULL, MEMORY_USAGE INTEGER NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, + IS_SYSTEM_APP SMALLINT NOT NULL DEFAULT 0, DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL,