Skip to content

Commit

Permalink
Merge pull request #397 from kayathiri4/patch-perf
Browse files Browse the repository at this point in the history
Clear shared app link to improve performance of org deletion
  • Loading branch information
kayathiri4 authored Oct 6, 2024
2 parents 7d85019 + f389c4b commit 116009b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,16 @@ List<ApplicationBasicInfo> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -69,9 +69,10 @@ public class OrganizationCreationHandler extends AbstractEventHandler {
public void handleEvent(Event event) throws IdentityEventException {

String eventName = event.getEventName();
Map<String, Object> eventProperties = event.getEventProperties();
String organizationId = (String) eventProperties.get(Constants.EVENT_PROP_ORGANIZATION_ID);

if (Constants.EVENT_POST_ADD_ORGANIZATION.equals(eventName)) {
Map<String, Object> eventProperties = event.getEventProperties();
Organization organization = (Organization) eventProperties.get(Constants.EVENT_PROP_ORGANIZATION);
try {
addSharedApplicationsToOrganization(organization);
Expand All @@ -82,8 +83,6 @@ public void handleEvent(Event event) throws IdentityEventException {
}

if (Constants.EVENT_PRE_DELETE_ORGANIZATION.equals(eventName)) {
Map<String, Object> eventProperties = event.getEventProperties();
String organizationId = (String) eventProperties.get(Constants.EVENT_PROP_ORGANIZATION_ID);
try {
handleMainApplicationUpdateForPreDeleteOrganization(organizationId);
} catch (IdentityApplicationManagementException | OrganizationManagementException e) {
Expand All @@ -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 " +
Expand Down Expand Up @@ -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<BasicOrganization> 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);
Expand All @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@
<org.wso2.identity.organization.mgt.imp.pkg.version.range>[1.0.0,2.0.0)
</org.wso2.identity.organization.mgt.imp.pkg.version.range>

<identity.organization.management.core.version>1.1.9</identity.organization.management.core.version>
<identity.organization.management.core.version>1.1.16</identity.organization.management.core.version>
<org.wso2.identity.organization.mgt.core.imp.pkg.version.range>[1.0.0,2.0.0)
</org.wso2.identity.organization.mgt.core.imp.pkg.version.range>

Expand Down

0 comments on commit 116009b

Please sign in to comment.