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