From f389c4bce320cfa8769d1a1e06e7dbfb82218fea Mon Sep 17 00:00:00 2001 From: Kayathiri Mahendrakumaran Date: Sun, 6 Oct 2024 15:19:29 +0530 Subject: [PATCH] Clear shared app link to improve performance of org deletion --- .../application/OrgApplicationManager.java | 12 ++++++++ .../OrgApplicationManagerImpl.java | 6 ++++ .../application/constant/SQLConstants.java | 3 ++ .../application/dao/OrgApplicationMgtDAO.java | 12 ++++++++ .../dao/impl/OrgApplicationMgtDAOImpl.java | 15 ++++++++++ .../listener/OrganizationCreationHandler.java | 30 +++++++++++++------ pom.xml | 2 +- 7 files changed, 70 insertions(+), 10 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java index 80dadcdd7..cb5c06d40 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManager.java @@ -212,4 +212,16 @@ default int getCountOfDiscoverableSharedApplications(String filter, String tenan return 0; } + + /** + * Check whether the main application has shared applications. + * + * @param mainApplicationId Main application ID. + * @return True if the main application has shared applications. + * @throws OrganizationManagementException If an error occurred when checking shared applications. + */ + default boolean hasSharedApps(String mainApplicationId) throws OrganizationManagementException { + + return false; + } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java index d6c325a56..f65434de2 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/OrgApplicationManagerImpl.java @@ -729,6 +729,12 @@ public int getCountOfDiscoverableSharedApplications(String filter, String tenant return getOrgApplicationMgtDAO().getCountOfDiscoverableSharedApplications(filter, tenantDomain, rootOrgId); } + @Override + public boolean hasSharedApps(String mainApplicationId) throws OrganizationManagementException { + + return getOrgApplicationMgtDAO().hasFragments(mainApplicationId); + } + /** * Returns whether the given application is a main application. * diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java index 3d8f8cf46..008a739e5 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/constant/SQLConstants.java @@ -176,6 +176,9 @@ public class SQLConstants { "sa_shared.APP_NAME LIKE ? AND ssa.OWNER_ORG_ID = ? AND (sa_main.IS_DISCOVERABLE = '1' OR " + "sa_shared.IS_DISCOVERABLE = '1')"; + public static final String DELETE_SHARED_APP_LINKS_OF_ORG = "DELETE FROM SP_SHARED_APP WHERE SHARED_ORG_ID = :" + + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID + ";"; + private SQLConstants() { } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java index 0ddd90815..7267f9669 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/OrgApplicationMgtDAO.java @@ -171,4 +171,16 @@ List getDiscoverableSharedApplicationBasicInfo(int limit, */ int getCountOfDiscoverableSharedApplications(String filter, String tenantDomain, String rootOrgId) throws OrganizationManagementException; + + /** + * Delete shared application links of an organization. + * + * @param organizationId The unique ID of the organization. + * @throws OrganizationManagementException the server exception is thrown in a failure when deleting the shared + * applications. + */ + default void deleteSharedAppLinks(String organizationId) throws OrganizationManagementException { + + return; + } } diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java index 9bfd73f2a..3717d1eff 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/dao/impl/OrgApplicationMgtDAOImpl.java @@ -57,6 +57,7 @@ import static org.wso2.carbon.identity.application.mgt.ApplicationMgtUtil.getConsoleAccessUrlFromServerConfig; import static org.wso2.carbon.identity.application.mgt.ApplicationMgtUtil.getMyAccountAccessUrlFromServerConfig; import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.IS_FRAGMENT_APP; +import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.DELETE_SHARED_APP_LINKS_OF_ORG; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_FILTERED_SHARED_APPLICATIONS; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_MAIN_APPLICATION; import static org.wso2.carbon.identity.organization.management.application.constant.SQLConstants.GET_SHARED_APPLICATION; @@ -93,6 +94,7 @@ import static org.wso2.carbon.identity.organization.management.application.util.OrgApplicationManagerUtil.getNewTemplate; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_APPLICATION_HAS_FRAGMENTS; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_APPLICATION_IS_A_FRAGMENT; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_DELETING_SHARED_APPLICATION_LINK; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_LINK_APPLICATIONS; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_SHARED_APPLICATION; @@ -380,6 +382,19 @@ public int getCountOfDiscoverableSharedApplications(String filter, String tenant return count; } + @Override + public void deleteSharedAppLinks(String organizationId) throws OrganizationManagementServerException { + + NamedJdbcTemplate namedJdbcTemplate = getNewTemplate(); + try { + namedJdbcTemplate.executeUpdate(DELETE_SHARED_APP_LINKS_OF_ORG, preparedStatement -> { + preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SHARED_ORG_ID, organizationId); + }); + } catch (DataAccessException e) { + throw handleServerException(ERROR_CODE_ERROR_DELETING_SHARED_APPLICATION_LINK, e, organizationId); + } + } + private int getCountOfDiscoverableSharedApplications(String tenantDomain, String rootOrgId) throws OrganizationManagementException { diff --git a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java index 4c76c4745..d1b94042a 100644 --- a/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java +++ b/components/org.wso2.carbon.identity.organization.management.application/src/main/java/org/wso2/carbon/identity/organization/management/application/listener/OrganizationCreationHandler.java @@ -19,6 +19,7 @@ package org.wso2.carbon.identity.organization.management.application.listener; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -41,7 +42,6 @@ import org.wso2.carbon.identity.organization.management.ext.Constants; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; -import org.wso2.carbon.identity.organization.management.service.model.BasicOrganization; import org.wso2.carbon.identity.organization.management.service.model.Organization; import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; @@ -69,9 +69,10 @@ public class OrganizationCreationHandler extends AbstractEventHandler { public void handleEvent(Event event) throws IdentityEventException { String eventName = event.getEventName(); + Map eventProperties = event.getEventProperties(); + String organizationId = (String) eventProperties.get(Constants.EVENT_PROP_ORGANIZATION_ID); if (Constants.EVENT_POST_ADD_ORGANIZATION.equals(eventName)) { - Map eventProperties = event.getEventProperties(); Organization organization = (Organization) eventProperties.get(Constants.EVENT_PROP_ORGANIZATION); try { addSharedApplicationsToOrganization(organization); @@ -82,8 +83,6 @@ public void handleEvent(Event event) throws IdentityEventException { } if (Constants.EVENT_PRE_DELETE_ORGANIZATION.equals(eventName)) { - Map eventProperties = event.getEventProperties(); - String organizationId = (String) eventProperties.get(Constants.EVENT_PROP_ORGANIZATION_ID); try { handleMainApplicationUpdateForPreDeleteOrganization(organizationId); } catch (IdentityApplicationManagementException | OrganizationManagementException e) { @@ -94,6 +93,7 @@ public void handleEvent(Event event) throws IdentityEventException { if (Constants.EVENT_POST_DELETE_ORGANIZATION.equals(eventName)) { try { + handleSharedAppDeletionForPostDeleteOrganization(organizationId); handleMainApplicationUpdateForPostDeleteOrganization(); } catch (OrganizationManagementException | IdentityApplicationManagementException e) { throw new IdentityEventException("An error occurred while updating main application based " + @@ -199,17 +199,15 @@ private void handleMainApplicationUpdateForPostDeleteOrganization() throws Ident // All the applications have the same tenant ID. Therefore, tenant ID of the first application is used. int rootTenantId = getApplicationManagementService().getTenantIdByApp(mainAppIds.get(0)); String rootTenantDomain = IdentityTenantUtil.getTenantDomain(rootTenantId); - String rootOrganizationId = getOrganizationManager().resolveOrganizationId(rootTenantDomain); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(rootTenantDomain, true); PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username); for (String mainAppId : mainAppIds) { - List applicationSharedOrganizations = getOrgApplicationManager() - .getApplicationSharedOrganizations(rootOrganizationId, mainAppId); - // Since the application doesn't have any shared organizations, isAppShared service provider property + boolean hasSharedApps = getOrgApplicationManager().hasSharedApps(mainAppId); + // Since the application doesn't have any shared applications, isAppShared service provider property // should be set to false. - if (CollectionUtils.isEmpty(applicationSharedOrganizations)) { + if (!hasSharedApps) { ServiceProvider mainApplication = getApplicationManagementService() .getApplicationByResourceId(mainAppId, rootTenantDomain); updateApplicationWithIsAppSharedProperty(false, mainApplication); @@ -221,6 +219,20 @@ private void handleMainApplicationUpdateForPostDeleteOrganization() throws Ident } } + /** + * Handle shared application deletion for post delete organization. + * + * @param organizationId ID of the organization. + */ + private void handleSharedAppDeletionForPostDeleteOrganization(String organizationId) + throws OrganizationManagementException { + + if (StringUtils.isBlank(organizationId)) { + return; + } + getOrgApplicationMgtDAO().deleteSharedAppLinks(organizationId); + } + private void updateApplicationWithIsAppSharedProperty(boolean isAppShared, ServiceProvider mainApplication) throws IdentityApplicationManagementException { diff --git a/pom.xml b/pom.xml index f2f1f6a76..3a8daca28 100644 --- a/pom.xml +++ b/pom.xml @@ -517,7 +517,7 @@ [1.0.0,2.0.0) - 1.1.9 + 1.1.16 [1.0.0,2.0.0)