001    /**
002     * Copyright (c) 2000-2010 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.util.ServerDetector;
019    
020    import java.util.List;
021    
022    import javax.servlet.jsp.JspException;
023    import javax.servlet.jsp.JspTagException;
024    
025    /**
026     * @author Raymond Augé
027     */
028    public class SearchContainerColumnScoreTag extends SearchContainerColumnTag {
029    
030            private static final String DEFAULT_NAME = "score";
031    
032            public int doEndTag() {
033                    try {
034                            SearchContainerRowTag parentTag =
035                                    (SearchContainerRowTag)findAncestorWithClass(
036                                            this, SearchContainerRowTag.class);
037    
038                            ResultRow row = parentTag.getRow();
039    
040                            if (index <= -1) {
041                                    index = row.getEntries().size();
042                            }
043    
044                            row.addScore(index, getScore());
045    
046                            return EVAL_PAGE;
047                    }
048                    finally {
049                            if (!ServerDetector.isResin()) {
050                                    index = -1;
051                                    _name = DEFAULT_NAME;
052                                    _score = 0;
053                            }
054                    }
055            }
056    
057            public int doStartTag() throws JspException {
058                    SearchContainerRowTag parentRowTag =
059                            (SearchContainerRowTag)findAncestorWithClass(
060                                    this, SearchContainerRowTag.class);
061    
062                    if (parentRowTag == null) {
063                            throw new JspTagException(
064                                    "Requires liferay-ui:search-container-row");
065                    }
066    
067                    if (!parentRowTag.isHeaderNamesAssigned()) {
068                            List<String> headerNames = parentRowTag.getHeaderNames();
069    
070                            headerNames.add(_name);
071                    }
072    
073                    return EVAL_BODY_INCLUDE;
074            }
075    
076            public float getScore() {
077                    return _score;
078            }
079    
080            public void setScore(float score) {
081                    _score = score;
082            }
083    
084            private String _name = DEFAULT_NAME;
085            private float _score;
086    
087    }