001    /**
002     * Copyright (c) 2000-2013 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.model.Layout;
021    import com.liferay.portal.model.LayoutTypePortlet;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.portal.model.PortletCategory;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.service.PortletLocalServiceUtil;
027    import com.liferay.portal.service.permission.PortletPermissionUtil;
028    
029    import java.util.HashSet;
030    import java.util.Set;
031    
032    /**
033     * @author Eudaldo Alonso
034     */
035    public class PortletCategoryUtil {
036    
037            public static PortletCategory getRelevantPortletCategory(
038                            PermissionChecker permissionChecker, long companyId, Layout layout,
039                            PortletCategory portletCategory,
040                            LayoutTypePortlet layoutTypePortlet)
041                    throws Exception {
042    
043                    UnicodeProperties typeSettingsProperties =
044                            layout.getTypeSettingsProperties();
045    
046                    Set<String> panelSelectedPortletIds = SetUtil.fromArray(
047                            StringUtil.split(
048                                    typeSettingsProperties.getProperty("panelSelectedPortlets")));
049    
050                    return getRelevantPortletCategory(
051                            permissionChecker, companyId, layout, portletCategory,
052                            panelSelectedPortletIds, layoutTypePortlet);
053            }
054    
055            protected static PortletCategory getRelevantPortletCategory(
056                            PermissionChecker permissionChecker, long companyId, Layout layout,
057                            PortletCategory portletCategory,
058                            Set<String> panelSelectedPortletIds,
059                            LayoutTypePortlet layoutTypePortlet)
060                    throws Exception {
061    
062                    PortletCategory relevantPortletCategory = new PortletCategory(
063                            portletCategory.getName(), portletCategory.getPortletIds());
064    
065                    for (PortletCategory curPortletCategory :
066                                    portletCategory.getCategories()) {
067    
068                            Set<String> portletIds = new HashSet<String>();
069    
070                            if (curPortletCategory.isHidden()) {
071                                    continue;
072                            }
073    
074                            for (String portletId : curPortletCategory.getPortletIds()) {
075                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
076                                            companyId, portletId);
077    
078                                    if (portlet != null) {
079                                            if (portlet.isSystem()) {
080                                            }
081                                            else if (!portlet.isActive() ||
082                                                             portlet.isUndeployedPortlet()) {
083                                            }
084                                            else if (layout.isTypePanel() &&
085                                                             panelSelectedPortletIds.contains(
086                                                                            portlet.getRootPortletId())) {
087    
088                                                    portletIds.add(portlet.getPortletId());
089                                            }
090                                            else if (layout.isTypePanel() &&
091                                                             !panelSelectedPortletIds.contains(
092                                                                            portlet.getRootPortletId())) {
093                                            }
094                                            else if (!PortletPermissionUtil.contains(
095                                                                    permissionChecker, layout, portlet,
096                                                                    ActionKeys.ADD_TO_PAGE)) {
097                                            }
098                                            else if (!portlet.isInstanceable() &&
099                                                             layoutTypePortlet.hasPortletId(
100                                                                            portlet.getPortletId())) {
101    
102                                                    portletIds.add(portlet.getPortletId());
103                                            }
104                                            else {
105                                                    portletIds.add(portlet.getPortletId());
106                                            }
107                                    }
108                            }
109    
110                            PortletCategory curRelevantPortletCategory =
111                                    getRelevantPortletCategory(
112                                            permissionChecker, companyId, layout, curPortletCategory,
113                                            panelSelectedPortletIds, layoutTypePortlet);
114    
115                            curRelevantPortletCategory.setPortletIds(portletIds);
116    
117                            if (!curRelevantPortletCategory.getCategories().isEmpty() ||
118                                    !portletIds.isEmpty()) {
119    
120                                    relevantPortletCategory.addCategory(curRelevantPortletCategory);
121                            }
122                    }
123    
124                    return relevantPortletCategory;
125            }
126    
127    }