001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.util.ListUtil;
020 import com.liferay.portal.kernel.util.ResourceBundleUtil;
021 import com.liferay.portal.kernel.util.TreeNodeView;
022 import com.liferay.portal.kernel.util.TreeView;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.kernel.util.WebKeys;
025 import com.liferay.portal.model.LayoutTypePortlet;
026 import com.liferay.portal.model.Portlet;
027 import com.liferay.portal.model.PortletApp;
028 import com.liferay.portal.model.PortletCategory;
029 import com.liferay.portal.model.User;
030 import com.liferay.portal.service.PortletLocalServiceUtil;
031 import com.liferay.portal.theme.ThemeDisplay;
032 import com.liferay.portal.util.comparator.PortletCategoryComparator;
033 import com.liferay.portal.util.comparator.PortletTitleComparator;
034 import com.liferay.portlet.PortletConfigFactoryUtil;
035
036 import java.util.ArrayList;
037 import java.util.List;
038 import java.util.Locale;
039 import java.util.ResourceBundle;
040 import java.util.Set;
041
042 import javax.portlet.PortletConfig;
043
044 import javax.servlet.ServletContext;
045
046
051 public class PortletListerImpl implements PortletLister {
052
053 @Override
054 public TreeView getTreeView() throws PortalException {
055 _nodeId = 1;
056
057 _list = new ArrayList<>();
058
059 TreeNodeView rootNodeView = null;
060
061 if (_rootNodeName != null) {
062 rootNodeView = new TreeNodeView(_nodeId);
063
064 rootNodeView.setLeaf(false);
065 rootNodeView.setName(_rootNodeName);
066
067 _list.add(rootNodeView);
068 }
069
070 PortletCategory portletCategory = (PortletCategory)WebAppPool.get(
071 _user.getCompanyId(), WebKeys.PORTLET_CATEGORY);
072
073 List<PortletCategory> portletCategories = ListUtil.fromCollection(
074 portletCategory.getCategories());
075
076 iteratePortletCategories(rootNodeView, portletCategories, _nodeId, 0);
077
078 return new TreeView(_list, _depth);
079 }
080
081 @Override
082 public void setHierarchicalTree(boolean hierarchicalTree) {
083 _hierarchicalTree = hierarchicalTree;
084 }
085
086 @Override
087 public void setIncludeInstanceablePortlets(
088 boolean includeInstanceablePortlets) {
089
090 _includeInstanceablePortlets = includeInstanceablePortlets;
091 }
092
093 @Override
094 public void setIteratePortlets(boolean iteratePortlets) {
095 _iteratePortlets = iteratePortlets;
096 }
097
098 @Override
099 public void setLayoutTypePortlet(LayoutTypePortlet layoutTypePortlet) {
100 _layoutTypePortlet = layoutTypePortlet;
101 }
102
103 @Override
104 public void setRootNodeName(String rootNodeName) {
105 _rootNodeName = rootNodeName;
106 }
107
108 @Override
109 public void setServletContext(ServletContext servletContext) {
110 _servletContext = servletContext;
111 }
112
113 @Override
114 public void setThemeDisplay(ThemeDisplay themeDisplay) {
115 _themeDisplay = themeDisplay;
116 }
117
118 @Override
119 public void setUser(User user) {
120 _user = user;
121 }
122
123 protected Locale getLocale() {
124 if (_themeDisplay == null) {
125 return _user.getLocale();
126 }
127
128 return _themeDisplay.getLocale();
129 }
130
131 protected void iteratePortletCategories(
132 TreeNodeView parentNodeView,
133 List<PortletCategory> portletCategories, long parentId, int depth)
134 throws PortalException {
135
136 portletCategories = ListUtil.sort(
137 portletCategories, new PortletCategoryComparator(getLocale()));
138
139 for (int i = 0; i < portletCategories.size(); i++) {
140 PortletCategory portletCategory = portletCategories.get(i);
141
142 if (portletCategory.isHidden()) {
143 continue;
144 }
145
146 if (i == 0) {
147 depth++;
148
149 if (depth > _depth) {
150 _depth = depth;
151 }
152 }
153
154 TreeNodeView nodeView = new TreeNodeView(++_nodeId);
155
156 nodeView.setDepth(depth);
157 nodeView.setLeaf(false);
158
159 if ((i + 1) == portletCategories.size()) {
160 nodeView.setLs("1");
161 }
162 else {
163 nodeView.setLs("0");
164 }
165
166 nodeView.setName(
167 LanguageUtil.get(getLocale(), portletCategory.getName()));
168 nodeView.setObjId(portletCategory.getPath());
169 nodeView.setParentId(parentId);
170
171 if (_hierarchicalTree) {
172 if (parentNodeView != null) {
173 parentNodeView.addChild(nodeView);
174 }
175 }
176 else {
177 _list.add(nodeView);
178 }
179
180 int nodeId = _nodeId;
181
182 List<PortletCategory> subCategories = ListUtil.fromCollection(
183 portletCategory.getCategories());
184
185 iteratePortletCategories(nodeView, subCategories, nodeId, depth);
186
187 if (_iteratePortlets) {
188 iteratePortlets(
189 nodeView, portletCategory, portletCategory.getPortletIds(),
190 nodeId, depth + 1);
191 }
192 }
193 }
194
195 protected void iteratePortlets(
196 TreeNodeView parentNodeView, PortletCategory portletCategory,
197 Set<String> portletIds, int parentNodeId, int depth) {
198
199 List<Portlet> portlets = new ArrayList<>();
200
201 String externalPortletCategory = null;
202
203 for (String portletId : portletIds) {
204 Portlet portlet = PortletLocalServiceUtil.getPortletById(
205 _user.getCompanyId(), portletId);
206
207 if (portlet != null) {
208 if (portlet.isSystem()) {
209 }
210 else if (!portlet.isActive()) {
211 }
212 else if (portlet.isInstanceable() &&
213 !_includeInstanceablePortlets) {
214 }
215 else if (!portlet.isInstanceable() &&
216 _layoutTypePortlet.hasPortletId(
217 portlet.getPortletId())) {
218
219 portlets.add(portlet);
220 }
221 else if (!portlet.hasAddPortletPermission(_user.getUserId())) {
222 }
223 else {
224 portlets.add(portlet);
225 }
226
227 PortletApp portletApp = portlet.getPortletApp();
228
229 if (portletApp.isWARFile() &&
230 Validator.isNull(externalPortletCategory)) {
231
232 PortletConfig portletConfig =
233 PortletConfigFactoryUtil.create(
234 portlet, _servletContext);
235
236 ResourceBundle resourceBundle =
237 portletConfig.getResourceBundle(getLocale());
238
239 externalPortletCategory = ResourceBundleUtil.getString(
240 resourceBundle, portletCategory.getName());
241 }
242 }
243 }
244
245 portlets = ListUtil.sort(
246 portlets, new PortletTitleComparator(getLocale()));
247
248 for (int i = 0; i < portlets.size(); i++) {
249 Portlet portlet = portlets.get(i);
250
251 TreeNodeView nodeView = new TreeNodeView(++_nodeId);
252
253 nodeView.setDepth(depth);
254 nodeView.setLeaf(true);
255
256 if ((i + 1) == portlets.size()) {
257 nodeView.setLs("1");
258 }
259 else {
260 nodeView.setLs("0");
261 }
262
263 nodeView.setName(
264 PortalUtil.getPortletTitle(
265 portlet, _servletContext, getLocale()));
266 nodeView.setObjId(portlet.getRootPortletId());
267 nodeView.setParentId(parentNodeId);
268
269 if (_hierarchicalTree) {
270 parentNodeView.addChild(nodeView);
271 }
272 else {
273 _list.add(nodeView);
274 }
275 }
276 }
277
278 private int _depth;
279 private boolean _hierarchicalTree;
280 private boolean _includeInstanceablePortlets;
281 private boolean _iteratePortlets;
282 private LayoutTypePortlet _layoutTypePortlet;
283 private List<TreeNodeView> _list;
284 private int _nodeId;
285 private String _rootNodeName;
286 private ServletContext _servletContext;
287 private ThemeDisplay _themeDisplay;
288 private User _user;
289
290 }