001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.model.Layout;
018    import com.liferay.portal.kernel.model.LayoutTypePortlet;
019    import com.liferay.portal.kernel.model.Portlet;
020    import com.liferay.portal.kernel.model.PortletCategory;
021    import com.liferay.portal.kernel.security.permission.ActionKeys;
022    import com.liferay.portal.kernel.security.permission.PermissionChecker;
023    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
024    import com.liferay.portal.kernel.service.permission.PortletPermissionUtil;
025    import com.liferay.portal.kernel.util.PortletCategoryKeys;
026    import com.liferay.portal.kernel.util.SetUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    
030    import java.util.HashSet;
031    import java.util.Objects;
032    import java.util.Set;
033    
034    /**
035     * @author Eudaldo Alonso
036     */
037    public class PortletCategoryUtil {
038    
039            public static String getPortletCategoryKey(
040                    String legacyPortletCategoryKey) {
041    
042                    if (Objects.equals(legacyPortletCategoryKey, "apps") ||
043                            Objects.equals(legacyPortletCategoryKey, "marketplace")) {
044    
045                            return PortletCategoryKeys.CONTROL_PANEL_APPS;
046                    }
047                    else if (Objects.equals(legacyPortletCategoryKey, "configuration") ||
048                                     Objects.equals(legacyPortletCategoryKey, "portal") ||
049                                     Objects.equals(legacyPortletCategoryKey, "server")) {
050    
051                            return PortletCategoryKeys.CONTROL_PANEL_CONFIGURATION;
052                    }
053                    else if (Objects.equals(legacyPortletCategoryKey, "content")) {
054                            return PortletCategoryKeys.SITE_ADMINISTRATION_CONTENT;
055                    }
056                    else if (Objects.equals(legacyPortletCategoryKey, "my")) {
057                            return PortletCategoryKeys.USER_MY_ACCOUNT;
058                    }
059                    else if (Objects.equals(legacyPortletCategoryKey, "sites")) {
060                            return PortletCategoryKeys.CONTROL_PANEL_SITES;
061                    }
062                    else if (Objects.equals(legacyPortletCategoryKey, "users")) {
063                            return PortletCategoryKeys.CONTROL_PANEL_USERS;
064                    }
065    
066                    return legacyPortletCategoryKey;
067            }
068    
069            public static PortletCategory getRelevantPortletCategory(
070                            PermissionChecker permissionChecker, long companyId, Layout layout,
071                            PortletCategory portletCategory,
072                            LayoutTypePortlet layoutTypePortlet)
073                    throws Exception {
074    
075                    UnicodeProperties typeSettingsProperties =
076                            layout.getTypeSettingsProperties();
077    
078                    Set<String> panelSelectedPortletIds = SetUtil.fromArray(
079                            StringUtil.split(
080                                    typeSettingsProperties.getProperty("panelSelectedPortlets")));
081    
082                    return getRelevantPortletCategory(
083                            permissionChecker, companyId, layout, portletCategory,
084                            panelSelectedPortletIds, layoutTypePortlet);
085            }
086    
087            protected static PortletCategory getRelevantPortletCategory(
088                            PermissionChecker permissionChecker, long companyId, Layout layout,
089                            PortletCategory portletCategory,
090                            Set<String> panelSelectedPortletIds,
091                            LayoutTypePortlet layoutTypePortlet)
092                    throws Exception {
093    
094                    PortletCategory relevantPortletCategory = new PortletCategory(
095                            portletCategory.getName(), portletCategory.getPortletIds());
096    
097                    for (PortletCategory curPortletCategory :
098                                    portletCategory.getCategories()) {
099    
100                            Set<String> portletIds = new HashSet<>();
101    
102                            if (curPortletCategory.isHidden()) {
103                                    continue;
104                            }
105    
106                            for (String portletId : curPortletCategory.getPortletIds()) {
107                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
108                                            companyId, portletId);
109    
110                                    if (portlet != null) {
111                                            if (portlet.isSystem()) {
112                                            }
113                                            else if (!portlet.isActive() ||
114                                                             portlet.isUndeployedPortlet()) {
115                                            }
116                                            else if (layout.isTypePanel() &&
117                                                             panelSelectedPortletIds.contains(
118                                                                     portlet.getRootPortletId())) {
119    
120                                                    portletIds.add(portlet.getPortletId());
121                                            }
122                                            else if (layout.isTypePanel() &&
123                                                             !panelSelectedPortletIds.contains(
124                                                                     portlet.getRootPortletId())) {
125                                            }
126                                            else if (!PortletPermissionUtil.contains(
127                                                                    permissionChecker, layout, portlet,
128                                                                    ActionKeys.ADD_TO_PAGE)) {
129                                            }
130                                            else if (!portlet.isInstanceable() &&
131                                                             layoutTypePortlet.hasPortletId(
132                                                                     portlet.getPortletId())) {
133    
134                                                    portletIds.add(portlet.getPortletId());
135                                            }
136                                            else {
137                                                    portletIds.add(portlet.getPortletId());
138                                            }
139                                    }
140                            }
141    
142                            PortletCategory curRelevantPortletCategory =
143                                    getRelevantPortletCategory(
144                                            permissionChecker, companyId, layout, curPortletCategory,
145                                            panelSelectedPortletIds, layoutTypePortlet);
146    
147                            curRelevantPortletCategory.setPortletIds(portletIds);
148    
149                            if (!curRelevantPortletCategory.getCategories().isEmpty() ||
150                                    !portletIds.isEmpty()) {
151    
152                                    relevantPortletCategory.addCategory(curRelevantPortletCategory);
153                            }
154                    }
155    
156                    return relevantPortletCategory;
157            }
158    
159    }