From 1c0393fbbb0ccdb7604bee043129c6abd6760d6a Mon Sep 17 00:00:00 2001 From: rajitha Date: Fri, 31 Mar 2023 22:54:28 +0530 Subject: [PATCH] Fix default categories not retreiving issue on sub tenants --- .../mgt/core/impl/ApplicationManagerImpl.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java index b570adff88..2bfdde11bf 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java @@ -2223,7 +2223,7 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.beginDBTransaction(); - List existingCategories = applicationDAO.getAllCategories(tenantId); + List existingCategories = this.getAllCategories(); List existingCategoryNames = existingCategories.stream().map(CategoryDTO::getCategoryName) .collect(Collectors.toList()); if(!existingCategoryNames.containsAll(categories)){ @@ -2395,7 +2395,7 @@ public class ApplicationManagerImpl implements ApplicationManager { List updatingAppCategries = applicationUpdateWrapper.getCategories(); if (updatingAppCategries != null){ - List allCategories = this.applicationDAO.getAllCategories(tenantId); + List allCategories = this.getAllCategories(); List allCategoryName = allCategories.stream().map(CategoryDTO::getCategoryName) .collect(Collectors.toList()); @@ -2522,7 +2522,7 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - List categories = applicationDAO.getAllCategories(tenantId); + List categories = this.getAllCategories(); List mappedCategoryIds = applicationDAO.getDistinctCategoryIdsInCategoryMapping(); List responseCategoryList = new ArrayList<>(); categories.forEach(category -> { @@ -2800,7 +2800,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try { if (categories != null && !categories.isEmpty()) { ConnectionManagerUtil.beginDBTransaction(); - List registeredCategories = applicationDAO.getAllCategories(tenantId); + List registeredCategories = this.getAllCategories(); List registeredCategoryNames = registeredCategories.stream().map(CategoryDTO::getCategoryName) .collect(Collectors.toList()); @@ -3623,7 +3623,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new BadRequestException(msg); } - List registeredCategories = this.applicationDAO.getAllCategories(tenantId); + List registeredCategories = this.getAllCategories(); if (registeredCategories.isEmpty()) { String msg = "Registered application category set is empty. Since it is mandatory to add application " @@ -3999,4 +3999,38 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.closeDBConnection(); } } + + /** + * Retrieve all categories after sync default categories with database + * @return List list of CategoryDTO objects + * @throws ApplicationManagementException throws when constructing all categories + */ + private List getAllCategories() throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + List defaultCategories = ConfigurationManager.getInstance().getConfiguration().getAppCategories(); + try { + ConnectionManagerUtil.getDBConnection(); + List existingCategoriesInDB = this.applicationDAO.getAllCategories(tenantId); + List existingCategoryNames = existingCategoriesInDB.stream(). + map(CategoryDTO::getCategoryName).collect(Collectors.toList()); + List notExistingDefaultCategoryNamesInDB = defaultCategories.stream(). + filter(defaultCategory -> !existingCategoryNames.contains(defaultCategory)).collect(Collectors.toList()); + if(!notExistingDefaultCategoryNamesInDB.isEmpty()) { + this.applicationDAO.addCategories(notExistingDefaultCategoryNamesInDB, tenantId); + if(log.isDebugEnabled()) { + log.debug("Database successfully sync with the default categories"); + } + existingCategoriesInDB = this.applicationDAO.getAllCategories(tenantId); + } + return existingCategoriesInDB; + } catch (DBConnectionException e) { + String msg = "Error occurred while getting the database connection"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while constructing and syncing categories with the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } }