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

feat(Locales) #30300 : Include a Feature Flag for the old Languages portlet #30341

Merged
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
12 changes: 11 additions & 1 deletion dotCMS/src/main/java/com/dotmarketing/business/ajax/DwrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Provides utility methods for DWR-related classes that allow developers to retrieve common-use
* information such as:
* <ul>
* <li>The current Session, Servlet Context, and DWR objects.</li>
* <li>The current Session, Request, Servlet Context, and DWR objects.</li>
* <li>The currently logged-in User and their Roles.</li>
* <li>Portlet validation data.</li>>
* </ul>
Expand Down Expand Up @@ -176,4 +176,14 @@ public static ServletContext getServletContext() {
return ctx.getServletContext();
}

/**
* Returns the current HTTP Request object from the DWR Web Context Factory.
*
* @return The current instance of the {@link HttpServletRequest} object.
*/
public static HttpServletRequest getHttpServletRequest() {
final WebContext ctx = WebContextFactory.get();
return ctx.getHttpServletRequest();
}

}
90 changes: 54 additions & 36 deletions dotCMS/src/main/java/com/dotmarketing/business/ajax/RoleAjax.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.dotmarketing.business.ajax;

import static com.dotmarketing.business.ajax.DwrUtil.getLoggedInUser;
import static com.dotmarketing.business.ajax.DwrUtil.validateRolesPortletPermissions;

import com.dotcms.api.system.event.Payload;
import com.dotcms.api.system.event.SystemEventType;
import com.dotcms.api.system.event.SystemEventsAPI;
Expand Down Expand Up @@ -54,6 +51,7 @@
import com.dotmarketing.quartz.job.CascadePermissionsJob;
import com.dotmarketing.util.ActivityLogger;
import com.dotmarketing.util.AdminLogger;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.DateUtil;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.SecurityLogger;
Expand All @@ -66,6 +64,9 @@
import com.liferay.portal.language.LanguageUtil;
import com.liferay.portal.model.Portlet;
import com.liferay.portal.model.User;
import io.vavr.Lazy;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -77,22 +78,40 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;

import static com.dotmarketing.business.ajax.DwrUtil.getLoggedInUser;
import static com.dotmarketing.business.ajax.DwrUtil.validateRolesPortletPermissions;
import static com.dotmarketing.util.PortletID.LANGUAGES;

/**
* This class exposes Role and Portlet-related information to the DWR framework. This class is used
* by several Dojo-based portlets in the dotCMS backend, and will be progressively migrated to the
* respective REST Endpoint classes in the near future.
*
* @author root
* @since Mar 22nd, 2012
*/
public class RoleAjax {

private final SystemEventsAPI systemEventsAPI;
private final PortletAPI portletAPI;
private final UserWebAPI userWebAPI;

private static final Lazy<Boolean> HIDE_OLD_LANGUAGES_PORTLET =
Lazy.of(() -> Config.getBooleanProperty("FEATURE_FLAG_LOCALES_HIDE-OLD-LANGUAGES-PORTLET", true));
jcastro-dotcms marked this conversation as resolved.
Show resolved Hide resolved

private static final ObjectMapper mapper = DotObjectMapperProvider.getInstance()
.getDefaultObjectMapper();

public RoleAjax(){
this(APILocator.getSystemEventsAPI());
this(APILocator.getSystemEventsAPI(), APILocator.getPortletAPI(), WebAPILocator.getUserWebAPI());
}

@VisibleForTesting
protected RoleAjax(SystemEventsAPI systemEventsAPI) {
protected RoleAjax(final SystemEventsAPI systemEventsAPI, final PortletAPI portletAPI, final UserWebAPI userWebAPI) {
this.systemEventsAPI = systemEventsAPI;
this.portletAPI = portletAPI;
this.userWebAPI = userWebAPI;
}

public List<Map<String, Object>> getRolesTreeFiltered(boolean onlyUserAssignableRoles, String excludeRoles) throws DotDataException{
Expand Down Expand Up @@ -592,38 +611,37 @@ public void saveRoleLayouts(String roleId, String[] layoutIds) throws DotDataExc
}

/**
* Retrieves the info { title, id } of all portlets that can be added to layouts
* @return
* @throws SystemException
* @throws LanguageException
* @throws DotRuntimeException
* @throws PortalException
* Retrieves the title and ID of all portlets that can be added to the main menu -- i.e.,
* layouts -- in the dotCMS backend
*
* @return A list of maps, each containing the title and ID of a portlet that can be added to
* the main menu.
*
* @throws SystemException An error occurred when retrieving all Portlets from the database.
* @throws LanguageException An error occurred when retrieving the localized title of a
* Portlet.
*/
public List<Map<String, Object>> getAllAvailablePortletInfoList() throws SystemException, LanguageException, DotRuntimeException, PortalException {

PortletAPI portletAPI = APILocator.getPortletAPI();
UserWebAPI uWebAPI = WebAPILocator.getUserWebAPI();
WebContext ctx = WebContextFactory.get();
HttpServletRequest request = ctx.getHttpServletRequest();

List<Map<String, Object>> listOfPortletsInfo = new ArrayList<>();

final Collection<Portlet> portlets = portletAPI.findAllPortlets();
for(final Portlet portlet: portlets) {
if(portletAPI.canAddPortletToLayout(portlet)) {
Map<String, Object> portletMap = new HashMap<>();
String portletTitle = LanguageUtil.get(uWebAPI.getLoggedInUser(request),"com.dotcms.repackage.javax.portlet.title." + portlet.getPortletId());
portletMap.put("title", portletTitle);
portletMap.put("id", portlet.getPortletId());
listOfPortletsInfo.add(portletMap);
@SuppressWarnings("unused")
public List<Map<String, Object>> getAllAvailablePortletInfoList() throws SystemException,
LanguageException {
final HttpServletRequest request = DwrUtil.getHttpServletRequest();
final List<Map<String, Object>> listOfPortletsInfo = new ArrayList<>();
final Collection<Portlet> portlets = this.portletAPI.findAllPortlets();
for (final Portlet portlet : portlets) {
if (LANGUAGES.name().equalsIgnoreCase(portlet.getPortletId())
&& Boolean.TRUE.equals(HIDE_OLD_LANGUAGES_PORTLET.get())) {
continue;
}
}
Collections.sort(listOfPortletsInfo, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return ((String)o1.get("title")).compareTo(((String)o2.get("title")));
if (this.portletAPI.canAddPortletToLayout(portlet)) {
final String portletTitle = LanguageUtil.get(this.userWebAPI.getLoggedInUser(request),
"com.dotcms.repackage.javax.portlet.title." + portlet.getPortletId());
listOfPortletsInfo.add(Map.of(
"title", portletTitle,
"id", portlet.getPortletId()
));
}
});

}
listOfPortletsInfo.sort(Comparator.comparing(o -> ((String) o.get("title")).toLowerCase()));
return listOfPortletsInfo;
}

Expand Down