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.util.SetUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.UnicodeProperties;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.LayoutTypePortlet;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.model.PortletCategory;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.PortletLocalServiceUtil;
028    import com.liferay.portal.service.permission.PortletPermissionUtil;
029    
030    import java.util.HashSet;
031    import java.util.Set;
032    
033    /**
034     * @author Eudaldo Alonso
035     */
036    public class PortletCategoryUtil {
037    
038            public static String getPortletCategoryKey(
039                    String legacyPortletCategoryKey) {
040    
041                    if (Validator.equals(legacyPortletCategoryKey, "apps") ||
042                            Validator.equals(legacyPortletCategoryKey, "marketplace")) {
043    
044                            return PortletCategoryKeys.CONTROL_PANEL_APPS;
045                    }
046                    else if (Validator.equals(legacyPortletCategoryKey, "configuration") ||
047                                     Validator.equals(legacyPortletCategoryKey, "portal") ||
048                                     Validator.equals(legacyPortletCategoryKey, "server")) {
049    
050                            return PortletCategoryKeys.CONTROL_PANEL_CONFIGURATION;
051                    }
052                    else if (Validator.equals(legacyPortletCategoryKey, "content")) {
053                            return PortletCategoryKeys.SITE_ADMINISTRATION_CONTENT;
054                    }
055                    else if (Validator.equals(legacyPortletCategoryKey, "my")) {
056                            return PortletCategoryKeys.USER_MY_ACCOUNT;
057                    }
058                    else if (Validator.equals(legacyPortletCategoryKey, "sites")) {
059                            return PortletCategoryKeys.CONTROL_PANEL_SITES;
060                    }
061                    else if (Validator.equals(legacyPortletCategoryKey, "users")) {
062                            return PortletCategoryKeys.CONTROL_PANEL_USERS;
063                    }
064    
065                    return legacyPortletCategoryKey;
066            }
067    
068            public static PortletCategory getRelevantPortletCategory(
069                            PermissionChecker permissionChecker, long companyId, Layout layout,
070                            PortletCategory portletCategory,
071                            LayoutTypePortlet layoutTypePortlet)
072                    throws Exception {
073    
074                    UnicodeProperties typeSettingsProperties =
075                            layout.getTypeSettingsProperties();
076    
077                    Set<String> panelSelectedPortletIds = SetUtil.fromArray(
078                            StringUtil.split(
079                                    typeSettingsProperties.getProperty("panelSelectedPortlets")));
080    
081                    return getRelevantPortletCategory(
082                            permissionChecker, companyId, layout, portletCategory,
083                            panelSelectedPortletIds, layoutTypePortlet);
084            }
085    
086            protected static PortletCategory getRelevantPortletCategory(
087                            PermissionChecker permissionChecker, long companyId, Layout layout,
088                            PortletCategory portletCategory,
089                            Set<String> panelSelectedPortletIds,
090                            LayoutTypePortlet layoutTypePortlet)
091                    throws Exception {
092    
093                    PortletCategory relevantPortletCategory = new PortletCategory(
094                            portletCategory.getName(), portletCategory.getPortletIds());
095    
096                    for (PortletCategory curPortletCategory :
097                                    portletCategory.getCategories()) {
098    
099                            Set<String> portletIds = new HashSet<>();
100    
101                            if (curPortletCategory.isHidden()) {
102                                    continue;
103                            }
104    
105                            for (String portletId : curPortletCategory.getPortletIds()) {
106                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
107                                            companyId, portletId);
108    
109                                    if (portlet != null) {
110                                            if (portlet.isSystem()) {
111                                            }
112                                            else if (!portlet.isActive() ||
113                                                             portlet.isUndeployedPortlet()) {
114                                            }
115                                            else if (layout.isTypePanel() &&
116                                                             panelSelectedPortletIds.contains(
117                                                                     portlet.getRootPortletId())) {
118    
119                                                    portletIds.add(portlet.getPortletId());
120                                            }
121                                            else if (layout.isTypePanel() &&
122                                                             !panelSelectedPortletIds.contains(
123                                                                     portlet.getRootPortletId())) {
124                                            }
125                                            else if (!PortletPermissionUtil.contains(
126                                                                    permissionChecker, layout, portlet,
127                                                                    ActionKeys.ADD_TO_PAGE)) {
128                                            }
129                                            else if (!portlet.isInstanceable() &&
130                                                             layoutTypePortlet.hasPortletId(
131                                                                     portlet.getPortletId())) {
132    
133                                                    portletIds.add(portlet.getPortletId());
134                                            }
135                                            else {
136                                                    portletIds.add(portlet.getPortletId());
137                                            }
138                                    }
139                            }
140    
141                            PortletCategory curRelevantPortletCategory =
142                                    getRelevantPortletCategory(
143                                            permissionChecker, companyId, layout, curPortletCategory,
144                                            panelSelectedPortletIds, layoutTypePortlet);
145    
146                            curRelevantPortletCategory.setPortletIds(portletIds);
147    
148                            if (!curRelevantPortletCategory.getCategories().isEmpty() ||
149                                    !portletIds.isEmpty()) {
150    
151                                    relevantPortletCategory.addCategory(curRelevantPortletCategory);
152                            }
153                    }
154    
155                    return relevantPortletCategory;
156            }
157    
158    }