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