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 10d6d6f651..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 @@ -1316,19 +1316,25 @@ 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{ + 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"; + 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"; + String msg = "Error occurred while saving application icon info. Application package name : " + applicationReleaseDTO.getPackageName(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } @@ -1942,7 +1948,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try { deleteAppIconInfo(applicationDTO); } catch (ApplicationManagementException e) { - String msg = "Error occurred while deleting application icon info"; + String msg = "Error occurred while deleting application icon info. Application package name: " + applicationDTO.getPackageName(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } @@ -4057,17 +4063,23 @@ public class ApplicationManagerImpl implements ApplicationManager { DataHolder.getInstance().getDeviceManagementService().updateApplicationIcon(applicationRelease.getIconPath(), oldPackageName, applicationRelease.getPackageName(), applicationRelease.getVersion()); } catch (DeviceManagementException e) { - String msg = "Error occurred while updating application icon info"; + String msg = "Error occurred while updating application icon info. Application package name: " + oldPackageName; log.error(msg, e); throw new ApplicationManagementException(msg, e); } } - private void deleteAppIconInfo(ApplicationDTO applicationDTO) throws ApplicationManagementException{ + /** + * 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"; + 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 e0d4d4474b..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 @@ -511,6 +511,13 @@ public class APIUtil { + 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; 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 7c2076fa26..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 @@ -30,7 +30,11 @@ import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUti import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; -import java.sql.*; +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; @@ -396,42 +400,52 @@ public class ApplicationDAOImpl implements ApplicationDAO { public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementDAOException{ Connection conn; - PreparedStatement stmt = null; - try{ + String sql = "INSERT INTO DM_APP_ICONS " + + "(ICON_PATH, " + + "PACKAGE_NAME, " + + "VERSION, " + + "CREATED_TIMESTAMP, " + + "TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?)"; + try { conn = this.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_APP_ICONS (ICON_PATH, PACKAGE_NAME, VERSION, CREATED_TIMESTAMP, TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?)"); - 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){ - throw new DeviceManagementDAOException("Error occurred while saving application icon details"); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); + 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; - PreparedStatement stmt = null; - ResultSet rs = null; - try{ + String sql = "SELECT " + + "COUNT(*) AS APP_PACKAGE_COUNT " + + "FROM DM_APP_ICONS " + + "WHERE PACKAGE_NAME = ?"; + try { conn = this.getConnection(); - stmt = conn.prepareStatement("SELECT COUNT(*) AS APP_PACKAGE_COUNT FROM DM_APP_ICONS WHERE PACKAGE_NAME = ?"); - stmt.setString(1, packageName); - rs = stmt.executeQuery(); - if (rs.next()) { - return rs.getInt("APP_PACKAGE_COUNT"); + 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; + } } - return 0; } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while saving application icon details"); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); + String msg = "Error occurred while getting application icon details"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } } @@ -439,63 +453,70 @@ public class ApplicationDAOImpl implements ApplicationDAO { public void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version) throws DeviceManagementDAOException{ Connection conn; - PreparedStatement stmt = null; - try{ + String sql = "UPDATE DM_APP_ICONS " + + "SET " + + "ICON_PATH= ?, " + + "PACKAGE_NAME = ?, " + + "VERSION = ? " + + "WHERE PACKAGE_NAME = ?"; + try { conn = this.getConnection(); - stmt = conn.prepareStatement("UPDATE DM_APP_ICONS " + - "SET " + - "ICON_PATH= ?, " + - "PACKAGE_NAME = ?, " + - "VERSION = ? " + - "WHERE PACKAGE_NAME = ?"); - stmt.setString(1,iconPath); - stmt.setString(2,newPackageName); - stmt.setString(3,version); - stmt.setString(4,oldPackageName); - stmt.executeUpdate(); - } catch(SQLException e){ - throw new DeviceManagementDAOException("Error occurred while updating application icon details"); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); + 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; - PreparedStatement stmt = null; - try{ + String sql = "DELETE " + + "FROM DM_APP_ICONS " + + "WHERE PACKAGE_NAME = ?"; + try { conn = this.getConnection(); - stmt = conn.prepareStatement("DELETE FROM DM_APP_ICONS WHERE PACKAGE_NAME = ?" ); - stmt.setString(1, packageName); - stmt.executeUpdate(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, packageName); + stmt.executeUpdate(); + } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while deleting application icon details"); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); + 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; - PreparedStatement stmt = null; + String sql = "SELECT " + + "ICON_PATH " + + "FROM DM_APP_ICONS " + + "WHERE PACKAGE_NAME = ?"; String iconPath = null; - try{ + try { conn = this.getConnection(); - stmt = conn.prepareStatement("SELECT ICON_PATH FROM DM_APP_ICONS" + - " WHERE PACKAGE_NAME = ?"); - stmt.setString(1,applicationIdentifier); - try (ResultSet rs = stmt.executeQuery()) { - if (rs.next()) { - iconPath = rs.getString("ICON_PATH"); + 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; } - return iconPath; } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving app icon path"); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); + String msg = "Error occurred while retrieving app icon path of the application"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } } @@ -503,30 +524,49 @@ public class ApplicationDAOImpl implements ApplicationDAO { public List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId) throws DeviceManagementDAOException { Connection conn; - PreparedStatement stmt = null; List applicationList = new ArrayList<>(); Application application; - ResultSet rs = null; + 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(); - 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 " + - "WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ? LIMIT ? OFFSET ?"); - stmt.setInt(1, deviceId); - stmt.setInt(2, enrolmentId); - stmt.setInt(3, tenantId); - stmt.setInt(4, limit); - stmt.setInt(5, offset); - rs = stmt.executeQuery(); - while (rs.next()) { - application = loadApplication(rs); - applicationList.add(application); + 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) { - throw new DeviceManagementDAOException("SQL Error occurred while retrieving the list of Applications " + - "installed in device id '" + deviceId, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + 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 4e1ea1f470..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 @@ -1048,6 +1048,12 @@ public interface DeviceManagementProviderService { */ 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 1e155791e2..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 @@ -4914,7 +4914,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throw new DeviceManagementException(msg, e); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while saving app icon info"; + 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 { @@ -4935,7 +4938,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throw new DeviceManagementException(msg, e); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while updating app icon info"; + String msg = "Error occurred while updating app icon info." + + " Package Name: " + oldPackageName; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -4956,7 +4960,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throw new DeviceManagementException(msg, e); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while deleting app icon info"; + String msg = "Error occurred while deleting app icon info." + + " Package Name: " + packageName ; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -4973,12 +4978,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv app.setImageUrl(iconPath); } } catch (DeviceManagementDAOException e) { - DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while retrieving installed app icon info"; log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (SQLException e) { - DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while opening a connection to the data source"; log.error(msg); throw new DeviceManagementException(msg, e); 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 9738000956..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 @@ -672,7 +672,7 @@ CREATE TABLE IF NOT EXISTS DM_OTP_DATA ( EMAIL VARCHAR(100) NOT NULL, EMAIL_TYPE VARCHAR(20) NOT NULL, META_INFO VARCHAR(20000) NOT NULL, - CREATED_AT TIMESTAMP NOT NULL, + CREATED_AT TIMESTAMP(0) NOT NULL, EXPIRY_TIME INT NOT NULL DEFAULT 3600, IS_EXPIRED BOOLEAN DEFAULT false, PRIMARY KEY (ID)