1   /**
2    * Copyright (c) 2000-2008 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  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.messaging.DestinationNames;
28  import com.liferay.portal.kernel.messaging.MessageBusException;
29  import com.liferay.portal.kernel.messaging.MessageBusUtil;
30  import com.liferay.portal.kernel.search.messaging.SearchRequest;
31  
32  /**
33   * <a href="SearchEngineUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Bruno Farache
36   *
37   */
38  public class SearchEngineUtil {
39  
40      /**
41       * @deprecated Use
42       * <code>com.liferay.portal.kernel.dao.orm.QueryUtil.ALL_POS</code>.
43       */
44      public static final int ALL_POS = -1;
45  
46      public static void addDocument(long companyId, Document doc)
47          throws SearchException {
48  
49          _instance._addDocument(companyId, doc);
50      }
51  
52      public static void deleteDocument(long companyId, String uid)
53          throws SearchException {
54  
55          _instance._deleteDocument(companyId, uid);
56      }
57  
58      public static void deletePortletDocuments(long companyId, String portletId)
59          throws SearchException {
60  
61          _instance._deletePortletDocuments(companyId, portletId);
62      }
63  
64      public static void init(
65          IndexSearcher defaultIndexSearcher, IndexWriter defaultIndexWriter) {
66  
67          _instance._init(defaultIndexSearcher, defaultIndexWriter);
68      }
69  
70      public static boolean isIndexReadOnly() {
71          return _instance._isIndexReadOnly();
72      }
73  
74      public static Hits search(long companyId, Query query, int start, int end)
75          throws SearchException {
76  
77          return _instance._search(companyId, query, start, end);
78      }
79  
80      public static Hits search(
81              long companyId, Query query, Sort sort, int start, int end)
82          throws SearchException {
83  
84          return _instance._search(
85              companyId, query, new Sort[] {sort}, start, end);
86      }
87  
88      public static Hits search(
89              long companyId, Query query, Sort[] sorts, int start, int end)
90          throws SearchException {
91  
92          return _instance._search(companyId, query, sorts, start, end);
93      }
94  
95      public static void updateDocument(long companyId, String uid, Document doc)
96          throws SearchException {
97  
98          _instance._updateDocument(companyId, uid, doc);
99      }
100 
101     private SearchEngineUtil() {
102     }
103 
104     private void _addDocument(long companyId, Document doc)
105         throws SearchException {
106 
107         _messageBusIndexWriter.addDocument(companyId, doc);
108     }
109 
110     private void _deleteDocument(long companyId, String uid)
111         throws SearchException {
112 
113         _messageBusIndexWriter.deleteDocument(companyId, uid);
114     }
115 
116     private void _deletePortletDocuments(long companyId, String portletId)
117         throws SearchException {
118 
119         _messageBusIndexWriter.deletePortletDocuments(companyId, portletId);
120     }
121 
122     private void _init(
123         IndexSearcher messageBusIndexSearcher,
124         IndexWriter messageBusIndexWriter) {
125 
126         _messageBusIndexSearcher = messageBusIndexSearcher;
127         _messageBusIndexWriter = messageBusIndexWriter;
128     }
129 
130     private boolean _isIndexReadOnly() {
131         if (_indexReadOnly != null) {
132             return _indexReadOnly.booleanValue();
133         }
134 
135         try {
136             SearchRequest searchRequest = new SearchRequest();
137 
138             searchRequest.setCommand(SearchRequest.COMMAND_INDEX_ONLY);
139 
140             _indexReadOnly = (Boolean)MessageBusUtil.sendSynchronousMessage(
141                 DestinationNames.SEARCH_READER, searchRequest);
142 
143             if (_indexReadOnly == null) {
144                 _indexReadOnly = Boolean.FALSE;
145             }
146 
147             return _indexReadOnly.booleanValue();
148         }
149         catch (MessageBusException mbe) {
150             if (_log.isWarnEnabled()) {
151                 _log.warn("Unable to check index status", mbe);
152             }
153 
154             return false;
155         }
156     }
157 
158     private Hits _search(long companyId, Query query, int start, int end)
159         throws SearchException {
160 
161         return _messageBusIndexSearcher.search(companyId, query, start, end);
162     }
163 
164     private Hits _search(
165             long companyId, Query query, Sort[] sorts, int start, int end)
166         throws SearchException {
167 
168         return _messageBusIndexSearcher.search(
169             companyId, query, sorts, start, end);
170     }
171 
172     private void _updateDocument(long companyId, String uid, Document doc)
173         throws SearchException {
174 
175         _messageBusIndexWriter.updateDocument(companyId, uid, doc);
176     }
177 
178     private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
179 
180     private static SearchEngineUtil _instance = new SearchEngineUtil();
181 
182     private IndexSearcher _messageBusIndexSearcher;
183     private IndexWriter _messageBusIndexWriter;
184     private Boolean _indexReadOnly;
185 
186 }