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