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

Feature/ivyportal 18050 create cache mechanism for dashboard converter to enhance performance #1299

Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ public PortalDashboardItemWrapper getDashboardCache() {
try {
portalDashboardItemWrapper = getPortalDashboardItemWrapper(sessionUserId, cacheService);
} catch (ClassCastException e) {
cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_MENU, sessionUserId);
cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_DASHBOARDS, sessionUserId);
}

if (portalDashboardItemWrapper == null) {
synchronized(PortalDashboardItemWrapper.class) {
synchronized (this) {
portalDashboardItemWrapper = new PortalDashboardItemWrapper(DashboardUtils.collectDashboards());
cacheService.setSessionCache(IvyCacheIdentifier.PORTAL_DASHBOARDS, sessionUserId, portalDashboardItemWrapper);
}
Expand All @@ -279,13 +279,10 @@ public PortalDashboardItemWrapper getDashboardCache() {
public void updateDashboardCache(List<Dashboard> dashboards) {
String sessionUserId = getSessionUserId();
IvyCacheService cacheService = IvyCacheService.getInstance();
cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_DASHBOARDS, sessionUserId);
cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_PUBLIC_DASHBOARD, sessionUserId);
cacheService.invalidateSessionEntry(IvyCacheIdentifier.PORTAL_PRIVATE_DASHBOARD, sessionUserId);

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

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

private String getSessionUserId() {
Expand All @@ -297,7 +294,8 @@ private String getSessionUserId() {
}

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

public String getDashboardLink() {
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,9 @@ 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;
List<SubMenuItem> subMenuItems = new ArrayList<>();
subMenuItems = getSubmenuList();
return subMenuItems;
}

public static void navigateToTargetPage(boolean isClickOnBreadcrumb, String destinationPage,
Expand All @@ -156,9 +125,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 @@ -45,7 +45,6 @@
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 +55,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 @@ -150,18 +148,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 @@ -180,16 +166,10 @@ 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);
List<Dashboard> visibleDashboards = DashboardUtils.getVisibleDashboards(true);
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 @@ -86,9 +86,7 @@
import ch.ivy.addon.portalkit.jsf.Attrs;
import ch.ivy.addon.portalkit.jsf.ManagedBeans;
import ch.ivy.addon.portalkit.service.DashboardService;
import ch.ivy.addon.portalkit.service.exception.PortalException;
import ch.ivy.addon.portalkit.util.CustomWidgetUtils;
import ch.ivy.addon.portalkit.util.DashboardUtils;
import ch.ivy.addon.portalkit.util.DashboardWidgetUtils;
import ch.ivy.addon.portalkit.util.Dates;
import ch.ivy.addon.portalkit.util.UserUtils;
Expand Down Expand Up @@ -169,23 +167,7 @@ private Function<IWebStartable, DashboardProcess> convertToDashboardProcess() {
};
}

@Override
protected List<Dashboard> collectDashboards() {
List<Dashboard> collectedDashboards = new ArrayList<>();
try {
if (isPublicDashboard) {
collectedDashboards = DashboardUtils.getPublicDashboards();
} else {
String dashboardInUserProperty = readDashboardBySessionUser();
collectedDashboards = getVisibleDashboards(dashboardInUserProperty);
}
} catch (PortalException e) {
Ivy.log().error(e);
}
DashboardUtils.addDefaultTaskCaseListDashboardsIfMissing(collectedDashboards);
return collectedDashboards.stream()
.filter(dashboard -> dashboard.getId().equals(selectedDashboardId)).collect(Collectors.toList());
}


private WidgetSample taskSample() {
return new WidgetSample(translate("/ch.ivy.addon.portalkit.ui.jsf/dashboard/taskList"), TASK,
Expand Down Expand Up @@ -830,14 +812,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 @@ -69,13 +69,10 @@ public void initConfigration(boolean isPublicDashboard) {

protected void collectDashboardsForManagement() {
this.dashboards = new ArrayList<>();
String dashboardInUserProperty = readDashboardBySessionUser();
if (isPublicDashboard) {
this.dashboards = DashboardUtils.getPublicDashboards();
DashboardUtils.addDefaultTaskCaseListDashboardsIfMissing(this.dashboards);
} else if (StringUtils.isNoneEmpty(dashboardInUserProperty)) {
List<Dashboard> myDashboards = getVisibleDashboards(dashboardInUserProperty);
this.dashboards.addAll(myDashboards);
this.dashboards = DashboardUtils.getVisibleDashboards(true);
} else {
this.dashboards = DashboardUtils.getVisibleDashboards(false);
}
}

Expand Down Expand Up @@ -181,14 +178,6 @@ private void saveDashboards(List<Dashboard> dashboards) {
menuView.updateDashboardCache(DashboardUtils.collectDashboards());
}

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

public void navigateToDashboardDetailsPage(String dashboardId) {
PortalNavigator.navigateToDashboardDetailsPage(dashboardId, isPublicDashboard);
}
Expand Down Expand Up @@ -406,8 +395,8 @@ public void savePublicArrangement() {
}
}
newDashboards.addAll(idToDashboard.values());
String dashboardsAsSJSON = BusinessEntityConverter.entityToJsonValue(newDashboards);
Ivy.var().set(PortalVariable.DASHBOARD.key, dashboardsAsSJSON);

saveDashboards(newDashboards);
}

public void savePrivateArrangement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import ch.ivy.addon.portalkit.bean.PortalExceptionBean;
import ch.ivy.addon.portalkit.dto.UserMenu;
import ch.ivy.addon.portalkit.enums.GlobalVariable;
import ch.ivy.addon.portalkit.enums.SessionAttribute;
import ch.ivy.addon.portalkit.jsf.Attrs;
import ch.ivy.addon.portalkit.jsf.ManagedBeans;
import ch.ivy.addon.portalkit.service.AnnouncementService;
import ch.ivy.addon.portalkit.service.GlobalSettingService;
import ch.ivy.addon.portalkit.service.IvyCacheService;
import ch.ivy.addon.portalkit.util.DashboardUtils;
import ch.ivy.addon.portalkit.util.PermissionUtils;
import ch.ivy.addon.portalkit.util.RequestUtils;
import ch.ivy.addon.portalkit.util.TaskUtils;
Expand Down Expand Up @@ -384,7 +384,7 @@ public String getGooglePlayImageLink() {
}

public String getInfoToHighlightMenu() {
return DashboardUtils.getSelectedMainDashboardIdFromSession();
return (String) Ivy.session().getAttribute(SessionAttribute.SELECTED_DASHBOARD_ID.toString());
}

public void navigateToChatBotOrDisplayWorkingTaskWarning(boolean isWorkingOnATask, ITask task) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public final class IvyCacheIdentifier {
public static final String LOGOUT_PAGE_CACHE_ENTRY_NAME = "LOGOUT_PAGE_ID";
public static final String STATISTIC_COLOR = "STATISTIC_COLOR";
public static final String ROLES_IN_SECURITY_CONTEXT = "ROLES_IN_SECURITY_CONTEXT";
public static final String PORTAL_MENU = "PORTAL_MENU";
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_PUBLIC_DASHBOARD = "PORTAL_PUBLIC_DASHBOARD";
public static final String PORTAL_PRIVATE_DASHBOARD = "PORTAL_PRIVATE_DASHBOARD";

// for caching locales
public static final String PORTAL_CONTENT_LOCALES = "PORTAL_CONTENT_LOCALES";
public static final String PORTAL_FORMATTING_LOCALES = "PORTAL_FORMATTING_LOCALES";
Expand Down
Loading
Loading