1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }