001    /**
002     * Copyright (c) 2000-2013 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.aui;
016    
017    import com.liferay.portal.kernel.util.JavaConstants;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.taglib.aui.base.BaseNavBarSearchTag;
021    import com.liferay.util.PwdGenerator;
022    
023    import javax.portlet.PortletResponse;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.jsp.JspException;
027    
028    /**
029     * @author Eduardo Lundgren
030     * @author Bruno Basto
031     * @author Nathan Cavanaugh
032     * @author Julio Camarero
033     */
034    public class NavBarSearchTag extends BaseNavBarSearchTag {
035    
036            @Override
037            public int doStartTag() throws JspException {
038                    NavBarTag navBarTag = (NavBarTag)findAncestorWithClass(
039                            this, NavBarTag.class);
040    
041                    if (navBarTag != null) {
042                            StringBundler sb = navBarTag.getResponsiveButtonsSB();
043    
044                            sb.append("<a class=\"btn btn-navbar\" id=\"");
045                            sb.append(_getNamespacedId());
046                            sb.append("NavbarBtn\"");
047                            sb.append("data-navId=\"");
048                            sb.append(_getNamespacedId());
049                            sb.append("\">");
050                            sb.append("<i class=\"icon-search\"></i></a>");
051                    }
052    
053                    return super.doStartTag();
054            }
055    
056            @Override
057            protected void cleanUp() {
058                    super.cleanUp();
059    
060                    _namespacedId = null;
061            }
062    
063            @Override
064            protected void setAttributes(HttpServletRequest request) {
065                    super.setAttributes(request);
066    
067                    setNamespacedAttribute(request, "id", _getNamespacedId());
068            }
069    
070            private String _getNamespacedId() {
071                    if (Validator.isNotNull(_namespacedId)) {
072                            return _namespacedId;
073                    }
074    
075                    _namespacedId = getId();
076    
077                    if (Validator.isNull(_namespacedId)) {
078                            _namespacedId = PwdGenerator.getPassword(4);
079                    }
080    
081                    HttpServletRequest request =
082                            (HttpServletRequest)pageContext.getRequest();
083    
084                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
085                            JavaConstants.JAVAX_PORTLET_RESPONSE);
086    
087                    if (portletResponse != null) {
088                            _namespacedId = portletResponse.getNamespace() + _namespacedId;
089                    }
090    
091                    return _namespacedId;
092            }
093    
094            private String _namespacedId;
095    
096    }