1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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, SortFactoryUtil.getDefaultSorts(), 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(
128             companyId, query, SortFactoryUtil.getDefaultSorts(), start, end);
129     }
130 
131     public static Hits search(
132             long companyId, long groupId, long userId, String className,
133             Query query, Sort sort, int start, int end)
134         throws SearchException {
135 
136         if (userId > 0) {
137             query = _searchPermissionChecker.getPermissionQuery(
138                 companyId, groupId, userId, className, query);
139         }
140 
141         return search(companyId, query, sort, start, end);
142     }
143 
144     public static Hits search(
145             long companyId, long groupId, long userId, String className,
146             Query query, Sort[] sorts, int start, int end)
147         throws SearchException {
148 
149         if (userId > 0) {
150             query = _searchPermissionChecker.getPermissionQuery(
151                 companyId, groupId, userId, className, query);
152         }
153 
154         return search(companyId, query, sorts, start, end);
155     }
156 
157     public static void setIndexReadOnly(boolean indexReadOnly) {
158         _portalSearchEngine.setIndexReadOnly(indexReadOnly);
159     }
160 
161     public static void updateDocument(long companyId, String uid, Document doc)
162         throws SearchException {
163 
164         if (isIndexReadOnly()) {
165             return;
166         }
167 
168         _searchPermissionChecker.addPermissionFields(companyId, doc);
169 
170         _searchEngine.getWriter().updateDocument(companyId, uid, doc);
171     }
172 
173     public static void updatePermissionFields(long resourceId) {
174         if (isIndexReadOnly()) {
175             return;
176         }
177 
178         _searchPermissionChecker.updatePermissionFields(resourceId);
179     }
180 
181     public static void updatePermissionFields(String name, String primKey) {
182         if (isIndexReadOnly()) {
183             return;
184         }
185 
186         _searchPermissionChecker.updatePermissionFields(name, primKey);
187     }
188 
189     public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
190         _portalSearchEngine = portalSearchEngine;
191     }
192 
193     public void setSearchEngine(SearchEngine searchEngine) {
194         _searchEngine = searchEngine;
195     }
196 
197     public void setSearchPermissionChecker(
198         SearchPermissionChecker searchPermissionChecker) {
199 
200         _searchPermissionChecker = searchPermissionChecker;
201     }
202 
203     private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
204 
205     private static PortalSearchEngine _portalSearchEngine;
206     private static SearchEngine _searchEngine;
207     private static SearchPermissionChecker _searchPermissionChecker;
208 
209 }