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.taglib.ui;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.portletdisplaytemplate.PortletDisplayTemplateManagerUtil;
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.theme.NavItem;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.taglib.util.IncludeTag;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Jorge Ferrer
036     * @author Tibor Lipusz
037     */
038    public class NavigationTag extends IncludeTag {
039    
040            public void setBulletStyle(String bulletStyle) {
041                    _bulletStyle = bulletStyle;
042            }
043    
044            public void setDdmTemplateGroupId(long ddmTemplateGroupId) {
045                    _ddmTemplateGroupId = ddmTemplateGroupId;
046            }
047    
048            public void setDdmTemplateKey(String ddmTemplateKey) {
049                    _ddmTemplateKey = ddmTemplateKey;
050            }
051    
052            public void setHeaderType(String headerType) {
053                    _headerType = headerType;
054            }
055    
056            public void setIncludedLayouts(String includedLayouts) {
057                    _includedLayouts = includedLayouts;
058            }
059    
060            public void setNestedChildren(boolean nestedChildren) {
061                    _nestedChildren = nestedChildren;
062            }
063    
064            public void setPreview(boolean preview) {
065                    _preview = preview;
066            }
067    
068            public void setRootLayoutLevel(int rootLayoutLevel) {
069                    _rootLayoutLevel = rootLayoutLevel;
070            }
071    
072            public void setRootLayoutType(String rootLayoutType) {
073                    _rootLayoutType = rootLayoutType;
074            }
075    
076            @Override
077            protected void cleanUp() {
078                    _bulletStyle = "1";
079                    _ddmTemplateGroupId = 0;
080                    _ddmTemplateKey = null;
081                    _headerType = "none";
082                    _includedLayouts = "auto";
083                    _nestedChildren = true;
084                    _preview = false;
085                    _rootLayoutLevel = 1;
086                    _rootLayoutType = "absolute";
087            }
088    
089            protected String getDisplayStyle() {
090                    if (Validator.isNotNull(_ddmTemplateKey)) {
091                            return PortletDisplayTemplateManagerUtil.getDisplayStyle(
092                                    _ddmTemplateKey);
093                    }
094    
095                    return null;
096            }
097    
098            protected long getDisplayStyleGroupId() {
099                    if (_ddmTemplateGroupId > 0) {
100                            return _ddmTemplateGroupId;
101                    }
102    
103                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
104                            WebKeys.THEME_DISPLAY);
105    
106                    return themeDisplay.getScopeGroupId();
107            }
108    
109            protected List<NavItem> getNavItems(HttpServletRequest request)
110                    throws PortalException {
111    
112                    List<NavItem> navItems = new ArrayList<>();
113    
114                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
115                            WebKeys.THEME_DISPLAY);
116    
117                    Layout layout = themeDisplay.getLayout();
118    
119                    NavItem navItem = new NavItem(request, layout, null);
120    
121                    navItems.add(navItem);
122    
123                    for (Layout ancestorLayout : layout.getAncestors()) {
124                            navItems.add(0, new NavItem(request, ancestorLayout, null));
125                    }
126    
127                    return navItems;
128            }
129    
130            @Override
131            protected String getPage() {
132                    return _PAGE;
133            }
134    
135            @Override
136            protected void setAttributes(HttpServletRequest request) {
137                    request.setAttribute("liferay-ui:navigation:bulletStyle", _bulletStyle);
138                    request.setAttribute(
139                            "liferay-ui:navigation:displayStyle", getDisplayStyle());
140                    request.setAttribute(
141                            "liferay-ui:navigation:displayStyleGroupId",
142                            String.valueOf(getDisplayStyleGroupId()));
143                    request.setAttribute("liferay-ui:navigation:headerType", _headerType);
144                    request.setAttribute(
145                            "liferay-ui:navigation:includedLayouts", _includedLayouts);
146    
147                    try {
148                            List<NavItem> navItems = getNavItems(request);
149    
150                            request.setAttribute("liferay-ui:navigation:navItems", navItems);
151                    }
152                    catch (PortalException pe) {
153                            _log.error(pe);
154                    }
155    
156                    request.setAttribute(
157                            "liferay-ui:navigation:nestedChildren",
158                            String.valueOf(_nestedChildren));
159                    request.setAttribute(
160                            "liferay-ui:navigation:preview", String.valueOf(_preview));
161                    request.setAttribute(
162                            "liferay-ui:navigation:rootLayoutLevel",
163                            String.valueOf(_rootLayoutLevel));
164                    request.setAttribute(
165                            "liferay-ui:navigation:rootLayoutType", _rootLayoutType);
166            }
167    
168            private static final String _PAGE = "/html/taglib/ui/navigation/page.jsp";
169    
170            private static final Log _log = LogFactoryUtil.getLog(NavigationTag.class);
171    
172            private String _bulletStyle = "1";
173            private long _ddmTemplateGroupId;
174            private String _ddmTemplateKey;
175            private String _headerType = "none";
176            private String _includedLayouts = "auto";
177            private boolean _nestedChildren = true;
178            private boolean _preview;
179            private int _rootLayoutLevel = 1;
180            private String _rootLayoutType = "absolute";
181    
182    }