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