1
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
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();) {
99 PortletCategory portletCategory = itr.next();
100
101 if (portletCategory.isHidden()) {
102 continue;
103 }
104
105 if (i == 0) {
106 depth++;
107
108 if (depth > _depth) {
109 _depth = depth;
110 }
111 }
112
113 TreeNodeView nodeView = new TreeNodeView(++_nodeId);
114
115 nodeView.setDepth(depth);
116
117 if ((i + 1) == categories.size()) {
118 nodeView.setLs("1");
119 }
120 else {
121 nodeView.setLs("0");
122 }
123
124 nodeView.setName(
125 LanguageUtil.get(_user.getLocale(), portletCategory.getName()));
126 nodeView.setParentId(parentId);
127
128 _list.add(nodeView);
129
130 List<PortletCategory> subCategories = ListUtil.fromCollection(
131 portletCategory.getCategories());
132
133 _iterateCategories(subCategories, _nodeId, depth);
134
135 _iteratePortlets(
136 portletCategory, portletCategory.getPortletIds(), _nodeId,
137 depth + 1);
138
139 i++;
140 }
141 }
142
143 private void _iteratePortlets(
144 PortletCategory portletCategory, Set<String> portletIds,
145 int parentNodeId, int depth)
146 throws SystemException {
147
148 List<Portlet> portlets = new ArrayList<Portlet>();
149
150 Iterator<String> portletIdsItr = portletIds.iterator();
151
152 String externalPortletCategory = null;
153
154 while (portletIdsItr.hasNext()) {
155 String portletId = portletIdsItr.next();
156
157 Portlet portlet = PortletLocalServiceUtil.getPortletById(
158 _user.getCompanyId(), portletId);
159
160 if (portlet != null) {
161 if (portlet.isSystem()) {
162 }
163 else if (!portlet.isActive()) {
164 }
165 else if (portlet.isInstanceable() &&
166 !_includeInstanceablePortlets) {
167 }
168 else if (!portlet.isInstanceable() &&
169 _layoutTypePortlet.hasPortletId(
170 portlet.getPortletId())) {
171
172 portlets.add(portlet);
173 }
174 else if (!portlet.hasAddPortletPermission(_user.getUserId())) {
175 }
176 else {
177 portlets.add(portlet);
178 }
179
180 PortletApp portletApp = portlet.getPortletApp();
181
182 if (portletApp.isWARFile() &&
183 Validator.isNull(externalPortletCategory)) {
184 PortletConfig portletConfig = PortletConfigFactory.create(
185 portlet, _servletContext);
186
187 ResourceBundle resourceBundle =
188 portletConfig.getResourceBundle(_user.getLocale());
189
190 try {
191 externalPortletCategory = resourceBundle.getString(
192 portletCategory.getName());
193 }
194 catch (MissingResourceException mre) {
195 }
196 }
197 }
198 }
199
200 portlets = ListUtil.sort(
201 portlets, new PortletTitleComparator(_user.getLocale()));
202
203 Iterator<Portlet> portletsItr = portlets.iterator();
204
205 for (int i = 0; portletsItr.hasNext(); i++) {
206 Portlet portlet = portletsItr.next();
207
208 TreeNodeView nodeView = new TreeNodeView(++_nodeId);
209
210 nodeView.setDepth(depth);
211
212 if ((i + 1) == portlets.size()) {
213 nodeView.setLs("1");
214 }
215 else {
216 nodeView.setLs("0");
217 }
218
219 nodeView.setName(PortalUtil.getPortletTitle(portlet, _user));
220 nodeView.setObjId(portlet.getRootPortletId());
221 nodeView.setParentId(parentNodeId);
222
223 _list.add(nodeView);
224 }
225 }
226
227 private LayoutTypePortlet _layoutTypePortlet;
228 private User _user;
229 private ServletContext _servletContext;
230 private int _nodeId;
231 private List<TreeNodeView> _list;
232 private int _depth;
233 private boolean _includeInstanceablePortlets;
234
235 }