From c06fd22ecd63a9b9beb355c17125dbe7fa81f805 Mon Sep 17 00:00:00 2001 From: Sanjana Rajapakshe Date: Thu, 1 Jun 2023 16:11:35 +0000 Subject: [PATCH] Add original app icons into single device app list and application restriction policy app list Co-authored-by: Sanjana Rajapakshe Co-committed-by: Sanjana Rajapakshe --- .../common/services/ApplicationManager.java | 8 + .../mgt/core/impl/ApplicationManagerImpl.java | 62 ++++++ .../application/mgt/core/util/APIUtil.java | 14 ++ ...ApplicationManagementPublisherAPIImpl.java | 4 + .../pom.xml | 12 ++ .../device/mgt/core/dao/ApplicationDAO.java | 51 +++++ .../mgt/core/dao/impl/ApplicationDAOImpl.java | 176 ++++++++++++++++++ .../DeviceManagementProviderService.java | 39 ++++ .../DeviceManagementProviderServiceImpl.java | 126 +++++++++++++ .../src/main/resources/dbscripts/cdm/h2.sql | 10 + .../main/resources/dbscripts/cdm/mssql.sql | 11 ++ .../main/resources/dbscripts/cdm/mysql.sql | 10 + .../main/resources/dbscripts/cdm/oracle.sql | 22 +++ .../resources/dbscripts/cdm/postgresql.sql | 13 ++ 14 files changed, 558 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/ApplicationManager.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/ApplicationManager.java index ff4ca7710a..88a6912a7d 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/ApplicationManager.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/ApplicationManager.java @@ -525,4 +525,12 @@ public interface ApplicationManager { String getPlistArtifact(String uuid) throws ApplicationManagementException; List getReleaseByPackageNames(List packageIds) throws ApplicationManagementException; + + /** + * @param applicationRelease {@link ApplicationRelease} + * @param oldPackageName Old package name of the application + * @throws ApplicationManagementException Application management exception + */ + void updateAppIconInfo(ApplicationRelease applicationRelease, String oldPackageName) + 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/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 736613e786..b7fd8f23b5 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 @@ -1287,6 +1287,9 @@ public class ApplicationManagerImpl implements ApplicationManager { } applicationDTO.setId(appId); applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities); + if (applicationDTO.getType().equals("ENTERPRISE") || applicationDTO.getType().equals("PUBLIC") ) { + persistAppIconInfo(applicationReleaseDTO); + } return APIUtil.appDtoToAppResponse(applicationDTO); } } catch (LifeCycleManagementDAOException e) { @@ -1313,6 +1316,30 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + /** + * Persist application icon information when creating an application + * + * @param applicationReleaseDTO {@link ApplicationReleaseDTO} + * @throws ApplicationManagementException if error occurred while persisting application icon information + */ + private void persistAppIconInfo(ApplicationReleaseDTO applicationReleaseDTO) + throws ApplicationManagementException { + try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO, tenantId); + DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(iconPath, + String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion(), tenantId); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while creating iconPath. Application package name : " + applicationReleaseDTO.getPackageName(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + String msg = "Error occurred while saving application icon info. Application package name : " + applicationReleaseDTO.getPackageName(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } + @Override public ApplicationRelease createRelease(ApplicationDTO applicationDTO, ApplicationReleaseDTO applicationReleaseDTO, ApplicationType type, boolean isPublished) @@ -1918,6 +1945,13 @@ public class ApplicationManagerImpl implements ApplicationManager { break; } } + try { + deleteAppIconInfo(applicationDTO); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while deleting application icon info. Application package name: " + applicationDTO.getPackageName(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } } @Override @@ -4022,4 +4056,32 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.closeDBConnection(); } } + + @Override + public void updateAppIconInfo(ApplicationRelease applicationRelease, String oldPackageName) throws ApplicationManagementException { + try { + DataHolder.getInstance().getDeviceManagementService().updateApplicationIcon(applicationRelease.getIconPath(), + oldPackageName, applicationRelease.getPackageName(), applicationRelease.getVersion()); + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating application icon info. Application package name: " + oldPackageName; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } + + /** + * Delete application icon information when deleting an application + * + * @param applicationDTO {@link ApplicationDTO} + * @throws ApplicationManagementException if error occurred while deleting application icon information + */ + private void deleteAppIconInfo(ApplicationDTO applicationDTO) throws ApplicationManagementException { + try { + DataHolder.getInstance().getDeviceManagementService().deleteApplicationIcon(applicationDTO.getPackageName()); + } catch (DeviceManagementException e) { + String msg = "Error occurred while deleting application icon info. Application package name: " + applicationDTO.getPackageName(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/APIUtil.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/APIUtil.java index 95f402e9cf..407b4ebcac 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/APIUtil.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/APIUtil.java @@ -510,4 +510,18 @@ public class APIUtil { return mdmConfig.getArtifactDownloadProtocol() + "://" + host + ":" + port + artifactDownloadEndpoint + Constants.FORWARD_SLASH; } + + /** + * To create the application icon path. + * + * @param applicationReleaseDTO {@link ApplicationReleaseDTO} + * @param tenantId tenant ID + * @return iconPath constructed icon path. + */ + public static String createAppIconPath(ApplicationReleaseDTO applicationReleaseDTO, int tenantId) throws ApplicationManagementException { + String basePath = getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + applicationReleaseDTO + .getAppHashValue() + Constants.FORWARD_SLASH; + String iconPath = basePath + Constants.ICON_ARTIFACT + Constants.FORWARD_SLASH + applicationReleaseDTO.getIconName(); + return iconPath; + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.publisher.api/src/main/java/io/entgra/device/mgt/core/application/mgt/publisher/api/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.publisher.api/src/main/java/io/entgra/device/mgt/core/application/mgt/publisher/api/impl/ApplicationManagementPublisherAPIImpl.java index 583d4de17c..6b3f9fc06d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.publisher.api/src/main/java/io/entgra/device/mgt/core/application/mgt/publisher/api/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.publisher.api/src/main/java/io/entgra/device/mgt/core/application/mgt/publisher/api/impl/ApplicationManagementPublisherAPIImpl.java @@ -532,6 +532,8 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } + String oldPackageName = applicationManager.getApplicationByUuid(applicationUUID).getPackageName(); + applicationManager.updateAppIconInfo(applicationRelease, oldPackageName); return Response.status(Response.Status.OK).entity(applicationRelease).build(); } catch (BadRequestException e) { String msg = e.getMessage(); @@ -572,6 +574,8 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } + String oldPackageName = applicationManager.getApplicationByUuid(applicationUUID).getPackageName(); + applicationManager.updateAppIconInfo(applicationRelease, oldPackageName); return Response.status(Response.Status.OK).entity(applicationRelease).build(); } catch (BadRequestException e) { String msg = e.getMessage(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml index 200fca7478..23f16f3cda 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml @@ -362,6 +362,18 @@ io.entgra.device.mgt.core io.entgra.device.mgt.core.notification.logger + + org.wso2.orbit.javax.xml.bind + jaxb-api + 2.3.1.wso2v1 + compile + + + org.wso2.orbit.javax.xml.bind + jaxb-api + 2.3.1.wso2v1 + compile + 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 ef3d07ee7c..0c1899e9af 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 @@ -63,4 +63,55 @@ public interface ApplicationDAO { * @throws DeviceManagementDAOException If any database error occured */ List getAppVersions(int tenantId, String packageName) throws DeviceManagementDAOException; + + /** + * This method is used to save application icon information. + * @param iconPath Icon path of the application + * @param packageName Package name of the application + * @param version version of the application + * @throws DeviceManagementDAOException If any database error occurred + */ + void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementDAOException; + + /** + * This method is used to check the package existence. + * @param packageName Package name of the application + * @throws DeviceManagementDAOException If any database error occurred + */ + int getApplicationPackageCount(String packageName) throws DeviceManagementDAOException; + + /** + * This method is used to update application icon information. + * @param iconPath Icon path of the application + * @param oldPackageName Old package name of the application + * @param newPackageName New package name of the application + * @param version Version of the application + * @throws DeviceManagementDAOException If any database error occurred + */ + void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version) throws DeviceManagementDAOException; + + /** + * This method is used to delete application icon information. + * @param packageName Package name of the application + * @throws DeviceManagementDAOException If any database error occurred + */ + void deleteApplicationIcon(String packageName) 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 offset, int limit, int tenantId) + throws DeviceManagementDAOException; + + /** + * This method is used to retrieve the icon info of an installed app in device. + * @param applicationIdentifier application identifier. + * @return returns the application icon path. + * @throws DeviceManagementDAOException + */ + String getIconPath(String applicationIdentifier) 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 3b4bb1642e..8aa49c92e0 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 @@ -34,7 +34,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Properties; @@ -394,4 +396,178 @@ public class ApplicationDAOImpl implements ApplicationDAO { return application; } + @Override + public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) + throws DeviceManagementDAOException{ + Connection conn; + String sql = "INSERT INTO DM_APP_ICONS " + + "(ICON_PATH, " + + "PACKAGE_NAME, " + + "VERSION, " + + "CREATED_TIMESTAMP, " + + "TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?)"; + try { + conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1,iconPath); + stmt.setString(2,packageName); + stmt.setString(3,version); + stmt.setTimestamp(4, new Timestamp(new Date().getTime())); + stmt.setInt(5, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while saving application icon details"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public int getApplicationPackageCount(String packageName) throws DeviceManagementDAOException{ + Connection conn; + String sql = "SELECT " + + "COUNT(*) AS APP_PACKAGE_COUNT " + + "FROM DM_APP_ICONS " + + "WHERE PACKAGE_NAME = ?"; + try { + conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, packageName); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("APP_PACKAGE_COUNT"); + } + return 0; + } + } + } catch (SQLException e) { + String msg = "Error occurred while getting application icon details"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version) + throws DeviceManagementDAOException{ + Connection conn; + String sql = "UPDATE DM_APP_ICONS " + + "SET " + + "ICON_PATH= ?, " + + "PACKAGE_NAME = ?, " + + "VERSION = ? " + + "WHERE PACKAGE_NAME = ?"; + try { + conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1,iconPath); + stmt.setString(2,newPackageName); + stmt.setString(3,version); + stmt.setString(4,oldPackageName); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while updating application icon details"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public void deleteApplicationIcon(String packageName) throws DeviceManagementDAOException { + Connection conn; + String sql = "DELETE " + + "FROM DM_APP_ICONS " + + "WHERE PACKAGE_NAME = ?"; + try { + conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, packageName); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting application icon details"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public String getIconPath(String applicationIdentifier) throws DeviceManagementDAOException{ + Connection conn; + String sql = "SELECT " + + "ICON_PATH " + + "FROM DM_APP_ICONS " + + "WHERE PACKAGE_NAME = ?"; + String iconPath = null; + try { + conn = this.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1,applicationIdentifier); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + iconPath = rs.getString("ICON_PATH"); + } + return iconPath; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving app icon path of the application"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, 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 = ? " + + "LIMIT ? " + + "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); + 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 98ef9fc4ae..cb372841e6 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 @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.service; +import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application; import org.apache.commons.collections.map.SingletonMap; import io.entgra.device.mgt.core.device.mgt.common.*; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.ApplicationManagementException; @@ -1017,4 +1018,42 @@ public interface DeviceManagementProviderService { throws DeviceManagementException; Boolean sendDeviceNameChangedNotification(Device device) throws DeviceManagementException; + + /** + * This method is for saving application icon info + * @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) + throws DeviceManagementException; + + /** + * This method is for updating application icon info + * @param iconPath Icon path of the application + * @param oldPackageName Old package name of the application + * @param newPackageName New package name of the application + * @param version Version of the application + * @throws DeviceManagementException if any service level or DAO level error occurs + */ + void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version) + throws DeviceManagementException; + + /** + * This method is for deleting application icon info + * @param packageName Package name of the application + * @throws DeviceManagementException if any service level or DAO level error occurs + */ + void deleteApplicationIcon(String packageName) 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, int offset, int limit) + 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 15955daee4..f9c5ec4f7c 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 @@ -4899,4 +4899,130 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throw new DeviceManagementException(msg, e); } } + + @Override + public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementException{ + try{ + DeviceManagementDAOFactory.beginTransaction(); + if(applicationDAO.getApplicationPackageCount(packageName) == 0){ + applicationDAO.saveApplicationIcon(iconPath, packageName, version, tenantId); + } + DeviceManagementDAOFactory.commitTransaction(); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while saving app icon. Icon Path: " + iconPath + + " Package Name: " + packageName + + " Version: " + version + + " Tenant Id: " + tenantId; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version) + throws DeviceManagementException{ + try { + DeviceManagementDAOFactory.beginTransaction(); + applicationDAO.updateApplicationIcon(iconPath, oldPackageName, newPackageName, version); + DeviceManagementDAOFactory.commitTransaction(); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while updating app icon info." + + " Package Name: " + oldPackageName; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public void deleteApplicationIcon(String packageName) + throws DeviceManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + applicationDAO.deleteApplicationIcon(packageName); + DeviceManagementDAOFactory.commitTransaction(); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while deleting app icon info." + + " Package Name: " + packageName ; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + private List getInstalledAppIconInfo(List applications) throws DeviceManagementException { + String iconPath; + try { + DeviceManagementDAOFactory.openConnection(); + for (Application app : applications) { + iconPath = applicationDAO.getIconPath(app.getApplicationIdentifier()); + app.setImageUrl(iconPath); + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving installed app icon info"; + 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); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return applications; + } + + @Override + public List getInstalledApplicationsOnDevice(Device device, int offset, int limit) throws DeviceManagementException { + List applications; + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + applications = applicationDAO.getInstalledApplicationListOnDevice(device.getId(), + device.getEnrolmentInfo().getId(), offset, limit, 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; + } } 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 f675cfb0f3..ad8d4e639e 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 @@ -417,6 +417,16 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( -- POLICY RELATED TABLES FINISHED -- +CREATE TABLE IF NOT EXISTS DM_APP_ICONS ( + ID INTEGER AUTO_INCREMENT NOT NULL, + ICON_PATH VARCHAR(150) DEFAULT NULL, + PACKAGE_NAME VARCHAR(150) NOT NULL, + VERSION VARCHAR(50) DEFAULT '1.1.0', + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID) +); + -- NOTIFICATION TABLE -- CREATE TABLE IF NOT EXISTS DM_NOTIFICATION ( NOTIFICATION_ID INTEGER AUTO_INCREMENT 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 028c24829f..c162d41e06 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,17 @@ CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENAN -- POLICY RELATED TABLES FINISHED -- +IF NOT EXISTS (SELECT * SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_APP_ICONS]') AND TYPE IN (N'U')) +CREATE TABLE DM_APP_ICONS ( + ID INTEGER IDENTITY(1,1) NOT NULL, + ICON_PATH VARCHAR(150) DEFAULT NULL, + PACKAGE_NAME VARCHAR(150) NOT NULL, + VERSION VARCHAR(50) DEFAULT '1.1.0', + CREATED_TIMESTAMP DATETIME2 NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID) +); + -- POLICY AND DEVICE GROUP MAPPING -- IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_GROUP_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_GROUP_POLICY ( 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 0c5a8fb090..6dcc7bec44 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 @@ -458,6 +458,16 @@ CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENAN -- END OF POLICY RELATED TABLES -- +CREATE TABLE IF NOT EXISTS DM_APP_ICONS ( + ID INTEGER AUTO_INCREMENT NOT NULL, + ICON_PATH VARCHAR(150) DEFAULT NULL, + PACKAGE_NAME VARCHAR(150) NOT NULL, + VERSION VARCHAR(50) DEFAULT '1.1.0', + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID) +)ENGINE = InnoDB; + -- POLICY AND DEVICE GROUP MAPPING -- CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( 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 1a0ddb3064..a6f87c9cb6 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 @@ -748,6 +748,28 @@ WHEN (NEW.ID IS NULL) -- POLICY RELATED TABLES FINISHED -- +CREATE TABLE DM_APP_ICONS ( + ID NUMBER(10) NOT NULL, + ICON_PATH VARCHAR2(150) DEFAULT NULL, + PACKAGE_NAME VARCHAR2(150) NOT NULL, + VERSION VARCHAR2(50) DEFAULT '1.1.0', + CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL, + TENANT_ID NUMBER(10) NOT NULL, + PRIMARY KEY (ID) +) +/ + +-- Generate ID using sequence and trigger +CREATE SEQUENCE DM_APP_ICONS_seq START WITH 1 INCREMENT BY 1 NOCACHE +/ +CREATE OR REPLACE TRIGGER DM_APP_ICONS_seq_tr +BEFORE INSERT ON DM_APP_ICONS FOR EACH ROW +WHEN (NEW.ID IS NULL) + BEGIN + SELECT DM_APP_ICONS_seq.NEXTVAL INTO :NEW.ID FROM DUAL; + END; +/ + -- NOTIFICATION TABLE -- CREATE TABLE DM_NOTIFICATION ( NOTIFICATION_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 7df9dcc065..8c601111b7 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 @@ -490,6 +490,19 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( -- END OF POLICY RELATED TABLES -- +CREATE SEQUENCE DM_APP_ICONS_seq; + +CREATE TABLE IF NOT EXISTS DM_APP_ICONS ( + ID INTEGER DEFAULT NEXTVAL ('DM_APP_ICONS_seq') NOT NULL, + ICON_PATH VARCHAR(150) DEFAULT NULL, + PACKAGE_NAME VARCHAR(150) NOT NULL, + VERSION VARCHAR(50) DEFAULT '1.1.0', + CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID) +) +; + -- POLICY AND DEVICE GROUP MAPPING -- CREATE SEQUENCE DM_DEVICE_GROUP_POLICY_seq;