Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivyportal 18050 create cache mechanism for dashboard converter to enhance performance ver2 #1342

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
Expand All @@ -37,14 +36,11 @@
import ch.addon.portal.generic.userprofile.homepage.HomepageUtils;
import ch.ivy.addon.portal.generic.navigation.PortalNavigator;
import ch.ivy.addon.portalkit.configuration.Application;
import ch.ivy.addon.portalkit.constant.IvyCacheIdentifier;
import ch.ivy.addon.portalkit.dto.DisplayName;
import ch.ivy.addon.portalkit.dto.dashboard.Dashboard;
import ch.ivy.addon.portalkit.enums.BreadCrumbKind;
import ch.ivy.addon.portalkit.enums.MenuKind;
import ch.ivy.addon.portalkit.enums.SessionAttribute;
import ch.ivy.addon.portalkit.service.ApplicationMultiLanguage;
import ch.ivy.addon.portalkit.service.IvyCacheService;
import ch.ivy.addon.portalkit.service.MainMenuEntryService;
import ch.ivy.addon.portalkit.util.DashboardUtils;
import ch.ivy.addon.portalkit.util.UrlUtils;
Expand Down Expand Up @@ -159,7 +155,7 @@ private MenuElement buildDashboardItem() {
String localizedTitle = getLocalizedTitle(dashboard, currentLanguage, dashboardTitle);
return buildSingleDashboardMenu(localizedTitle, dashboardId, dashboardLink, dashboard.getIcon());
}

return buildSingleDashboardMenu(dashboardTitle, "", dashboardLink, "");
}

Expand All @@ -175,8 +171,7 @@ private String determineDashboardLink() {
}

private List<Dashboard> getSubItemDashboards() {
var dashboards = getDashboardCache().dashboards;
return dashboards.stream().filter(dashboard -> !dashboard.getIsTopMenu()).toList();
return DashboardUtils.getDashboardsWithoutMenuItem();
}

private MenuElement buildDashboardGroupMenu(List<Dashboard> subItemDashboards, String defaultTitle,
Expand Down Expand Up @@ -256,50 +251,6 @@ private MenuElement buildSingleDashboardMenu(String dashboardTitle, String dashb
return dashboardMenu;
}


public PortalDashboardItemWrapper getDashboardCache() {
String sessionUserId = getSessionUserId();
IvyCacheService cacheService = IvyCacheService.getInstance();
PortalDashboardItemWrapper portalDashboardItemWrapper = null;
try {
portalDashboardItemWrapper = getPortalDashboardItemWrapper(sessionUserId, cacheService);
} catch (ClassCastException e) {
cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_MENU, sessionUserId);
}

if (portalDashboardItemWrapper == null) {
synchronized(PortalDashboardItemWrapper.class) {
portalDashboardItemWrapper = new PortalDashboardItemWrapper(DashboardUtils.collectDashboards());
cacheService.setSessionCache(IvyCacheIdentifier.PORTAL_DASHBOARDS, sessionUserId, portalDashboardItemWrapper);
}
}
return portalDashboardItemWrapper;
}

public void updateDashboardCache(List<Dashboard> dashboards) {
String sessionUserId = getSessionUserId();
IvyCacheService cacheService = IvyCacheService.getInstance();

synchronized (PortalDashboardItemWrapper.class) {
cacheService.setSessionCache(IvyCacheIdentifier.PORTAL_DASHBOARDS, sessionUserId,
new PortalDashboardItemWrapper(dashboards));
}

cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_MENU, sessionUserId);
}

private String getSessionUserId() {
String sessionIdAttribute = SessionAttribute.SESSION_IDENTIFIER.name();
if (session().getAttribute(sessionIdAttribute) == null) {
session().setAttribute(sessionIdAttribute, UUID.randomUUID().toString());
}
return (String) session().getAttribute(sessionIdAttribute);
}

private PortalDashboardItemWrapper getPortalDashboardItemWrapper(String sessionUserId, IvyCacheService cacheService) {
return (PortalDashboardItemWrapper) cacheService.getSessionCacheValue(IvyCacheIdentifier.PORTAL_DASHBOARDS, sessionUserId).orElse(null);
}

public String getDashboardLink() {
return PortalNavigator.getDashboardLink();
}
Expand Down Expand Up @@ -522,9 +473,6 @@ private IWorkflowSession session() {
return Ivy.session();
}

public record PortalDashboardItemWrapper(List<Dashboard> dashboards) {
}

private void buildBreadCrumbForNotification() {
setPortalHomeMenuToBreadcrumbModel();
breadcrumbModel.getElements().add(buildGenericMenuItem("/ch.ivy.addon.portalkit.ui.jsf/notifications/notificationTitle"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
Expand All @@ -28,13 +26,10 @@
import ch.ivy.addon.portal.generic.navigation.PortalNavigator;
import ch.ivy.addon.portalkit.comparator.ApplicationIndexAscendingComparator;
import ch.ivy.addon.portalkit.configuration.Application;
import ch.ivy.addon.portalkit.constant.IvyCacheIdentifier;
import ch.ivy.addon.portalkit.dto.DisplayName;
import ch.ivy.addon.portalkit.dto.dashboard.Dashboard;
import ch.ivy.addon.portalkit.enums.BreadCrumbKind;
import ch.ivy.addon.portalkit.enums.MenuKind;
import ch.ivy.addon.portalkit.enums.SessionAttribute;
import ch.ivy.addon.portalkit.service.IvyCacheService;
import ch.ivy.addon.portalkit.service.RegisteredApplicationService;
import ch.ivy.addon.portalkit.util.DashboardUtils;
import ch.ivy.addon.portalkit.util.PermissionUtils;
Expand Down Expand Up @@ -111,35 +106,7 @@ public static List<Application> getThirdPartyApps() {
}

public static List<SubMenuItem> callSubMenuItemsProcess() {
Locale requestLocale = Ivy.session().getContentLocale();
String sessionIdAttribute = SessionAttribute.SESSION_IDENTIFIER.toString();
if (Ivy.session().getAttribute(sessionIdAttribute) == null) {
Ivy.session().setAttribute(sessionIdAttribute, UUID.randomUUID().toString());
}
String sessionUserId = (String) Ivy.session().getAttribute(sessionIdAttribute);
IvyCacheService cacheService = IvyCacheService.getInstance();
PortalSubMenuItemWrapper portalSubMenuItemWrapper = null;
try {
portalSubMenuItemWrapper = (PortalSubMenuItemWrapper) cacheService
.getSessionCacheValue(IvyCacheIdentifier.PORTAL_MENU, sessionUserId).orElse(null);
} catch (ClassCastException e) {
cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_MENU, sessionUserId);
}

if (portalSubMenuItemWrapper == null || !requestLocale.equals(portalSubMenuItemWrapper.loadedLocale)) {
synchronized (PortalSubMenuItemWrapper.class) {
List<SubMenuItem> subMenuItems = new ArrayList<>();
try {
subMenuItems = getSubmenuList();
} catch (Exception e) {
Ivy.log().error("Cannot load SubMenuItems {0}", e.getMessage());
}

portalSubMenuItemWrapper = new PortalSubMenuItemWrapper(requestLocale, subMenuItems);
cacheService.setSessionCache(IvyCacheIdentifier.PORTAL_MENU, sessionUserId, portalSubMenuItemWrapper);
}
}
return portalSubMenuItemWrapper.portalSubMenuItems;
return getSubmenuList();
}

public static void navigateToTargetPage(boolean isClickOnBreadcrumb, String destinationPage,
Expand All @@ -156,9 +123,6 @@ public static void navigateToTargetPage(boolean isClickOnBreadcrumb, String dest
navigateToTargetPage(params);
}

private record PortalSubMenuItemWrapper(Locale loadedLocale, List<SubMenuItem> portalSubMenuItems) {
}

private static List<SubMenuItem> getSubmenuList() {
String currentLanguage = UserUtils.getUserLanguage();
List<SubMenuItem> subMenuItems = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.axonivy.portal.dto.dashboard.filter.DashboardFilter;
import com.axonivy.portal.service.DeepLTranslationService;

import ch.addon.portal.generic.menu.MenuView;
import ch.ivy.addon.portal.generic.navigation.PortalNavigator;
import ch.ivy.addon.portalkit.constant.PortalConstants;
import ch.ivy.addon.portalkit.dto.DisplayName;
Expand All @@ -44,8 +43,6 @@
import ch.ivy.addon.portalkit.enums.TaskEmptyMessage;
import ch.ivy.addon.portalkit.exporter.Exporter;
import ch.ivy.addon.portalkit.ivydata.service.impl.LanguageService;
import ch.ivy.addon.portalkit.jsf.ManagedBeans;
import ch.ivy.addon.portalkit.persistence.converter.BusinessEntityConverter;
import ch.ivy.addon.portalkit.service.GlobalSettingService;
import ch.ivy.addon.portalkit.service.WidgetFilterService;
import ch.ivy.addon.portalkit.support.HtmlParser;
Expand All @@ -56,7 +53,6 @@
import ch.ivy.addon.portalkit.util.UrlUtils;
import ch.ivy.addon.portalkit.util.UserUtils;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.security.ISecurityConstants;
import ch.ivyteam.ivy.security.IUser;
import ch.ivyteam.ivy.workflow.ICase;
import ch.ivyteam.ivy.workflow.ITask;
Expand Down Expand Up @@ -91,11 +87,6 @@ public void init() {
currentDashboardIndex = 0;
dashboards = collectDashboards();

if (isReadOnlyMode) {
MenuView menuView = (MenuView) ManagedBeans.get("menuView");
menuView.updateDashboardCache(dashboards);
}

if (CollectionUtils.isNotEmpty(DashboardUtils.getDashboardsWithoutMenuItem())
|| isRequestPathForMainOrDetailModification()) {
updateSelectedDashboardIdFromSessionAttribute();
Expand Down Expand Up @@ -156,18 +147,6 @@ public void loadDashboardTemplate() {
this.dashboardTemplates = DashboardUtils.getDashboardTemplates();
}

protected List<Dashboard> jsonToDashboards(String dashboardJSON) {
List<Dashboard> mappingDashboards = BusinessEntityConverter.jsonValueToEntities(dashboardJSON, Dashboard.class);
for (Dashboard dashboard : mappingDashboards) {
if (CollectionUtils.isEmpty(dashboard.getPermissions())) {
ArrayList<String> defaultPermissions = new ArrayList<>();
defaultPermissions.add(ISecurityConstants.TOP_LEVEL_ROLE_NAME);
dashboard.setPermissions(defaultPermissions);
}
}
return mappingDashboards;
}

protected String readDashboardBySessionUser() {
return currentUser().getProperty(PortalVariable.DASHBOARD.key);
}
Expand All @@ -185,17 +164,6 @@ protected void buildWidgetModels(Dashboard dashboard) {
}
}

protected List<Dashboard> getVisiblePublicDashboards() {
String dashboardJson = Ivy.var().get(PortalVariable.DASHBOARD.key);
List<Dashboard> visibleDashboards = DashboardUtils.getVisibleDashboards(dashboardJson);
setDashboardAsPublic(visibleDashboards);
return visibleDashboards;
}

private void setDashboardAsPublic(List<Dashboard> visibleDashboards) {
visibleDashboards.stream().forEach(dashboard -> dashboard.setIsPublic(true));
}

public List<Dashboard> getDashboards() {
return dashboards;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ protected List<Dashboard> collectDashboards() {
if (isPublicDashboard) {
collectedDashboards = DashboardUtils.getPublicDashboards();
} else {
String dashboardInUserProperty = readDashboardBySessionUser();
collectedDashboards = getVisibleDashboards(dashboardInUserProperty);
collectedDashboards = DashboardUtils.getPrivateDashboards();
}
} catch (PortalException e) {
Ivy.log().error(e);
Expand Down Expand Up @@ -830,14 +829,6 @@ public void reloadParamtersFromProcessForCustomWidget(DashboardWidget widget) {
customWidget.loadParametersFromProcess();
}

private List<Dashboard> getVisibleDashboards(String dashboardJson) {
if (isPublicDashboard) {
return DashboardUtils.jsonToDashboards(dashboardJson);
} else {
return DashboardUtils.getVisibleDashboards(dashboardJson);
}
}

public void navigatetoDashboardConfigurationPage() throws IOException {
String dashboardConfigurationUrl = PortalNavigator.buildDashboardConfigurationUrl();
FacesContext.getCurrentInstance().getExternalContext().redirect(dashboardConfigurationUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@
import com.axonivy.portal.service.DeepLTranslationService;
import com.axonivy.portal.util.WelcomeWidgetUtils;

import ch.addon.portal.generic.menu.MenuView;
import ch.ivy.addon.portal.generic.navigation.PortalNavigator;
import ch.ivy.addon.portalkit.dto.DisplayName;
import ch.ivy.addon.portalkit.dto.dashboard.Dashboard;
import ch.ivy.addon.portalkit.dto.dashboard.DashboardWidget;
import ch.ivy.addon.portalkit.dto.dashboard.WelcomeDashboardWidget;
import ch.ivy.addon.portalkit.enums.PortalVariable;
import ch.ivy.addon.portalkit.ivydata.mapper.SecurityMemberDTOMapper;
import ch.ivy.addon.portalkit.jsf.ManagedBeans;
import ch.ivy.addon.portalkit.persistence.converter.BusinessEntityConverter;
import ch.ivy.addon.portalkit.util.DashboardUtils;
import ch.ivy.addon.portalkit.util.PermissionUtils;
Expand Down Expand Up @@ -74,7 +72,7 @@ protected void collectDashboardsForManagement() {
this.dashboards = DashboardUtils.getPublicDashboards();
DashboardUtils.addDefaultTaskCaseListDashboardsIfMissing(this.dashboards);
} else if (StringUtils.isNoneEmpty(dashboardInUserProperty)) {
List<Dashboard> myDashboards = getVisibleDashboards(dashboardInUserProperty);
List<Dashboard> myDashboards = DashboardUtils.getPrivateDashboards();
this.dashboards.addAll(myDashboards);
}
}
Expand Down Expand Up @@ -176,17 +174,7 @@ private void saveDashboards(List<Dashboard> dashboards) {
} else {
currentUser().setProperty(PortalVariable.DASHBOARD.key, dashboardJson);
}

MenuView menuView = (MenuView) ManagedBeans.get("menuView");
menuView.updateDashboardCache(DashboardUtils.collectDashboards());
}

private List<Dashboard> getVisibleDashboards(String dashboardJson) {
if (isPublicDashboard) {
return DashboardUtils.jsonToDashboards(dashboardJson);
} else {
return DashboardUtils.getVisibleDashboards(dashboardJson);
}
updateDashboardCache();
}

public void navigateToDashboardDetailsPage(String dashboardId) {
Expand Down Expand Up @@ -388,6 +376,7 @@ public void saveArrangment() {
} else {
savePrivateArrangement();
}
updateDashboardCache();
}

public void savePublicArrangement() {
Expand All @@ -414,4 +403,8 @@ public void savePrivateArrangement() {
String dashboardJson = BusinessEntityConverter.entityToJsonValue(this.dashboards);
Ivy.session().getSessionUser().setProperty(PortalVariable.DASHBOARD.key, dashboardJson);
}

private void updateDashboardCache() {
DashboardUtils.updateDashboardCache();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@

import com.axonivy.portal.util.WelcomeWidgetUtils;

import ch.addon.portal.generic.menu.MenuView;
import ch.ivy.addon.portalkit.dto.DisplayName;
import ch.ivy.addon.portalkit.dto.dashboard.Dashboard;
import ch.ivy.addon.portalkit.dto.dashboard.WelcomeDashboardWidget;
import ch.ivy.addon.portalkit.enums.DashboardWidgetType;
import ch.ivy.addon.portalkit.enums.PortalVariable;
import ch.ivy.addon.portalkit.enums.WelcomeTextPosition;
import ch.ivy.addon.portalkit.jsf.Attrs;
import ch.ivy.addon.portalkit.jsf.ManagedBeans;
import ch.ivy.addon.portalkit.persistence.converter.BusinessEntityConverter;
import ch.ivy.addon.portalkit.support.HtmlParser;
import ch.ivy.addon.portalkit.util.DashboardUtils;
Expand Down Expand Up @@ -97,8 +95,7 @@ private void removeImageContentOfWidget(ContentObject imageContent) {
String dashboardJson = BusinessEntityConverter.entityToJsonValue(dashboards);
Ivy.var().set(PortalVariable.DASHBOARD.key, dashboardJson);

MenuView menuView = (MenuView) ManagedBeans.get("menuView");
menuView.updateDashboardCache(DashboardUtils.collectDashboards());
DashboardUtils.updateDashboardCache();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public final class IvyCacheIdentifier {
public static final String PORTAL_CUSTOM_MENU = "PORTAL_CUSTOM_MENU";
public static final String PORTAL_DASHBOARDS = "PORTAL_DASHBOARDS";
public static final String PORTAL_DASHBOARDS_MENU_ITEM = "PORTAL_DASHBOARDS_MENU_ITEM";
public static final String PORTAL_PRIVATE_DASHBOARDS = "PORTAL_PRIVATE_DASHBOARDS";
public static final String PORTAL_PUBLIC_DASHBOARDS = "PORTAL_PUBLIC_DASHBOARDS";

// for caching locales
public static final String PORTAL_CONTENT_LOCALES = "PORTAL_CONTENT_LOCALES";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void setTemplateId(String templateId) {
}

public Boolean getIsTopMenu() {
return isTopMenu;
return isTopMenu == null ? false : isTopMenu;
}

public void setIsTopMenu(Boolean isTopMenu) {
Expand Down
Loading
Loading