Fix default categories not retreiving issue on sub tenants

fix-issue-with-default-categories
Rajitha Kumara 2 years ago
parent c8c18c4f3d
commit 1c0393fbbb

@ -2223,7 +2223,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
List<CategoryDTO> existingCategories = applicationDAO.getAllCategories(tenantId); List<CategoryDTO> existingCategories = this.getAllCategories();
List<String> existingCategoryNames = existingCategories.stream().map(CategoryDTO::getCategoryName) List<String> existingCategoryNames = existingCategories.stream().map(CategoryDTO::getCategoryName)
.collect(Collectors.toList()); .collect(Collectors.toList());
if(!existingCategoryNames.containsAll(categories)){ if(!existingCategoryNames.containsAll(categories)){
@ -2395,7 +2395,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<String> updatingAppCategries = applicationUpdateWrapper.getCategories(); List<String> updatingAppCategries = applicationUpdateWrapper.getCategories();
if (updatingAppCategries != null){ if (updatingAppCategries != null){
List<CategoryDTO> allCategories = this.applicationDAO.getAllCategories(tenantId); List<CategoryDTO> allCategories = this.getAllCategories();
List<String> allCategoryName = allCategories.stream().map(CategoryDTO::getCategoryName) List<String> allCategoryName = allCategories.stream().map(CategoryDTO::getCategoryName)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -2522,7 +2522,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
List<CategoryDTO> categories = applicationDAO.getAllCategories(tenantId); List<CategoryDTO> categories = this.getAllCategories();
List<Integer> mappedCategoryIds = applicationDAO.getDistinctCategoryIdsInCategoryMapping(); List<Integer> mappedCategoryIds = applicationDAO.getDistinctCategoryIdsInCategoryMapping();
List<Category> responseCategoryList = new ArrayList<>(); List<Category> responseCategoryList = new ArrayList<>();
categories.forEach(category -> { categories.forEach(category -> {
@ -2800,7 +2800,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
try { try {
if (categories != null && !categories.isEmpty()) { if (categories != null && !categories.isEmpty()) {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
List<CategoryDTO> registeredCategories = applicationDAO.getAllCategories(tenantId); List<CategoryDTO> registeredCategories = this.getAllCategories();
List<String> registeredCategoryNames = registeredCategories.stream().map(CategoryDTO::getCategoryName) List<String> registeredCategoryNames = registeredCategories.stream().map(CategoryDTO::getCategoryName)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -3623,7 +3623,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
List<CategoryDTO> registeredCategories = this.applicationDAO.getAllCategories(tenantId); List<CategoryDTO> registeredCategories = this.getAllCategories();
if (registeredCategories.isEmpty()) { if (registeredCategories.isEmpty()) {
String msg = "Registered application category set is empty. Since it is mandatory to add application " 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(); ConnectionManagerUtil.closeDBConnection();
} }
} }
/**
* Retrieve all categories after sync default categories with database
* @return List<CategoryDTO> list of CategoryDTO objects
* @throws ApplicationManagementException throws when constructing all categories
*/
private List<CategoryDTO> getAllCategories() throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
List<String> defaultCategories = ConfigurationManager.getInstance().getConfiguration().getAppCategories();
try {
ConnectionManagerUtil.getDBConnection();
List<CategoryDTO> existingCategoriesInDB = this.applicationDAO.getAllCategories(tenantId);
List<String> existingCategoryNames = existingCategoriesInDB.stream().
map(CategoryDTO::getCategoryName).collect(Collectors.toList());
List<String> 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);
}
}
} }

Loading…
Cancel
Save