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