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