001    /**
002     * Copyright (c) 2000-2013 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.portlet.journalcontentsearch.util;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.search.Field;
019    import com.liferay.portal.kernel.search.Hits;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.Time;
022    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    /**
028     * @author Alexander Chow
029     * @author Raymond Aug??
030     */
031    public class ContentHits {
032    
033            public boolean isShowListed() {
034                    return _showListed;
035            }
036    
037            public void recordHits(
038                            Hits hits, long groupId, boolean privateLayout, int start, int end)
039                    throws Exception {
040    
041                    // This can later be optimized according to LEP-915.
042    
043                    List<Document> docs = new ArrayList<Document>();
044                    List<Float> scores = new ArrayList<Float>();
045    
046                    for (int i = 0; i < hits.getLength(); i++) {
047                            Document doc = hits.doc(i);
048    
049                            long articleGroupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
050                            String articleId = doc.get("articleId");
051    
052                            int layoutIdsCount =
053                                    JournalContentSearchLocalServiceUtil.getLayoutIdsCount(
054                                            groupId, privateLayout, articleId);
055    
056                            if ((layoutIdsCount > 0) ||
057                                    (!isShowListed() && (articleGroupId == groupId))) {
058    
059                                    docs.add(hits.doc(i));
060                                    scores.add(hits.score(i));
061                            }
062                    }
063    
064                    int length = docs.size();
065    
066                    hits.setLength(length);
067    
068                    if (end > length) {
069                            end = length;
070                    }
071    
072                    docs = docs.subList(start, end);
073                    scores = scores.subList(start, end);
074    
075                    hits.setDocs(docs.toArray(new Document[docs.size()]));
076                    hits.setScores(scores.toArray(new Float[docs.size()]));
077    
078                    hits.setSearchTime(
079                            (float)(System.currentTimeMillis() - hits.getStart()) /
080                                    Time.SECOND);
081            }
082    
083            public void setShowListed(boolean showListed) {
084                    _showListed = showListed;
085            }
086    
087            private boolean _showListed = true;
088    
089    }