1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.search;
24  
25  /**
26   * <a href="SearchEngineUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Bruno Farache
29   * @author Raymond Augé
30   *
31   */
32  public class SearchEngineUtil {
33  
34      /**
35       * @deprecated Use
36       * <code>com.liferay.portal.kernel.dao.orm.QueryUtil.ALL_POS</code>.
37       */
38      public static final int ALL_POS = -1;
39  
40      public static void addDocument(long companyId, Document doc)
41          throws SearchException {
42  
43          if (isIndexReadOnly()) {
44              return;
45          }
46  
47          _searchPermissionChecker.addPermissionFields(companyId, doc);
48  
49          _searchEngine.getWriter().addDocument(companyId, doc);
50      }
51  
52      public static void deleteDocument(long companyId, String uid)
53          throws SearchException {
54  
55          if (isIndexReadOnly()) {
56              return;
57          }
58  
59          _searchEngine.getWriter().deleteDocument(companyId, uid);
60      }
61  
62      public static void deletePortletDocuments(long companyId, String portletId)
63          throws SearchException {
64  
65          if (isIndexReadOnly()) {
66              return;
67          }
68  
69          _searchEngine.getWriter().deletePortletDocuments(companyId, portletId);
70      }
71  
72      public static PortalSearchEngine getPortalSearchEngine() {
73          return _portalSearchEngine;
74      }
75  
76      public static SearchEngine getSearchEngine() {
77          return _searchEngine;
78      }
79  
80      public static boolean isIndexReadOnly() {
81          return _portalSearchEngine.isIndexReadOnly();
82      }
83  
84      public static void register(String name) {
85          _searchEngine.register(name);
86      }
87  
88      public static void registerDefaultSearchEngine() {
89          SearchEngineUtil.register(_defaultSearchEngineName);
90      }
91  
92      public static Hits search(long companyId, Query query, int start, int end)
93          throws SearchException {
94  
95          return _searchEngine.getSearcher().search(
96              companyId, query, _DEFAULT_SORT, start, end);
97      }
98  
99      public static Hits search(
100             long companyId, Query query, Sort sort, int start, int end)
101         throws SearchException {
102 
103         return _searchEngine.getSearcher().search(
104             companyId, query, new Sort[] {sort}, start, end);
105     }
106 
107     public static Hits search(
108             long companyId, Query query, Sort[] sorts, int start, int end)
109         throws SearchException {
110 
111         return _searchEngine.getSearcher().search(
112             companyId, query, sorts, start, end);
113     }
114 
115     public static Hits search(
116             long companyId, long groupId, long userId, String className,
117             Query query, int start, int end)
118         throws SearchException {
119 
120         if (userId > 0) {
121             query = _searchPermissionChecker.getPermissionQuery(
122                 companyId, groupId, userId, className, query);
123         }
124 
125         return search(companyId, query, _DEFAULT_SORT, start, end);
126     }
127 
128     public static Hits search(
129             long companyId, long groupId, long userId, String className,
130             Query query, Sort sort, int start, int end)
131         throws SearchException {
132 
133         if (userId > 0) {
134             query = _searchPermissionChecker.getPermissionQuery(
135                 companyId, groupId, userId, className, query);
136         }
137 
138         return search(companyId, query, sort, start, end);
139     }
140 
141     public static Hits search(
142             long companyId, long groupId, long userId, String className,
143             Query query, Sort[] sorts, int start, int end)
144         throws SearchException {
145 
146         if (userId > 0) {
147             query = _searchPermissionChecker.getPermissionQuery(
148                 companyId, groupId, userId, className, query);
149         }
150 
151         return search(companyId, query, sorts, start, end);
152     }
153 
154     public static void setIndexReadOnly(boolean indexReadOnly) {
155         _portalSearchEngine.setIndexReadOnly(indexReadOnly);
156     }
157 
158     public static void unregister(String fromName) {
159         _searchEngine.unregister(fromName);
160     }
161 
162     public static void updateDocument(long companyId, String uid, Document doc)
163         throws SearchException {
164 
165         if (isIndexReadOnly()) {
166             return;
167         }
168 
169         _searchPermissionChecker.addPermissionFields(companyId, doc);
170 
171         _searchEngine.getWriter().updateDocument(companyId, uid, doc);
172     }
173 
174     public static void updatePermissionFields(long resourceId) {
175         if (isIndexReadOnly()) {
176             return;
177         }
178 
179         _searchPermissionChecker.updatePermissionFields(resourceId);
180     }
181 
182     public static void updatePermissionFields(String name, String primKey) {
183         if (isIndexReadOnly()) {
184             return;
185         }
186 
187         _searchPermissionChecker.updatePermissionFields(name, primKey);
188     }
189 
190     public void setDefaultSearchEngineName(String defaultSearchEngineName) {
191         _defaultSearchEngineName = defaultSearchEngineName;
192     }
193 
194     public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
195         _portalSearchEngine = portalSearchEngine;
196     }
197 
198     public void setSearchEngine(SearchEngine searchEngine) {
199         _searchEngine = searchEngine;
200     }
201 
202     public void setSearchPermissionChecker(
203         SearchPermissionChecker searchPermissionChecker) {
204 
205         _searchPermissionChecker = searchPermissionChecker;
206     }
207 
208     private static final Sort[] _DEFAULT_SORT = new Sort[] {
209         new Sort(null, Sort.SCORE_TYPE, false),
210         new Sort(Field.MODIFIED, Sort.LONG_TYPE, true)
211     };
212 
213     private static String _defaultSearchEngineName;
214     private static PortalSearchEngine _portalSearchEngine;
215     private static SearchEngine _searchEngine;
216     private static SearchPermissionChecker _searchPermissionChecker;
217 
218 }