From 003ddfc56a07a6fdae3333e6bb1bd12c36395c56 Mon Sep 17 00:00:00 2001 From: Thashmi-nil Date: Tue, 16 May 2023 19:11:53 +0530 Subject: [PATCH] add update & delete operations --- .../common/services/ApplicationManager.java | 8 +++ .../mgt/core/impl/ApplicationManagerImpl.java | 38 +++++++++++-- .../application/mgt/core/util/APIUtil.java | 3 +- ...ApplicationManagementPublisherAPIImpl.java | 4 ++ .../device/mgt/core/dao/ApplicationDAO.java | 19 ++++++- .../mgt/core/dao/impl/ApplicationDAOImpl.java | 55 ++++++++++++++++--- .../DeviceManagementProviderService.java | 21 ++++++- .../DeviceManagementProviderServiceImpl.java | 48 +++++++++++++++- 8 files changed, 177 insertions(+), 19 deletions(-) diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java index dcc63be3b3..a996b07a90 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java @@ -524,4 +524,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.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 28e77c311e..83aea85883 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 @@ -1318,9 +1318,10 @@ public class ApplicationManagerImpl implements ApplicationManager { private void persistAppIconInfo(ApplicationReleaseDTO applicationReleaseDTO) throws ApplicationManagementException { try{ - String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO, tenantId); DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(iconPath, - String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion()); + String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion(), tenantId); } catch (ApplicationManagementException e) { String msg = "Error occurred while creating iconPath"; log.error(msg, e); @@ -1378,8 +1379,6 @@ 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) { @@ -1403,8 +1402,6 @@ 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(); } @@ -1941,6 +1938,13 @@ public class ApplicationManagerImpl implements ApplicationManager { break; } } + try { + deleteAppIconInfo(applicationDTO); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while deleting application icon info"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } } @Override @@ -4045,4 +4049,26 @@ 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"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } + + 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"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } } 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 720d6696f3..53540ad768 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 @@ -511,8 +511,7 @@ public class APIUtil { + artifactDownloadEndpoint + Constants.FORWARD_SLASH; } - public static String createAppIconPath(ApplicationReleaseDTO applicationReleaseDTO) throws ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + 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(); diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index df98b1ee8a..c83cb4fca2 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -531,6 +531,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(); @@ -571,6 +573,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/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 676bd74c96..6ff8e3651e 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 @@ -72,7 +72,7 @@ public interface ApplicationDAO { * @param version version of the application * @throws DeviceManagementDAOException If any database error occurred */ - void saveApplicationIcon(String iconPath, String packageName, String version) throws DeviceManagementDAOException; + void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementDAOException; /** * This method is used to check the package existence. @@ -80,4 +80,21 @@ public interface ApplicationDAO { * @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; } 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 0c4941c188..14ab224b1f 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 @@ -31,11 +31,9 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Properties; @@ -396,17 +394,19 @@ public class ApplicationDAOImpl implements ApplicationDAO { } @Override - public void saveApplicationIcon(String iconPath, String packageName, String version) + public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) 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 = 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"); @@ -435,4 +435,45 @@ public class ApplicationDAOImpl implements ApplicationDAO { DeviceManagementDAOUtil.cleanupResources(stmt, null); } } + + @Override + public void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version) + throws DeviceManagementDAOException{ + Connection conn; + PreparedStatement stmt = null; + 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); + } + } + + @Override + public void deleteApplicationIcon(String packageName) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + try{ + conn = this.getConnection(); + stmt = conn.prepareStatement("DELETE FROM DM_APP_ICONS WHERE PACKAGE_NAME = ?" ); + stmt.setString(1, packageName); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while deleting 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 951227ec06..4ca4eafaea 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 @@ -1040,8 +1040,27 @@ 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) + 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; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 0cb7937057..87e3452a4d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -4926,11 +4926,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public void saveApplicationIcon(String iconPath, String packageName, String version) throws DeviceManagementException{ + 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); + applicationDAO.saveApplicationIcon(iconPath, packageName, version, tenantId); } DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { @@ -4947,6 +4947,47 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @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"; + 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"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } private void getInstalledAppIconInfo(Device device) throws DeviceManagementException { List applications = device.getApplications(); String iconPath; @@ -4956,11 +4997,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv iconPath = deviceDAO.getIconPath(app.getApplicationIdentifier()); app.setImageUrl(iconPath); } + DeviceManagementDAOFactory.commitTransaction(); } 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);