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.messageboards.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.Hits;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.Time;
21  
22  import java.util.ArrayList;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  /**
28   * <a href="ThreadHits.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class ThreadHits {
33  
34      public void recordHits(Hits hits, int start, int end) throws Exception {
35          Set<Long> threadIds = new HashSet<Long>();
36  
37          List<Document> docs = new ArrayList<Document>();
38          List<Float> scores = new ArrayList<Float>();
39  
40          for (int i = 0; i < hits.getLength(); i++) {
41              Document doc = hits.doc(i);
42  
43              Long threadId = GetterUtil.getLong(doc.get("threadId"));
44  
45              if (!threadIds.contains(threadId)) {
46                  threadIds.add(threadId);
47  
48                  docs.add(hits.doc(i));
49                  scores.add(hits.score(i));
50              }
51          }
52  
53          int length = docs.size();
54  
55          hits.setLength(length);
56  
57          if (end > length) {
58              end = length;
59          }
60  
61          docs = docs.subList(start, end);
62  
63          hits.setDocs(docs.toArray(new Document[docs.size()]));
64          hits.setScores(scores.toArray(new Float[docs.size()]));
65  
66          hits.setSearchTime(
67              (float)(System.currentTimeMillis() - hits.getStart()) /
68                  Time.SECOND);
69      }
70  
71  }