1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class SearchEngineUtil {
32  
33      /**
34       * @deprecated Use {@link
35       *             com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}.
36       */
37      public static final int ALL_POS = -1;
38  
39      public static void addDocument(long companyId, Document doc)
40          throws SearchException {
41  
42          if (isIndexReadOnly()) {
43              return;
44          }
45  
46          _searchPermissionChecker.addPermissionFields(companyId, doc);
47  
48          _searchEngine.getWriter().addDocument(companyId, doc);
49      }
50  
51      public static void deleteDocument(long companyId, String uid)
52          throws SearchException {
53  
54          if (isIndexReadOnly()) {
55              return;
56          }
57  
58          _searchEngine.getWriter().deleteDocument(companyId, uid);
59      }
60  
61      public static void deletePortletDocuments(long companyId, String portletId)
62          throws SearchException {
63  
64          if (isIndexReadOnly()) {
65              return;
66          }
67  
68          _searchEngine.getWriter().deletePortletDocuments(companyId, portletId);
69      }
70  
71      public static PortalSearchEngine getPortalSearchEngine() {
72          return _portalSearchEngine;
73      }
74  
75      public static SearchEngine getSearchEngine() {
76          return _searchEngine;
77      }
78  
79      public static boolean isIndexReadOnly() {
80          return _portalSearchEngine.isIndexReadOnly();
81      }
82  
83      public static Hits search(long companyId, Query query, int start, int end)
84          throws SearchException {
85  
86          return _searchEngine.getSearcher().search(
87              companyId, query, _DEFAULT_SORT, start, end);
88      }
89  
90      public static Hits search(
91              long companyId, Query query, Sort sort, int start, int end)
92          throws SearchException {
93  
94          return _searchEngine.getSearcher().search(
95              companyId, query, new Sort[] {sort}, start, end);
96      }
97  
98      public static Hits search(
99              long companyId, Query query, Sort[] sorts, int start, int end)
100         throws SearchException {
101 
102         return _searchEngine.getSearcher().search(
103             companyId, query, sorts, start, end);
104     }
105 
106     public static Hits search(
107             long companyId, long groupId, long userId, String className,
108             Query query, int start, int end)
109         throws SearchException {
110 
111         if (userId > 0) {
112             query = _searchPermissionChecker.getPermissionQuery(
113                 companyId, groupId, userId, className, query);
114         }
115 
116         return search(companyId, query, _DEFAULT_SORT, start, end);
117     }
118 
119     public static Hits search(
120             long companyId, long groupId, long userId, String className,
121             Query query, Sort sort, int start, int end)
122         throws SearchException {
123 
124         if (userId > 0) {
125             query = _searchPermissionChecker.getPermissionQuery(
126                 companyId, groupId, userId, className, query);
127         }
128 
129         return search(companyId, query, sort, start, end);
130     }
131 
132     public static Hits search(
133             long companyId, long groupId, long userId, String className,
134             Query query, Sort[] sorts, int start, int end)
135         throws SearchException {
136 
137         if (userId > 0) {
138             query = _searchPermissionChecker.getPermissionQuery(
139                 companyId, groupId, userId, className, query);
140         }
141 
142         return search(companyId, query, sorts, start, end);
143     }
144 
145     public static void setIndexReadOnly(boolean indexReadOnly) {
146         _portalSearchEngine.setIndexReadOnly(indexReadOnly);
147     }
148 
149     public static void updateDocument(long companyId, String uid, Document doc)
150         throws SearchException {
151 
152         if (isIndexReadOnly()) {
153             return;
154         }
155 
156         _searchPermissionChecker.addPermissionFields(companyId, doc);
157 
158         _searchEngine.getWriter().updateDocument(companyId, uid, doc);
159     }
160 
161     public static void updatePermissionFields(long resourceId) {
162         if (isIndexReadOnly()) {
163             return;
164         }
165 
166         _searchPermissionChecker.updatePermissionFields(resourceId);
167     }
168 
169     public static void updatePermissionFields(String name, String primKey) {
170         if (isIndexReadOnly()) {
171             return;
172         }
173 
174         _searchPermissionChecker.updatePermissionFields(name, primKey);
175     }
176 
177     public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
178         _portalSearchEngine = portalSearchEngine;
179     }
180 
181     public void setSearchEngine(SearchEngine searchEngine) {
182         _searchEngine = searchEngine;
183     }
184 
185     public void setSearchPermissionChecker(
186         SearchPermissionChecker searchPermissionChecker) {
187 
188         _searchPermissionChecker = searchPermissionChecker;
189     }
190 
191     private static final Sort[] _DEFAULT_SORT = new Sort[] {
192         new Sort(null, Sort.SCORE_TYPE, false),
193         new Sort(Field.MODIFIED, Sort.LONG_TYPE, true)
194     };
195 
196     private static PortalSearchEngine _portalSearchEngine;
197     private static SearchEngine _searchEngine;
198     private static SearchPermissionChecker _searchPermissionChecker;
199 
200 }