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.exception.PortalException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.model.LayoutTypePortlet;
020    import com.liferay.portal.kernel.model.Portlet;
021    import com.liferay.portal.kernel.model.PortletApp;
022    import com.liferay.portal.kernel.model.PortletCategory;
023    import com.liferay.portal.kernel.model.User;
024    import com.liferay.portal.kernel.portlet.PortletConfigFactoryUtil;
025    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
026    import com.liferay.portal.kernel.theme.ThemeDisplay;
027    import com.liferay.portal.kernel.util.ListUtil;
028    import com.liferay.portal.kernel.util.PortalUtil;
029    import com.liferay.portal.kernel.util.PortletLister;
030    import com.liferay.portal.kernel.util.ResourceBundleUtil;
031    import com.liferay.portal.kernel.util.TreeNodeView;
032    import com.liferay.portal.kernel.util.TreeView;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.util.WebKeys;
035    import com.liferay.portal.kernel.util.comparator.PortletCategoryComparator;
036    import com.liferay.portal.kernel.util.comparator.PortletTitleComparator;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    import java.util.Locale;
041    import java.util.ResourceBundle;
042    import java.util.Set;
043    
044    import javax.portlet.PortletConfig;
045    
046    import javax.servlet.ServletContext;
047    
048    /**
049     * @author Jorge Ferrer
050     * @author Dennis Ju
051     * @author Brian Wing Shun Chan
052     */
053    public class PortletListerImpl implements PortletLister {
054    
055            @Override
056            public TreeView getTreeView() throws PortalException {
057                    _nodeId = 1;
058    
059                    _list = new ArrayList<>();
060    
061                    TreeNodeView rootNodeView = null;
062    
063                    if (_rootNodeName != null) {
064                            rootNodeView = new TreeNodeView(_nodeId);
065    
066                            rootNodeView.setLeaf(false);
067                            rootNodeView.setName(_rootNodeName);
068    
069                            _list.add(rootNodeView);
070                    }
071    
072                    PortletCategory portletCategory = (PortletCategory)WebAppPool.get(
073                            _user.getCompanyId(), WebKeys.PORTLET_CATEGORY);
074    
075                    List<PortletCategory> portletCategories = ListUtil.fromCollection(
076                            portletCategory.getCategories());
077    
078                    iteratePortletCategories(rootNodeView, portletCategories, _nodeId, 0);
079    
080                    return new TreeView(_list, _depth);
081            }
082    
083            @Override
084            public void setHierarchicalTree(boolean hierarchicalTree) {
085                    _hierarchicalTree = hierarchicalTree;
086            }
087    
088            @Override
089            public void setIncludeInstanceablePortlets(
090                    boolean includeInstanceablePortlets) {
091    
092                    _includeInstanceablePortlets = includeInstanceablePortlets;
093            }
094    
095            @Override
096            public void setIteratePortlets(boolean iteratePortlets) {
097                    _iteratePortlets = iteratePortlets;
098            }
099    
100            @Override
101            public void setLayoutTypePortlet(LayoutTypePortlet layoutTypePortlet) {
102                    _layoutTypePortlet = layoutTypePortlet;
103            }
104    
105            @Override
106            public void setRootNodeName(String rootNodeName) {
107                    _rootNodeName = rootNodeName;
108            }
109    
110            @Override
111            public void setServletContext(ServletContext servletContext) {
112                    _servletContext = servletContext;
113            }
114    
115            @Override
116            public void setThemeDisplay(ThemeDisplay themeDisplay) {
117                    _themeDisplay = themeDisplay;
118            }
119    
120            @Override
121            public void setUser(User user) {
122                    _user = user;
123            }
124    
125            protected Locale getLocale() {
126                    if (_themeDisplay == null) {
127                            return _user.getLocale();
128                    }
129    
130                    return _themeDisplay.getLocale();
131            }
132    
133            protected void iteratePortletCategories(
134                            TreeNodeView parentNodeView,
135                            List<PortletCategory> portletCategories, long parentId, int depth)
136                    throws PortalException {
137    
138                    portletCategories = ListUtil.sort(
139                            portletCategories, new PortletCategoryComparator(getLocale()));
140    
141                    for (int i = 0; i < portletCategories.size(); i++) {
142                            PortletCategory portletCategory = portletCategories.get(i);
143    
144                            if (portletCategory.isHidden()) {
145                                    continue;
146                            }
147    
148                            if (i == 0) {
149                                    depth++;
150    
151                                    if (depth > _depth) {
152                                            _depth = depth;
153                                    }
154                            }
155    
156                            TreeNodeView nodeView = new TreeNodeView(++_nodeId);
157    
158                            nodeView.setDepth(depth);
159                            nodeView.setLeaf(false);
160    
161                            if ((i + 1) == portletCategories.size()) {
162                                    nodeView.setLs("1");
163                            }
164                            else {
165                                    nodeView.setLs("0");
166                            }
167    
168                            nodeView.setName(
169                                    LanguageUtil.get(getLocale(), portletCategory.getName()));
170                            nodeView.setObjId(portletCategory.getPath());
171                            nodeView.setParentId(parentId);
172    
173                            if (_hierarchicalTree) {
174                                    if (parentNodeView != null) {
175                                            parentNodeView.addChild(nodeView);
176                                    }
177                            }
178                            else {
179                                    _list.add(nodeView);
180                            }
181    
182                            int nodeId = _nodeId;
183    
184                            List<PortletCategory> subCategories = ListUtil.fromCollection(
185                                    portletCategory.getCategories());
186    
187                            iteratePortletCategories(nodeView, subCategories, nodeId, depth);
188    
189                            if (_iteratePortlets) {
190                                    iteratePortlets(
191                                            nodeView, portletCategory, portletCategory.getPortletIds(),
192                                            nodeId, depth + 1);
193                            }
194                    }
195            }
196    
197            protected void iteratePortlets(
198                    TreeNodeView parentNodeView, PortletCategory portletCategory,
199                    Set<String> portletIds, int parentNodeId, int depth) {
200    
201                    List<Portlet> portlets = new ArrayList<>();
202    
203                    String externalPortletCategory = null;
204    
205                    for (String portletId : portletIds) {
206                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
207                                    _user.getCompanyId(), portletId);
208    
209                            if (portlet != null) {
210                                    if (portlet.isSystem()) {
211                                    }
212                                    else if (!portlet.isActive()) {
213                                    }
214                                    else if (portlet.isInstanceable() &&
215                                                     !_includeInstanceablePortlets) {
216                                    }
217                                    else if (!portlet.isInstanceable() &&
218                                                     _layoutTypePortlet.hasPortletId(
219                                                             portlet.getPortletId())) {
220    
221                                            portlets.add(portlet);
222                                    }
223                                    else if (!portlet.hasAddPortletPermission(_user.getUserId())) {
224                                    }
225                                    else {
226                                            portlets.add(portlet);
227                                    }
228    
229                                    PortletApp portletApp = portlet.getPortletApp();
230    
231                                    if (portletApp.isWARFile() &&
232                                            Validator.isNull(externalPortletCategory)) {
233    
234                                            PortletConfig portletConfig =
235                                                    PortletConfigFactoryUtil.create(
236                                                            portlet, _servletContext);
237    
238                                            ResourceBundle resourceBundle =
239                                                    portletConfig.getResourceBundle(getLocale());
240    
241                                            externalPortletCategory = ResourceBundleUtil.getString(
242                                                    resourceBundle, portletCategory.getName());
243                                    }
244                            }
245                    }
246    
247                    portlets = ListUtil.sort(
248                            portlets, new PortletTitleComparator(getLocale()));
249    
250                    for (int i = 0; i < portlets.size(); i++) {
251                            Portlet portlet = portlets.get(i);
252    
253                            TreeNodeView nodeView = new TreeNodeView(++_nodeId);
254    
255                            nodeView.setDepth(depth);
256                            nodeView.setLeaf(true);
257    
258                            if ((i + 1) == portlets.size()) {
259                                    nodeView.setLs("1");
260                            }
261                            else {
262                                    nodeView.setLs("0");
263                            }
264    
265                            nodeView.setName(
266                                    PortalUtil.getPortletTitle(
267                                            portlet, _servletContext, getLocale()));
268                            nodeView.setObjId(portlet.getRootPortletId());
269                            nodeView.setParentId(parentNodeId);
270    
271                            if (_hierarchicalTree) {
272                                    parentNodeView.addChild(nodeView);
273                            }
274                            else {
275                                    _list.add(nodeView);
276                            }
277                    }
278            }
279    
280            private int _depth;
281            private boolean _hierarchicalTree;
282            private boolean _includeInstanceablePortlets;
283            private boolean _iteratePortlets;
284            private LayoutTypePortlet _layoutTypePortlet;
285            private List<TreeNodeView> _list;
286            private int _nodeId;
287            private String _rootNodeName;
288            private ServletContext _servletContext;
289            private ThemeDisplay _themeDisplay;
290            private User _user;
291    
292    }