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, new PortletCategoryComparator(_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 = resourceBundle.getString(
194                             portletCategory.getName());
195                     }
196                     catch (MissingResourceException mre) {
197                     }
198                 }
199             }
200         }
201 
202         portlets = ListUtil.sort(
203             portlets, new PortletTitleComparator(_user.getLocale()));
204 
205         Iterator<Portlet> portletsItr = portlets.iterator();
206 
207         for (int i = 0; portletsItr.hasNext(); i++) {
208             Portlet portlet = portletsItr.next();
209 
210             TreeNodeView nodeView = new TreeNodeView(++_nodeId);
211 
212             nodeView.setDepth(depth);
213 
214             if ((i + 1) == portlets.size()) {
215                 nodeView.setLs("1");
216             }
217             else {
218                 nodeView.setLs("0");
219             }
220 
221             nodeView.setName(PortalUtil.getPortletTitle(portlet, _user));
222             nodeView.setObjId(portlet.getRootPortletId());
223             nodeView.setParentId(parentNodeId);
224 
225             _list.add(nodeView);
226         }
227     }
228 
229     private LayoutTypePortlet _layoutTypePortlet;
230     private User _user;
231     private ServletContext _servletContext;
232     private int _nodeId;
233     private List<TreeNodeView> _list;
234     private int _depth;
235     private boolean _includeInstanceablePortlets;
236 
237 }