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.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            @Deprecated
035            public static void doTag(
036                            String id, PageContext pageContext, HttpServletRequest request)
037                    throws Exception {
038    
039                    doTag(id, "block", pageContext);
040            }
041    
042            public static void doTag(
043                            String id, String defaultValue, PageContext pageContext)
044                    throws Exception {
045    
046                    HttpServletRequest request =
047                            (HttpServletRequest)pageContext.getRequest();
048    
049                    String value = SessionClicks.get(request, id, StringPool.BLANK);
050    
051                    if (value.equals(StringPool.BLANK)) {
052                            value = defaultValue;
053                    }
054    
055                    JspWriter jspWriter = pageContext.getOut();
056    
057                    jspWriter.write(value);
058            }
059    
060            @Override
061            public int doEndTag() throws JspException {
062                    try {
063                            doTag(_id, _defaultValue, pageContext);
064    
065                            return EVAL_PAGE;
066                    }
067                    catch (Exception e) {
068                            throw new JspException(e);
069                    }
070            }
071    
072            public void setDefaultValue(String defaultValue) {
073                    _defaultValue = defaultValue;
074            }
075    
076            @Override
077            public void setId(String id) {
078                    _id = id;
079            }
080    
081            private String _defaultValue = "block";
082            private String _id;
083    
084    }