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.dao.search.ResultRow;
018    import com.liferay.portal.kernel.dao.search.SearchEntry;
019    import com.liferay.portal.kernel.servlet.ServletContextPool;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.taglib.search.IconSearchEntry;
024    
025    import java.util.List;
026    
027    import javax.portlet.PortletURL;
028    
029    import javax.servlet.ServletContext;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    import javax.servlet.jsp.JspException;
033    import javax.servlet.jsp.JspTagException;
034    
035    /**
036     * @author Eudaldo Alonso
037     */
038    public class SearchContainerColumnIconTag<R> extends SearchContainerColumnTag {
039    
040            @Override
041            public int doEndTag() {
042                    try {
043                            SearchContainerRowTag<R> searchContainerRowTag =
044                                    (SearchContainerRowTag<R>)findAncestorWithClass(
045                                            this, SearchContainerRowTag.class);
046    
047                            ResultRow resultRow = searchContainerRowTag.getRow();
048    
049                            if (index <= -1) {
050                                    List<SearchEntry> searchEntries = resultRow.getEntries();
051    
052                                    index = searchEntries.size();
053                            }
054    
055                            if (resultRow.isRestricted()) {
056                                    _href = null;
057                            }
058    
059                            IconSearchEntry iconSearchEntry = new IconSearchEntry();
060    
061                            iconSearchEntry.setAlign(getAlign());
062                            iconSearchEntry.setColspan(getColspan());
063                            iconSearchEntry.setCssClass(getCssClass());
064                            iconSearchEntry.setRequest(
065                                    (HttpServletRequest)pageContext.getRequest());
066                            iconSearchEntry.setResponse(
067                                    (HttpServletResponse)pageContext.getResponse());
068                            iconSearchEntry.setToggleRowChecker(isToggleRowChecker());
069    
070                            ServletContext servletContext = ServletContextPool.get(
071                                    PortalUtil.getServletContextName());
072    
073                            iconSearchEntry.setServletContext(servletContext);
074    
075                            iconSearchEntry.setIcon(_icon);
076                            iconSearchEntry.setValign(getValign());
077    
078                            resultRow.addSearchEntry(index, iconSearchEntry);
079    
080                            return EVAL_PAGE;
081                    }
082                    finally {
083                            index = -1;
084                            _icon = null;
085    
086                            if (!ServerDetector.isResin()) {
087                                    align = SearchEntry.DEFAULT_ALIGN;
088                                    colspan = SearchEntry.DEFAULT_COLSPAN;
089                                    cssClass = SearchEntry.DEFAULT_CSS_CLASS;
090                                    _href = null;
091                                    name = null;
092                                    _toggleRowChecker = false;
093                                    valign = SearchEntry.DEFAULT_VALIGN;
094                            }
095                    }
096            }
097    
098            @Override
099            public int doStartTag() throws JspException {
100                    SearchContainerRowTag<R> searchContainerRowTag =
101                            (SearchContainerRowTag<R>)findAncestorWithClass(
102                                    this, SearchContainerRowTag.class);
103    
104                    if (searchContainerRowTag == null) {
105                            throw new JspTagException(
106                                    "Requires liferay-ui:search-container-row");
107                    }
108    
109                    if (!searchContainerRowTag.isHeaderNamesAssigned()) {
110                            List<String> headerNames = searchContainerRowTag.getHeaderNames();
111    
112                            String name = getName();
113    
114                            if (Validator.isNotNull(name)) {
115                                    headerNames.add(name);
116                            }
117                    }
118    
119                    return EVAL_BODY_INCLUDE;
120            }
121    
122            public Object getHref() {
123                    if (_href instanceof PortletURL) {
124                            _href = _href.toString();
125                    }
126    
127                    return _href;
128            }
129    
130            public String getIcon() {
131                    return _icon;
132            }
133    
134            public boolean isToggleRowChecker() {
135                    return _toggleRowChecker;
136            }
137    
138            public void setHref(Object href) {
139                    _href = href;
140            }
141    
142            public void setIcon(String icon) {
143                    _icon = icon;
144            }
145    
146            public void setToggleRowChecker(boolean toggleRowChecker) {
147                    _toggleRowChecker = toggleRowChecker;
148            }
149    
150            private Object _href;
151            private String _icon;
152            private boolean _toggleRowChecker = false;
153    
154    }