001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.Collection;
021    
022    /**
023     * @author Bruno Farache
024     * @author Raymond Augé
025     */
026    public class SearchEngineUtil {
027    
028            /**
029             * @deprecated Use {@link
030             *                         com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}.
031             */
032            public static final int ALL_POS = -1;
033    
034            public static void addDocument(long companyId, Document document)
035                    throws SearchException {
036    
037                    if (isIndexReadOnly()) {
038                            return;
039                    }
040    
041                    if (_log.isDebugEnabled()) {
042                            _log.debug("Add document " + document.toString());
043                    }
044    
045                    _searchPermissionChecker.addPermissionFields(companyId, document);
046    
047                    _searchEngine.getWriter().addDocument(companyId, document);
048            }
049    
050            public static void addDocuments(
051                            long companyId, Collection<Document> documents)
052                    throws SearchException {
053    
054                    if (isIndexReadOnly() || (documents == null) || documents.isEmpty()) {
055                            return;
056                    }
057    
058                    for (Document document : documents) {
059                            if (_log.isDebugEnabled()) {
060                                    _log.debug("Add document " + document.toString());
061                            }
062    
063                            _searchPermissionChecker.addPermissionFields(companyId, document);
064                    }
065    
066                    _searchEngine.getWriter().addDocuments(companyId, documents);
067            }
068    
069            public static void deleteDocument(long companyId, String uid)
070                    throws SearchException {
071    
072                    if (isIndexReadOnly()) {
073                            return;
074                    }
075    
076                    _searchEngine.getWriter().deleteDocument(companyId, uid);
077            }
078    
079            public static void deleteDocuments(long companyId, Collection<String> uids)
080                    throws SearchException {
081    
082                    if (isIndexReadOnly() || (uids == null) || uids.isEmpty()) {
083                            return;
084                    }
085    
086                    _searchEngine.getWriter().deleteDocuments(companyId, uids);
087            }
088    
089            public static void deletePortletDocuments(long companyId, String portletId)
090                    throws SearchException {
091    
092                    if (isIndexReadOnly()) {
093                            return;
094                    }
095    
096                    _searchEngine.getWriter().deletePortletDocuments(companyId, portletId);
097            }
098    
099            public static PortalSearchEngine getPortalSearchEngine() {
100                    return _portalSearchEngine;
101            }
102    
103            public static SearchEngine getSearchEngine() {
104                    return _searchEngine;
105            }
106    
107            public static boolean isIndexReadOnly() {
108                    return _portalSearchEngine.isIndexReadOnly();
109            }
110    
111            public static Hits search(long companyId, Query query, int start, int end)
112                    throws SearchException {
113    
114                    if (_log.isDebugEnabled()) {
115                            _log.debug("Search query " + query.toString());
116                    }
117    
118                    return _searchEngine.getSearcher().search(
119                            companyId, query, SortFactoryUtil.getDefaultSorts(), start, end);
120            }
121    
122            public static Hits search(
123                            long companyId, Query query, Sort sort, int start, int end)
124                    throws SearchException {
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug("Search query " + query.toString());
128                    }
129    
130                    return _searchEngine.getSearcher().search(
131                            companyId, query, new Sort[] {sort}, start, end);
132            }
133    
134            public static Hits search(
135                            long companyId, Query query, Sort[] sorts, int start, int end)
136                    throws SearchException {
137    
138                    if (_log.isDebugEnabled()) {
139                            _log.debug("Search query " + query.toString());
140                    }
141    
142                    return _searchEngine.getSearcher().search(
143                            companyId, query, sorts, start, end);
144            }
145    
146            public static Hits search(
147                            long companyId, long[] groupIds, long userId, String className,
148                            Query query, int start, int end)
149                    throws SearchException {
150    
151                    if (userId > 0) {
152                            query = _searchPermissionChecker.getPermissionQuery(
153                                    companyId, groupIds, userId, className, query);
154                    }
155    
156                    return search(
157                            companyId, query, SortFactoryUtil.getDefaultSorts(), start, end);
158            }
159    
160            public static Hits search(
161                            long companyId, long[] groupIds, long userId, String className,
162                            Query query, Sort sort, int start, int end)
163                    throws SearchException {
164    
165                    if (userId > 0) {
166                            query = _searchPermissionChecker.getPermissionQuery(
167                                    companyId, groupIds, userId, className, query);
168                    }
169    
170                    return search(companyId, query, sort, start, end);
171            }
172    
173            public static Hits search(
174                            long companyId, long[] groupIds, long userId, String className,
175                            Query query, Sort[] sorts, int start, int end)
176                    throws SearchException {
177    
178                    if (userId > 0) {
179                            query = _searchPermissionChecker.getPermissionQuery(
180                                    companyId, groupIds, userId, className, query);
181                    }
182    
183                    return search(companyId, query, sorts, start, end);
184            }
185    
186            public static void setIndexReadOnly(boolean indexReadOnly) {
187                    _portalSearchEngine.setIndexReadOnly(indexReadOnly);
188            }
189    
190            public static void updateDocument(long companyId, Document document)
191                    throws SearchException {
192    
193                    if (isIndexReadOnly()) {
194                            return;
195                    }
196    
197                    if (_log.isDebugEnabled()) {
198                            _log.debug("Document " + document.toString());
199                    }
200    
201                    _searchPermissionChecker.addPermissionFields(companyId, document);
202    
203                    _searchEngine.getWriter().updateDocument(companyId, document);
204            }
205    
206            public static void updateDocuments(
207                            long companyId, Collection<Document> documents)
208                    throws SearchException {
209    
210                    if (isIndexReadOnly() || (documents == null) || documents.isEmpty()) {
211                            return;
212                    }
213    
214                    for (Document document : documents) {
215                            if (_log.isDebugEnabled()) {
216                                    _log.debug("Document " + document.toString());
217                            }
218    
219                            _searchPermissionChecker.addPermissionFields(companyId, document);
220                    }
221    
222                    _searchEngine.getWriter().updateDocuments(companyId, documents);
223            }
224    
225            public static void updatePermissionFields(long resourceId) {
226                    if (isIndexReadOnly()) {
227                            return;
228                    }
229    
230                    _searchPermissionChecker.updatePermissionFields(resourceId);
231            }
232    
233            public static void updatePermissionFields(String name, String primKey) {
234                    if (isIndexReadOnly()) {
235                            return;
236                    }
237    
238                    _searchPermissionChecker.updatePermissionFields(name, primKey);
239            }
240    
241            public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
242                    _portalSearchEngine = portalSearchEngine;
243            }
244    
245            public void setSearchEngine(SearchEngine searchEngine) {
246                    _searchEngine = searchEngine;
247            }
248    
249            public void setSearchPermissionChecker(
250                    SearchPermissionChecker searchPermissionChecker) {
251    
252                    _searchPermissionChecker = searchPermissionChecker;
253            }
254    
255            private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
256    
257            private static PortalSearchEngine _portalSearchEngine;
258            private static SearchEngine _searchEngine;
259            private static SearchPermissionChecker _searchPermissionChecker;
260    
261    }