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.language.LanguageUtil;
018    import com.liferay.portal.kernel.servlet.taglib.ui.QuickAccessEntry;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.taglib.BaseBodyTagSupport;
023    import com.liferay.taglib.util.TagResourceBundleUtil;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    import java.util.ResourceBundle;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.jsp.JspException;
031    import javax.servlet.jsp.tagext.BodyTag;
032    
033    /**
034     * @author Eudaldo Alonso
035     */
036    public class QuickAccessEntryTag extends BaseBodyTagSupport implements BodyTag {
037    
038            @Override
039            public int doEndTag() throws JspException {
040                    try {
041                            return processEndTag();
042                    }
043                    catch (Exception e) {
044                            throw new JspException(e);
045                    }
046                    finally {
047                            if (!ServerDetector.isResin()) {
048                                    _label = null;
049                                    _onClick = null;
050                                    _url = null;
051                            }
052                    }
053            }
054    
055            public void setLabel(String label) {
056                    ResourceBundle resourceBundle = TagResourceBundleUtil.getResourceBundle(
057                            pageContext);
058    
059                    _label = LanguageUtil.get(resourceBundle, label);
060            }
061    
062            public void setOnClick(String onClick) {
063                    _onClick = onClick;
064            }
065    
066            public void setUrl(String url) {
067                    _url = url;
068            }
069    
070            protected String getEndPage() {
071                    return _END_PAGE;
072            }
073    
074            protected String getStartPage() {
075                    return _START_PAGE;
076            }
077    
078            protected int processEndTag() throws Exception {
079                    HttpServletRequest request =
080                            (HttpServletRequest)pageContext.getRequest();
081    
082                    List<QuickAccessEntry> quickAccessEntries =
083                            (List<QuickAccessEntry>)request.getAttribute(
084                                    WebKeys.PORTLET_QUICK_ACCESS_ENTRIES);
085    
086                    if (quickAccessEntries == null) {
087                            quickAccessEntries = new ArrayList<>();
088    
089                            request.setAttribute(
090                                    WebKeys.PORTLET_QUICK_ACCESS_ENTRIES, quickAccessEntries);
091                    }
092    
093                    QuickAccessEntry quickAccessEntry = new QuickAccessEntry();
094    
095                    quickAccessEntry.setBody(getBodyContentAsStringBundler());
096                    quickAccessEntry.setId(StringUtil.randomId());
097                    quickAccessEntry.setLabel(_label);
098                    quickAccessEntry.setOnClick(_onClick);
099                    quickAccessEntry.setURL(_url);
100    
101                    quickAccessEntries.add(quickAccessEntry);
102    
103                    return EVAL_PAGE;
104            }
105    
106            private static final String _END_PAGE =
107                    "/html/taglib/ui/quick_access_entry/end.jsp";
108    
109            private static final String _START_PAGE =
110                    "/html/taglib/ui/quick_access_entry/start.jsp";
111    
112            private String _label;
113            private String _onClick;
114            private String _url;
115    
116    }