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.portal.kernel.search;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  /**
21   * <a href="SearchEngineUtil.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Bruno Farache
24   * @author Raymond Augé
25   */
26  public class SearchEngineUtil {
27  
28      /**
29       * @deprecated Use {@link
30       *             com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}.
31       */
32      public static final int ALL_POS = -1;
33  
34      public static void addDocument(long companyId, Document doc)
35          throws SearchException {
36  
37          if (isIndexReadOnly()) {
38              return;
39          }
40  
41          if (_log.isDebugEnabled()) {
42              _log.debug("Add document " + doc.toString());
43          }
44  
45          _searchPermissionChecker.addPermissionFields(companyId, doc);
46  
47          _searchEngine.getWriter().addDocument(companyId, doc);
48      }
49  
50      public static void deleteDocument(long companyId, String uid)
51          throws SearchException {
52  
53          if (isIndexReadOnly()) {
54              return;
55          }
56  
57          _searchEngine.getWriter().deleteDocument(companyId, uid);
58      }
59  
60      public static void deletePortletDocuments(long companyId, String portletId)
61          throws SearchException {
62  
63          if (isIndexReadOnly()) {
64              return;
65          }
66  
67          _searchEngine.getWriter().deletePortletDocuments(companyId, portletId);
68      }
69  
70      public static PortalSearchEngine getPortalSearchEngine() {
71          return _portalSearchEngine;
72      }
73  
74      public static SearchEngine getSearchEngine() {
75          return _searchEngine;
76      }
77  
78      public static boolean isIndexReadOnly() {
79          return _portalSearchEngine.isIndexReadOnly();
80      }
81  
82      public static Hits search(long companyId, Query query, int start, int end)
83          throws SearchException {
84  
85          if (_log.isDebugEnabled()) {
86              _log.debug("Search query " + query.toString());
87          }
88  
89          return _searchEngine.getSearcher().search(
90              companyId, query, _DEFAULT_SORT, start, end);
91      }
92  
93      public static Hits search(
94              long companyId, Query query, Sort sort, int start, int end)
95          throws SearchException {
96  
97          if (_log.isDebugEnabled()) {
98              _log.debug("Search query " + query.toString());
99          }
100 
101         return _searchEngine.getSearcher().search(
102             companyId, query, new Sort[] {sort}, start, end);
103     }
104 
105     public static Hits search(
106             long companyId, Query query, Sort[] sorts, int start, int end)
107         throws SearchException {
108 
109         if (_log.isDebugEnabled()) {
110             _log.debug("Search query " + query.toString());
111         }
112 
113         return _searchEngine.getSearcher().search(
114             companyId, query, sorts, start, end);
115     }
116 
117     public static Hits search(
118             long companyId, long groupId, long userId, String className,
119             Query query, int start, int end)
120         throws SearchException {
121 
122         if (userId > 0) {
123             query = _searchPermissionChecker.getPermissionQuery(
124                 companyId, groupId, userId, className, query);
125         }
126 
127         return search(companyId, query, _DEFAULT_SORT, start, end);
128     }
129 
130     public static Hits search(
131             long companyId, long groupId, long userId, String className,
132             Query query, Sort sort, int start, int end)
133         throws SearchException {
134 
135         if (userId > 0) {
136             query = _searchPermissionChecker.getPermissionQuery(
137                 companyId, groupId, userId, className, query);
138         }
139 
140         return search(companyId, query, sort, start, end);
141     }
142 
143     public static Hits search(
144             long companyId, long groupId, long userId, String className,
145             Query query, Sort[] sorts, int start, int end)
146         throws SearchException {
147 
148         if (userId > 0) {
149             query = _searchPermissionChecker.getPermissionQuery(
150                 companyId, groupId, userId, className, query);
151         }
152 
153         return search(companyId, query, sorts, start, end);
154     }
155 
156     public static void setIndexReadOnly(boolean indexReadOnly) {
157         _portalSearchEngine.setIndexReadOnly(indexReadOnly);
158     }
159 
160     public static void updateDocument(long companyId, String uid, Document doc)
161         throws SearchException {
162 
163         if (isIndexReadOnly()) {
164             return;
165         }
166 
167         if (_log.isDebugEnabled()) {
168             _log.debug("Document " + doc.toString());
169         }
170 
171         _searchPermissionChecker.addPermissionFields(companyId, doc);
172 
173         _searchEngine.getWriter().updateDocument(companyId, uid, doc);
174     }
175 
176     public static void updatePermissionFields(long resourceId) {
177         if (isIndexReadOnly()) {
178             return;
179         }
180 
181         _searchPermissionChecker.updatePermissionFields(resourceId);
182     }
183 
184     public static void updatePermissionFields(String name, String primKey) {
185         if (isIndexReadOnly()) {
186             return;
187         }
188 
189         _searchPermissionChecker.updatePermissionFields(name, primKey);
190     }
191 
192     public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
193         _portalSearchEngine = portalSearchEngine;
194     }
195 
196     public void setSearchEngine(SearchEngine searchEngine) {
197         _searchEngine = searchEngine;
198     }
199 
200     public void setSearchPermissionChecker(
201         SearchPermissionChecker searchPermissionChecker) {
202 
203         _searchPermissionChecker = searchPermissionChecker;
204     }
205 
206     private static final Sort[] _DEFAULT_SORT = new Sort[] {
207         new Sort(null, Sort.SCORE_TYPE, false),
208         new Sort(Field.MODIFIED, Sort.LONG_TYPE, true)
209     };
210 
211     private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
212 
213     private static PortalSearchEngine _portalSearchEngine;
214     private static SearchEngine _searchEngine;
215     private static SearchPermissionChecker _searchPermissionChecker;
216 
217 }