1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journalcontentsearch.util;
21  
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.Field;
24  import com.liferay.portal.kernel.search.Hits;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.Time;
27  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * <a href="ContentHits.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alexander Chow
36   * @author Raymond Augé
37   *
38   */
39  public class ContentHits {
40  
41      public void recordHits(
42              Hits hits, long groupId, boolean privateLayout, int start, int end)
43          throws Exception {
44  
45          // This can later be optimized according to LEP-915.
46  
47          List<Document> docs = new ArrayList<Document>();
48          List<Float> scores = new ArrayList<Float>();
49  
50          for (int i = 0; i < hits.getLength(); i++) {
51              Document doc = hits.doc(i);
52  
53              String articleId = doc.get(Field.ENTRY_CLASS_PK);
54              long articleGroupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
55  
56              if (JournalContentSearchLocalServiceUtil.getLayoutIdsCount(
57                      groupId, privateLayout, articleId) > 0) {
58  
59                  docs.add(hits.doc(i));
60                  scores.add(hits.score(i));
61              }
62              else if (!isShowListed() && (articleGroupId == groupId)) {
63                  docs.add(hits.doc(i));
64                  scores.add(hits.score(i));
65              }
66          }
67  
68          int length = docs.size();
69  
70          hits.setLength(length);
71  
72          if (end > length) {
73              end = length;
74          }
75  
76          docs = docs.subList(start, end);
77  
78          hits.setDocs(docs.toArray(new Document[docs.size()]));
79          hits.setScores(scores.toArray(new Float[docs.size()]));
80  
81          hits.setSearchTime(
82              (float)(System.currentTimeMillis() - hits.getStart()) /
83                  Time.SECOND);
84      }
85  
86      public boolean isShowListed() {
87          return _showListed;
88      }
89  
90      public void setShowListed(boolean showListed) {
91          _showListed = showListed;
92      }
93  
94      private boolean _showListed = true;
95  
96  }