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