001
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
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
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 }