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.ui;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.util.SessionClicks;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.jsp.JspException;
022    import javax.servlet.jsp.JspWriter;
023    import javax.servlet.jsp.PageContext;
024    import javax.servlet.jsp.tagext.TagSupport;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class ToggleValueTag extends TagSupport {
030    
031            /**
032             * @deprecated As of 6.1.0
033             */
034            public static void doTag(
035                            String id, PageContext pageContext, HttpServletRequest request)
036                    throws Exception {
037    
038                    doTag(id, "block", pageContext);
039            }
040    
041            public static void doTag(
042                            String id, String defaultValue, PageContext pageContext)
043                    throws Exception {
044    
045                    HttpServletRequest request =
046                            (HttpServletRequest)pageContext.getRequest();
047    
048                    String value = SessionClicks.get(request, id, StringPool.BLANK);
049    
050                    if (value.equals(StringPool.BLANK)) {
051                            value = defaultValue;
052                    }
053    
054                    JspWriter jspWriter = pageContext.getOut();
055    
056                    jspWriter.write(value);
057            }
058    
059            @Override
060            public int doEndTag() throws JspException {
061                    try {
062                            doTag(_id, _defaultValue, pageContext);
063    
064                            return EVAL_PAGE;
065                    }
066                    catch (Exception e) {
067                            throw new JspException(e);
068                    }
069            }
070    
071            public void setDefaultValue(String defaultValue) {
072                    _defaultValue = defaultValue;
073            }
074    
075            @Override
076            public void setId(String id) {
077                    _id = id;
078            }
079    
080            private String _defaultValue = "block";
081            private String _id;
082    
083    }