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