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 57890f53eb..cb7cf75684 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 @@ -546,4 +546,12 @@ public interface ApplicationManager { */ void updateAppIconInfo(ApplicationRelease applicationRelease, String oldPackageName) throws ApplicationManagementException; + + /** + * Delete all application related data of a tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementException thrown if an error occurs when deleting data + */ + void deleteApplicationDataOfTenant(int tenantId) throws ApplicationManagementException; } 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/ApplicationStorageManager.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/ApplicationStorageManager.java index 68b82facbc..698c57f55a 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/ApplicationStorageManager.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/ApplicationStorageManager.java @@ -132,4 +132,12 @@ public interface ApplicationStorageManager { * @throws StorageManagementException if errors while generating md5 string */ String getMD5(InputStream inputStream) throws StorageManagementException; + + /** + * Delete the folder containing all the app releases of a tenant + * + * @param tenantId Tenant ID + * @throws ApplicationStorageManagementException thrown if + */ + void deleteAppFolderOfTenant(int tenantId) throws ApplicationStorageManagementException; } 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/dao/ApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java index ffa85b260d..3d460fb4db 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java @@ -238,4 +238,52 @@ public interface ApplicationDAO { int getApplicationCount(Filter filter, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException; void deleteApplication(int appId, int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete favourite applications of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteAppFavouritesByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Application category mapping of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteApplicationCategoryMappingByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Application categories of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteApplicationCategoriesByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Application tags mapping of Tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteApplicationTagsMappingByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Application tags of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteApplicationTagsByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Applications of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteApplicationsByTenant(int tenantId) throws ApplicationManagementDAOException; } 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/dao/ApplicationReleaseDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationReleaseDAO.java index cd893979dd..8314388798 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -127,4 +127,12 @@ public interface ApplicationReleaseDAO { */ List getReleaseByPackages(List packages, int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Application releases of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteReleasesByTenant(int tenantId) throws ApplicationManagementDAOException; } 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/dao/LifecycleStateDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/LifecycleStateDAO.java index 3f8656f858..ecf8b1e807 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/LifecycleStateDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/LifecycleStateDAO.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.LifecycleState; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; import io.entgra.device.mgt.core.application.mgt.core.exception.LifeCycleManagementDAOException; import java.util.List; @@ -75,4 +76,11 @@ public interface LifecycleStateDAO { */ String getAppReleaseCreatedUsername(int appId, String uuid, int tenantId) throws LifeCycleManagementDAOException; - } + /** + * Delete Application lifecycle states of tenant + * + * @param tenantId Tenant ID + * @throws LifeCycleManagementDAOException thrown if an error occurs while deleting data + */ + void deleteAppLifecycleStatesByTenant(int tenantId) throws LifeCycleManagementDAOException; +} 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/dao/ReviewDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ReviewDAO.java index 9a12859b5c..8fcc3abb54 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ReviewDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ReviewDAO.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.response.Review; import io.entgra.device.mgt.core.application.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.application.mgt.common.dto.ReviewDTO; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; import io.entgra.device.mgt.core.application.mgt.core.exception.ReviewManagementDAOException; import java.util.List; @@ -119,4 +120,11 @@ import java.util.List; void deleteAllChildCommentsOfReview(int rootParentId, int tenantId) throws ReviewManagementDAOException; + /** + * Delete reviews of a tenant + * + * @param tenantId Tenant ID + * @throws ReviewManagementDAOException thrown if an error occurs while deleting data + */ + void deleteReviewsByTenant(int tenantId) throws ReviewManagementDAOException; } 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/dao/SPApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SPApplicationDAO.java index 712a0e28b3..aa84543e6f 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SPApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SPApplicationDAO.java @@ -128,4 +128,20 @@ public interface SPApplicationDAO { * @throws ApplicationManagementDAOException if any db error occurred */ void deleteIdentityServer(int id, int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Identity servers of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteIdentityServerByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Service provide mapping details of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteSPApplicationMappingByTenant(int tenantId) throws ApplicationManagementDAOException; } 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/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index 804a4a29e2..3213db3484 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -264,4 +264,52 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException if error occurred while retrieving the app details */ Activity getOperationAppDetails(int operationId, int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Operation mapping details of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteOperationMappingByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete device subscriptions of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteDeviceSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete group subscriptions of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteGroupSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete role subscriptions of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteRoleSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete user subscriptions of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteUserSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete scheduled subscription details of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteScheduledSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException; } 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/dao/VisibilityDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VisibilityDAO.java index 45664a9d61..49290336c4 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VisibilityDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VisibilityDAO.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.application.mgt.core.dao; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; import io.entgra.device.mgt.core.application.mgt.core.exception.VisibilityManagementDAOException; import java.util.List; @@ -70,4 +71,11 @@ public interface VisibilityDAO { */ void deleteAppUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException; + /** + * Delete app unrestricted roles of tenant + * + * @param tenantId Tenant ID + * @throws VisibilityManagementDAOException thrown if an error occurs while deleting data + */ + void deleteAppUnrestrictedRolesByTenant(int tenantId) throws VisibilityManagementDAOException; } 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/dao/VppApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java index 02aec95174..a9b637ab86 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/VppApplicationDAO.java @@ -43,4 +43,28 @@ public interface VppApplicationDAO { int addAssociation(VppAssociationDTO vppAssociationDTO, int tenantId) throws ApplicationManagementDAOException; VppAssociationDTO updateAssociation(VppAssociationDTO vppAssociationDTO, int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete associations of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteAssociationByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete Vpp users of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteVppUserByTenant(int tenantId) throws ApplicationManagementDAOException; + + /** + * Delete assets of tenant + * + * @param tenantId Tenant ID + * @throws ApplicationManagementDAOException thrown if an error occurs while deleting data + */ + void deleteAssetsByTenant(int tenantId) throws ApplicationManagementDAOException; } 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/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 09bea80fa5..0d6970b70d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -1884,4 +1884,159 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public void deleteAppFavouritesByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete app of tenant of id " + tenantId + " from favourites"); + } + String sql = "DELETE FROM AP_APP_FAVOURITES " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing app from favourites of tenant " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing app of tenant of id " + tenantId + " from favourites. " + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteApplicationCategoryMappingByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete application category mapping of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP_CATEGORY_MAPPING " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing application category mapping of tenant" + +tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing application category mapping of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteApplicationCategoriesByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete application category of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP_CATEGORY " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing application category of tenant " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing application category of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteApplicationTagsMappingByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete application tags mapping of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP_TAG_MAPPING " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing application tags mapping of tenant" + +tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing application tags mapping of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteApplicationTagsByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete application tags of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP_TAG " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing application tags of tenant" + +tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing application tags of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteApplicationsByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete applications of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing applications of tenant" + +tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing applications of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index 400ff9e7ef..9cebf37f41 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -624,4 +624,29 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements throw new ApplicationManagementDAOException(msg, e); } } -} + + @Override + public void deleteReleasesByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete application releases of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP_RELEASE " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing application release of tenant" + +tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing application release of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java index 5a5b130157..0511602f65 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java @@ -484,4 +484,56 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp } } + @Override + public void deleteIdentityServerByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete identity server of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_IDENTITY_SERVER " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteSPApplicationMappingByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete applications of tenant of id " + tenantId + + " from service providers"); + } + String sql = "DELETE FROM AP_IS_SP_APP_MAPPING " + + "WHERE TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing applications of tenant" + +tenantId+ "from service providers"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing applications of tenant of id " + tenantId + + "from service providers. Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java index d190d88296..993e9476f4 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java @@ -485,4 +485,57 @@ public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp } } + @Override + public void deleteIdentityServerByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete identity server of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_IDENTITY_SERVER " + + "WHERE TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteSPApplicationMappingByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete applications of tenant of id " + tenantId + + " from service providers"); + } + String sql = "DELETE FROM AP_IS_SP_APP_MAPPING " + + "WHERE TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing applications of tenant" + +tenantId+ "from service providers"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing applications of tenant of id " + tenantId + + "from service providers. Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java index d599d0db69..bc1fc660e9 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java @@ -485,4 +485,57 @@ public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements S } } + @Override + public void deleteIdentityServerByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete identity server of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_IDENTITY_SERVER " + + "WHERE TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteSPApplicationMappingByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete applications of tenant of id " + tenantId + + " from service providers"); + } + String sql = "DELETE FROM AP_IS_SP_APP_MAPPING " + + "WHERE TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing applications of tenant" + +tenantId+ "from service providers"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing applications of tenant of id " + tenantId + + "from service providers. Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java index 3061b96a86..5b6b2571ab 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java @@ -485,4 +485,57 @@ public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements S } } + @Override + public void deleteIdentityServerByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete identity server of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_IDENTITY_SERVER " + + "WHERE TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete an identity server of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteSPApplicationMappingByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete applications of tenant of id " + tenantId + + " from service providers"); + } + String sql = "DELETE FROM AP_IS_SP_APP_MAPPING " + + "WHERE TENANT_ID = ?"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing applications of tenant" + +tenantId+ "from service providers"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing applications of tenant of id " + tenantId + + "from service providers. Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java index f1fdece953..77026bb04a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java @@ -114,6 +114,31 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } } + @Override + public void deleteAppLifecycleStatesByTenant(int tenantId) throws LifeCycleManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete app lifecycle states of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP_LIFECYCLE_STATE " + + "WHERE TENANT_ID = ?"; + try ( Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing app lifecycle states of tenant" + +tenantId; + log.error(msg, e); + throw new LifeCycleManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing app lifecycle states of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new LifeCycleManagementDAOException(msg, e); + } + } + @Override public List getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException { try { 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/dao/impl/review/GenericReviewDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/review/GenericReviewDAOImpl.java index 075ba6b01f..71323aa868 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/review/GenericReviewDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/review/GenericReviewDAOImpl.java @@ -602,4 +602,29 @@ public class GenericReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { throw new ReviewManagementDAOException(msg, e); } } + + @Override + public void deleteReviewsByTenant(int tenantId) throws ReviewManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete app reviews of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_APP_REVIEW " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing app reviews of tenant " + + tenantId; + log.error(msg, e); + throw new ReviewManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing app reviews of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ReviewManagementDAOException(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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 457de13bee..e8a030a01d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1476,4 +1476,157 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public void deleteOperationMappingByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete operation mapping of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_APP_SUB_OP_MAPPING " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete operation mapping of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete operation mapping of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteRoleSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete role subscription of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_ROLE_SUBSCRIPTION " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete role subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete role subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteUserSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete user subscription of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_USER_SUBSCRIPTION " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete user subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete user subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteGroupSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete user subscription of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_GROUP_SUBSCRIPTION " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete group subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete group subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteScheduledSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete scheduled subscription of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_SCHEDULED_SUBSCRIPTION " + + "WHERE APPLICATION_UUID IN " + + "(SELECT UUID FROM AP_APP_RELEASE WHERE TENANT_ID = ?)"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeBatch(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete scheduled subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete scheduled subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteDeviceSubscriptionByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete device subscription of the tenant of id: " + tenantId); + } + String sql = "DELETE FROM AP_DEVICE_SUBSCRIPTION " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete device subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to delete device subscription of tenant of id " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/dao/impl/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java index ea0b0fb758..446a54c706 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -176,4 +176,31 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil throw new VisibilityManagementDAOException(msg, e); } } + + @Override + public void deleteAppUnrestrictedRolesByTenant(int tenantId) throws VisibilityManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete Application unrestricted roles of tenant of ID " + + tenantId); + } + String sql = "DELETE " + + "FROM AP_UNRESTRICTED_ROLE " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to delete application unrestricted roles of tenant: " + + tenantId; + log.error(msg, e); + throw new VisibilityManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing query to delete application unrestricted roles which of" + + " tenant Id " + tenantId + ". executed query: " + sql; + log.error(msg, e); + throw new VisibilityManagementDAOException(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/dao/impl/vpp/GenericVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java index 7ff06c41eb..4b618894b5 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java @@ -506,4 +506,81 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public void deleteAssetsByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete application releases of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_ASSETS " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing application release of tenant " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing application release of tenant of id " + tenantId + + " Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public void deleteVppUserByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete vpp user of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_VPP_USER " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing vpp user of tenant " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing vpp user of tenant of id " + tenantId + + "Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + + } + + @Override + public void deleteAssociationByTenant(int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete association of tenant of id " + tenantId); + } + String sql = "DELETE FROM AP_VPP_ASSOCIATION " + + "WHERE TENANT_ID = ?"; + try (Connection conn = this.getDBConnection()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection when removing association of tenant" + + tenantId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while removing association of tenant of id " + tenantId + + " Executed Query: " + sql; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/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 3b2eb638aa..c72f61942c 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 @@ -19,6 +19,8 @@ package io.entgra.device.mgt.core.application.mgt.core.impl; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; +import io.entgra.device.mgt.core.application.mgt.core.dao.*; +import io.entgra.device.mgt.core.application.mgt.core.exception.*; import io.entgra.device.mgt.core.device.mgt.common.Base64File; import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil; @@ -75,11 +77,6 @@ import io.entgra.device.mgt.core.application.mgt.common.wrapper.PublicAppWrapper import io.entgra.device.mgt.core.application.mgt.common.wrapper.WebAppReleaseWrapper; import io.entgra.device.mgt.core.application.mgt.common.wrapper.WebAppWrapper; import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager; -import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.LifecycleStateDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; -import io.entgra.device.mgt.core.application.mgt.core.dao.VisibilityDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import io.entgra.device.mgt.core.application.mgt.core.util.APIUtil; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; @@ -128,6 +125,8 @@ public class ApplicationManagerImpl implements ApplicationManager { private SubscriptionDAO subscriptionDAO; private LifecycleStateManager lifecycleStateManager; private SPApplicationDAO spApplicationDAO; + private VppApplicationDAO vppApplicationDAO; + private ReviewDAO reviewDAO; public ApplicationManagerImpl() { initDataAccessObjects(); @@ -141,6 +140,8 @@ public class ApplicationManagerImpl implements ApplicationManager { this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); this.subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); this.spApplicationDAO = ApplicationManagementDAOFactory.getSPApplicationDAO(); + this.vppApplicationDAO = ApplicationManagementDAOFactory.getVppApplicationDAO(); + this.reviewDAO = ApplicationManagementDAOFactory.getCommentDAO(); } @Override @@ -4167,4 +4168,66 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException(msg, e); } } + + @Override + public void deleteApplicationDataOfTenant(int tenantId) throws ApplicationManagementException { + if (log.isDebugEnabled()) { + log.debug("Request is received to delete application related data of tenant with ID: " + tenantId); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + + vppApplicationDAO.deleteAssociationByTenant(tenantId); + vppApplicationDAO.deleteVppUserByTenant(tenantId); + vppApplicationDAO.deleteAssetsByTenant(tenantId); + reviewDAO.deleteReviewsByTenant(tenantId); + subscriptionDAO.deleteOperationMappingByTenant(tenantId); + subscriptionDAO.deleteDeviceSubscriptionByTenant(tenantId); + subscriptionDAO.deleteGroupSubscriptionByTenant(tenantId); + subscriptionDAO.deleteRoleSubscriptionByTenant(tenantId); + subscriptionDAO.deleteUserSubscriptionByTenant(tenantId); + applicationDAO.deleteAppFavouritesByTenant(tenantId); + applicationDAO.deleteApplicationTagsMappingByTenant(tenantId); + applicationDAO.deleteApplicationTagsByTenant(tenantId); + applicationDAO.deleteApplicationCategoryMappingByTenant(tenantId); + applicationDAO.deleteApplicationCategoriesByTenant(tenantId); + subscriptionDAO.deleteScheduledSubscriptionByTenant(tenantId); + lifecycleStateDAO.deleteAppLifecycleStatesByTenant(tenantId); + applicationReleaseDAO.deleteReleasesByTenant(tenantId); + visibilityDAO.deleteAppUnrestrictedRolesByTenant(tenantId); + spApplicationDAO.deleteSPApplicationMappingByTenant(tenantId); + spApplicationDAO.deleteIdentityServerByTenant(tenantId); + applicationDAO.deleteApplicationsByTenant(tenantId); + APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); + + ConnectionManagerUtil.commitDBTransaction(); + } catch (DBConnectionException e) { + String msg = "Error occurred while observing the database connection to delete applications for tenant with ID: " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Database access error is occurred when getting applications for tenant with ID: " + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (LifeCycleManagementDAOException e) { + String msg = "Error occurred while deleting life-cycle state data of application releases of the tenant" + + " of ID: " + tenantId ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ReviewManagementDAOException e) { + String msg = "Error occurred while deleting reviews of application releases of the applications" + + " of tenant ID: " + tenantId ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationStorageManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while deleting App folder of tenant" + + " of tenant ID: " + tenantId ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } } 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/ApplicationStorageManagerImpl.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/ApplicationStorageManagerImpl.java index a79a3d23ee..158361232c 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/ApplicationStorageManagerImpl.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/ApplicationStorageManagerImpl.java @@ -310,4 +310,18 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager throw new StorageManagementException(msg, e); } } + + @Override + public void deleteAppFolderOfTenant(int tenantId) throws ApplicationStorageManagementException{ + String folderPath = storagePath + File.separator + tenantId; + File folder = new File(folderPath); + if (folder.exists()) { + try { + StorageManagementUtil.delete(folder); + } catch (IOException e) { + throw new ApplicationStorageManagementException( + "Error occurred while deleting App folder of tenant:" + tenantId, e); + } + } + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java index a62341756e..5d324aeb02 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java @@ -61,6 +61,13 @@ import javax.ws.rs.core.Response; key = "um:admin:users:remove", roles = {"Internal/devicemgt-admin"}, permissions = {"/device-mgt/admin/users/delete"} + ), + @Scope( + name = "Delete Tenant Information", + description = "Delete tenant details", + key = "um:admin:tenants:remove", + roles = {"Internal/devicemgt-admin"}, + permissions = {"/device-mgt/admin/tenants/delete"} ) } ) @@ -258,5 +265,41 @@ public interface UserManagementAdminService { @Size(max = 45) String deviceId); + @DELETE + @Path("/domain/{tenantDomain}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Delete a tenant by tenant domain.", + notes = "This API allows the deletion of a tenant by providing the tenant domain.", + tags = "Tenant details remove", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "um:admin:tenants:remove") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Tenant has been deleted successfully."), + @ApiResponse( + code = 404, + message = "Not Found. \n The tenant with the provided domain does not exist.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while removing the tenant.", + response = ErrorResponse.class) + }) + Response deleteTenantByDomain( + @ApiParam( + name = "tenantDomain", + value = "The domain of the tenant to be deleted.", + required = true) + + @PathParam("tenantDomain") + String tenantDomain); } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index 26cd8c4c54..296bdf5060 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.admin; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; @@ -25,6 +26,11 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.PasswordResetWrapper import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.admin.UserManagementAdminService; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.CredentialManagementResponseBuilder; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; +import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.stratos.common.exception.StratosException; +import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; +import org.wso2.carbon.user.api.UserStoreException; import javax.validation.constraints.Size; import javax.ws.rs.*; @@ -81,4 +87,28 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic } } + @DELETE + @Path("/domain/{tenantDomain}") + @Override + public Response deleteTenantByDomain(@PathParam("tenantDomain") String tenantDomain) { + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + if(tenantId != MultitenantConstants.SUPER_TENANT_ID){ + String msg = "Only super tenants are allowed to delete tenants"; + log.error(msg); + return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); + }else{ + TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); + tenantMgtAdminService.deleteTenant(tenantDomain); + String msg = "Tenant Deletion process has been initiated for tenant:" + tenantDomain; + return Response.status(Response.Status.OK).entity(msg).build(); + } + + } catch (StratosException | UserStoreException e) { + String msg = "Error deleting tenant: " + tenantDomain; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + } 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/DeviceManagementDAOFactory.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/DeviceManagementDAOFactory.java index 7b105df14d..b7768f9c52 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/DeviceManagementDAOFactory.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/DeviceManagementDAOFactory.java @@ -139,6 +139,22 @@ public class DeviceManagementDAOFactory { throw new IllegalStateException("Database engine has not initialized properly."); } + public static TenantDAO getTenantDAO() { + if (databaseEngine != null) { + switch (databaseEngine) { + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: + return new TenantDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); + } + public static TrackerDAO getTrackerDAO() { if (databaseEngine != null) { switch (databaseEngine) { 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/TenantDAO.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/TenantDAO.java new file mode 100644 index 0000000000..372df2583a --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/TenantDAO.java @@ -0,0 +1,431 @@ +package io.entgra.device.mgt.core.device.mgt.core.dao; + +public interface TenantDAO { + + /** + * Delete device certificates of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceCertificateByTenantId(int tenantId)throws DeviceManagementDAOException; + + /** + * Delete groups of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteGroupByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete role-group mapping data of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteRoleGroupMapByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete devices of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device properties of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDevicePropertiesByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete group properties of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteGroupPropertiesByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device-group mapping details of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceGroupMapByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete operations of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteOperationByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete enrolments of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteEnrolmentByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device statuses of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceStatusByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete enrolment mapping of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteEnrolmentOpMappingByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device operation responses of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceOperationResponseByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete large-device operations responses of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceOperationResponseLargeByTenantId(int tenantId) throws DeviceManagementDAOException; + + // Delete policy related tables + + /** + * Delete applications of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteApplicationByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete policy compliance features of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deletePolicyComplianceFeaturesByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete policy change management data of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deletePolicyChangeManagementByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete policy compliance statuses of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deletePolicyComplianceStatusByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete policy criteria properties of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deletePolicyCriteriaPropertiesByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete policy criteria of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deletePolicyCriteriaByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete policies of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deletePolicyByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete role policies of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteRolePolicyByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete user policies of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteUserPolicyByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device policies of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDevicePolicyAppliedByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete criteria of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteCriteriaByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device type properties of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceTypePolicyByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device policies of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDevicePolicyByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete profile features of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteProfileFeaturesByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete policy corrective actions of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deletePolicyCorrectiveActionByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete profiles of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteProfileByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete app icons of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteAppIconsByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device group policies of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceGroupPolicyByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete notifications of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteNotificationByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device information of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceInfoByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device location of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceLocationByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device history of last seven days of a tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceHistoryLastSevenDaysByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device details of a tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceDetailByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete metadata of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteMetadataByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete OTP data of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteOTPDataByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete geo fences of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteGeofenceByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete geo fence group mapping data of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteGeofenceGroupMappingByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device events of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceEventByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device event group mapping of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceEventGroupMappingByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete geo fence event mapping of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteGeofenceEventMappingByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete external group mapping of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteExternalGroupMappingByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete External device mapping of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteExternalDeviceMappingByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete external permission mapping of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteExternalPermissionMapping(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete dynamic tasks of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDynamicTaskByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete dynamic task properties of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDynamicTaskPropertiesByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device subtypes of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceSubTypeByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete traccar unsynced devices of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteTraccarUnsyncedDevicesByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete sub operation templates of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteSubOperationTemplate(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete device organizations of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteDeviceOrganizationByTenantId(int tenantId) throws DeviceManagementDAOException; + + /** + * Delete CEA policies of tenant + * + * @param tenantId Tenant ID + * @throws DeviceManagementDAOException thrown if there is an error when deleting data + */ + void deleteCEAPoliciesByTenantId(int tenantId) 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/TenantDAOImpl.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/TenantDAOImpl.java new file mode 100644 index 0000000000..69abf756a0 --- /dev/null +++ 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/TenantDAOImpl.java @@ -0,0 +1,939 @@ +package io.entgra.device.mgt.core.device.mgt.core.dao.impl; + +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; +import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + + +public class TenantDAOImpl implements TenantDAO { + + private static final Log log = LogFactory.getLog(TenantDAOImpl.class); + + + @Override + public void deleteDeviceCertificateByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting certificates for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteGroupByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_GROUP WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting groups for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteRoleGroupMapByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_ROLE_GROUP_MAP WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting role group mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting devices for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDevicePropertiesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_PROPERTIES WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device properties for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public void deleteGroupPropertiesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM GROUP_PROPERTIES WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting group properties for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceGroupMapByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device group mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteOperationByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_OPERATION WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting operations for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteEnrolmentByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_ENROLMENT WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting enrolment for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceStatusByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID IN " + + "(SELECT ID FROM DM_ENROLMENT WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device status for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteEnrolmentOpMappingByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting enrolment op mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceOperationResponseByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ID IN " + + "(SELECT ID FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device operation response for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceOperationResponseLargeByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE_LARGE WHERE EN_OP_MAP_ID IN "+ + "(SELECT ID FROM DM_ENROLMENT_OP_MAPPING WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device operation response large for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteApplicationByTenantId(int tenantId) throws DeviceManagementDAOException{ + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_APPLICATION WHERE DEVICE_ID IN " + + "(SELECT ID FROM DM_DEVICE WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting applications for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + + } + + @Override + public void deletePolicyComplianceFeaturesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_POLICY_COMPLIANCE_FEATURES WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy compliance features for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deletePolicyChangeManagementByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_POLICY_CHANGE_MGT WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy change management for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deletePolicyComplianceStatusByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy compliance status for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deletePolicyCriteriaPropertiesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_POLICY_CRITERIA_PROPERTIES WHERE POLICY_CRITERION_ID IN " + + "(SELECT ID FROM DM_POLICY_CRITERIA WHERE POLICY_ID IN " + + "(SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?))"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy criteria properties for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deletePolicyCriteriaByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_POLICY_CRITERIA WHERE POLICY_ID IN " + + "(SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy criteria for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deletePolicyByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_POLICY WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteRolePolicyByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_ROLE_POLICY WHERE POLICY_ID IN " + + "(SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting role policy for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteUserPolicyByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_USER_POLICY WHERE POLICY_ID IN " + + "(SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting user policy for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDevicePolicyAppliedByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy applied for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteCriteriaByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_CRITERIA WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting criteria for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceTypePolicyByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_TYPE_POLICY WHERE POLICY_ID IN " + + "(SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device type policy for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public void deleteDevicePolicyByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_POLICY WHERE POLICY_ID IN " + + "(SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device policy for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteProfileFeaturesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID IN " + + "(SELECT ID FROM DM_PROFILE WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting profile features for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deletePolicyCorrectiveActionByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION WHERE POLICY_ID IN " + + "(SELECT ID FROM DM_POLICY WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting policy corrective action for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteProfileByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_PROFILE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting profile for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteAppIconsByTenantId(int tenantId) throws DeviceManagementDAOException{ + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_APP_ICONS WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting App Icons for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceGroupPolicyByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_GROUP_POLICY WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device group policy for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteNotificationByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_NOTIFICATION WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting notifications for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceInfoByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID IN " + + "(SELECT ID FROM DM_ENROLMENT WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device info for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceLocationByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID IN " + + "(SELECT ID FROM DM_ENROLMENT WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device location for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceHistoryLastSevenDaysByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_HISTORY_LAST_SEVEN_DAYS WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device history for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceDetailByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID IN " + + "(SELECT ID FROM DM_ENROLMENT WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device detail for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteMetadataByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_METADATA WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting metadata for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteOTPDataByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_OTP_DATA WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting OTP data for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteGeofenceByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_GEOFENCE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting geo fence for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteGeofenceGroupMappingByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_GEOFENCE_GROUP_MAPPING WHERE FENCE_ID IN " + + "(SELECT ID FROM DM_GEOFENCE WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting geo fence group mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceEventByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_EVENT WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device event for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceEventGroupMappingByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_EVENT_GROUP_MAPPING WHERE EVENT_ID IN " + + "(SELECT ID FROM DM_DEVICE_EVENT WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device event group mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteGeofenceEventMappingByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_GEOFENCE_EVENT_MAPPING WHERE FENCE_ID IN " + + "(SELECT ID FROM DM_GEOFENCE WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting geo fence event mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteExternalGroupMappingByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_EXT_GROUP_MAPPING WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting external group mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteExternalDeviceMappingByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_OTP_DATA WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting external device mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteExternalPermissionMapping(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_EXT_PERMISSION_MAPPING WHERE TRACCAR_USER_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting ext permission mapping for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + + } + + @Override + public void deleteDynamicTaskByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DYNAMIC_TASK WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting dynamic task for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDynamicTaskPropertiesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DYNAMIC_TASK_PROPERTIES WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting dynamic task properties for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceSubTypeByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_SUB_TYPE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device sub types for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteTraccarUnsyncedDevicesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_TRACCAR_UNSYNCED_DEVICES WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting Traccar unsynced devices for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteSubOperationTemplate(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM SUB_OPERATION_TEMPLATE WHERE SUB_TYPE_ID IN " + + "(SELECT SUB_TYPE_ID FROM DM_DEVICE_SUB_TYPE WHERE TENANT_ID = ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting sub operation template for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteDeviceOrganizationByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting device organization for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } + + @Override + public void deleteCEAPoliciesByTenantId(int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = DeviceManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_CEA_POLICIES WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting CEA policies for Tenant ID " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + + } +} diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java index a43f20f24c..cbc67114ee 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java @@ -26,4 +26,6 @@ public interface TenantManagerService { void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException; + void deleteTenantApplicationData(int tenantId) throws TenantMgtException; + void deleteTenantDeviceData(int tenantId) throws TenantMgtException; } \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java index 97eb706be7..ac3ee406f9 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java @@ -43,4 +43,18 @@ public interface TenantManager { * @throws TenantMgtException Throws when error occurred while adding default application categories */ void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException; + + /** + * Delete Application related details of a tenant + * @param tenantId ID of the tenant + * @throws TenantMgtException Throws when deleting Tenant related application data + */ + void deleteTenantApplicationData(int tenantId) throws TenantMgtException; + + /** + * Delete Device related details of a tenant + * @param tenantId ID of the tenant + * @throws TenantMgtException Throws when deleting Tenant related device data + */ + void deleteTenantDeviceData(int tenantId) throws TenantMgtException; } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java index 5dc2eb7f08..cbac0013b4 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java @@ -20,6 +20,9 @@ package io.entgra.device.mgt.core.tenant.mgt.core.impl; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; +import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import io.entgra.device.mgt.core.tenant.mgt.core.internal.TenantMgtDataHolder; @@ -38,6 +41,7 @@ import io.entgra.device.mgt.core.device.mgt.common.roles.config.Role; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; +import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -46,6 +50,11 @@ import java.util.Map; public class TenantManagerImpl implements TenantManager { private static final Log log = LogFactory.getLog(TenantManagerImpl.class); private static final String PERMISSION_ACTION = "ui.execute"; + TenantDAO tenantDao; + + public TenantManagerImpl() { + this.tenantDao = DeviceManagementDAOFactory.getTenantDAO(); + } @Override public void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException { @@ -113,6 +122,99 @@ public class TenantManagerImpl implements TenantManager { } } + @Override + public void deleteTenantApplicationData(int tenantId) throws TenantMgtException { + try { + TenantMgtDataHolder.getInstance().getApplicationManager(). + deleteApplicationDataOfTenant(tenantId); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while deleting Application related data of Tenant of " + + "tenant Id" + tenantId; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } + } + + @Override + public void deleteTenantDeviceData(int tenantId) throws TenantMgtException { + if (log.isDebugEnabled()) { + log.debug("Request is received to delete Device related data of tenant with ID: " + tenantId); + } + try { + DeviceManagementDAOFactory.openConnection(); + + tenantDao.deleteExternalPermissionMapping(tenantId); + tenantDao.deleteExternalDeviceMappingByTenantId(tenantId); + tenantDao.deleteExternalGroupMappingByTenantId(tenantId); + tenantDao.deleteDeviceOrganizationByTenantId(tenantId); + tenantDao.deleteDeviceHistoryLastSevenDaysByTenantId(tenantId); + tenantDao.deleteDeviceDetailByTenantId(tenantId); + tenantDao.deleteDeviceLocationByTenantId(tenantId); + tenantDao.deleteDeviceInfoByTenantId(tenantId); + tenantDao.deleteNotificationByTenantId(tenantId); + tenantDao.deleteAppIconsByTenantId(tenantId); + tenantDao.deleteTraccarUnsyncedDevicesByTenantId(tenantId); + tenantDao.deleteDeviceEventGroupMappingByTenantId(tenantId); + tenantDao.deleteDeviceEventByTenantId(tenantId); + tenantDao.deleteGeofenceEventMappingByTenantId(tenantId); + tenantDao.deleteGeofenceGroupMappingByTenantId(tenantId); + tenantDao.deleteGeofenceByTenantId(tenantId); + tenantDao.deleteDeviceGroupPolicyByTenantId(tenantId); + tenantDao.deleteDynamicTaskPropertiesByTenantId(tenantId); + tenantDao.deleteDynamicTaskByTenantId(tenantId); + tenantDao.deleteMetadataByTenantId(tenantId); + tenantDao.deleteOTPDataByTenantId(tenantId); + tenantDao.deleteSubOperationTemplate(tenantId); + tenantDao.deleteDeviceSubTypeByTenantId(tenantId); + tenantDao.deleteCEAPoliciesByTenantId(tenantId); + + tenantDao.deleteApplicationByTenantId(tenantId); + tenantDao.deletePolicyCriteriaPropertiesByTenantId(tenantId); + tenantDao.deletePolicyCriteriaByTenantId(tenantId); + tenantDao.deleteCriteriaByTenantId(tenantId); + tenantDao.deletePolicyChangeManagementByTenantId(tenantId); + tenantDao.deletePolicyComplianceFeaturesByTenantId(tenantId); + tenantDao.deletePolicyComplianceStatusByTenantId(tenantId); + tenantDao.deleteRolePolicyByTenantId(tenantId); + tenantDao.deleteUserPolicyByTenantId(tenantId); + tenantDao.deleteDeviceTypePolicyByTenantId(tenantId); + tenantDao.deleteDevicePolicyAppliedByTenantId(tenantId); + tenantDao.deleteDevicePolicyByTenantId(tenantId); + tenantDao.deletePolicyCorrectiveActionByTenantId(tenantId); + tenantDao.deletePolicyByTenantId(tenantId); + tenantDao.deleteProfileFeaturesByTenantId(tenantId); + tenantDao.deleteProfileByTenantId(tenantId); + + tenantDao.deleteDeviceOperationResponseLargeByTenantId(tenantId); + tenantDao.deleteDeviceOperationResponseByTenantId(tenantId); + tenantDao.deleteEnrolmentOpMappingByTenantId(tenantId); + tenantDao.deleteDeviceStatusByTenantId(tenantId); + tenantDao.deleteEnrolmentByTenantId(tenantId); + tenantDao.deleteOperationByTenantId(tenantId); + tenantDao.deleteDeviceGroupMapByTenantId(tenantId); + tenantDao.deleteGroupPropertiesByTenantId(tenantId); + tenantDao.deleteDevicePropertiesByTenantId(tenantId); + tenantDao.deleteDeviceByTenantId(tenantId); + tenantDao.deleteRoleGroupMapByTenantId(tenantId); + tenantDao.deleteGroupByTenantId(tenantId); + tenantDao.deleteDeviceCertificateByTenantId(tenantId); + + DeviceManagementDAOFactory.commitTransaction(); + } catch (SQLException e){ + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error accessing the database when trying to delete tenant info of '" + tenantId + "'"; + log.error(msg); + throw new TenantMgtException(msg, e); + } catch (DeviceManagementDAOException e) { + String msg = "Error deleting data of tenant of ID: '" + tenantId + "'"; + log.error(msg); + throw new TenantMgtException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + } + private void initTenantFlow(TenantInfoBean tenantInfoBean) { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java index da21a52100..bc82bef3db 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java @@ -38,4 +38,14 @@ public class TenantManagerServiceImpl implements TenantManagerService { public void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException { TenantMgtDataHolder.getInstance().getTenantManager().addDefaultDeviceStatusFilters(tenantInfoBean); } + + @Override + public void deleteTenantApplicationData(int tenantId) throws TenantMgtException { + TenantMgtDataHolder.getInstance().getTenantManager().deleteTenantApplicationData(tenantId); + } + + @Override + public void deleteTenantDeviceData(int tenantId) throws TenantMgtException { + TenantMgtDataHolder.getInstance().getTenantManager().deleteTenantDeviceData(tenantId); + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java index 978e69b0e4..257206f84f 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -88,5 +88,13 @@ public class DeviceMgtTenantListener implements TenantMgtListener { @Override public void onPreDelete(int i) throws StratosException { // Any work to be performed before a tenant is deleted + TenantManager tenantManager = TenantMgtDataHolder.getInstance().getTenantManager(); + try{ + tenantManager.deleteTenantDeviceData(i); + tenantManager.deleteTenantApplicationData(i); + } catch (TenantMgtException e) { + String msg = "Error occurred while deleting tenant data"; + log.error(msg, e); + } } } diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml index f5208c958f..6df8455797 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml @@ -289,6 +289,7 @@ um:users:update um:users:invite um:admin:users:view + um:admin:tenants:remove dm:admin:enrollment:update gm:devices:view gm:groups:update