001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.theme;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.template.Template;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import java.io.Serializable;
027    
028    import java.lang.reflect.Method;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * Represents a portal navigation item, providing access to layouts and metadata
037     * from templates, which can be found in a theme's
038     * <code>portal-normal.vm</code>.
039     *
040     * @author Brian Wing Shun Chan
041     * @author Shuyang Zhou
042     */
043    public class NavItem implements Serializable {
044    
045            /**
046             * Creates a single level of navigation items from the layouts. Navigation
047             * items for nested layouts are only created when they are accessed.
048             *
049             * <p>
050             * No permission checks are performed in this method. Permissions of child
051             * layouts are honored when accessing them via {@link #getChildren()}.
052             * </p>
053             *
054             * @param  request the currently served {@link HttpServletRequest}
055             * @param  layouts the layouts from which to create the navigation items
056             * @param  template the template to add navigation items to
057             * @return a single level of navigation items from the layouts, or
058             *         <code>null</code> if the collection of layouts was
059             *         <code>null</code>.
060             */
061            public static List<NavItem> fromLayouts(
062                    HttpServletRequest request, List<Layout> layouts, Template template) {
063    
064                    if (layouts == null) {
065                            return null;
066                    }
067    
068                    List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
069    
070                    for (Layout layout : layouts) {
071                            navItems.add(new NavItem(request, layout, template));
072                    }
073    
074                    return navItems;
075            }
076    
077            public NavItem(
078                    HttpServletRequest request, Layout layout, Template template) {
079    
080                    _request = request;
081                    _themeDisplay = (ThemeDisplay)request.getAttribute(
082                            WebKeys.THEME_DISPLAY);
083                    _layout = layout;
084                    _template = template;
085            }
086    
087            /**
088             * Returns all of child layouts that the current user has permission to
089             * access from this navigation item's layout.
090             *
091             * @return the list of all child layouts that the current user has
092             *         permission to access from this navigation item's layout
093             * @throws Exception if an exception occurred
094             */
095            public List<NavItem> getChildren() throws Exception {
096                    if (_children == null) {
097                            List<Layout> layouts = _layout.getChildren(
098                                    _themeDisplay.getPermissionChecker());
099    
100                            _children = fromLayouts(_request, layouts, _template);
101                    }
102    
103                    return _children;
104            }
105    
106            /**
107             * Returns the navigation item's layout.
108             *
109             * @return the navigation item's layout
110             */
111            public Layout getLayout() {
112                    return _layout;
113            }
114    
115            /**
116             * Returns the ID of the navigation item's layout.
117             *
118             * @return the ID of the navigation item's layout
119             */
120            public long getLayoutId() {
121                    return _layout.getLayoutId();
122            }
123    
124            /**
125             * Returns the HTML-escaped name of the navigation item's layout.
126             *
127             * @return the HTML-escaped name of the navigation item's layout
128             */
129            public String getName() {
130                    return HtmlUtil.escape(getUnescapedName());
131            }
132    
133            /**
134             * Returns the full, absolute URL (including the portal's URL) of the
135             * navigation item's layout.
136             *
137             * @return the full, absolute URL of the navigation item's layout
138             * @throws Exception if an exception occurred
139             */
140            public String getRegularFullURL() throws Exception {
141                    String portalURL = PortalUtil.getPortalURL(_request);
142    
143                    String regularURL = getRegularURL();
144    
145                    if (StringUtil.startsWith(regularURL, portalURL) ||
146                            Validator.isUrl(regularURL)) {
147    
148                            return regularURL;
149                    }
150                    else {
151                            return portalURL.concat(regularURL);
152                    }
153            }
154    
155            /**
156             * Returns the regular URL of the navigation item's layout.
157             *
158             * @return the regular URL of the navigation item's layout
159             * @throws Exception if an exception occurred
160             */
161            public String getRegularURL() throws Exception {
162                    return _layout.getRegularURL(_request);
163            }
164    
165            public String getResetLayoutURL() throws Exception {
166                    return _layout.getResetLayoutURL(_request);
167            }
168    
169            public String getResetMaxStateURL() throws Exception {
170                    return _layout.getResetMaxStateURL(_request);
171            }
172    
173            /**
174             * Returns the target of the navigation item's layout.
175             *
176             * @return the target of the navigation item's layout
177             */
178            public String getTarget() {
179                    return _layout.getTarget();
180            }
181    
182            /**
183             * Returns the title of the navigation item's layout in the current
184             * request's locale.
185             *
186             * @return the title of the navigation item's layout in the current
187             *         request's locale
188             */
189            public String getTitle() {
190                    return _layout.getTitle(_themeDisplay.getLocale());
191            }
192    
193            /**
194             * Returns the unescaped name of the navigation item's layout in the current
195             * request's locale.
196             *
197             * @return the unescaped name of the navigation item's layout in the current
198             *         request's locale
199             */
200            public String getUnescapedName() {
201                    return _layout.getName(_themeDisplay.getLocale());
202            }
203    
204            /**
205             * Returns the URL of the navigation item's layout, in a format that makes
206             * it safe to use the URL as an HREF attribute value
207             *
208             * @return the URL of the navigation item's layout, in a format that makes
209             *         it safe to use the URL as an HREF attribute value
210             * @throws Exception if an exception occurred
211             */
212            public String getURL() throws Exception {
213                    return HtmlUtil.escapeHREF(getRegularFullURL());
214            }
215    
216            /**
217             * Returns <code>true</code> if the navigation item's layout has child
218             * layouts.
219             *
220             * @return <code>true</code> if the navigation item's layout has child
221             *         layouts; <code>false</code> otherwise
222             * @throws Exception if an exception occurred
223             */
224            public boolean hasChildren() throws Exception {
225                    List<NavItem> children = getChildren();
226    
227                    if (!children.isEmpty()) {
228                            return true;
229                    }
230                    else {
231                            return false;
232                    }
233            }
234    
235            public void icon() throws Exception {
236                    Object velocityTaglib = _template.get("theme");
237    
238                    Method method = (Method)_template.get("velocityTaglib_layoutIcon");
239    
240                    method.invoke(velocityTaglib, _layout);
241            }
242    
243            public boolean isChildSelected() throws PortalException {
244                    return _layout.isChildSelected(
245                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
246            }
247    
248            public boolean isSelected() throws Exception {
249                    return _layout.isSelected(
250                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
251                            _themeDisplay.getLayout().getAncestorPlid());
252            }
253    
254            private List<NavItem> _children;
255            private final Layout _layout;
256            private final HttpServletRequest _request;
257            private final Template _template;
258            private final ThemeDisplay _themeDisplay;
259    
260    }