001
014
015 package com.liferay.portal.theme;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.util.HtmlUtil;
019 import com.liferay.portal.kernel.util.ListUtil;
020 import com.liferay.portal.kernel.util.PredicateFilter;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.util.WebKeys;
026 import com.liferay.portal.kernel.webserver.WebServerServletTokenUtil;
027 import com.liferay.portal.model.Layout;
028 import com.liferay.portal.model.LayoutType;
029 import com.liferay.portal.util.PortalUtil;
030
031 import java.io.Serializable;
032
033 import java.util.ArrayList;
034 import java.util.List;
035 import java.util.Map;
036
037 import javax.servlet.http.HttpServletRequest;
038
039
047 public class NavItem implements Serializable {
048
049
064 public static List<NavItem> fromLayouts(
065 HttpServletRequest request, List<Layout> layouts,
066 Map<String, Object> contextObjects) {
067
068 if (layouts == null) {
069 return null;
070 }
071
072 List<NavItem> navItems = new ArrayList<>(layouts.size());
073
074 for (Layout layout : layouts) {
075 navItems.add(new NavItem(request, layout, contextObjects));
076 }
077
078 return navItems;
079 }
080
081 public NavItem(
082 HttpServletRequest request, Layout layout,
083 Map<String, Object> contextObjects) {
084
085 _request = request;
086 _themeDisplay = (ThemeDisplay)request.getAttribute(
087 WebKeys.THEME_DISPLAY);
088 _layout = layout;
089 _contextObjects = contextObjects;
090 }
091
092 @Override
093 public boolean equals(Object obj) {
094 if (this == obj) {
095 return true;
096 }
097
098 if (!(obj instanceof NavItem)) {
099 return false;
100 }
101
102 NavItem navItem = (NavItem)obj;
103
104 if (Validator.equals(getLayoutId(), navItem.getLayoutId())) {
105 return true;
106 }
107
108 return false;
109 }
110
111
119 public List<NavItem> getBrowsableChildren() throws Exception {
120 if (_browsableChildren == null) {
121 List<NavItem> children = getChildren();
122
123 _browsableChildren = ListUtil.filter(
124 children,
125 new PredicateFilter<NavItem>() {
126
127 @Override
128 public boolean filter(NavItem navItem) {
129 return navItem.isBrowsable();
130 }
131
132 });
133 }
134
135 return _browsableChildren;
136 }
137
138
146 public List<NavItem> getChildren() throws Exception {
147 if (_children == null) {
148 List<Layout> layouts = _layout.getChildren(
149 _themeDisplay.getPermissionChecker());
150
151 _children = fromLayouts(_request, layouts, _contextObjects);
152 }
153
154 return _children;
155 }
156
157
162 public Layout getLayout() {
163 return _layout;
164 }
165
166
171 public long getLayoutId() {
172 return _layout.getLayoutId();
173 }
174
175
180 public String getName() {
181 return HtmlUtil.escape(getUnescapedName());
182 }
183
184
191 public String getRegularFullURL() throws Exception {
192 String portalURL = PortalUtil.getPortalURL(_request);
193
194 String regularURL = getRegularURL();
195
196 if (StringUtil.startsWith(regularURL, portalURL) ||
197 Validator.isUrl(regularURL)) {
198
199 return regularURL;
200 }
201 else {
202 return portalURL.concat(regularURL);
203 }
204 }
205
206
212 public String getRegularURL() throws Exception {
213 return _layout.getRegularURL(_request);
214 }
215
216 public String getResetLayoutURL() throws Exception {
217 return _layout.getResetLayoutURL(_request);
218 }
219
220 public String getResetMaxStateURL() throws Exception {
221 return _layout.getResetMaxStateURL(_request);
222 }
223
224
229 public String getTarget() {
230 return _layout.getTarget();
231 }
232
233
240 public String getTitle() {
241 return _layout.getTitle(_themeDisplay.getLocale());
242 }
243
244
251 public String getUnescapedName() {
252 return _layout.getName(_themeDisplay.getLocale());
253 }
254
255
263 public String getURL() throws Exception {
264 return HtmlUtil.escapeHREF(getRegularFullURL());
265 }
266
267
275 public boolean hasBrowsableChildren() throws Exception {
276 List<NavItem> browsableChildren = getBrowsableChildren();
277
278 if (!browsableChildren.isEmpty()) {
279 return true;
280 }
281 else {
282 return false;
283 }
284 }
285
286
294 public boolean hasChildren() throws Exception {
295 List<NavItem> children = getChildren();
296
297 if (!children.isEmpty()) {
298 return true;
299 }
300 else {
301 return false;
302 }
303 }
304
305 @Override
306 public int hashCode() {
307 return _layout.hashCode();
308 }
309
310 public String iconURL() throws Exception {
311 if ((_layout == null) || !_layout.isIconImage()) {
312 return StringPool.BLANK;
313 }
314
315 StringBundler sb = new StringBundler(5);
316
317 sb.append(_themeDisplay.getPathImage());
318 sb.append("/layout_icon?img_id");
319 sb.append(_layout.getIconImageId());
320 sb.append("&t=");
321 sb.append(WebServerServletTokenUtil.getToken(_layout.getIconImageId()));
322
323 return sb.toString();
324 }
325
326 public boolean isBrowsable() {
327 LayoutType layoutType = _layout.getLayoutType();
328
329 return layoutType.isBrowsable();
330 }
331
332 public boolean isChildSelected() throws PortalException {
333 return _layout.isChildSelected(
334 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
335 }
336
337 public boolean isInNavigation(List<NavItem> navItems) {
338 if (navItems == null) {
339 return false;
340 }
341
342 return navItems.contains(this);
343 }
344
345 public boolean isSelected() throws Exception {
346 return _layout.isSelected(
347 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
348 _themeDisplay.getLayout().getAncestorPlid());
349 }
350
351 private List<NavItem> _browsableChildren;
352 private List<NavItem> _children;
353 private final Map<String, Object> _contextObjects;
354 private final Layout _layout;
355 private final HttpServletRequest _request;
356 private final ThemeDisplay _themeDisplay;
357
358 }