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