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