1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.journalcontentsearch.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.Field;
19  import com.liferay.portal.kernel.search.Hits;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.Time;
22  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * <a href="ContentHits.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Alexander Chow
31   * @author Raymond Augé
32   */
33  public class ContentHits {
34  
35      public void recordHits(
36              Hits hits, long groupId, boolean privateLayout, int start, int end)
37          throws Exception {
38  
39          // This can later be optimized according to LEP-915.
40  
41          List<Document> docs = new ArrayList<Document>();
42          List<Float> scores = new ArrayList<Float>();
43  
44          for (int i = 0; i < hits.getLength(); i++) {
45              Document doc = hits.doc(i);
46  
47              String articleId = doc.get(Field.ENTRY_CLASS_PK);
48              long articleGroupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
49  
50              if (JournalContentSearchLocalServiceUtil.getLayoutIdsCount(
51                      groupId, privateLayout, articleId) > 0) {
52  
53                  docs.add(hits.doc(i));
54                  scores.add(hits.score(i));
55              }
56              else if (!isShowListed() && (articleGroupId == groupId)) {
57                  docs.add(hits.doc(i));
58                  scores.add(hits.score(i));
59              }
60          }
61  
62          int length = docs.size();
63  
64          hits.setLength(length);
65  
66          if (end > length) {
67              end = length;
68          }
69  
70          docs = docs.subList(start, end);
71  
72          hits.setDocs(docs.toArray(new Document[docs.size()]));
73          hits.setScores(scores.toArray(new Float[docs.size()]));
74  
75          hits.setSearchTime(
76              (float)(System.currentTimeMillis() - hits.getStart()) /
77                  Time.SECOND);
78      }
79  
80      public boolean isShowListed() {
81          return _showListed;
82      }
83  
84      public void setShowListed(boolean showListed) {
85          _showListed = showListed;
86      }
87  
88      private boolean _showListed = true;
89  
90  }