001    /**
002     * Copyright (c) 2000-2011 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.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    }