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, SortFactoryUtil.getDefaultSorts(), 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[] groupIds, 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, groupIds, userId, className, query);
156         }
157 
158         return search(
159             companyId, query, SortFactoryUtil.getDefaultSorts(), start, end);
160     }
161 
162     public static Hits search(
163             long companyId, long[] groupIds, long userId, String className,
164             Query query, Sort sort, int start, int end)
165         throws SearchException {
166 
167         if (userId > 0) {
168             query = _searchPermissionChecker.getPermissionQuery(
169                 companyId, groupIds, userId, className, query);
170         }
171 
172         return search(companyId, query, sort, start, end);
173     }
174 
175     public static Hits search(
176             long companyId, long[] groupIds, long userId, String className,
177             Query query, Sort[] sorts, int start, int end)
178         throws SearchException {
179 
180         if (userId > 0) {
181             query = _searchPermissionChecker.getPermissionQuery(
182                 companyId, groupIds, userId, className, query);
183         }
184 
185         return search(companyId, query, sorts, start, end);
186     }
187 
188     public static void setIndexReadOnly(boolean indexReadOnly) {
189         _portalSearchEngine.setIndexReadOnly(indexReadOnly);
190     }
191 
192     public static void updateDocument(long companyId, Document document)
193         throws SearchException {
194 
195         if (isIndexReadOnly()) {
196             return;
197         }
198 
199         if (_log.isDebugEnabled()) {
200             _log.debug("Document " + document.toString());
201         }
202 
203         _searchPermissionChecker.addPermissionFields(companyId, document);
204 
205         _searchEngine.getWriter().updateDocument(companyId, document);
206     }
207 
208     public static void updateDocuments(
209             long companyId, Collection<Document> documents)
210         throws SearchException {
211 
212         if (isIndexReadOnly() || (documents == null) || documents.isEmpty()) {
213             return;
214         }
215 
216         for (Document document : documents) {
217             if (_log.isDebugEnabled()) {
218                 _log.debug("Document " + document.toString());
219             }
220 
221             _searchPermissionChecker.addPermissionFields(companyId, document);
222         }
223 
224         _searchEngine.getWriter().updateDocuments(companyId, documents);
225     }
226 
227     public static void updatePermissionFields(long resourceId) {
228         if (isIndexReadOnly()) {
229             return;
230         }
231 
232         _searchPermissionChecker.updatePermissionFields(resourceId);
233     }
234 
235     public static void updatePermissionFields(String name, String primKey) {
236         if (isIndexReadOnly()) {
237             return;
238         }
239 
240         _searchPermissionChecker.updatePermissionFields(name, primKey);
241     }
242 
243     public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
244         _portalSearchEngine = portalSearchEngine;
245     }
246 
247     public void setSearchEngine(SearchEngine searchEngine) {
248         _searchEngine = searchEngine;
249     }
250 
251     public void setSearchPermissionChecker(
252         SearchPermissionChecker searchPermissionChecker) {
253 
254         _searchPermissionChecker = searchPermissionChecker;
255     }
256 
257     private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
258 
259     private static PortalSearchEngine _portalSearchEngine;
260     private static SearchEngine _searchEngine;
261     private static SearchPermissionChecker _searchPermissionChecker;
262 
263 }