1
14
15 package com.liferay.portal.util;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.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 PortalException, 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
185 PortletConfig portletConfig = PortletConfigFactory.create(
186 portlet, _servletContext);
187
188 ResourceBundle resourceBundle =
189 portletConfig.getResourceBundle(_user.getLocale());
190
191 try {
192 externalPortletCategory = resourceBundle.getString(
193 portletCategory.getName());
194 }
195 catch (MissingResourceException mre) {
196 }
197 }
198 }
199 }
200
201 portlets = ListUtil.sort(
202 portlets, new PortletTitleComparator(_user.getLocale()));
203
204 Iterator<Portlet> portletsItr = portlets.iterator();
205
206 for (int i = 0; portletsItr.hasNext(); i++) {
207 Portlet portlet = portletsItr.next();
208
209 TreeNodeView nodeView = new TreeNodeView(++_nodeId);
210
211 nodeView.setDepth(depth);
212
213 if ((i + 1) == portlets.size()) {
214 nodeView.setLs("1");
215 }
216 else {
217 nodeView.setLs("0");
218 }
219
220 nodeView.setName(PortalUtil.getPortletTitle(portlet, _user));
221 nodeView.setObjId(portlet.getRootPortletId());
222 nodeView.setParentId(parentNodeId);
223
224 _list.add(nodeView);
225 }
226 }
227
228 private LayoutTypePortlet _layoutTypePortlet;
229 private User _user;
230 private ServletContext _servletContext;
231 private int _nodeId;
232 private List<TreeNodeView> _list;
233 private int _depth;
234 private boolean _includeInstanceablePortlets;
235
236 }