diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java index 98a50e427a..28e77c311e 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java @@ -1286,6 +1286,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) { @@ -1312,6 +1315,23 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + private void persistAppIconInfo(ApplicationReleaseDTO applicationReleaseDTO) + throws ApplicationManagementException { + try{ + String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO); + DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(iconPath, + String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion()); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while creating iconPath"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + String msg = "Error occurred while saving application icon info"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } + @Override public ApplicationRelease createRelease(ApplicationDTO applicationDTO, ApplicationReleaseDTO applicationReleaseDTO, ApplicationType type, boolean isPublished) @@ -1358,6 +1378,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } } ApplicationRelease applicationRelease = APIUtil.releaseDtoToRelease(applicationReleaseDTO); + DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(applicationReleaseDTO.getIconName(), + String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion()); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; } catch (TransactionManagementException e) { @@ -1381,6 +1403,8 @@ public class ApplicationManagerImpl implements ApplicationManager { String msg = "Error occurred while adding new application release for application " + applicationDTO.getId(); log.error(msg, e); throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + throw new RuntimeException(e); } finally { ConnectionManagerUtil.closeDBConnection(); } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java index 7eba303451..720d6696f3 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java @@ -510,4 +510,12 @@ public class APIUtil { return mdmConfig.getArtifactDownloadProtocol() + "://" + host + ":" + port + artifactDownloadEndpoint + Constants.FORWARD_SLASH; } + + public static String createAppIconPath(ApplicationReleaseDTO applicationReleaseDTO) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + 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/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java index 78cf35a228..676bd74c96 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java @@ -64,4 +64,20 @@ 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) 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; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index 23d2148f1b..0c4941c188 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -395,4 +395,44 @@ public class ApplicationDAOImpl implements ApplicationDAO { return application; } + @Override + public void saveApplicationIcon(String iconPath, String packageName, String version) + throws DeviceManagementDAOException{ + Connection conn; + PreparedStatement stmt = null; + try{ + conn = this.getConnection(); + stmt = conn.prepareStatement("INSERT INTO DM_APP_ICONS (ICON_PATH, PACKAGE_NAME, VERSION) " + + "VALUES (?, ?, ?)"); + stmt.setString(1,iconPath); + stmt.setString(2,packageName); + stmt.setString(3,version); + stmt.executeUpdate(); + } catch(SQLException e){ + throw new DeviceManagementDAOException("Error occurred while saving application icon details"); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public int getApplicationPackageCount(String packageName) throws DeviceManagementDAOException{ + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + 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"); + } + return 0; + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while saving application icon details"); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 3dedb8d551..951227ec06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -1034,4 +1034,14 @@ 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 + * @throws DeviceManagementException if any service level or DAO level error occurs + */ + void saveApplicationIcon(String iconPath, String packageName, String version) + throws DeviceManagementException; }