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   *
30   */
31  public class SearchEngineUtil {
32  
33      /**
34       * @deprecated Use
35       * <code>com.liferay.portal.kernel.dao.orm.QueryUtil.ALL_POS</code>.
36       */
37      public static final int ALL_POS = -1;
38  
39      public static void addDocument(long companyId, Document doc)
40          throws SearchException {
41  
42          _searchPermissionChecker.addPermissionFields(companyId, doc);
43  
44          getSearchEngine().getWriter().addDocument(companyId, doc);
45      }
46  
47      public static void deleteDocument(long companyId, String uid)
48          throws SearchException {
49  
50          getSearchEngine().getWriter().deleteDocument(companyId, uid);
51      }
52  
53      public static void deletePortletDocuments(long companyId, String portletId)
54          throws SearchException {
55  
56          getSearchEngine().getWriter().deletePortletDocuments(
57              companyId, portletId);
58      }
59  
60      public static SearchEngine getSearchEngine() {
61          return _searchEngine;
62      }
63  
64      public static boolean isIndexReadOnly() {
65          return getSearchEngine().isIndexReadOnly();
66      }
67  
68      public static void register(String name) {
69          getSearchEngine().register(name);
70      }
71  
72      public static void registerDefaultSearchEngine() {
73          SearchEngineUtil.register(_defaultSearchEngineName);
74      }
75  
76      public static Hits search(long companyId, Query query, int start, int end)
77          throws SearchException {
78  
79          return getSearchEngine().getSearcher().search(
80              companyId, query, _DEFAULT_SORT, start, end);
81      }
82  
83      public static Hits search(
84              long companyId, Query query, Sort sort, int start, int end)
85          throws SearchException {
86  
87          return getSearchEngine().getSearcher().search(
88              companyId, query, new Sort[] {sort}, start, end);
89      }
90  
91      public static Hits search(
92              long companyId, Query query, Sort[] sorts, int start, int end)
93          throws SearchException {
94  
95          return getSearchEngine().getSearcher().search(
96              companyId, query, sorts, start, end);
97      }
98  
99      public static Hits search(
100             long companyId, long groupId, long userId, String className,
101             Query query, int start, int end)
102         throws SearchException {
103 
104         if (userId > 0) {
105             query = _searchPermissionChecker.getPermissionQuery(
106                 companyId, groupId, userId, className, query);
107         }
108 
109         return search(companyId, query, _DEFAULT_SORT, start, end);
110     }
111 
112     public static Hits search(
113             long companyId, long groupId, long userId, String className,
114             Query query, Sort sort, int start, int end)
115         throws SearchException {
116 
117         if (userId > 0) {
118             query = _searchPermissionChecker.getPermissionQuery(
119                 companyId, groupId, userId, className, query);
120         }
121 
122         return search(companyId, query, sort, start, end);
123     }
124 
125     public static Hits search(
126             long companyId, long groupId, long userId, String className,
127             Query query, Sort[] sorts, int start, int end)
128         throws SearchException {
129 
130         if (userId > 0) {
131             query = _searchPermissionChecker.getPermissionQuery(
132                 companyId, groupId, userId, className, query);
133         }
134 
135         return search(companyId, query, sorts, start, end);
136     }
137 
138     public static void unregister(String fromName) {
139         getSearchEngine().unregister(fromName);
140     }
141 
142     public static void updateDocument(long companyId, String uid, Document doc)
143         throws SearchException {
144 
145         _searchPermissionChecker.addPermissionFields(companyId, doc);
146 
147         getSearchEngine().getWriter().updateDocument(companyId, uid, doc);
148     }
149 
150     public static void updatePermissionFields(long resourceId) {
151         _searchPermissionChecker.updatePermissionFields(resourceId);
152     }
153 
154     public void setDefaultSearchEngineName(String defaultSearchEngineName) {
155         _defaultSearchEngineName = defaultSearchEngineName;
156     }
157 
158     public void setSearchEngine(SearchEngine searchEngine) {
159         _searchEngine = searchEngine;
160     }
161 
162     public void setSearchPermissionChecker(
163         SearchPermissionChecker searchPermissionChecker) {
164 
165         _searchPermissionChecker = searchPermissionChecker;
166     }
167 
168     private static final Sort[] _DEFAULT_SORT = new Sort[] {
169         new Sort(Field.MODIFIED, Sort.LONG_TYPE, true)
170     };
171 
172     private static String _defaultSearchEngineName;
173     private static SearchEngine _searchEngine;
174     private static SearchPermissionChecker _searchPermissionChecker;
175 
176 }