1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.language.LanguageUtil;
20  import com.liferay.portal.kernel.util.ListUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.LayoutTypePortlet;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.model.PortletApp;
25  import com.liferay.portal.model.PortletCategory;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.service.PortletLocalServiceUtil;
28  import com.liferay.portal.util.comparator.PortletCategoryComparator;
29  import com.liferay.portal.util.comparator.PortletTitleComparator;
30  import com.liferay.portlet.PortletConfigFactory;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.MissingResourceException;
36  import java.util.ResourceBundle;
37  import java.util.Set;
38  
39  import javax.portlet.PortletConfig;
40  
41  import javax.servlet.ServletContext;
42  
43  /**
44   * <a href="PortletLister.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Jorge Ferrer
47   */
48  public class PortletLister {
49  
50      public TreeView getTreeView(
51              LayoutTypePortlet layoutTypePortlet, String rootNodeName, User user,
52              ServletContext servletContext)
53          throws PortalException, SystemException {
54  
55          _layoutTypePortlet = layoutTypePortlet;
56          _user = user;
57          _servletContext = servletContext;
58          _nodeId = 1;
59  
60          _list = new ArrayList<TreeNodeView>();
61  
62          TreeNodeView rootNodeView = new TreeNodeView(_nodeId);
63  
64          rootNodeView.setName(rootNodeName);
65  
66          _list.add(rootNodeView);
67  
68          PortletCategory portletCategory = (PortletCategory)WebAppPool.get(
69              String.valueOf(user.getCompanyId()), WebKeys.PORTLET_CATEGORY);
70  
71          List<PortletCategory> categories = ListUtil.fromCollection(
72              portletCategory.getCategories());
73  
74          _iterateCategories(categories, _nodeId, 0);
75  
76          return new TreeView(_list, _depth);
77      }
78  
79      public boolean isIncludeInstanceablePortlets() {
80          return _includeInstanceablePortlets;
81      }
82  
83      public void setIncludeInstanceablePortlets(
84          boolean includeInstanceablePortlets) {
85  
86          _includeInstanceablePortlets = includeInstanceablePortlets;
87      }
88  
89      private void _iterateCategories(
90              List<PortletCategory> categories, long parentId, int depth)
91          throws PortalException, SystemException {
92  
93          categories = ListUtil.sort(
94              categories, new PortletCategoryComparator(_user.getLocale()));
95  
96          Iterator<PortletCategory> itr = categories.iterator();
97  
98          for (int i = 0; itr.hasNext(); i++) {
99              PortletCategory portletCategory = itr.next();
100 
101             if (i == 0) {
102                 depth++;
103 
104                 if (depth > _depth) {
105                     _depth = depth;
106                 }
107             }
108 
109             TreeNodeView nodeView = new TreeNodeView(++_nodeId);
110 
111             nodeView.setDepth(depth);
112 
113             if ((i + 1) == categories.size()) {
114                 nodeView.setLs("1");
115             }
116             else {
117                 nodeView.setLs("0");
118             }
119 
120             nodeView.setName(
121                 LanguageUtil.get(_user.getLocale(), portletCategory.getName()));
122             nodeView.setParentId(parentId);
123 
124             _list.add(nodeView);
125 
126             List<PortletCategory> subCategories = ListUtil.fromCollection(
127                 portletCategory.getCategories());
128 
129             _iterateCategories(subCategories, _nodeId, depth);
130 
131             _iteratePortlets(
132                 portletCategory, portletCategory.getPortletIds(), _nodeId,
133                 depth + 1);
134         }
135     }
136 
137     private void _iteratePortlets(
138             PortletCategory portletCategory, Set<String> portletIds,
139             int parentNodeId, int depth)
140         throws SystemException {
141 
142         List<Portlet> portlets = new ArrayList<Portlet>();
143 
144         Iterator<String> portletIdsItr = portletIds.iterator();
145 
146         String externalPortletCategory = null;
147 
148         while (portletIdsItr.hasNext()) {
149             String portletId = portletIdsItr.next();
150 
151             Portlet portlet = PortletLocalServiceUtil.getPortletById(
152                 _user.getCompanyId(), portletId);
153 
154             if (portlet != null) {
155                 if (portlet.isSystem()) {
156                 }
157                 else if (!portlet.isActive()) {
158                 }
159                 else if (portlet.isInstanceable() &&
160                          !_includeInstanceablePortlets) {
161                 }
162                 else if (!portlet.isInstanceable() &&
163                         _layoutTypePortlet.hasPortletId(
164                             portlet.getPortletId())) {
165 
166                     portlets.add(portlet);
167                 }
168                 else if (!portlet.hasAddPortletPermission(_user.getUserId())) {
169                 }
170                 else {
171                     portlets.add(portlet);
172                 }
173 
174                 PortletApp portletApp = portlet.getPortletApp();
175 
176                 if (portletApp.isWARFile() &&
177                         Validator.isNull(externalPortletCategory)) {
178                     PortletConfig portletConfig = PortletConfigFactory.create(
179                         portlet, _servletContext);
180 
181                     ResourceBundle resourceBundle =
182                         portletConfig.getResourceBundle(_user.getLocale());
183 
184                     try {
185                         externalPortletCategory = resourceBundle.getString(
186                             portletCategory.getName());
187                     }
188                     catch (MissingResourceException mre) {
189                     }
190                 }
191             }
192         }
193 
194         portlets = ListUtil.sort(
195             portlets, new PortletTitleComparator(_user.getLocale()));
196 
197         Iterator<Portlet> portletsItr = portlets.iterator();
198 
199         for (int i = 0; portletsItr.hasNext(); i++) {
200             Portlet portlet = portletsItr.next();
201 
202             TreeNodeView nodeView = new TreeNodeView(++_nodeId);
203 
204             nodeView.setDepth(depth);
205 
206             if ((i + 1) == portlets.size()) {
207                 nodeView.setLs("1");
208             }
209             else {
210                 nodeView.setLs("0");
211             }
212 
213             nodeView.setName(PortalUtil.getPortletTitle(portlet, _user));
214             nodeView.setObjId(portlet.getRootPortletId());
215             nodeView.setParentId(parentNodeId);
216 
217             _list.add(nodeView);
218         }
219     }
220 
221     private LayoutTypePortlet _layoutTypePortlet;
222     private User _user;
223     private ServletContext _servletContext;
224     private int _nodeId;
225     private List<TreeNodeView> _list;
226     private int _depth;
227     private boolean _includeInstanceablePortlets;
228 
229 }