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