001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.ScoreSearchEntry;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    
021    import java.util.List;
022    
023    import javax.servlet.jsp.JspException;
024    import javax.servlet.jsp.JspTagException;
025    
026    /**
027     * @author Raymond Augé
028     */
029    public class SearchContainerColumnScoreTag<R> extends SearchContainerColumnTag {
030    
031            private static final String DEFAULT_NAME = "score";
032    
033            @Override
034            public int doEndTag() {
035                    try {
036                            SearchContainerRowTag<R> searchContainerRowTag =
037                                    (SearchContainerRowTag<R>)findAncestorWithClass(
038                                            this, SearchContainerRowTag.class);
039    
040                            ResultRow row = searchContainerRowTag.getRow();
041    
042                            if (index <= -1) {
043                                    index = row.getEntries().size();
044                            }
045    
046                            ScoreSearchEntry scoreSearchEntry = new ScoreSearchEntry();
047    
048                            scoreSearchEntry.setScore(getScore());
049    
050                            row.addSearchEntry(index, scoreSearchEntry);
051    
052                            return EVAL_PAGE;
053                    }
054                    finally {
055                            index = -1;
056    
057                            if (!ServerDetector.isResin()) {
058                                    _name = DEFAULT_NAME;
059                                    _score = 0;
060                            }
061                    }
062            }
063    
064            @Override
065            public int doStartTag() throws JspException {
066                    SearchContainerRowTag<R> searchContainerRowTag =
067                            (SearchContainerRowTag<R>)findAncestorWithClass(
068                                    this, SearchContainerRowTag.class);
069    
070                    if (searchContainerRowTag == null) {
071                            throw new JspTagException(
072                                    "Requires liferay-ui:search-container-row");
073                    }
074    
075                    if (!searchContainerRowTag.isHeaderNamesAssigned()) {
076                            List<String> headerNames = searchContainerRowTag.getHeaderNames();
077    
078                            headerNames.add(_name);
079                    }
080    
081                    return EVAL_BODY_INCLUDE;
082            }
083    
084            public float getScore() {
085                    return _score;
086            }
087    
088            public void setScore(float score) {
089                    _score = score;
090            }
091    
092            private String _name = DEFAULT_NAME;
093            private float _score;
094    
095    }