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.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 void recordHits(
034                            Hits hits, long groupId, boolean privateLayout, int start, int end)
035                    throws Exception {
036    
037                    // This can later be optimized according to LEP-915.
038    
039                    List<Document> docs = new ArrayList<Document>();
040                    List<Float> scores = new ArrayList<Float>();
041                    List<String> snippets = new ArrayList<String>();
042    
043                    for (int i = 0; i < hits.getLength(); i++) {
044                            Document doc = hits.doc(i);
045    
046                            long articleGroupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
047                            String articleId = doc.get("articleId");
048    
049                            if (JournalContentSearchLocalServiceUtil.getLayoutIdsCount(
050                                            groupId, privateLayout, articleId) > 0) {
051    
052                                    docs.add(hits.doc(i));
053                                    scores.add(hits.score(i));
054                                    snippets.add(hits.snippet(i));
055                            }
056                            else if (!isShowListed() && (articleGroupId == groupId)) {
057                                    docs.add(hits.doc(i));
058                                    scores.add(hits.score(i));
059                                    snippets.add(hits.snippet(i));
060                            }
061                    }
062    
063                    int length = docs.size();
064    
065                    hits.setLength(length);
066    
067                    if (end > length) {
068                            end = length;
069                    }
070    
071                    docs = docs.subList(start, end);
072                    scores = scores.subList(start, end);
073                    snippets = snippets.subList(start, end);
074    
075                    hits.setDocs(docs.toArray(new Document[docs.size()]));
076                    hits.setScores(scores.toArray(new Float[docs.size()]));
077                    hits.setSnippets(snippets.toArray(new String[docs.size()]));
078    
079                    hits.setSearchTime(
080                            (float)(System.currentTimeMillis() - hits.getStart()) /
081                                    Time.SECOND);
082            }
083    
084            public boolean isShowListed() {
085                    return _showListed;
086            }
087    
088            public void setShowListed(boolean showListed) {
089                    _showListed = showListed;
090            }
091    
092            private boolean _showListed = true;
093    
094    }