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