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