001    /**
002     * Copyright (c) 2000-2013 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.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                    Document[] docsArray = hits.getDocs();
047    
048                    for (int i = 0; i < docsArray.length; i++) {
049                            Document doc = hits.doc(i);
050    
051                            long articleGroupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
052                            String articleId = doc.get("articleId");
053    
054                            int layoutIdsCount =
055                                    JournalContentSearchLocalServiceUtil.getLayoutIdsCount(
056                                            groupId, privateLayout, articleId);
057    
058                            if ((layoutIdsCount > 0) ||
059                                    (!isShowListed() && (articleGroupId == groupId))) {
060    
061                                    docs.add(hits.doc(i));
062                                    scores.add(hits.score(i));
063                            }
064                    }
065    
066                    int length = docs.size();
067    
068                    hits.setLength(length);
069    
070                    if (end > length) {
071                            end = length;
072                    }
073    
074                    docs = docs.subList(start, end);
075                    scores = scores.subList(start, end);
076    
077                    hits.setDocs(docs.toArray(new Document[docs.size()]));
078                    hits.setScores(scores.toArray(new Float[docs.size()]));
079    
080                    hits.setSearchTime(
081                            (float)(System.currentTimeMillis() - hits.getStart()) /
082                                    Time.SECOND);
083            }
084    
085            public void setShowListed(boolean showListed) {
086                    _showListed = showListed;
087            }
088    
089            private boolean _showListed = true;
090    
091    }