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