1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.language.LanguageUtil;
28  import com.liferay.portal.kernel.util.ListUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.LayoutTypePortlet;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.PortletApp;
33  import com.liferay.portal.model.PortletCategory;
34  import com.liferay.portal.model.User;
35  import com.liferay.portal.service.PortletLocalServiceUtil;
36  import com.liferay.portal.util.comparator.PortletCategoryComparator;
37  import com.liferay.portal.util.comparator.PortletTitleComparator;
38  import com.liferay.portlet.PortletConfigFactory;
39  
40  import java.util.ArrayList;
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.MissingResourceException;
44  import java.util.ResourceBundle;
45  import java.util.Set;
46  
47  import javax.portlet.PortletConfig;
48  
49  import javax.servlet.ServletContext;
50  
51  /**
52   * <a href="PortletLister.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Jorge Ferrer
55   *
56   */
57  public class PortletLister {
58  
59      public TreeView getTreeView(
60              LayoutTypePortlet layoutTypePortlet, String rootNodeName, User user,
61              ServletContext servletContext)
62          throws PortalException, SystemException {
63  
64          _layoutTypePortlet = layoutTypePortlet;
65          _user = user;
66          _servletContext = servletContext;
67          _nodeId = 1;
68  
69          _list = new ArrayList<TreeNodeView>();
70  
71          TreeNodeView rootNodeView = new TreeNodeView(_nodeId);
72  
73          rootNodeView.setName(rootNodeName);
74  
75          _list.add(rootNodeView);
76  
77          PortletCategory portletCategory = (PortletCategory)WebAppPool.get(
78              String.valueOf(user.getCompanyId()), WebKeys.PORTLET_CATEGORY);
79  
80          List<PortletCategory> categories = ListUtil.fromCollection(
81              portletCategory.getCategories());
82  
83          _iterateCategories(categories, _nodeId, 0);
84  
85          return new TreeView(_list, _depth);
86      }
87  
88      public boolean isIncludeInstanceablePortlets() {
89          return _includeInstanceablePortlets;
90      }
91  
92      public void setIncludeInstanceablePortlets(
93          boolean includeInstanceablePortlets) {
94  
95          _includeInstanceablePortlets = includeInstanceablePortlets;
96      }
97  
98      private void _iterateCategories(
99              List<PortletCategory> categories, long parentId, int depth)
100         throws PortalException, SystemException {
101 
102         categories = ListUtil.sort(
103             categories,
104             new PortletCategoryComparator(
105                 _user.getCompanyId(), _user.getLocale()));
106 
107         Iterator<PortletCategory> itr = categories.iterator();
108 
109         for (int i = 0; itr.hasNext(); i++) {
110             PortletCategory portletCategory = itr.next();
111 
112             if (i == 0) {
113                 depth++;
114 
115                 if (depth > _depth) {
116                     _depth = depth;
117                 }
118             }
119 
120             TreeNodeView nodeView = new TreeNodeView(++_nodeId);
121 
122             nodeView.setDepth(depth);
123 
124             if ((i + 1) == categories.size()) {
125                 nodeView.setLs("1");
126             }
127             else {
128                 nodeView.setLs("0");
129             }
130 
131             nodeView.setName(
132                 LanguageUtil.get(_user.getLocale(), portletCategory.getName()));
133             nodeView.setParentId(parentId);
134 
135             _list.add(nodeView);
136 
137             List<PortletCategory> subCategories = ListUtil.fromCollection(
138                 portletCategory.getCategories());
139 
140             _iterateCategories(subCategories, _nodeId, depth);
141 
142             _iteratePortlets(
143                 portletCategory, portletCategory.getPortletIds(), _nodeId,
144                 depth + 1);
145         }
146     }
147 
148     private void _iteratePortlets(
149             PortletCategory portletCategory, Set<String> portletIds,
150             int parentNodeId, int depth)
151         throws SystemException {
152 
153         List<Portlet> portlets = new ArrayList<Portlet>();
154 
155         Iterator<String> portletIdsItr = portletIds.iterator();
156 
157         String externalPortletCategory = null;
158 
159         while (portletIdsItr.hasNext()) {
160             String portletId = portletIdsItr.next();
161 
162             Portlet portlet = PortletLocalServiceUtil.getPortletById(
163                 _user.getCompanyId(), portletId);
164 
165             if (portlet != null) {
166                 if (portlet.isSystem()) {
167                 }
168                 else if (!portlet.isActive()) {
169                 }
170                 else if (portlet.isInstanceable() &&
171                          !_includeInstanceablePortlets) {
172                 }
173                 else if (!portlet.isInstanceable() &&
174                         _layoutTypePortlet.hasPortletId(
175                             portlet.getPortletId())) {
176 
177                     portlets.add(portlet);
178                 }
179                 else if (!portlet.hasAddPortletPermission(_user.getUserId())) {
180                 }
181                 else {
182                     portlets.add(portlet);
183                 }
184 
185                 PortletApp portletApp = portlet.getPortletApp();
186 
187                 if (portletApp.isWARFile() &&
188                         Validator.isNull(externalPortletCategory)) {
189                     PortletConfig portletConfig = PortletConfigFactory.create(
190                         portlet, _servletContext);
191 
192                     ResourceBundle resourceBundle =
193                         portletConfig.getResourceBundle(_user.getLocale());
194 
195                     try {
196                         externalPortletCategory =
197                             resourceBundle.getString(portletCategory.getName());
198                     }
199                     catch (MissingResourceException mre) {
200                     }
201                 }
202             }
203         }
204 
205         portlets = ListUtil.sort(
206             portlets,
207             new PortletTitleComparator(
208                 _user.getCompanyId(), _user.getLocale()));
209 
210         Iterator<Portlet> portletsItr = portlets.iterator();
211 
212         for (int i = 0; portletsItr.hasNext(); i++) {
213             Portlet portlet = portletsItr.next();
214 
215             TreeNodeView nodeView = new TreeNodeView(++_nodeId);
216 
217             nodeView.setDepth(depth);
218 
219             if ((i + 1) == portlets.size()) {
220                 nodeView.setLs("1");
221             }
222             else {
223                 nodeView.setLs("0");
224             }
225 
226             nodeView.setName(PortalUtil.getPortletTitle(portlet, _user));
227             nodeView.setObjId(portlet.getRootPortletId());
228             nodeView.setParentId(parentNodeId);
229 
230             _list.add(nodeView);
231         }
232     }
233 
234     private LayoutTypePortlet _layoutTypePortlet;
235     private User _user;
236     private ServletContext _servletContext;
237     private int _nodeId;
238     private List<TreeNodeView> _list;
239     private int _depth;
240     private boolean _includeInstanceablePortlets;
241 
242 }